Ignore:
Timestamp:
Nov 20, 2020, 12:08:12 AM (3 years ago)
Author:
chronos
Message:
  • Added: Static types added to almost all classes, methods and function. Supported by PHP 7.4.
  • Fixed: Various found code issues.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/File/File.php

    r885 r887  
    55class File extends Model
    66{
    7   var $FilesDir;
    8 
    9   function __construct($System)
     7  public string $FilesDir;
     8
     9  function __construct(System $System)
    1010  {
    1111    parent::__construct($System);
     
    1313  }
    1414
    15   function Delete($Id)
     15  function Delete($Id): void
    1616  {
    1717    $DbResult = $this->Database->select('File', 'Name', 'Id='.$Id);
     
    2424  }
    2525
    26   function CreateFromUpload($Name)
     26  function CreateFromUpload(string $Name): string
    2727  {
    2828    // Submited form with file input have to be enctype="multipart/form-data"
     
    4141  }
    4242
    43   function DetectMimeType($FileName)
     43  function DetectMimeType(string $FileName): string
    4444  {
    4545    $MimeTypes = GetMimeTypes();
     
    4949  }
    5050
    51   function Download($Id)
     51  function Download(string $Id): void
    5252  {
    5353    $DbResult = $this->Database->select('File', '*', 'Id='.addslashes($Id));
     
    6767  }
    6868
    69   function GetDir($Id)
     69  function GetDir(string $Id): string
    7070  {
    7171    $DbResult = $this->Database->select('FileDirectory', '*', 'Id='.$Id);
     
    7777  }
    7878
    79   function GetFullPath($Id)
     79  function GetFullPath(string $Id): string
    8080  {
    8181    $DbResult = $this->Database->select('File', '*', 'Id='.addslashes($Id));
     
    9393class PageFile extends Page
    9494{
    95   function Show()
     95  function Show(): string
    9696  {
    9797    if (array_key_exists('id', $_GET)) $Id = $_GET['id'];
     
    9999    else return $this->SystemMessage('Chyba', 'Nezadáno id souboru');
    100100    $this->ClearPage = true;
    101     $Output = $this->System->Modules['File']->Download($Id);
     101    $Output = ModuleFile::Cast($this->System->GetModule('File'))->File->Download($Id);
    102102    return $Output;
    103103  }
     
    106106class PageFileCheck extends Page
    107107{
    108   function __construct($System)
     108  function __construct(System $System)
    109109  {
    110110    parent::__construct($System);
     
    114114  }
    115115
    116   function GetAbsolutePath($Path)
     116  function GetAbsolutePath(string $Path): string
    117117  {
    118118    $Path = trim($Path);
     
    137137  }
    138138
    139   function Show()
     139  function Show(): string
    140140  {
    141141    $Output = '<strong>Missing files:</strong><br>';
    142     if (!$this->System->User->CheckPermission('File', 'Check'))
     142    if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('File', 'Check'))
    143143      return 'Nemáte oprávnění';
    144144    $DbResult = $this->Database->select('File', 'Id');
     
    146146    {
    147147      $Id = $DbRow['Id'];
    148       $FileName = $this->GetAbsolutePath($this->System->Modules['File']->GetFullPath($Id));
     148      $FileName = $this->GetAbsolutePath(ModuleFile::Cast($this->System->GetModule('File'))->File->GetFullPath($Id));
    149149      if (!file_exists($FileName))
    150150        $Output .= '<a href="'.$this->System->Link('/is/?a=view&amp;t=File&amp;i='.$Id).'">'.$FileName.'</a><br>';
     
    156156class ModuleFile extends AppModule
    157157{
    158   function __construct($System)
     158  public File $File;
     159
     160  function __construct(System $System)
    159161  {
    160162    parent::__construct($System);
     
    164166    $this->License = 'GNU/GPLv3';
    165167    $this->Description = 'Base module for file management';
    166     $this->Dependencies = array();
    167   }
    168 
    169   function DoInstall()
    170   {
    171   }
    172 
    173   function DoUninstall()
    174   {
    175   }
    176 
    177   function DoStart()
     168    $this->Dependencies = array('User');
     169
     170    $this->File = new File($this->System);
     171  }
     172
     173  function DoInstall(): void
     174  {
     175  }
     176
     177  function DoUninstall(): void
     178  {
     179  }
     180
     181  function DoStart(): void
    178182  {
    179183    global $Config;
    180184
    181     $this->System->RegisterPage('file', 'PageFile');
    182     $this->System->RegisterPage('file-check', 'PageFileCheck');
    183     $this->System->AddModule(new File($this->System));
    184     $this->System->Modules['File']->FilesDir = dirname(__FILE__).'/../../'.$Config['Web']['UploadFileFolder'];
     185    $this->System->RegisterPage(['file'], 'PageFile');
     186    $this->System->RegisterPage(['file-check'], 'PageFileCheck');
     187    $this->File->FilesDir = dirname(__FILE__).'/../../'.$Config['Web']['UploadFileFolder'];
    185188    $this->System->FormManager->RegisterClass('File', array(
    186189      'Title' => 'Soubor',
     
    271274  }
    272275
    273   function DoStop()
    274   {
    275   }
    276 }
     276  static function Cast(AppModule $AppModule): ModuleFile
     277  {
     278    if ($AppModule instanceof ModuleFile)
     279    {
     280      return $AppModule;
     281    }
     282    throw new Exception('Expected ModuleFile type but got '.gettype($AppModule));
     283  }
     284}
Note: See TracChangeset for help on using the changeset viewer.