Name = 'News';
$this->Version = '1.0';
$this->Creator = 'Chronos';
$this->License = 'GNU/GPL';
$this->Description = 'Web site annoucements management';
$this->Dependencies = array();
$this->RSSChannels = array();
}
function DoStart(): void
{
$this->System->RegisterPage(['news'], 'PageNews');
$this->System->RegisterPage(['rss'], 'PageRSS');
$this->RegisterRSS(array('Title' => T('News'), 'Channel' => 'news',
'Callback' => array('PageNews', 'ShowRSS'), 'Permission' => LICENCE_ANONYMOUS));
Core::Cast($this->System)->RegisterPageHeader('New', array($this, 'ShowRSSHeader'));
}
function ShowBox()
{
$Count = 10;
$Output = ''.T('News').':'.
'
';
$DbResult = $this->Database->query('SELECT `News`.`Time`, `User`.`Name`, `News`.`Text`,`News`.`Title`, `News`.`Id` '.
' FROM `News` JOIN `User` ON `User`.`ID` = `News`.`User` ORDER BY `Time` DESC LIMIT '.$Count);
while ($DbRow = $DbResult->fetch_assoc())
{
$Output .= '
'.
'
'.$DbRow['Text'].' ('.$DbRow['Name'].')
';
}
$Output .= '
';
return $Output;
}
function RegisterRSS($Channel, $Pos = NULL, $Callback = NULL)
{
$this->RSSChannels[$Channel['Channel']] = $Channel;
if (is_null($Pos)) $this->RSSChannelsPos[] = $Channel['Channel'];
else {
array_splice($this->RSSChannelsPos, $Pos, 0, $Channel['Channel']);
}
}
function ShowRSSHeader()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
$Output = '';
foreach ($this->RSSChannels as $Channel)
{
if ($User->Licence($Channel['Permission']))
$Output .= ' ';
}
return $Output;
}
}
class PageNews extends Page
{
function Show(): string
{
$this->Title = T('News');
if (array_key_exists('a', $_POST)) $Action = $_POST['a'];
else if (array_key_exists('a', $_GET)) $Action = $_GET['a'];
else $Action = '';
if ($Action == 'add2') $Output = $this->SaveNew();
else if ($Action == 'add') $Output = $this->ShowAddForm();
else if ($Action == 'item') $Output = $this->ShowItem();
else $Output = $this->ShowList();
return $Output;
}
function ShowList()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
$DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `News`');
$DbRow = $DbResult->fetch_row();
$PageList = GetPageList($DbRow[0]);
$Output = ''.T('News').'
';
if ($User->Licence(LICENCE_ADMIN))
$Output .= ' '.T('Add').'';
$Output .= $PageList['Output'];
$Output .= ''.$PageList['Output'];
return $Output;
}
function ShowItem()
{
$Id = 0;
if (TryGetUrlParameterInt('i', $Id))
{
$Output = ''.T('News').'
';
$DbResult = $this->System->Database->query('SELECT `News`.`Time`, `News`.`Text`, `News`.`Title`, `News`.`Id`, '.
'`User`.`Name` AS `User` FROM `News` JOIN `User` ON `User`.`Id`=`News`.`User` WHERE `News`.`Id` = '.$Id);
if ($DbResult->num_rows == 1)
{
$Line = $DbResult->fetch_assoc();
$Output .= ''.$Line['Title'].' ('.HumanDate($Line['Time']).')
'.$Line['Text'].' ('.$Line['User'].')
';
} else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
} else $Output = ShowMessage(T('Id not valid'), MESSAGE_CRITICAL);
$Output .= '
'.T('All news').'';
return $Output;
}
function ShowAddForm()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
if ($User->Licence(LICENCE_ADMIN))
{
$Output = '';
} else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
$Output .= $this->ShowList();
return $Output;
}
function SaveNew()
{
$User = ModuleUser::Cast($this->System->GetModule('User'))->User;
if ($User->Licence(LICENCE_ADMIN))
{
if (array_key_exists('text', $_POST) and array_key_exists('title', $_POST))
{
$querty = 'INSERT INTO `News` (`Title`, `Time` ,`User` ,`Text`) VALUES ( "'.$_POST['title'].'", NOW( ) , '.
$User->Id.', "'.$_POST['text'].'")';
$this->System->Database->query($querty);
$Output = ShowMessage(T('News added'));
$this->System->ModuleManager->Modules['Log']->WriteLog('Vložena nová aktualita', LOG_TYPE_ADMINISTRATION);
$Output .= $this->ShowList();
} else $Output = ShowMessage(T('Data not specified'), MESSAGE_CRITICAL);
} else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
return $Output;
}
function ShowRSS()
{
$Items = array();
$DbResult = $this->Database->query('SELECT UNIX_TIMESTAMP(`News`.`Time`) AS `UnixTime`, '.
'`News`.`Title`, `News`.`Time`, `User`.`Name`, `News`.`Text`, `News`.`Id` '.
'FROM `News` JOIN `User` ON `User`.`ID` = `News`.`User` ORDER BY `Time` DESC LIMIT 10');
while ($DbRow = $DbResult->fetch_assoc())
{
$Items[] = array
(
'Title' => $DbRow['Title'],
'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/news/?a=item&i='.$DbRow['Id']),
'Description' => $DbRow['Text'].' ('.$DbRow['Name'].')',
'Time' => $DbRow['UnixTime'],
);
}
$Output = GenerateRSS(array
(
'Title' => Core::Cast($this->System)->Config['Web']['Title'].' - '.T('System changes'),
'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/news/'),
'Description' => Core::Cast($this->System)->Config['Web']['Description'],
'WebmasterEmail' => Core::Cast($this->System)->Config['Web']['AdminEmail'],
'Items' => $Items,
));
return $Output;
}
}