1 | unit News;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, SqlDatabase, ModuleUser, Utils, Generics.Collections, WebSession,
|
---|
7 | HTTPServer, ModuleBase, Common;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TNewsSettingItem = class
|
---|
11 | Index: Integer;
|
---|
12 | Enabled: Boolean;
|
---|
13 | CategoryId: Integer;
|
---|
14 | Group: Integer;
|
---|
15 | ItemCount: Integer;
|
---|
16 | DaysAgo: Integer;
|
---|
17 | end;
|
---|
18 |
|
---|
19 | TNewsSettingItems = class(TObjectList<TNewsSettingItem>)
|
---|
20 | end;
|
---|
21 |
|
---|
22 | { TNews }
|
---|
23 |
|
---|
24 | TNews = class
|
---|
25 | //Dependencies: TListString; // array('User', 'Log');
|
---|
26 | NewsCountPerCategory: Integer; // = 3;
|
---|
27 | UploadedFilesFolder: string; // = 'aktuality/uploads/';
|
---|
28 | public
|
---|
29 | Settings: TNewsSettingItems;
|
---|
30 | Database: TSqlDatabase;
|
---|
31 | ModuleUser: TModuleUser;
|
---|
32 | ModuleBase: TModuleBase;
|
---|
33 | procedure CustomizeSave;
|
---|
34 | procedure LoadSettingsFromCookies;
|
---|
35 | function ModifyContent(Content: string): string;
|
---|
36 | function Show(HandlerData: THTTPHandlerData): string;
|
---|
37 | function ShowCustomizeMenu: string;
|
---|
38 | function ShowNews(Category, ItemCount, DaysAgo: Integer): string;
|
---|
39 | constructor Create;
|
---|
40 | destructor Destroy; override;
|
---|
41 | end;
|
---|
42 |
|
---|
43 |
|
---|
44 | implementation
|
---|
45 |
|
---|
46 | (*function CategoryItemCompare(Item1, Item2: Integer): Integer;
|
---|
47 | begin
|
---|
48 | if Item1['Index'] > Item2['Index'] then Result := 1
|
---|
49 | else if Item1['Index'] < Item2['Index'] then Result := -1
|
---|
50 | else Result := 0;
|
---|
51 | end;*)
|
---|
52 |
|
---|
53 |
|
---|
54 | function TNews.ModifyContent(Content: string): string;
|
---|
55 | var
|
---|
56 | I: Integer;
|
---|
57 | URL: string;
|
---|
58 | begin
|
---|
59 | Result := '';
|
---|
60 |
|
---|
61 | // Make HTML link from URL
|
---|
62 | I := 0;
|
---|
63 | while Pos('http://', Content) > 0 do begin
|
---|
64 | I := Pos('http://', Content);
|
---|
65 | if (I > 0) and (Content[I - 1] <> '"') then begin
|
---|
66 | Result := Result + Copy(Content, 1, I);
|
---|
67 | Content := Copy(Content, I, Length(Content));
|
---|
68 | if Pos(' ', Content) > 0 then
|
---|
69 | URL := Copy(Content, 1, Pos(' ', Content))
|
---|
70 | else URL := Copy(Content, 1, Length(Content));
|
---|
71 | Result := Result + '<a href="' + URL + '">' + URL + '</a>';
|
---|
72 | Content := Copy(Content, Length(URL), Length(Content));
|
---|
73 | end else begin
|
---|
74 | Result := Result + Copy(Content, 0, I + 1);
|
---|
75 | Content := Copy(Content, I + 1, Length(Content));
|
---|
76 | end;
|
---|
77 | end;
|
---|
78 | Result := Result + Content;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | function TNews.ShowNews(Category, ItemCount, DaysAgo: Integer): string;
|
---|
82 | var
|
---|
83 | DbRows: TDbRows;
|
---|
84 | Output: string;
|
---|
85 | Index: Integer;
|
---|
86 | FontSize: Integer;
|
---|
87 | I: Integer;
|
---|
88 | J: Integer;
|
---|
89 | Author: string;
|
---|
90 | Enclosures: TStringArray;
|
---|
91 | begin
|
---|
92 | //global Database, NewsCategoryNames, NewsCountPerCategory, UploadedFilesFolder;
|
---|
93 |
|
---|
94 | ItemCount := Abs(ItemCount);
|
---|
95 | DaysAgo := Abs(DaysAgo);
|
---|
96 | try
|
---|
97 | DbRows := TDbRows.Create;
|
---|
98 | Database.Select(DbRows, 'NewsCategory', '*', 'Id=' + IntToStr(Category));
|
---|
99 | Output := '<div class="NewsPanel"><div class="Title">' + DbRows[0].Items['Caption'];
|
---|
100 | Output := Output + '<div class="Action"><a href="aktuality/index.php?category=' + IntToStr(Category) + '">Zobrazit</a>';
|
---|
101 | if ModuleUser.User.CheckPermission('News', 'Insert', 'Group', Category) then
|
---|
102 | Output := Output + ' <a href="aktuality/index.php?action=add&category=' + IntToStr(Category) + '">Přidat</a>';
|
---|
103 | Output := Output + '</div></div><div class="Content">';
|
---|
104 | Database.Query(DbRows, 'SELECT `News`.*, `User`.`Name` FROM `News` LEFT JOIN `User` ON `User`.`Id`=`News`.`User` WHERE (`News`.`Category`=' +
|
---|
105 | IntToStr(Category) + ') AND (DATE_SUB(NOW(), INTERVAL ' + IntToStr(DaysAgo) +
|
---|
106 | ' DAY) < `News`.`Date`) ORDER BY `News`.`Date` DESC LIMIT 0,' + IntToStr(ItemCount));
|
---|
107 | //echo(Database->error.'<br />');
|
---|
108 | //echo(Database->LastQuery.'<br />');
|
---|
109 | //echo('<table cellpadding="0" cellspacing="0" width="100%"><tr><td>');
|
---|
110 | Index := 0;
|
---|
111 | FontSize := 12;
|
---|
112 | if DbRows.Count > 0 then begin
|
---|
113 | Output := Output + '<table class="NewsTable">';
|
---|
114 | for I := 0 to DbRows.Count - 1 do begin
|
---|
115 | if DbRows[I].Items['Name'] = '' then Author := DbRows[I].Items['Author']
|
---|
116 | else Author := DbRows[I].Items['Name'];
|
---|
117 | Output := Output + '<tr><td onclick="window.location=''aktuality/index.php?action=view&id=' +
|
---|
118 | DbRows[I].Items['Id'] + '''" onmouseover="zobraz(' + '''new' + IntToStr(Category) +
|
---|
119 | IntToStr(Index) + ''')" style="cursor: pointer; margin: 0px;"><table class="NewsItemFrame"><tr><td style="font-size: ' +
|
---|
120 | IntToStr(FontSize) + 'pt"><strong>' + DbRows[I].Items['Title'] +
|
---|
121 | '</strong></td><td align="right" style="font-size: ' + IntToStr(FontSize) + 'pt">' +
|
---|
122 | Author + ' (' + HumanDate(SQLToDateTime(DbRows[I].Items['Date'])) + ')</td></tr></table>';
|
---|
123 | Output := Output + '<div id="new' + IntToStr(Category) + IntToStr(Index) + '" class="NewsTableItem">' + ModifyContent(DbRows[I].Items['Content']);
|
---|
124 | if DbRows[I].Items['Link'] <> '' then Output := Output + '<br/><a href="' + DbRows[I].Items['Link'] + '">Odkaz</a>';
|
---|
125 |
|
---|
126 | if DbRows[I].Items['Enclosure'] <> '' then begin
|
---|
127 | Output := Output + '<br />Přílohy: ';
|
---|
128 | Enclosures := Explode(';', DbRows[I].Items['Enclosure']);
|
---|
129 | for J := 0 to Length(Enclosures) - 1 do begin
|
---|
130 | if FileExists(UploadedFilesFolder + Enclosures[J]) then
|
---|
131 | Output := Output + ' <a href="' + UploadedFilesFolder + Enclosures[J] +
|
---|
132 | '">' + Enclosures[J] + '</a>';
|
---|
133 | end;
|
---|
134 | end;
|
---|
135 | Output := Output + '</div></td></tr>';
|
---|
136 | Index := Index + 1;
|
---|
137 | FontSize := FontSize - 1;
|
---|
138 | end;
|
---|
139 | Output := Output + '</table>';
|
---|
140 | end;
|
---|
141 | Output := Output + '</div></div>';
|
---|
142 | Result := Output;
|
---|
143 | finally
|
---|
144 | DbRows.Free;
|
---|
145 | end;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | constructor TNews.Create;
|
---|
149 | begin
|
---|
150 | inherited;
|
---|
151 | Settings := TNewsSettingItems.Create;
|
---|
152 | end;
|
---|
153 |
|
---|
154 | destructor TNews.Destroy;
|
---|
155 | begin
|
---|
156 | FreeAndNil(Settings);
|
---|
157 | inherited;
|
---|
158 | end;
|
---|
159 |
|
---|
160 | procedure TNews.LoadSettingsFromCookies;
|
---|
161 | var
|
---|
162 | I: Integer;
|
---|
163 | DbRows: TDbRows;
|
---|
164 | NewSetting: TNewsSettingItem;
|
---|
165 | NewsSettingCookie: TNewsSettingItem;
|
---|
166 | begin
|
---|
167 | try
|
---|
168 | DbRows := TDbRows.Create;
|
---|
169 | NewsSettingCookie := TNewsSettingItem.Create;
|
---|
170 | // Initialize default news setting
|
---|
171 | Settings.Clear;
|
---|
172 | Database.Select(DbRows, 'NewsCategory', '*', '1 ORDER BY Sequence');
|
---|
173 | for I := 0 to DbRows.Count - 1 do begin
|
---|
174 | NewSetting := TNewsSettingItem.Create;
|
---|
175 | with NewSetting do begin
|
---|
176 | CategoryId := StrToInt(DbRows[I].Items['Id']);
|
---|
177 | Index := I;
|
---|
178 | Enabled := True;
|
---|
179 | ItemCount := 6; // System->Config['Web']['News']['Count']
|
---|
180 | DaysAgo := 30; // System->Config['Web']['News']['DaysAgo']
|
---|
181 | Group := StrToInt(DbRows[I].Items['Group']);
|
---|
182 | end;
|
---|
183 | Settings.Add(NewSetting);
|
---|
184 | end;
|
---|
185 |
|
---|
186 | (*// Merge defaults with user setting
|
---|
187 | if ModuleBase.HandlerData.Request.Cookies.SearchKey('NewsSetting') <> -1 then begin
|
---|
188 | NewsSettingCookie.AsString := ModuleBase.HandlerData.Request.Cookies.Values['NewsSetting'];
|
---|
189 | foreach(NewsSetting as Index => NewSetting) do
|
---|
190 | begin
|
---|
191 | if array_key_exists(Index, NewsSettingCookie) then
|
---|
192 | NewsSetting[Index] := array_merge(NewSetting, NewsSettingCookie[Index]);
|
---|
193 | end;
|
---|
194 | end; *)
|
---|
195 |
|
---|
196 | finally
|
---|
197 | NewsSettingCookie.Free;
|
---|
198 | DbRows.Free;
|
---|
199 | end;
|
---|
200 | end;
|
---|
201 |
|
---|
202 | function TNews.Show(HandlerData: THTTPHandlerData): string;
|
---|
203 | var
|
---|
204 | Output: string;
|
---|
205 | ColumnCount: Integer;
|
---|
206 | Column: Integer;
|
---|
207 | I: Integer;
|
---|
208 | Action: string;
|
---|
209 | begin
|
---|
210 | Output := '';
|
---|
211 |
|
---|
212 | UploadedFilesFolder := 'aktuality/uploads/';
|
---|
213 | LoadSettingsFromCookies;
|
---|
214 |
|
---|
215 | if HandlerData.Request.Query.TryGetValue('Action', Action) then begin
|
---|
216 | // Show news customize menu
|
---|
217 | if Action = 'CustomizeNews' then begin
|
---|
218 | Output := Output + ShowCustomizeMenu;
|
---|
219 | end;
|
---|
220 | end;
|
---|
221 |
|
---|
222 | Output := Output + '<div onmouseout="skryj(predchozi)">';
|
---|
223 |
|
---|
224 | ColumnCount := 2;
|
---|
225 | Output := Output + '<table style="width: 100%"><tr>';
|
---|
226 | for Column := 1 to ColumnCount do begin
|
---|
227 | Output := Output + '<td style="vertical-align: top; width: ' + IntToStr(Round(100 / ColumnCount)) + '%;">';
|
---|
228 | for I := 0 to Settings.Count - 1 do
|
---|
229 | with TNewsSettingItem(Settings[I]) do
|
---|
230 | if Enabled and (Group = Column) then
|
---|
231 | Output := Output + ShowNews(CategoryId, ItemCount, DaysAgo);
|
---|
232 | Output := Output + '</td>';
|
---|
233 | end;
|
---|
234 | Output := Output + '</tr></table>';
|
---|
235 |
|
---|
236 | Output := Output + '<a href="aktuality/subscription.php"><img class="RSSIcon" src="images/rss20.png" alt="Aktuality přes RSS" /></a> <a href="aktuality/subscription.php">Automatické sledování novinek</a>';
|
---|
237 | Output := Output + '</div>';
|
---|
238 | Result := Output;
|
---|
239 | end;
|
---|
240 |
|
---|
241 | function TNews.ShowCustomizeMenu: string;
|
---|
242 | var
|
---|
243 | Output: string;
|
---|
244 | I: Integer;
|
---|
245 | DbRows: TDbRows;
|
---|
246 | begin
|
---|
247 | Output := '<form action="?Action=CustomizeNewsSave" method="post">';
|
---|
248 | Output := Output + '<table width="100%" cellspacing="0" border="1"><tr><td><table cellspacing="0" width="100%">';
|
---|
249 | Output := 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>';
|
---|
250 | try
|
---|
251 | DbRows := TDbRows.Create;
|
---|
252 | for I := 0 to Settings.Count - 1 do
|
---|
253 | with TNewsSettingItem(Settings[I]) do begin
|
---|
254 | Database.Select(DbRows, 'NewsCategory', '*', 'Id=' + IntToStr(CategoryId));
|
---|
255 | Output := Output + '<tr><td>' + DbRows[0].Items['Caption'] +
|
---|
256 | '</td><td align="center"><input type="text" size="2" name="NewsCategoryIndex' +
|
---|
257 | IntToStr(I) + '" value="' + IntToStr(Index) + '" /></td><td align="center"><input type="checkbox" name="NewsCategoryEnabled' + IntToStr(I) + '"';
|
---|
258 | if Enabled then Output := Output + ' checked="checked"';
|
---|
259 | Output := Output + ' /></td>' +
|
---|
260 | '<td align="center"><input type="text" size="2" name="NewsCategoryCount' + IntToStr(I) + '" value="' + IntToStr(ItemCount) + '" />' +
|
---|
261 | '<input type="hidden" name="NewsCategoryId' + IntToStr(I) + '" value="' + IntToStr(CategoryId) + '" /></td>' +
|
---|
262 | '<td align="center"><input type="text" size="3" name="NewsCategoryDaysAgo' + IntToStr(I) + '" value="' + IntToStr(DaysAgo) + '" /></td>' +
|
---|
263 | '<td><input type="text" size="3" name="NewsColumn' + IntToStr(I) + '" value="' + IntToStr(Group) + '"/></td></tr>';
|
---|
264 | end;
|
---|
265 | finally
|
---|
266 | DbRows.Free;
|
---|
267 | end;
|
---|
268 | Output := Output + '</table><input type="hidden" name="NewsCategoryCount" value="' + IntToStr(Settings.Count) +
|
---|
269 | '" /><input type="submit" value="Uložit" /></form></td></tr></table><br>';
|
---|
270 | Result := Output;
|
---|
271 | end;
|
---|
272 |
|
---|
273 | procedure TNews.CustomizeSave;
|
---|
274 | begin
|
---|
275 | (* Checkbox := array('' => 0, 'on' => 1);
|
---|
276 | // print_r(_POST);
|
---|
277 | Setting := array();
|
---|
278 | for I := 0 to _POST['NewsCategoryCount'] - 1 then begin
|
---|
279 | if (_POST['NewsCategoryDaysAgo'.I] * 1) < 0 then _POST['NewsCategoryIndex'.I] := 0;
|
---|
280 | if (_POST['NewsCategoryCount'.I] * 1) < 0 then _POST['NewsCategoryCount'.I] := 0;
|
---|
281 | if (_POST['NewsColumn'.I] * 1) < 1 then _POST['NewsColumn'.I] := 1;
|
---|
282 | if not array_key_exists('NewsCategoryEnabled'.I, _POST) then _POST['NewsCategoryEnabled'.I] := '';
|
---|
283 | 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),
|
---|
284 | 'Group' => _POST['NewsColumn'.I]);
|
---|
285 | end;
|
---|
286 | // Sort indexes
|
---|
287 | usort(Setting, 'CategoryItemCompare');
|
---|
288 | Setting := array_reverse(Setting);
|
---|
289 | // Normalize indexes
|
---|
290 | foreach(Setting as Index => Item)
|
---|
291 | Setting[Index]['Index'] := Index + 1;
|
---|
292 | // print_r(Setting);
|
---|
293 |
|
---|
294 | // Store new cookie value
|
---|
295 | _COOKIE['NewsSetting'] := serialize(Setting);
|
---|
296 | setcookie('NewsSetting', _COOKIE['NewsSetting'], time() + 60 * 60 * 24 * 365);
|
---|
297 | *)
|
---|
298 | end;
|
---|
299 |
|
---|
300 | end.
|
---|
301 |
|
---|