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

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