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

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