unit uControlPanel;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.Buttons,Vcl.Dialogs, System.ImageList,
  Vcl.ImgList,uAttachedFiles,Vcl.ComCtrls;

type
  TControlPanel = class(TFrame)
    bInsertToDocument: TBitBtn;
    bAdd: TBitBtn;
    bDelete: TBitBtn;
    bCut: TBitBtn;
    bCopy: TBitBtn;
    bPaste: TBitBtn;
    bSearch: TBitBtn;
    bSort: TBitBtn;
    ilNormal32: TImageList;
    bGetLink: TBitBtn;
    bDownLoadFile: TBitBtn;
    procedure FrameClick(Sender: TObject);
    procedure bSearchClick(Sender: TObject);
    procedure bSortClick(Sender: TObject);
    procedure bAddClick(Sender: TObject);
    procedure bGetLinkClick(Sender: TObject);
    procedure bInsertToDocumentClick(Sender: TObject);
    procedure bDeleteClick(Sender: TObject);
    procedure bDownLoadFileClick(Sender: TObject);
  private
    { Private declarations }

        FButtonHeight:integer;

        FMaxHeightCompact:Integer;

        FAttachedFiles:TfAttachedFiles;

        procedure GetAllIDFromAllFolders(InitialIDList:TStringList; var FinalIDList:TStringList; var FinalNamesList:TStringList );

        {<  goes through InitialIDList, if some ID has children recursievely call self  }



  public
    { Public declarations }

    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;

    procedure VerticalAlign;
    procedure AlignCompact;
    procedure AlignCompact2; // without CopyCutPasteButtons

    procedure Delete(AIDList:TStringList);

    property MaxHeightCompact:Integer read FMaxHeightCompact write FMaxHeightCompact;

  end;

implementation

{$R *.dfm}

uses uVisualFrame;
var VisualFrame_HTTP:TVisualFrame_HTTP;



procedure TControlPanel.AlignCompact;
begin

  with bInsertToDocument do begin

   Left:=10;
   Top:=10;
   Width:=Self.Width-10;
   Height:=FButtonHeight+20;

   ilNormal32.GetBitmap(20,Glyph);

  end;


    with bAdd do begin

   Left:=10;
   Width:=Trunc(Self.Width/3) -10;
   Top:=bInsertToDocument.Top+bInsertToDocument.Height+10;
   Height:=FButtonHeight;

   Caption:='';
   ilNormal32.GetBitmap(0,Glyph);


  end;


  with bDelete do begin

   Left:=Trunc(Self.Width/3) +10;
   Width:=Trunc(Self.Width/3)-10;

   Top:=bAdd.Top;
   Height:=FButtonHeight;

      Caption:='';
   ilNormal32.GetBitmap(35,Glyph);


  end;


   with bGetLink do begin

   Left:=2*Trunc(Self.Width/3) +10;
   Width:=Trunc(Self.Width/3)-10;;

   Top:=bAdd.Top;
   Height:=FButtonHeight;

   Caption:='';
   ilNormal32.GetBitmap(17,Glyph);


  end;




  with bCut do begin

   Left:=10;
   Top:=bDelete.Top+bDelete.Height+10;
   Width:=Trunc(Self.Width/3)-10;
   Height:=FButtonHeight;

      Caption:='';
   ilNormal32.GetBitmap(58,Glyph);


  end;


  with bCopy do begin

   Left:=Trunc(Self.Width/3)+10;
   Top:=bCut.Top;

   Width:=Trunc(Self.Width/3)-10;
   Height:=FButtonHeight;


      Caption:='';
   ilNormal32.GetBitmap(11,Glyph);


  end;


  with bPaste do begin

   Left:=2*Trunc(Self.Width/3)+10;
   Top:=bCopy.Top;
   Width:=Trunc(Self.Width/3)-10;
   Height:=FButtonHeight;

      Caption:='';
   ilNormal32.GetBitmap(12,Glyph);


  end;




  with bSearch do begin

   Left:=10;
   Top:=bPaste.Top+bPaste.Height+10;
   Width:=Trunc(Self.Width/2) -10;
   Height:=FButtonHeight;

         Caption:='';
   ilNormal32.GetBitmap(37,Glyph);


  end;



  with bSort do begin

   Left:=Trunc(Self.Width/2) +10;
   Top:=bSearch.Top;
   Width:=Trunc(Self.Width/2) -10;
   Height:=FButtonHeight;

         Caption:='';
   ilNormal32.GetBitmap(59,Glyph);


  end;



 FMaxHeightCompact:=4*FButtonHeight+FButtonHeight;
 Self.Height:=FMaxHeightCompact+20;

end;




procedure TControlPanel.AlignCompact2;
begin

  with bInsertToDocument do begin

   Left:=10;
   Top:=10;
   Width:=Self.Width-10;
   Height:=FButtonHeight+20;

   ilNormal32.GetBitmap(20,Glyph);

  end;


    with bAdd do begin

   Left:=10;
   Width:=Trunc(Self.Width/3) -10;
   Top:=bInsertToDocument.Top+bInsertToDocument.Height+10;
   Height:=FButtonHeight;

   Caption:='';
   ilNormal32.GetBitmap(0,Glyph);


  end;


  with bDelete do begin

   Left:=Trunc(Self.Width/3) +10;
   Width:=Trunc(Self.Width/3)-10;

   Top:=bAdd.Top;
   Height:=FButtonHeight;

      Caption:='';
   ilNormal32.GetBitmap(35,Glyph);


  end;


   with bGetLink do begin

   Left:=2*Trunc(Self.Width/3) +10;
   Width:=Trunc(Self.Width/3)-10;;

   Top:=bAdd.Top;
   Height:=FButtonHeight;

   Caption:='';
   ilNormal32.GetBitmap(17,Glyph);


  end;




  with bCut do begin
   Visible:=False;

   {
   Left:=10;
   Top:=bDelete.Top+bDelete.Height+10;
   Width:=Trunc(Self.Width/3)-10;
   Height:=FButtonHeight;

      Caption:='';
   ilNormal32.GetBitmap(58,Glyph);
    }

  end;


  with bCopy do begin

   Visible:=false;
   {
   Left:=Trunc(Self.Width/3)+10;
   Top:=bCut.Top;

   Width:=Trunc(Self.Width/3)-10;
   Height:=FButtonHeight;


      Caption:='';
   ilNormal32.GetBitmap(11,Glyph);
   }

  end;


  with bPaste do begin

   Visible:=False;

   {
   Left:=2*Trunc(Self.Width/3)+10;
   Top:=bCopy.Top;
   Width:=Trunc(Self.Width/3)-10;
   Height:=FButtonHeight;

      Caption:='';
   ilNormal32.GetBitmap(12,Glyph);
   }

  end;




  with bSearch do begin

   Left:=10;

   Width:=Trunc(Self.Width/2) -10;


   Top:=bGetLink.Top+bGetLink.Height+10;

   Height:=FButtonHeight;

         Caption:='';
   ilNormal32.GetBitmap(37,Glyph);


  end;



  with bSort do begin


   Top:=bSearch.Top;

   Left:=Trunc(Self.Width/2) +10;

   Width:=Trunc(Self.Width/2) -10;



   Height:=FButtonHeight;

         Caption:='';
   ilNormal32.GetBitmap(59,Glyph);


  end;



  with bDownLoadFile do begin


   Top:=bSearch.Top+bSearch.Height+10;

   Left:=10;

   Width:=bInsertToDocument.Width;



   Height:=FButtonHeight;

         //Caption:='';
   ilNormal32.GetBitmap(61,Glyph);


  end;



 FMaxHeightCompact:=4*FButtonHeight+FButtonHeight;
 Self.Height:=FMaxHeightCompact+20;

end;



procedure TControlPanel.bAddClick(Sender: TObject);
begin

with VisualFrame_HTTP do
bUploadClick(Self);

end;



procedure TControlPanel.bDeleteClick(Sender: TObject);


var InitialIDListToDelete:TStringList;
    FinalIDListToDelete:TStringList;
    FinalNamesListToDelete:TStringList;


    i: integer;
    s:string;
    FileName: String;
    j: integer;
    AllAttachedFiles: TStringList;


     AttachedToTestsSL:TStringList;
     AttachedToQuestionairesSL:TStringList;
     AttachedToCoursesSL:TStringList;

     GeneralAttachedList:TStringList;

     AllNotAttachedFileNames:TStringList;

     Item:TListItem;
     k:integer;
     idPointer:^integer;


     AttachedFilesForm:TfAttachedFiles;

begin

//


case
  Application.MessageBox('Вы действительно хотите удалить выбранное ?',
  'Сообщение системы', MB_YESNO + MB_ICONQUESTION) of
  IDYES:
    begin


             InitialIDListToDelete:=TStringList.Create;
             FinalIDListToDelete:=TStringList.Create;
             FinalNamesListToDelete:=TStringList.Create;
             AllAttachedFiles:=TStringList.Create;




             AttachedToTestsSL:=TStringList.Create;
             AttachedToQuestionairesSL:=TStringList.Create;
             AttachedToCoursesSL:=TStringList.Create;

             GeneralAttachedList:=TStringList.Create;

             AllNotAttachedFileNames:=TStringList.Create;


             AttachedFilesForm:=TfAttachedFiles.Create(Self);

            try

            with VisualFrame_HTTP do begin



            if DBGrid.SelectedRows.Count>0 then
                      with DBGrid.DataSource.DataSet do

                      for i:=0 to DBGrid.SelectedRows.Count-1 do

                          begin

                          GotoBookmark(pointer(DBGrid.SelectedRows.Items[i]));

                          InitialIDListToDelete.Add(

                          FieldByName('id').AsInteger.ToString()

                          );

                          end else


                          if DBGrid.DataSource.DataSet.RecordCount>0 then

                          InitialIDListToDelete.Add(
                          DBGrid.DataSource.DataSet.FieldByName('id').AsInteger.ToString()
                          );




                   // Now we have InitialIDListToDelete
                   { but each ID can be a folder so we have to recursievely get all ID from all folders if they are }


                   GetAllIDFromAllFolders(InitialIDListToDelete,FinalIDListToDelete,FinalNamesListToDelete);
        
                   // Filling FAttachedFiles



                   //Check if some Records Attached

                   for j := 0 to FinalIDListToDelete.Count-1 do

                   begin

                   if

                     VisualFrame_HTTP.DataBaseUnit.
                     IsFileAttached(FinalIDListToDelete[j].ToInteger(),

                       Filename,

                       AttachedToTestsSL,
                       AttachedToQuestionairesSL,
                       AttachedToCoursesSL


                       )

                     then

                             begin



                             // fill ListView of FAttachedFiles

                              with AttachedFilesForm do begin

                                // Creating 1 List of ALL 3 Lists

                                 for k:= 0 to AttachedToTestsSL.Count-1 do
                                 GeneralAttachedList.Add(AttachedToTestsSL[k]+'( тест )');


                                 for k:= 0 to AttachedToQuestionairesSL.Count-1 do
                                 GeneralAttachedList.Add(AttachedToQuestionairesSL[k]+'( опросник )');


                                  for k:= 0 to AttachedToCoursesSL.Count-1 do
                                 GeneralAttachedList.Add(AttachedToCoursesSL[k]+'( курс )');



                                  ListViewAttachedToDocuments.Items.Clear;

                                  Item := ListViewAttachedToDocuments.Items.Add;
                                  Item.Caption := Filename;
                                  Item.SubItems.Add('Да');
                                  Item.SubItems.Add(GeneralAttachedList[0]);
                                  Item.Checked:=true;


                                  new(idPointer);
                                  idPointer^:=FinalIDListToDelete[j].ToInteger();
                                  Item.Data:=idPointer;

                                                  // ask database Folder or File?
                                  with VisualFrame_HTTP.DataBaseUnit.qSelectFromFoldersAndFilesWhereID do begin

                                   Params.ParamValues['id']:=Integer(idPointer^);

                                   Open();

                                   if FieldByName('IsFolder').AsBoolean then  Item.ImageIndex:=0
                                   else Item.ImageIndex:=1;

                                   Close();

                                  end;





                                      for k := 1 to GeneralAttachedList.Count-1 do begin

                                      Item := ListViewAttachedToDocuments.Items.Add;
                                      Item.Caption := '  ';
                                      Item.SubItems.Add('Да');
                                      Item.SubItems.Add(GeneralAttachedList[k]);
                                      Item.Checked:=true;

                                      new(idPointer);
                                      idPointer^:=FinalIDListToDelete[j].ToInteger();
                                      Item.Data:=idPointer;



                                                            // ask database Folder or File?
                                              // ask database Folder or File?
                                          if VisualFrame_HTTP.DataBaseUnit.IsFolder(Integer(idPointer^)) then
                                          Item.ImageIndex:=0 else Item.ImageIndex:=1;



                                      end;


                                    // Adding To All List
                                   // ListViewAllDeletedFiles;



                                  Item := ListViewAllDeletedFiles.Items.Add;
                                  Item.Caption := Filename;
                                  Item.SubItems.Add('Да');
                                  Item.SubItems.Add(GeneralAttachedList[0]);
                                  Item.Checked:=true;


                                  new(idPointer);
                                  idPointer^:=FinalIDListToDelete[j].ToInteger();
                                  Item.Data:=idPointer;

                                                          // ask database Folder or File?
                                            // ask database Folder or File?
                                          if VisualFrame_HTTP.DataBaseUnit.IsFolder(Integer(idPointer^)) then
                                          Item.ImageIndex:=0 else Item.ImageIndex:=1;






                                      for k := 1 to GeneralAttachedList.Count-1 do begin

                                      Item := ListViewAllDeletedFiles.Items.Add;
                                      Item.Caption := '  ';
                                      Item.SubItems.Add('Да');
                                      Item.SubItems.Add(GeneralAttachedList[k]);
                                      Item.Checked:=true;

                                      new(idPointer);
                                      idPointer^:=FinalIDListToDelete[j].ToInteger();
                                      Item.Data:=idPointer;



                                                            // ask database Folder or File?
                                                 // ask database Folder or File?
                                          if VisualFrame_HTTP.DataBaseUnit.IsFolder(Integer(idPointer^)) then
                                          Item.ImageIndex:=0 else Item.ImageIndex:=1;


                                      end;





                              end;


                             end else


                             // Not Attached Folders And Files

                             begin

                             //

                              with VisualFrame_HTTP.DataBaseUnit.qSelectFromFoldersAndFilesWhereID do begin
                              AllNotAttachedFileNames.Clear;

                               params.ParamValues['id']:=FinalIDListToDelete[j].ToInteger();

                               Open();

                               AllNotAttachedFileNames.Add( FieldByName('name').AsString );


                               Close();

                              end;

                               with AttachedFilesForm.ListViewNotAttached do begin

                                      for k := 0 to AllNotAttachedFileNames.Count-1 do begin

                                        Item := Items.Add;
                                        Item.Caption :=AllNotAttachedFileNames[k];
                                        Item.SubItems.Add('N/A');
                                        Item.SubItems.Add('N/A');
                                        Item.Checked:=true;

                                        new(idPointer);
                                        idPointer^:=FinalIDListToDelete[j].ToInteger();
                                        Item.Data:=idPointer;


                                                      // ask database Folder or File?
                                                    // ask database Folder or File?
                                          if VisualFrame_HTTP.DataBaseUnit.IsFolder(Integer(idPointer^)) then
                                          Item.ImageIndex:=0 else Item.ImageIndex:=1;


                                      end;


                             // Fill All Deleted FIles

                                  With AttachedFilesForm.ListViewAllDeletedFiles do begin


                                    for k := 0 to AllNotAttachedFileNames.Count-1 do begin

                                        Item := Items.Add;
                                        Item.Caption :=AllNotAttachedFileNames[k];
                                        Item.SubItems.Add('N/A');
                                        Item.SubItems.Add('N/A');
                                        Item.Checked:=true;

                                        new(idPointer);
                                        idPointer^:=FinalIDListToDelete[j].ToInteger();
                                        Item.Data:=idPointer;


                                                      // ask database Folder or File?
                                          if VisualFrame_HTTP.DataBaseUnit.IsFolder(Integer(idPointer^)) then
                                          Item.ImageIndex:=0 else Item.ImageIndex:=1;






                                      end;



                             end;




                   end;


                     end;

                   end;





                   // if something is attached then manage it
                  if GeneralAttachedList.Count>0 then AttachedFilesForm.ShowModal()

                   else

                    // Deleting All
                     begin

                      VisualFrame_HTTP.Delete_PS_HTTPClient.DeleteByIDList(FinalIDListToDelete);
                        VisualFrame_HTTP.UpdateCurrentFolder;


                     end;



            end;


            finally

            InitialIDListToDelete.Free;
            FinalIDListToDelete.Free;
            FinalNamesListToDelete.Free;

            AllAttachedFiles.Free;

             AttachedToTestsSL.Free;
             AttachedToQuestionairesSL.Free;
             AttachedToCoursesSL.Free;

             GeneralAttachedList.Free;

             AllNotAttachedFileNames.Free;


             AttachedFilesForm.DisposePointers;
             AttachedFilesForm.Free;
            end;







    end;
  IDNO:
    begin
    Exit;
    end;
end;







end;

procedure TControlPanel.bDownLoadFileClick(Sender: TObject);
begin


VisualFrame_HTTP.DownloadFile;

//

end;

procedure TControlPanel.bGetLinkClick(Sender: TObject);
var Link:string;

begin

with VisualFrame_HTTP do begin

Link:=DBGrid.DataSource.DataSet.
FieldByName('Link').AsString;

 GetLinkForm.eLink.Text:=Link;

 GetLinkForm.ShowModal;

end;


end;

procedure TControlPanel.bInsertToDocumentClick(Sender: TObject);
begin


 with VisualFrame_HTTP do begin

 if DataBaseUnit.IsThisPictureByID(
 DBGrid.DataSource.DataSet.FieldByName('id').AsInteger
 )

 then

    begin

    InsertPictureInDocument.Image.Picture.Bitmap.Assign(
    image.Picture.Bitmap
    );

    InsertPictureInDocument.ShowModal; 
    
    end;
 
 end;



end;

procedure TControlPanel.bSearchClick(Sender: TObject);
begin

  with VisualFrame_HTTP do begin


    if pSearchPanel.Visible  then pSearchPanel.Hide
    else pSearchPanel.Show;


  end;


end;

procedure TControlPanel.bSortClick(Sender: TObject);
begin

  with VisualFrame_HTTP do begin


    if pSortPanel.Visible  then pSortPanel.Hide
    else pSortPanel.Show;


  end;



end;

constructor TControlPanel.Create(AOwner: TComponent);
begin

inherited;

//

    if (AOwner is TVisualFrame_HTTP)  then
    VisualFrame_HTTP:=(AOwner as TVisualFrame_HTTP);

    FButtonHeight:=40;

    FAttachedFiles:=TfAttachedFiles.Create(Self);

end;

procedure TControlPanel.Delete(AIDList: TStringList);
begin

    VisualFrame_HTTP.Delete_PS_HTTPClient.DeleteByIDList(AIDList);
                        VisualFrame_HTTP.UpdateCurrentFolder;


end;

destructor TControlPanel.Destroy;
begin

//FAttachedFiles.DisposePointers;

  inherited;
end;

procedure TControlPanel.FrameClick(Sender: TObject);
begin

// ShowMessage('Hello');

end;

procedure TControlPanel.GetAllIDFromAllFolders(InitialIDList:TStringList;
var FinalIDList: TStringList; var FinalNamesList:TStringList  );

var i:integer;
    IDChildrenList:TStringList;

begin

//

IDChildrenList:=TStringList.Create;

try

      try


       for i := 0 to InitialIDList.Count-1 do

             begin

             FinalIDList.Add(InitialIDList[i]);


              with VisualFrame_HTTP.DataBaseUnit.qSelectFromFoldersAndFilesWhereID do begin

              Params.ParamValues['id']:=InitialIDList[i];

              Open();

              FinalNamesList.Add( FieldByName('name').AsString );


              Close();


              end;


             //Check if there are children


                  with VisualFrame_HTTP.DataBaseUnit.qSelectChildrenFromFoldersAndFiles do begin

                  Params.ParamValues['idParent']:=InitialIDList[i];

                  Open();


                   while not EOF do begin

                    IDChildrenList.Add( FieldByName('id').AsInteger.ToString );

                   Next();
                   end;

                  Close();

                  end;


                  if IDChildrenList.Count>0 then

                        //recursievely call itself
                        GetAllIDFromAllFolders(IDChildrenList,FinalIDList,FinalNamesList);
             end;

      except

       on E:Exception do raise Exception.Create('Error Message '+ E.Message);

      end;


finally
  IDChildrenList.Free;
end;



end;

procedure TControlPanel.VerticalAlign;
begin


  with bInsertToDocument do begin

   Left:=10;
   Top:=10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

   Caption:='В документ';
   ilNormal32.GetBitmap(20,Glyph);

  end;


  with bAdd do begin

   Left:=10;
   Top:=bInsertToDocument.Top+FButtonHeight+10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

   Caption:='Добавить';
   ilNormal32.GetBitmap(0,Glyph);

  end;

  with bDelete do begin

   Left:=10;
   Top:=bAdd.Top+FButtonHeight+10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

   Caption:='Удалить';
   ilNormal32.GetBitmap(35,Glyph);


  end;


    with bGetLink do begin

   Left:=10;
   Top:=bDelete.Top+FButtonHeight+10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

   Caption:='Получить ссылку';
   ilNormal32.GetBitmap(17,Glyph);

  end;




  with bCut do begin

   Left:=10;
   Top:=bGetLink.Top+FButtonHeight+10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

   Caption:='Вырезать';
   ilNormal32.GetBitmap(58,Glyph);

  end;


  with bCopy do begin

   Left:=10;
   Top:=bCut.Top+FButtonHeight+10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

   Caption:='Копировать';
   ilNormal32.GetBitmap(11,Glyph);


  end;


  with bPaste do begin

   Left:=10;
   Top:=bCopy.Top+FButtonHeight+10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

   Caption:='Вставвить';
   ilNormal32.GetBitmap(12,Glyph);


  end;


  with bSearch do begin

   Left:=10;
   Top:=bPaste.Top+FButtonHeight+10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

      Caption:='Поиск';
   ilNormal32.GetBitmap(37,Glyph);


  end;



  with bSort do begin

   Left:=10;
   Top:=bSearch.Top+FButtonHeight+10;
   Width:=Self.Width-10;
   Height:=FButtonHeight;

      Caption:='Сортировка';
   ilNormal32.GetBitmap(59,Glyph);


  end;


 Self.Height:=10*FButtonHeight+FButtonHeight;
// FMaxHeightCompact;

end;

end.
