unit AttachmentInfoDm;

interface

uses
  SysUtils, Classes, sdDB, sdProvider, ADODB, Variants;

type
  TAttachmentInfoData = class(TDataModule)
    sdpAttachmentInfo: TsdADOProvider;
    sddAttachmentInfo: TsdDataSet;
  private
    FProjectData: TObject;
    FMacAddr: string;

    function GetAttachmentInfoRec(AID: Integer): TsdDataRecord;
  public
    constructor Create(AProjectData: TObject);
    destructor Destroy; override;

    procedure Open(AConnection: TADOConnection);
    procedure Close;
    procedure Save;

    function GetAttachmentPath(AID: Integer): string;
    procedure SaveAttachmentPath(AID: Integer; APath: string);
  end;

implementation

uses UtilMethods, PHPWebDM;

{$R *.dfm}

{ TAttachmentInfoData }

procedure TAttachmentInfoData.Close;
begin
  sddAttachmentInfo.Close;
end;

constructor TAttachmentInfoData.Create(AProjectData: TObject);
begin
  inherited Create(nil);
  FProjectData := AProjectData;
  FMacAddr := GetMacAddr;
end;

destructor TAttachmentInfoData.Destroy;
begin
  inherited;
end;

function TAttachmentInfoData.GetAttachmentInfoRec(AID: Integer): TsdDataRecord;
begin
  Result := sddAttachmentInfo.Locate('ID;Mac', VarArrayOf([AID, FMacAddr]));
end;

function TAttachmentInfoData.GetAttachmentPath(AID: Integer): string;
var
  Rec: TsdDataRecord;
begin
  Result := '';
  Rec := GetAttachmentInfoRec(AID);
  if Assigned(Rec) then
    Result := Rec.ValueByName('Path').AsString;
end;

procedure TAttachmentInfoData.Open(AConnection: TADOConnection);
begin
  sdpAttachmentInfo.Connection := AConnection;
  sddAttachmentInfo.Open;
end;

procedure TAttachmentInfoData.Save;
begin
  sddAttachmentInfo.Save;
end;

procedure TAttachmentInfoData.SaveAttachmentPath(AID: Integer;
  APath: string);
var
  Rec: TsdDataRecord;
begin
  Rec := GetAttachmentInfoRec(AID);
  if not Assigned(Rec) then
  begin
    Rec := sddAttachmentInfo.Add;
    Rec.ValueByName('ID').AsInteger := AID;
    Rec.ValueByName('UserID').AsInteger := PHPWeb.UserID;
    Rec.ValueByName('Mac').AsString := FMacAddr;
  end;
  Rec.ValueByName('Path').AsString := APath;
  sddAttachmentInfo.Save;
end;

end.
