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

Last change on this file since 711 was 711, checked in by chronos, 10 years ago
  • Modified: Some FormType definition moved from global file to corresponding modules.
  • Property svn:executable set to *
File size: 11.4 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/NewsPage.php');
4
5function CategoryItemCompare($Item1, $Item2)
6{
7 if ($Item1['Index'] == $Item2['Index']) return(0);
8 return ($Item1['Index'] > $Item2['Index']) ? -1 : 1;
9}
10
11class ModuleNews extends AppModule
12{
13 var $NewsCountPerCategory = 3;
14 var $UploadedFilesFolder = 'files/news/';
15
16 function __construct($System)
17 {
18 parent::__construct($System);
19 $this->Name = 'News';
20 $this->Version = '1.0';
21 $this->Creator = 'Chronos';
22 $this->License = 'GNU/GPL';
23 $this->Description = 'News and news groups management';
24 $this->Dependencies = array('User', 'Log', 'File');
25 $this->SupportedModules = array('Search');
26 }
27
28 function DoInstall()
29 {
30 }
31
32 function DoUnInstall()
33 {
34 }
35
36 function DoStart()
37 {
38 $this->System->RegisterPage('aktuality', 'PageNews');
39 $this->System->FormManager->RegisterClass('News', array(
40 'Title' => 'Aktualita',
41 'Table' => 'News',
42 'DefaultSortColumn' => 'Date',
43 'DefaultSortOrder' => 1,
44 'Items' => array(
45 'Category' => array('Type' => 'TNewsCategory', 'Caption' => 'Kategorie', 'Default' => 0),
46 'Title' => array('Type' => 'String', 'Caption' => 'Nadpis', 'Default' => ''),
47 'Content' => array('Type' => 'Text', 'Caption' => 'Obsah', 'Default' => ''),
48 'Date' => array('Type' => 'Date', 'Caption' => 'Datum', 'Default' => ''),
49 'Author' => array('Type' => 'String', 'Caption' => 'Autor', 'Default' => ''),
50 'Enclosure' => array('Type' => 'String', 'Caption' => 'Přílohy', 'Default' => ''),
51 'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => ''),
52 'IP' => array('Type' => 'IPv4Address', 'Caption' => 'IP adresa', 'Default' => '', 'ReadOnly' => true),
53 'Link' => array('Type' => 'Hyperlink', 'Caption' => 'Odkaz', 'Default' => ''),
54 ),
55 ));
56 $this->System->FormManager->RegisterClass('NewsCategory', array(
57 'Title' => 'Kategorie aktualit',
58 'Table' => 'NewsCategory',
59 'Items' => array(
60 'Caption' => array('Type' => 'String', 'Caption' => 'Titulek', 'Default' => ''),
61 'RSS' => array('Type' => 'Hyperlink', 'Caption' => 'Zdroj RSS', 'Default' => ''),
62 'Permission' => array('Type' => 'Boolean', 'Caption' => 'Veřejné upravitelné', 'Default' => ''),
63 'Sequence' => array('Type' => 'Integer', 'Caption' => 'Pořadí', 'Default' => ''),
64 'Group' => array('Type' => 'Integer', 'Caption' => 'Skupina', 'Default' => ''),
65 'News' => array('Type' => 'TNewsList', 'Caption' => 'Aktuality', 'Default' => ''),
66 ),
67 ));
68 $this->System->FormManager->RegisterFormType('TNewsCategory', array(
69 'Type' => 'Reference',
70 'Table' => 'NewsCategory',
71 'Id' => 'Id',
72 'Name' => 'Caption',
73 'Filter' => '1',
74 ));
75 $this->System->FormManager->RegisterFormType('TNewsList', array(
76 'Type' => 'ManyToOne',
77 'Table' => 'News',
78 'Id' => 'Id',
79 'Ref' => 'Category',
80 'Filter' => '1',
81 ));
82
83 if($this->System->ModuleManager->ModulePresent('Search'))
84 {
85 $this->System->ModuleManager->Modules['Search']->RegisterSearch('Novinky', 'News', array('Title', 'Content'));
86 }
87 }
88
89 function ShowNews($Category, $ItemCount, $DaysAgo)
90 {
91 $ItemCount = abs($ItemCount);
92 $DaysAgo = abs($DaysAgo);
93 $DbResult = $this->Database->select('NewsCategory', '*', 'Id='.$Category);
94 $Row = $DbResult->fetch_array();
95 $Output = '<div class="NewsPanel"><div class="Title">'.$Row['Caption'];
96 $Output .= '<div class="Action"><a href="aktuality/?category='.$Category.'">Zobrazit</a>';
97 if($this->System->User->CheckPermission('News', 'Insert', 'Group', $Category))
98 $Output .= ' <a href="aktuality/?action=add&amp;category='.$Category.'">Přidat</a>';
99 $Output .= '</div></div><div class="Content">';
100 $DbResult = $this->Database->query('SELECT `News`.*, `User`.`Name` FROM `News` LEFT JOIN `User` ON `User`.`Id`=`News`.`User` WHERE (`News`.`Category`='.$Category.') AND (DATE_SUB(NOW(), INTERVAL '.$DaysAgo.' DAY) < `News`.`Date`) ORDER BY `News`.`Date` DESC LIMIT 0,'.$ItemCount);
101
102 $Index = 0;
103 $FontSize = 12;
104 if($DbResult->num_rows > 0)
105 {
106 $Output .= '<table class="NewsTable">';
107 while($Row = $DbResult->fetch_array())
108 {
109 if($Row['Name'] == '') $Author = $Row['Author'];
110 else $Author = $Row['Name'];
111 $Output .= '<tr><td onclick="window.location=\'aktuality/?action=view&amp;id='.$Row['Id'].
112 '\'" onmouseover="zobraz('."'new".$Category.$Index."'".')" style="cursor: pointer; margin: 0px;">'.
113 '<table class="NewsItemFrame"><tr><td style="font-size: '.$FontSize.'pt"><strong>'.$Row['Title'].'</strong></td>'.
114 '<td align="right" style="font-size: '.$FontSize.'pt">'.$Author.' ('.HumanDate($Row['Date']).')</td></tr></table>';
115 $Output .= '<div id="new'.$Category.$Index.'" class="NewsTableItem">'.$this->ModifyContent($Row['Content']);
116 if($Row['Link'] != '') $Output .= '<br/><a href="'.$Row['Link'].'">Odkaz</a>';
117
118 if($Row['Enclosure'] != '')
119 {
120 $Output .= '<br />Přílohy: ';
121 $Enclosures = explode(';', $Row['Enclosure']);
122 foreach($Enclosures as $Enclosure)
123 {
124 if(file_exists($this->UploadedFilesFolder.$Enclosure))
125 $Output .= ' <a href="'.$this->UploadedFilesFolder.$Enclosure.'">'.$Enclosure.'</a>';
126 }
127 }
128 $Output .= '</div></td></tr>';
129 $Index = $Index + 1;
130 $FontSize = $FontSize - 1;
131 }
132 $Output .= '</table>';
133 }
134 $Output .= '</div></div>';
135 return($Output);
136 }
137
138 function LoadSettingsFromCookies()
139 {
140 // Initialize default news setting
141 $this->NewsSetting = array();
142 $I = 1;
143 $DbResult = $this->Database->select('NewsCategory', '*', '1 ORDER BY Sequence');
144 while($NewsCategory = $DbResult->fetch_array())
145 {
146 $this->NewsSetting[] = array('CategoryId' => $NewsCategory['Id'], 'Index' => $I, 'Enabled' => 1,
147 'ItemCount' => $this->System->Config['Web']['News']['Count'], 'DaysAgo' => $this->System->Config['Web']['News']['DaysAgo'], 'Group' => $NewsCategory['Group']);
148 $I++;
149 }
150 // Merge defaults with user setting
151 if(array_key_exists('NewsSetting', $_COOKIE))
152 {
153 $NewsSettingCookie = unserialize($_COOKIE['NewsSetting']);
154 foreach($this->NewsSetting as $Index => $this->NewSetting)
155 {
156 if(array_key_exists($Index, $NewsSettingCookie))
157 $this->NewsSetting[$Index] = array_merge($this->NewSetting, $NewsSettingCookie[$Index]);
158 }
159 }
160 }
161
162 function Show()
163 {
164 $Output = '';
165
166 $this->LoadSettingsFromCookies();
167
168 if(array_key_exists('Action', $_GET))
169 {
170 // Show news customize menu
171 if($_GET['Action'] == 'CustomizeNews')
172 {
173 $Output .= $this->ShowCustomizeMenu();
174 }
175 }
176
177 $Output .= '<div onmouseout="skryj(predchozi)">';
178
179 $ColumnCount = 2;
180 $Output .= '<table style="width: 100%"><tr>';
181 for($Column = 1; $Column <= $ColumnCount; $Column++)
182 {
183 $Output .= '<td style="vertical-align: top; width: '.round(100 / $ColumnCount).'%;">';
184 foreach($this->NewsSetting as $SettingItem)
185 if(($SettingItem['Enabled'] == 1) and ($SettingItem['Group'] == $Column))
186 $Output .= $this->ShowNews($SettingItem['CategoryId'], $SettingItem['ItemCount'], $SettingItem['DaysAgo']);
187 $Output .= '</td>';
188 }
189 $Output .= '</tr></table>';
190
191 $Output .= '<a href="aktuality/subscription"><img class="RSSIcon" src="images/rss20.png" alt="Aktuality přes RSS" /></a> <a href="aktuality/subscription">Automatické sledování novinek</a>';
192 $Output .= '</div>';
193 return($Output);
194 }
195
196 function ShowCustomizeMenu()
197 {
198 $Output = '<form action="?Action=CustomizeNewsSave" method="post">';
199 $Output .= '<table width="100%" cellspacing="0" border="1"><tr><td><table cellspacing="0" width="100%">';
200 $Output .= '<tr><th>Kategorie</th><th>Pozice</th><th>Zobrazit</th><th>Max. počet</th><th>Posledních dnů</th><th>Sloupec</th></tr>';
201 $I = 0;
202 foreach($this->NewsSetting as $SettingItem)
203 {
204 $DbResult = $this->Database->select('NewsCategory', '*', 'Id='.$SettingItem['CategoryId']);
205 $NewsCategory = $DbResult->fetch_array();
206 $Output .= '<tr><td>'.$NewsCategory['Caption'].'</td><td align="center"><input type="text" size="2" name="NewsCategoryIndex'.$I.'" value="'.$SettingItem['Index'].'" /></td><td align="center"><input type="checkbox" name="NewsCategoryEnabled'.$I.'"';
207 if($SettingItem['Enabled'] == 1) $Output .= ' checked="checked"';
208 $Output .= ' /></td>'.
209 '<td align="center"><input type="text" size="2" name="NewsCategoryCount'.$I.'" value="'.$SettingItem['ItemCount'].'" />'.
210 '<input type="hidden" name="NewsCategoryId'.$I.'" value="'.$SettingItem['CategoryId'].'" /></td>'.
211 '<td align="center"><input type="text" size="3" name="NewsCategoryDaysAgo'.$I.'" value="'.$SettingItem['DaysAgo'].'" /></td>'.
212 '<td><input type="text" size="3" name="NewsColumn'.$I.'" value="'.$SettingItem['Group'].'"/></td></tr>';
213 $I++;
214 }
215 $Output .= '</table><input type="hidden" name="NewsCategoryCount" value="'.count($this->NewsSetting).'" /><input type="submit" value="Uložit" /></form></td></tr></table><br>';
216 return($Output);
217 }
218
219 function CustomizeSave()
220 {
221 $Checkbox = array('' => 0, 'on' => 1);
222 $Setting = array();
223 for($I = 0; $I < $_POST['NewsCategoryCount']; $I++)
224 {
225 if(($_POST['NewsCategoryDaysAgo'.$I] * 1) < 0) $_POST['NewsCategoryIndex'.$I] = 0;
226 if(($_POST['NewsCategoryCount'.$I] * 1) < 0) $_POST['NewsCategoryCount'.$I] = 0;
227 if(($_POST['NewsColumn'.$I] * 1) < 1) $_POST['NewsColumn'.$I] = 1;
228 if(!array_key_exists('NewsCategoryEnabled'.$I, $_POST)) $_POST['NewsCategoryEnabled'.$I] = '';
229 $Setting[] = array('CategoryId' => $_POST['NewsCategoryId'.$I], 'Enabled' => $Checkbox[$_POST['NewsCategoryEnabled'.$I]], 'ItemCount' => ($_POST['NewsCategoryCount'.$I]*1), 'DaysAgo' => ($_POST['NewsCategoryDaysAgo'.$I]*1), 'Index' => ($_POST['NewsCategoryIndex'.$I]*1),
230 'Group' => $_POST['NewsColumn'.$I]);
231 }
232 // Sort indexes
233 usort($Setting, 'CategoryItemCompare');
234 $Setting = array_reverse($Setting);
235 // Normalize indexes
236 foreach($Setting as $Index => $Item)
237 $Setting[$Index]['Index'] = $Index + 1;
238
239 // Store new cookie value
240 $_COOKIE['NewsSetting'] = serialize($Setting);
241 setcookie('NewsSetting', $_COOKIE['NewsSetting'], time() + 60 * 60 * 24 * 365);
242 }
243
244 function ModifyContent($Content)
245 {
246 $Result = '';
247
248 // Make HTML link from URL
249 $I = 0;
250 while(strpos($Content, 'http://') !== false)
251 {
252 $I = strpos($Content, 'http://');
253 if(($I > 0) and ($Content{$I - 1} != '"'))
254 {
255 $Result .= substr($Content, 0, $I);
256 $Content = substr($Content, $I);
257 if(strpos($Content, ' ') !== false)
258 $URL = substr($Content, 0, strpos($Content, ' '));
259 else $URL = substr($Content, 0);
260 $Result .= '<a href="'.$URL.'">'.$URL.'</a>';
261 $Content = substr($Content, strlen($URL));
262 } else
263 {
264 $Result .= substr($Content, 0, $I + 1);
265 $Content = substr($Content, $I + 1);
266 }
267 }
268 $Result .= $Content;
269 return($Result);
270 }
271}
Note: See TracBrowser for help on using the repository browser.