unit u_profileedit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, ComCtrls, fpjson, u_api, u_norms; type { TProfileEditForm } TProfileEditForm = class(TForm) btnSave: TButton; btnCancel: TButton; edtName: TEdit; Label1: TLabel; LabelGroup: TLabel; cboGroup: TComboBox; edtNewGroup: TEdit; LabelNewGroup: TLabel; PageControl1: TPageControl; TabSE: TTabSheet; TabFE: TTabSheet; ScrollBoxSE: TScrollBox; ScrollBoxFE: TScrollBox; private FUserID: string; FSession: string; FProfileID: string; FGroups: TJSONArray; FSEValues: array[1..36] of TRadioGroup; FFEValues: array[1..36] of TRadioGroup; procedure LoadGroups; procedure CreateItemControls; procedure LoadProfileData; procedure SaveProfile; procedure OnSaveClick(Sender: TObject); procedure OnCancelClick(Sender: TObject); public constructor Create(AOwner: TComponent; const UserID, Session: string; ProfileID: string = ''); reintroduce; end; implementation {$R *.lfm} { TProfileEditForm } constructor TProfileEditForm.Create(AOwner: TComponent; const UserID, Session: string; ProfileID: string); begin inherited Create(AOwner); FUserID := UserID; FSession := Session; FProfileID := ProfileID; if ProfileID = '' then Caption := 'Neues Profil' else Caption := 'Profil bearbeiten'; Width := 950; Height := 750; Position := poScreenCenter; ShowMessage('ProfileEditForm.Create: ProfileID=' + ProfileID); // Buttons manuell verbinden btnSave.OnClick := @OnSaveClick; btnCancel.OnClick := @OnCancelClick; LoadGroups; CreateItemControls; if FProfileID <> '' then LoadProfileData; end; procedure TProfileEditForm.LoadGroups; var i: Integer; Group: TJSONObject; GroupName: string; begin ShowMessage('LoadGroups: Start'); FGroups := GetGroups(FUserID, FSession); cboGroup.Items.Clear; if Assigned(FGroups) then begin ShowMessage('LoadGroups: Anzahl Gruppen=' + IntToStr(FGroups.Count)); for i := 0 to FGroups.Count - 1 do begin Group := TJSONObject(FGroups.Objects[i]); GroupName := Group.Get('name', ''); if GroupName <> '' then begin cboGroup.Items.Add(GroupName); ShowMessage('LoadGroups: Gruppe ' + IntToStr(i) + ' = ' + GroupName); end; end; end else ShowMessage('LoadGroups: Keine Gruppen gefunden'); if cboGroup.Items.Count > 0 then cboGroup.ItemIndex := 0; ShowMessage('LoadGroups: Ende'); end; procedure TProfileEditForm.CreateItemControls; var i: Integer; GroupBox: TGroupBox; rg: TRadioGroup; begin ShowMessage('CreateItemControls: Start'); for i := 1 to 36 do begin // SE GroupBox := TGroupBox.Create(ScrollBoxSE); GroupBox.Parent := ScrollBoxSE; GroupBox.Caption := Format('%d. %s', [i, ITEMS[i]]); GroupBox.Top := (i - 1) * 70 + 5; GroupBox.Left := 5; GroupBox.Width := ScrollBoxSE.Width - 20; GroupBox.Height := 65; rg := TRadioGroup.Create(GroupBox); rg.Parent := GroupBox; rg.SetBounds(5, 15, GroupBox.Width - 10, 40); rg.Columns := 4; rg.Items.Add('trifft voll zu (4)'); rg.Items.Add('trifft zu (3)'); rg.Items.Add('trifft teilweise zu (2)'); rg.Items.Add('trifft nicht zu (1)'); rg.ItemIndex := 2; FSEValues[i] := rg; // FE GroupBox := TGroupBox.Create(ScrollBoxFE); GroupBox.Parent := ScrollBoxFE; GroupBox.Caption := Format('%d. %s', [i, ITEMS[i]]); GroupBox.Top := (i - 1) * 70 + 5; GroupBox.Left := 5; GroupBox.Width := ScrollBoxFE.Width - 20; GroupBox.Height := 65; rg := TRadioGroup.Create(GroupBox); rg.Parent := GroupBox; rg.SetBounds(5, 15, GroupBox.Width - 10, 40); rg.Columns := 4; rg.Items.Add('trifft voll zu (4)'); rg.Items.Add('trifft zu (3)'); rg.Items.Add('trifft teilweise zu (2)'); rg.Items.Add('trifft nicht zu (1)'); rg.ItemIndex := 2; FFEValues[i] := rg; end; ShowMessage('CreateItemControls: Ende'); end; procedure TProfileEditForm.LoadProfileData; var ProfileData: TJSONObject; i: Integer; GroupName: string; begin ShowMessage('LoadProfileData: Start für ID ' + FProfileID); ProfileData := GetProfile(FUserID, FSession, FProfileID); if Assigned(ProfileData) then begin ShowMessage('LoadProfileData: Profil geladen'); edtName.Text := ProfileData.Get('name', ''); ShowMessage('LoadProfileData: Name=' + edtName.Text); GroupName := ProfileData.Get('gruppename', ''); ShowMessage('LoadProfileData: Gruppe=' + GroupName); if GroupName <> '' then cboGroup.Text := GroupName; for i := 1 to 36 do begin if FSEValues[i] <> nil then FSEValues[i].ItemIndex := ProfileData.Get('item' + IntToStr(i), 2) - 1; if FFEValues[i] <> nil then FFEValues[i].ItemIndex := ProfileData.Get('feitem' + IntToStr(i), 2) - 1; end; ShowMessage('LoadProfileData: Items geladen'); ProfileData.Free; end else ShowMessage('LoadProfileData: Profil nicht gefunden'); ShowMessage('LoadProfileData: Ende'); end; procedure TProfileEditForm.OnSaveClick(Sender: TObject); begin ShowMessage('OnSaveClick: Start'); SaveProfile; end; procedure TProfileEditForm.OnCancelClick(Sender: TObject); begin ModalResult := mrCancel; end; procedure TProfileEditForm.SaveProfile; var Data: TJSONObject; i: Integer; GroupID: Integer; GroupName: string; begin ShowMessage('SaveProfile: Start'); if Trim(edtName.Text) = '' then begin ShowMessage('Bitte Name eingeben'); Exit; end; Data := TJSONObject.Create; try Data.Add('name', edtName.Text); // Gruppe bestimmen GroupName := Trim(edtNewGroup.Text); ShowMessage('SaveProfile: GroupName=' + GroupName); if GroupName <> '' then begin Data.Add('namegruppe', GroupName); ShowMessage('SaveProfile: Neue Gruppe wird erstellt'); end else if cboGroup.Text <> '' then begin GroupName := cboGroup.Text; GroupID := 0; if Assigned(FGroups) then begin for i := 0 to FGroups.Count - 1 do begin if TJSONObject(FGroups.Objects[i]).Get('name', '') = GroupName then begin GroupID := TJSONObject(FGroups.Objects[i]).Get('gruppeID', 0); Break; end; end; end; if GroupID > 0 then begin Data.Add('gruppeID', GroupID); ShowMessage('SaveProfile: Bestehende Gruppe ID=' + IntToStr(GroupID)); end; end; for i := 1 to 36 do begin if FSEValues[i] <> nil then Data.Add('item' + IntToStr(i), FSEValues[i].ItemIndex + 1) else Data.Add('item' + IntToStr(i), 2); if FFEValues[i] <> nil then Data.Add('feitem' + IntToStr(i), FFEValues[i].ItemIndex + 1) else Data.Add('feitem' + IntToStr(i), 2); end; if FProfileID <> '' then begin Data.Add('profilID', StrToInt(FProfileID)); ShowMessage('SaveProfile: Update vorhandenes Profil'); if UpdateProfile(FUserID, FSession, Data) then begin ShowMessage('SaveProfile: Update erfolgreich'); ModalResult := mrOk; end else ShowMessage('Fehler beim Speichern'); end else begin ShowMessage('SaveProfile: Neues Profil erstellen'); if CreateProfile(FUserID, FSession, Data) then begin ShowMessage('SaveProfile: Erstellen erfolgreich'); ModalResult := mrOk; end else ShowMessage('Fehler beim Erstellen'); end; finally Data.Free; end; ShowMessage('SaveProfile: Ende'); end; end.