source: trunk/Modules/News/News.php@ 838

Last change on this file since 838 was 838, checked in by chronos, 9 years ago
  • Modified: Some libraries moved to Common package located at Packages directory.
  • Modified: Application class System renamed to Core. System class is not just basic parent class for application.
  • Fixed: alert file now use same application class as other files.
  • Modified: Error application module.
File size: 6.7 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/RSS.php');
4
5class ModuleNews extends AppModule
6{
7 var $RSSChannels;
8
9 function __construct(System $System)
10 {
11 parent::__construct($System);
12 $this->Name = 'News';
13 $this->Version = '1.0';
14 $this->Creator = 'Chronos';
15 $this->License = 'GNU/GPL';
16 $this->Description = 'Web site annoucements management';
17 $this->Dependencies = array();
18 $this->RSSChannels = array();
19 }
20
21 function DoStart()
22 {
23 $this->System->RegisterPage('news', 'PageNews');
24 $this->System->RegisterPage('rss', 'PageRSS');
25 $this->RegisterRSS(array('Title' => T('News'), 'Channel' => 'news',
26 'Callback' => array('PageNews', 'ShowRSS'), 'Permission' => LICENCE_ANONYMOUS));
27 }
28
29 function ShowBox()
30 {
31 $Output = '<strong><a href="'.$this->System->Link('/news/').'">'.T('News').':</a></strong><div class="NewsBox">';
32 $DbResult = $this->Database->query('SELECT `News`.`Time`, `User`.`Name`, `News`.`Text`,`News`.`Title`, `News`.`Id` '.
33 ' FROM `News` JOIN `User` ON `User`.`ID` = `News`.`User` ORDER BY `Time` DESC LIMIT 10');
34 while($DbRow = $DbResult->fetch_assoc())
35 {
36 $Output .= '<h4><a href="'.$this->System->Link('/news/?a=item&amp;i='.$DbRow['Id']).'">'.$DbRow['Title'].'</a> ('.HumanDate($DbRow['Time']).')</h4>'.
37 ' <div>'.$DbRow['Text'].' ('.$DbRow['Name'].')</div>';
38 }
39 $Output .= '</div>';
40 return($Output);
41 }
42
43 function RegisterRSS($Channel, $Pos = NULL, $Callback = NULL)
44 {
45 $this->RSSChannels[$Channel['Channel']] = $Channel;
46
47 if(is_null($Pos)) $this->RSSChannelsPos[] = $Channel['Channel'];
48 else {
49 array_splice($this->RSSChannelsPos, $Pos, 0, $Channel['Channel']);
50 }
51 }
52
53 function ShowRSSHeader()
54 {
55 $Output = '';
56 foreach($this->RSSChannels as $Channel)
57 {
58 if($this->System->User->Licence($Channel['Permission']))
59 $Output .= ' <link rel="alternate" title="'.$Channel['Title'].'" href="'.
60 $this->System->Link('/rss/?channel='.$Channel['Channel']).'" type="application/rss+xml" />';
61 }
62 return($Output);
63 }
64}
65
66class PageNews extends Page
67{
68 function Show()
69 {
70 $this->Title = T('News');
71 if(array_key_exists('a', $_POST)) $Action = $_POST['a'];
72 else if(array_key_exists('a', $_GET)) $Action = $_GET['a'];
73 else $Action = '';
74 if($Action == 'add2') $Output = $this->SaveNew();
75 else if($Action == 'add') $Output = $this->ShowAddForm();
76 else if($Action == 'item') $Output = $this->ShowItem();
77 else $Output = $this->ShowList();
78 return($Output);
79 }
80
81 function ShowList()
82 {
83 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `News`');
84 $DbRow = $DbResult->fetch_row();
85 $PageList = GetPageList($DbRow[0]);
86
87 $Output = '<h3>'.T('News').'</h3>'.$PageList['Output'];
88 if($this->System->User->Licence(LICENCE_ADMIN))
89 $Output .= ' <a href="?a=add">'.T('Add').'</a>';
90 $Output .= '<div class="shoutbox">';
91 $DbResult = $this->System->Database->query('SELECT `News`.`Time`, `News`.`Text`, `News`.`Title`, `News`.`Id`, '.
92 '`User`.`Name` AS `User` FROM `News` JOIN `User` ON `User`.`Id`=`News`.`User` ORDER BY `News`.`Time` DESC '.$PageList['SQLLimit']);
93 while($Line = $DbResult->fetch_assoc())
94 {
95 $Output .= '<h4><a href="?a=item&amp;i='.$Line['Id'].'">'.$Line['Title'].'</a> ('.HumanDate($Line['Time']).')</h4><div>'.$Line['Text'].' ('.$Line['User'].')</div>';
96 }
97 $Output .= '</div>'.$PageList['Output'];
98 return($Output);
99 }
100
101 function ShowItem()
102 {
103 if(array_key_exists('i', $_GET))
104 {
105 $Output = '<h3>'.T('News').'</h3>';
106 $DbResult = $this->System->Database->query('SELECT `News`.`Time`, `News`.`Text`, `News`.`Title`, `News`.`Id`, '.
107 '`User`.`Name` AS `User` FROM `News` JOIN `User` ON `User`.`Id`=`News`.`User` WHERE `News`.`Id` = '.($_GET['i'] * 1));
108 if($DbResult->num_rows == 1)
109 {
110 $Line = $DbResult->fetch_assoc();
111 $Output .= '<h4>'.$Line['Title'].' ('.HumanDate($Line['Time']).')</h4><div>'.$Line['Text'].' ('.$Line['User'].')</div>';
112 } else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
113 } else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
114 $Output .= '<br/><a href="'.$this->System->Link('/news/').'">'.T('All news').'</a>';
115 return($Output);
116 }
117
118 function ShowAddForm()
119 {
120 if($this->System->User->Licence(LICENCE_ADMIN))
121 {
122 $Output = '<form action="?" method="POST">'.
123 T('User').': '.$this->System->User->Name.'('.$this->System->User->Id.')<br/> '.
124 T('Title').': <input type="text" name="title" size="40"/><br/>'.
125 T('Content').': <textarea rows="8" cols="40" onkeydown="ResizeTextArea(this)" class="textedit" id="Text" name="text"></textarea><br/>'.
126 '<input type="hidden" name="a" value="add2"/>'.
127 '<input type="submit" value="'.T('Save').'"/><br/>'.
128 '</form>';
129 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
130 return($Output);
131 }
132
133 function SaveNew()
134 {
135 if($this->System->User->Licence(LICENCE_ADMIN))
136 {
137 if(array_key_exists('text', $_POST) and array_key_exists('title', $_POST))
138 {
139 $querty = 'INSERT INTO `News` (`Title`, `Time` ,`User` ,`Text`) VALUES ( "'.$_POST['title'].'", NOW( ) , '.
140 $this->System->User->Id.', "'.$_POST['text'].'")';
141 $this->System->Database->query($querty);
142 $Output = ShowMessage(T('News added'));
143 $this->System->ModuleManager->Modules['Log']->WriteLog('Vložena nová aktualita', LOG_TYPE_ADMINISTRATION);
144 $Output .= $this->ShowList();
145 } else $Output = ShowMessage(T('Data not specified'), MESSAGE_CRITICAL);
146 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
147 return($Output);
148 }
149
150 function ShowRSS()
151 {
152 $Items = array();
153 $DbResult = $this->Database->query('SELECT UNIX_TIMESTAMP(`News`.`Time`) AS `UnixTime`, '.
154 '`News`.`Title`, `News`.`Time`, `User`.`Name`, `News`.`Text`, `News`.`Id` '.
155 'FROM `News` JOIN `User` ON `User`.`ID` = `News`.`User` ORDER BY `Time` DESC LIMIT 10');
156 while($DbRow = $DbResult->fetch_assoc())
157 {
158 $Items[] = array
159 (
160 'Title' => $DbRow['Title'],
161 'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/news/?a=item&amp;i='.$DbRow['Id']),
162 'Description' => $DbRow['Text'].' ('.$DbRow['Name'].')',
163 'Time' => $DbRow['UnixTime'],
164 );
165 }
166 $Output = GenerateRSS(array
167 (
168 'Title' => $this->System->Config['Web']['Title'].' - '.T('System changes'),
169 'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/news/'),
170 'Description' => $this->System->Config['Web']['Description'],
171 'WebmasterEmail' => $this->System->Config['Web']['AdminEmail'],
172 'Items' => $Items,
173 ));
174 return($Output);
175 }
176}
Note: See TracBrowser for help on using the repository browser.