Changeset 887 for trunk/Modules/News


Ignore:
Timestamp:
Nov 20, 2020, 12:08:12 AM (4 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.
Location:
trunk/Modules/News
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/News/Import/Vismo.php

    r878 r887  
    33class NewsSourceVismo extends NewsSource
    44{
    5   function Import()
     5  function Import(): string
    66  {
    77    $Output = parent::Import();
  • trunk/Modules/News/ImportKinoVatra.php

    r873 r887  
    11<?php
    22
    3 include_once(dirname(__FILE__).'/../../Application/System.php');
    4 $System = new System();
     3include_once(dirname(__FILE__).'/../../Application/Core.php');
     4$System = new Core();
    55$System->ShowPage = false;
    66$System->Run();
  • trunk/Modules/News/ImportTvBeskyd.php

    r873 r887  
    11<?php
    22
    3 include_once(dirname(__FILE__).'/../../Application/System.php');
     3include_once(dirname(__FILE__).'/../../Application/Core.php');
    44$System = new Core();
    55$System->ShowPage = false;
  • trunk/Modules/News/News.php

    r878 r887  
    1313class ModuleNews extends AppModule
    1414{
    15   var $NewsCountPerCategory = 3;
    16   var $UploadedFilesFolder = 'files/news/';
    17 
    18   function __construct($System)
     15  public int $NewsCountPerCategory = 3;
     16  public string $UploadedFilesFolder = 'files/news/';
     17
     18  function __construct(System $System)
    1919  {
    2020    parent::__construct($System);
     
    2828  }
    2929
    30   function DoInstall()
    31   {
    32   }
    33 
    34   function DoUnInstall()
    35   {
    36   }
    37 
    38   function DoStart()
    39   {
    40     $this->System->RegisterPage('aktuality', 'PageNews');
    41     $this->System->RegisterPage(array('aktuality', 'aktualizace'), 'PageNewsUpdate');
     30  function DoInstall(): void
     31  {
     32  }
     33
     34  function DoUnInstall(): void
     35  {
     36  }
     37
     38  function DoStart(): void
     39  {
     40    $this->System->RegisterPage(['aktuality'], 'PageNews');
     41    $this->System->RegisterPage(['aktuality', 'subscription'], 'PageNewsSubscription');
     42    $this->System->RegisterPage(['aktuality', 'rss'], 'PageNewsRss');
     43    $this->System->RegisterPage(['aktuality', 'aktualizace'], 'PageNewsUpdate');
    4244    $this->System->FormManager->RegisterClass('News', array(
    4345      'Title' => 'Aktualita',
     
    8688    if ($this->System->ModuleManager->ModulePresent('Search'))
    8789    {
    88       $this->System->ModuleManager->Modules['Search']->RegisterSearch('Novinky', 'News', array('Title', 'Content'));
    89     }
    90   }
    91 
    92   function ShowNews($Category, $ItemCount, $DaysAgo)
     90      ModuleSearch::Cast($this->System->GetModule('Search'))->RegisterSearch('Novinky', 'News', array('Title', 'Content'));
     91    }
     92  }
     93
     94  function ShowNews(string $Category, int $ItemCount, int $DaysAgo): string
    9395  {
    9496    $ItemCount = abs($ItemCount);
     
    98100    $Output = '<div class="NewsPanel"><div class="Title">'.$Row['Caption'];
    99101    $Output .= '<div class="Action"><a href="aktuality/?category='.$Category.'">Zobrazit</a>';
    100     if ($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category))
     102    if (ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('News', 'Insert', 'Group', $Category))
    101103      $Output .= ' <a href="aktuality/?action=add&amp;category='.$Category.'">Přidat</a>';
    102104    $Output .= '</div></div><div class="Content">';
     
    139141  }
    140142
    141   function LoadSettingsFromCookies()
     143  function LoadSettingsFromCookies(): void
    142144  {
    143145    // Initialize default news setting
     
    163165  }
    164166
    165   function Show()
     167  function Show(): string
    166168  {
    167169    $Output = '';
     
    197199  }
    198200
    199   function ShowCustomizeMenu()
     201  function ShowCustomizeMenu(): string
    200202  {
    201203    $Output = '<form action="?Action=CustomizeNewsSave" method="post">';
     
    220222  }
    221223
    222   function CustomizeSave()
     224  function CustomizeSave(): void
    223225  {
    224226    $Checkbox = array('' => 0, 'on' => 1);
     
    245247  }
    246248
    247   function ModifyContent($Content)
     249  function ModifyContent(string $Content): string
    248250  {
    249251    $Result = '';
     
    254256    {
    255257      $I = strpos($Content, 'http://');
    256       if (($I > 0) and ($Content{$I - 1} != '"'))
     258      if (($I > 0) and ($Content[$I - 1] != '"'))
    257259      {
    258260        $Result .= substr($Content, 0, $I);
     
    272274    return $Result;
    273275  }
     276
     277  static function Cast(AppModule $AppModule): ModuleNews
     278  {
     279    if ($AppModule instanceof ModuleNews)
     280    {
     281      return $AppModule;
     282    }
     283    throw new Exception('Expected ModuleNews type but got '.gettype($AppModule));
     284  }
    274285}
  • trunk/Modules/News/NewsPage.php

    r878 r887  
    33class PageNews extends Page
    44{
    5   var $FullTitle = 'Aktualní informace';
    6   var $ShortTitle = 'Aktuality';
    7   var $ParentClass = 'PagePortal';
    8   var $UploadedFilesFolder;
    9 
    10   function Show()
    11   {
    12     $this->UploadedFilesFolder = $this->System->ModuleManager->Modules['News']->UploadedFilesFolder;
    13     if (count($this->System->PathItems) > 1)
    14     {
    15       if ($this->System->PathItems[1] == 'subscription') return $this->ShowSubscription();
    16         else if ($this->System->PathItems[1] == 'rss') return $this->ShowRSS();
    17         else return PAGE_NOT_FOUND;
    18     } else return $this->ShowMain();
    19   }
    20 
    21   function ShowView()
    22   {
    23     $Output = '';
    24     if (!$this->System->User->CheckPermission('News', 'Display', 'Item')) $Output .= 'Nemáte oprávnění';
     5  function __construct(System $System)
     6  {
     7    parent::__construct($System);
     8    $this->FullTitle = 'Aktualní informace';
     9    $this->ShortTitle = 'Aktuality';
     10    $this->ParentClass = 'PagePortal';
     11  }
     12
     13  function ShowView(): string
     14  {
     15    $Output = '';
     16    if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('News', 'Display', 'Item')) $Output .= 'Nemáte oprávnění';
    2517    else
    2618    {
     
    3527          else $Author = $Row['Name'];
    3628        $Output .= '<div class="Panel"><div class="Title">'.$Row['Title'].' ('.HumanDate($Row['Date']).', '.$Author.')';
    37         if (($this->System->User->User['Id'] == $Row['User']) and ($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category['Id'])))
     29        if ((ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id'] == $Row['User']) and
     30          (ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('News', 'Insert', 'Group', $Category['Id'])))
    3831        {
    3932          $Output .= '<div class="Action">';
     
    4235          $Output .= '</div>';
    4336        }
    44         $Output .= '</div><div class="Content">'.$this->System->ModuleManager->Modules['News']->ModifyContent($Row['Content']).'<br />';
     37        $Output .= '</div><div class="Content">'.ModuleNews::Cast($this->System->GetModule('News'))->ModifyContent($Row['Content']).'<br />';
    4538        if ($Row['Link'] != '') $Output .= '<br/><a href="'.$Row['Link'].'">Odkaz</a>';
    4639        if ($Row['Enclosure'] != '')
     
    5043          foreach ($Enclosures as $Enclosure)
    5144          {
    52             if (file_exists($this->UploadedFilesFolder.$Enclosure))
    53               $Output .= ' <a href="'.$this->System->Link('/'.$this->UploadedFilesFolder.$Enclosure).'">'.$Enclosure.'</a>';
     45            if (file_exists(ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.$Enclosure))
     46              $Output .= ' <a href="'.$this->System->Link('/'.ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.$Enclosure).'">'.$Enclosure.'</a>';
    5447          }
    5548        }
     
    6053  }
    6154
    62   function ShowAdd()
    63   {
    64     $Output = '';
    65     $Category = $this->GetCategory();
    66     if ($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
     55  function ShowAdd(): string
     56  {
     57    $User = &ModuleUser::Cast($this->System->GetModule('User'))->User;
     58    $Output = '';
     59    $Category = $this->GetCategory();
     60    if ($User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
    6761    {
    6862      $this->System->PageHeaders[] = array($this, 'GetPageHeader');
     
    7569      while ($DbRow = $DbResult->fetch_array())
    7670      {
    77         if ($this->System->User->CheckPermission('News', 'Insert', 'Group', $DbRow['Id']))
     71        if ($User->CheckPermission('News', 'Insert', 'Group', $DbRow['Id']))
    7872        {
    7973          if ($DbRow['Id'] == $Category['Id']) $Selected = ' selected="1"';
     
    9690  }
    9791
    98   function ShowAdd2()
    99   {
     92  function ShowAdd2(): string
     93  {
     94    $User = &ModuleUser::Cast($this->System->GetModule('User'))->User;
    10095    $Output = '';
    10196    $RemoteAddr = GetRemoteAddress();
    10297    $Category = $this->GetCategory();
    103     if ($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
     98    if ($User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
    10499    {
    105100      // Process uploaded file
     
    110105        if (array_key_exists($EnclosureName, $_FILES) and ($_FILES[$EnclosureName]['name'] != ''))
    111106        {
    112           $UploadedFilePath = $this->UploadedFilesFolder.basename($_FILES[$EnclosureName]['name']);
     107          $UploadedFilePath = ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.basename($_FILES[$EnclosureName]['name']);
    113108          if (move_uploaded_file($_FILES[$EnclosureName]['tmp_name'], $UploadedFilePath))
    114109          {
     
    124119        $this->Database->insert('News', array('Category' => $Category['Id'], 'Title' => $_POST['title'],
    125120          'Content' => $_POST['content'], 'Date' => 'NOW()', 'IP' => $RemoteAddr,
    126           'Enclosure' => $Enclosures, 'Author' => $this->System->User->User['Name'],
    127           'User' => $this->System->User->User['Id'], 'Link' => $_POST['link']));
     121          'Enclosure' => $Enclosures, 'Author' => $User->User['Name'],
     122          'User' => $User->User['Id'], 'Link' => $_POST['link']));
    128123        $Output .= 'Aktualita přidána!<br />Pokud budete chtít vaši aktualitu smazat, klikněte na odkaz Smazat v seznamu všech aktualit v kategorii.<br /><br />';
    129124        $Output .= '<a href="?category='.$_POST['category'].'">Zpět na seznam aktualit</a>';
    130         $this->System->ModuleManager->Modules['Log']->NewRecord('News', 'Aktualita přidána', $this->Database->insert_id);
     125        ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('News', 'Aktualita přidána', $this->Database->insert_id);
    131126    } else $Output .= 'Do této kategorie nemůžete vkládat aktuality!';
    132127    return $Output;
    133128  }
    134129
    135   function GetPageHeader()
     130  function GetPageHeader(): string
    136131  {
    137132    return '<script src="'.$this->System->Link('/Packages/TinyMCE/tinymce.min.js').'"></script>'.
     
    152147  }
    153148
    154   function ShowEdit()
    155   {
    156     $Output = '';
    157     $Category = $this->GetCategory();
    158     if ($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
     149  function ShowEdit(): string
     150  {
     151    $User = &ModuleUser::Cast($this->System->GetModule('User'))->User;
     152    $Output = '';
     153    $Category = $this->GetCategory();
     154    if ($User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
    159155    {
    160156      $DbResult = $this->Database->query('SELECT * FROM `News` WHERE `Id`='.$_GET['id']);
    161157      $Row = $DbResult->fetch_array();
    162       if (($this->System->User->User['Id'] == $Row['User']))
     158      if (($User->User['Id'] == $Row['User']))
    163159      {
    164160        $this->System->PageHeaders[] = array($this, 'GetPageHeader');
     
    177173  }
    178174
    179   function ShowUpdate()
    180   {
     175  function ShowUpdate(): string
     176  {
     177    $User = &ModuleUser::Cast($this->System->GetModule('User'))->User;
    181178    $Output = '';
    182179    $RemoteAddr = GetRemoteAddress();
    183180    $Category = $this->GetCategory();
    184     if ($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
     181    if ($User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
    185182    {
    186183      $_POST['id'] = $_POST['id'] * 1;
     
    189186      {
    190187        $Row = $DbResult->fetch_array();
    191         if ($this->System->User->User['Id'] == $Row['User'])
     188        if ($User->User['Id'] == $Row['User'])
    192189        {
    193190          $this->Database->update('News', 'Id='.$_POST['id'], array('Title' => $_POST['title'],
     
    201198  }
    202199
    203   function ShowDelete()
    204   {
    205     $Output = '';
    206     $Category = $this->GetCategory();
    207     if ($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
     200  function ShowDelete(): string
     201  {
     202    $User = &ModuleUser::Cast($this->System->GetModule('User'))->User;
     203    $Output = '';
     204    $Category = $this->GetCategory();
     205    if ($User->CheckPermission('News', 'Insert', 'Group', $Category['Id']))
    208206    {
    209207      $DbResult = $this->Database->query('SELECT * FROM `News` WHERE `Id`='.$_GET['id']);
    210208      $Row = $DbResult->fetch_array();
    211       if ($this->System->User->User['Id'] == $Row['User'])
     209      if ($User->User['Id'] == $Row['User'])
    212210      {
    213211        // TODO: Make upload using general File class
     
    218216          foreach ($Enclosures as $Enclosure)
    219217          {
    220             if (file_exists($this->UploadedFilesFolder.$Enclosure)) unlink($this->UploadedFilesFolder.$Enclosure);
     218            if (file_exists(ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.$Enclosure)) unlink(ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.$Enclosure);
    221219          }
    222220        }
     
    228226  }
    229227
    230   function ShowList()
    231   {
    232     $Output = '';
    233     $Category = $this->GetCategory();
    234     if ($this->System->User->CheckPermission('News', 'Display', 'Group', $Category['Id']))
     228  function ShowList(): string
     229  {
     230    $User = &ModuleUser::Cast($this->System->GetModule('User'))->User;
     231    $Output = '';
     232    $Category = $this->GetCategory();
     233    if ($User->CheckPermission('News', 'Display', 'Group', $Category['Id']))
    235234    {
    236235      $PerPage = 20;
     
    250249          else $Author = $Row['Name'];
    251250        $Output .= '<div class="Panel"><div class="Title"><a href="?action=view&amp;id='.$Row['Id'].'">'.$Row['Title'].'</a> ('.HumanDate($Row['Date']).', '.$Author.')';
    252         if (($this->System->User->User['Id'] == $Row['User']) and ($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category['Id'])))
     251        if (($User->User['Id'] == $Row['User']) and ($User->CheckPermission('News', 'Insert', 'Group', $Category['Id'])))
    253252        {
    254253          $Output .= '<div class="Action">';
     
    257256          $Output .= '</div>';
    258257        }
    259         $Output .= '</div><div class="Content">'.$this->System->ModuleManager->Modules['News']->ModifyContent($Row['Content']).'<br />';
     258        $Output .= '</div><div class="Content">'.ModuleNews::Cast($this->System->GetModule('News'))->ModifyContent($Row['Content']).'<br />';
    260259        if ($Row['Link'] != '') $Output .= '<br/><a href="'.$Row['Link'].'">Odkaz</a>';
    261260        if ($Row['Enclosure'] != '')
     
    265264          foreach ($Enclosures as $Enclosure)
    266265          {
    267             if (file_exists($this->UploadedFilesFolder.$Enclosure))
    268               $Output .= ' <a href="'.$this->System->Link('/'.$this->UploadedFilesFolder.$Enclosure).'">'.$Enclosure.'</a>';
     266            if (file_exists(ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.$Enclosure))
     267              $Output .= ' <a href="'.$this->System->Link('/'.ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.$Enclosure).'">'.$Enclosure.'</a>';
    269268          }
    270269        }
     
    277276  }
    278277
    279   function GetCategory()
     278  function GetCategory(): array
    280279  {
    281280    $Category = array('Id' => 1); // Default category
     
    292291  }
    293292
    294   function ShowMain()
     293  function Show(): string
    295294  {
    296295    $Output = '';
     
    306305    return $Output;
    307306  }
    308 
    309   function ShowSubscription()
     307}
     308
     309class PageNewsUpdate extends Page
     310{
     311  function __construct(System $System)
     312  {
     313    parent::__construct($System);
     314    $this->FullTitle = 'Aktualizace aktualit';
     315    $this->ShortTitle = 'Aktualizace aktualit';
     316    $this->ParentClass = 'PageNews';
     317  }
     318
     319  function Show(): string
     320  {
     321    $NewsSources = new NewsSources();
     322    $NewsSources->Database = $this->Database;
     323    if (array_key_exists('i', $_GET)) $Output = $NewsSources->Parse($_GET['i']);
     324      else $Output = $NewsSources->Parse();
     325    return $Output;
     326  }
     327}
     328
     329class PageNewsSubscription extends Page
     330{
     331  function __construct(System $System)
     332  {
     333    parent::__construct($System);
     334    $this->FullTitle = 'Odběry aktualit';
     335    $this->ShortTitle = 'Odběry aktualit';
     336    $this->ParentClass = 'PageNews';
     337  }
     338
     339  function Show(): string
    310340  {
    311341    if (array_key_exists('build', $_GET))
     
    333363    return $Output;
    334364  }
    335 
    336   function ShowRSS()
     365}
     366
     367class PageNewsRss extends Page
     368{
     369  function __construct(System $System)
     370  {
     371    parent::__construct($System);
     372    $this->FullTitle = 'RSS kanál aktualit';
     373    $this->ShortTitle = 'Aktuality RSS';
     374    $this->ParentClass = 'PageNews';
     375  }
     376
     377  function Show(): string
    337378  {
    338379    $this->ClearPage = true;
     
    409450        foreach ($Enclosures as $Enclosure)
    410451        {
    411           if (file_exists($this->UploadedFilesFolder.$Enclosure))
    412             $EnclosuresText .= ' <a href="'.$this->System->Link('/aktuality/'.$this->UploadedFilesFolder.$Enclosure).'">'.$Enclosure.'</a>';
     452          if (file_exists(ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.$Enclosure))
     453            $EnclosuresText .= ' <a href="'.$this->System->Link('/aktuality/'.ModuleNews::Cast($this->System->GetModule('News'))->UploadedFilesFolder.$Enclosure).'">'.$Enclosure.'</a>';
    413454        }
    414455      }
     
    432473  }
    433474}
    434 
    435 class PageNewsUpdate extends Page
    436 {
    437   function __construct($System)
    438   {
    439     parent::__construct($System);
    440     $this->FullTitle = 'Aktualizace aktualit';
    441     $this->ShortTitle = 'Aktualizace aktualit';
    442     $this->ParentClass = 'PageNews';
    443   }
    444 
    445   function Show()
    446   {
    447     $NewsSources = new NewsSources();
    448     $NewsSources->Database = $this->Database;
    449     if (array_key_exists('i', $_GET)) $Output = $NewsSources->Parse($_GET['i']);
    450       else $Output = $NewsSources->Parse();
    451     return $Output;
    452   }
    453 }
    454 
  • trunk/Modules/News/NewsSource.php

    r878 r887  
    11<?php
    22
    3 function GetTextBetween(&$Text, $Start, $End)
     3function GetTextBetween(string &$Text, string $Start, string $End): string
    44{
    55  $Result = '';
     
    3434}
    3535
    36 function HumanDateTimeToTime($DateTime)
     36function HumanDateTimeToTime(string $DateTime): int
    3737{
    3838  if ($DateTime == '') return NULL;
     
    4949}
    5050
    51 function HumanDateToTime($Date)
     51function HumanDateToTime(string $Date): int
    5252{
    5353  if ($Date == '') return NULL;
     
    5555}
    5656
    57 function GetUrlBase($Url)
     57function GetUrlBase(string $Url): string
    5858{
    5959  $Result = parse_url($Url);
     
    6363class NewsSources
    6464{
    65   public $Database;
     65  public Database $Database;
    6666
    6767  function Parse($Id = null)
     
    9494class NewsSource
    9595{
    96   public $Name;
    97   public $URL;
     96  public string $Name;
     97  public string $URL;
    9898  public $Method;
    99   public $Id;
    100   public $Database;
    101   public $NewsItems;
    102   public $AddedCount;
     99  public int $Id;
     100  public Database $Database;
     101  public array $NewsItems;
     102  public int $AddedCount;
    103103  public $Category;
    104104
     
    109109  }
    110110
    111   function Import()
     111  function Import(): string
    112112  {
    113113    return '';
    114114  }
    115115
    116   function DoImport()
     116  function DoImport(): string
    117117  {
    118118    $this->NewsItems = array();
     
    133133class NewsItem
    134134{
    135   var $Database;
    136   var $Title = '';
    137   var $Content = '';
    138   var $Date = '';
    139   var $Link = '';
    140   var $Category = '';
    141   var $Author = '';
    142   var $IP = '';
    143   var $Enclosure = '';
     135  public Database $Database;
     136  public string $Title = '';
     137  public string $Content = '';
     138  public int $Date = 0;
     139  public string $Link = '';
     140  public string $Category = '';
     141  public string $Author = '';
     142  public string $IP = '';
     143  public string $Enclosure = '';
    144144
    145   function AddIfNotExist()
     145  function AddIfNotExist(): int
    146146  {
    147147    $Where = '(`Title` = "'.$this->Database->real_escape_string($this->Title).'") AND '.
Note: See TracChangeset for help on using the changeset viewer.