Inhalt

Aktueller Ordner: duesseldorfer-schuelerinventar-freepascal-client
⬅ Übergeordnet

u_profilelist.pas

unit u_profilelist;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  Grids, Menus, fpjson, u_api, u_profiledetail, u_profileedit, u_timeseries;

type

  { TProfileListForm }

  TProfileListForm = class(TForm)
    btnNew: TButton;
    btnRefresh: TButton;
    btnEdit: TButton;
    btnDelete: TButton;
    btnTimeSeries: TButton;
    btnLogout: TButton;
    Panel1: TPanel;
    StringGrid1: TStringGrid;
    PopupMenu1: TPopupMenu;
    miView: TMenuItem;
    miEdit: TMenuItem;
    miDelete: TMenuItem;
    miTimeSeries: TMenuItem;
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
    procedure StringGrid1DblClick(Sender: TObject);
  private
    FUserID: string;
    FSession: string;
    FProfiles: TJSONArray;
    procedure LoadProfiles;
    procedure UpdateGrid;
    function GetSelectedProfileID: string;
    function GetSelectedProfileName: string;
    function GetSelectedProfile: TJSONObject;
    procedure OnNewClick(Sender: TObject);
    procedure OnRefreshClick(Sender: TObject);
    procedure OnEditClick(Sender: TObject);
    procedure OnDeleteClick(Sender: TObject);
    procedure OnTimeSeriesClick(Sender: TObject);
    procedure OnLogoutClick(Sender: TObject);
    procedure OnViewClick(Sender: TObject);
    procedure OnMiEditClick(Sender: TObject);
    procedure OnMiDeleteClick(Sender: TObject);
    procedure OnMiTimeSeriesClick(Sender: TObject);
  public
    constructor Create(AOwner: TComponent; const UserID, Session: string); reintroduce;
  end;

implementation

{$R *.lfm}

{ TProfileListForm }

constructor TProfileListForm.Create(AOwner: TComponent; const UserID, Session: string);
begin
  inherited Create(AOwner);
  FUserID := UserID;
  FSession := Session;
  FProfiles := nil;
  Caption := 'DÜSK - Profile';
  Position := poScreenCenter;
  Width := 900;
  Height := 550;
  
  StringGrid1.FixedCols := 0;
  StringGrid1.ColCount := 3;
  StringGrid1.RowCount := 1;
  StringGrid1.Cells[0, 0] := 'Name';
  StringGrid1.Cells[1, 0] := 'Gruppe';
  StringGrid1.Cells[2, 0] := 'ProfilID';
  StringGrid1.ColWidths[0] := 350;
  StringGrid1.ColWidths[1] := 250;
  StringGrid1.ColWidths[2] := 80;
  StringGrid1.Options := StringGrid1.Options + [goRowSelect];
  StringGrid1.OnClick := @StringGrid1Click;
  StringGrid1.OnDblClick := @StringGrid1DblClick;
  
  btnNew.OnClick := @OnNewClick;
  btnRefresh.OnClick := @OnRefreshClick;
  btnEdit.OnClick := @OnEditClick;
  btnDelete.OnClick := @OnDeleteClick;
  btnTimeSeries.OnClick := @OnTimeSeriesClick;
  btnLogout.OnClick := @OnLogoutClick;
  
  miView.OnClick := @OnViewClick;
  miEdit.OnClick := @OnMiEditClick;
  miDelete.OnClick := @OnMiDeleteClick;
  miTimeSeries.OnClick := @OnMiTimeSeriesClick;
  
  LoadProfiles;
end;

procedure TProfileListForm.FormShow(Sender: TObject);
begin
  LoadProfiles;
end;

procedure TProfileListForm.LoadProfiles;
begin
  Screen.Cursor := crHourGlass;
  try
    if Assigned(FProfiles) then
      FProfiles.Free;
    
    FProfiles := GetProfiles(FUserID, FSession);
    
    if Assigned(FProfiles) then
      UpdateGrid
    else
      StringGrid1.RowCount := 1;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TProfileListForm.UpdateGrid;
var
  i: Integer;
  Profile: TJSONObject;
begin
  if not Assigned(FProfiles) then
  begin
    StringGrid1.RowCount := 1;
    Exit;
  end;
  
  StringGrid1.RowCount := FProfiles.Count + 1;
  for i := 0 to FProfiles.Count - 1 do
  begin
    Profile := TJSONObject(FProfiles.Objects[i]);
    StringGrid1.Cells[0, i + 1] := Profile.Get('name', '');
    StringGrid1.Cells[1, i + 1] := Profile.Get('gruppename', '');
    StringGrid1.Cells[2, i + 1] := Profile.Get('profilID', '');
  end;
end;

function TProfileListForm.GetSelectedProfileID: string;
begin
  if StringGrid1.Row > 0 then
    Result := StringGrid1.Cells[2, StringGrid1.Row]
  else
    Result := '';
end;

function TProfileListForm.GetSelectedProfileName: string;
begin
  if StringGrid1.Row > 0 then
    Result := StringGrid1.Cells[0, StringGrid1.Row]
  else
    Result := '';
end;

function TProfileListForm.GetSelectedProfile: TJSONObject;
var
  i: Integer;
begin
  Result := nil;
  if (StringGrid1.Row < 1) or not Assigned(FProfiles) then
    Exit;
  
  i := StringGrid1.Row - 1;
  if i < FProfiles.Count then
    Result := TJSONObject(FProfiles.Objects[i]);
end;

procedure TProfileListForm.StringGrid1Click(Sender: TObject);
begin
  if StringGrid1.Row > 0 then
    StringGrid1.Row := StringGrid1.Row;
end;

procedure TProfileListForm.StringGrid1DblClick(Sender: TObject);
var
  ProfileID: string;
  ProfileData: TJSONObject;
begin
  ProfileID := GetSelectedProfileID;
  if ProfileID = '' then
  begin
    ShowMessage('Bitte wählen Sie ein Profil aus');
    Exit;
  end;
  
  ProfileData := GetProfile(FUserID, FSession, ProfileID);
  if Assigned(ProfileData) then
  begin
    with TProfileDetailForm.Create(Self, ProfileData) do
    begin
      ShowModal;
      Free;
    end;
  end;
end;

procedure TProfileListForm.OnRefreshClick(Sender: TObject);
begin
  LoadProfiles;
end;

procedure TProfileListForm.OnNewClick(Sender: TObject);
begin
  with TProfileEditForm.Create(Self, FUserID, FSession, '') do
  begin
    if ShowModal = mrOk then
      LoadProfiles;
    Free;
  end;
end;

procedure TProfileListForm.OnEditClick(Sender: TObject);
var
  ProfileID: string;
begin
  ProfileID := GetSelectedProfileID;
  if ProfileID = '' then
  begin
    ShowMessage('Bitte wählen Sie ein Profil aus');
    Exit;
  end;
  
  with TProfileEditForm.Create(Self, FUserID, FSession, ProfileID) do
  begin
    if ShowModal = mrOk then
      LoadProfiles;
    Free;
  end;
end;

procedure TProfileListForm.OnDeleteClick(Sender: TObject);
var
  ProfileID: string;
  ProfileName: string;
begin
  ProfileID := GetSelectedProfileID;
  ProfileName := GetSelectedProfileName;
  if ProfileID = '' then
  begin
    ShowMessage('Bitte wählen Sie ein Profil aus');
    Exit;
  end;
    
  if MessageDlg('Profil löschen', Format('Möchten Sie "%s" wirklich löschen?', [ProfileName]),
    mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    if DeleteProfile(FUserID, FSession, ProfileID) then
      LoadProfiles
    else
      ShowMessage('Fehler beim Löschen des Profils');
  end;
end;

procedure TProfileListForm.OnTimeSeriesClick(Sender: TObject);
var
  GroupID: string;
  GroupName: string;
  GroupProfiles: TJSONArray;
  i: Integer;
  Profile: TJSONObject;
  ProfileData: TJSONObject;
begin
  if GetSelectedProfile = nil then
  begin
    ShowMessage('Bitte wählen Sie ein Profil aus');
    Exit;
  end;
  
  GroupID := GetSelectedProfile.Get('gruppeID', '');
  GroupName := GetSelectedProfile.Get('gruppename', '');
  
  if GroupID = '' then
  begin
    ShowMessage('Keine Gruppe für dieses Profil');
    Exit;
  end;
  
  GroupProfiles := TJSONArray.Create;
  try
    for i := 0 to FProfiles.Count - 1 do
    begin
      Profile := TJSONObject(FProfiles.Objects[i]);
      if Profile.Get('gruppeID', '') = GroupID then
      begin
        ProfileData := GetProfile(FUserID, FSession, Profile.Get('profilID', ''));
        if Assigned(ProfileData) then
          GroupProfiles.Add(ProfileData);
      end;
    end;
    
    if GroupProfiles.Count > 0 then
    begin
      with TTimeSeriesForm.Create(Self, FUserID, FSession, GroupName, GroupProfiles) do
      begin
        ShowModal;
        Free;
      end;
    end
    else
      ShowMessage('Keine weiteren Profile in dieser Gruppe.');
  finally
    // GroupProfiles wird im TTimeSeriesForm freigegeben
  end;
end;

procedure TProfileListForm.OnLogoutClick(Sender: TObject);
begin
  Close;
end;

procedure TProfileListForm.OnViewClick(Sender: TObject);
begin
  StringGrid1DblClick(Sender);
end;

procedure TProfileListForm.OnMiEditClick(Sender: TObject);
begin
  OnEditClick(Sender);
end;

procedure TProfileListForm.OnMiDeleteClick(Sender: TObject);
begin
  OnDeleteClick(Sender);
end;

procedure TProfileListForm.OnMiTimeSeriesClick(Sender: TObject);
begin
  OnTimeSeriesClick(Sender);
end;

procedure TProfileListForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if Assigned(FProfiles) then
    FProfiles.Free;
  CloseAction := caFree;
end;

end.