1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of cinecturlink2, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2009-2013 Jean-Christian Denis and contributors |
---|
7 | # contact@jcdenis.fr http://jcd.lv |
---|
8 | # |
---|
9 | # Licensed under the GPL version 2.0 license. |
---|
10 | # A copy of this license is available in LICENSE file or at |
---|
11 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
12 | # |
---|
13 | # -- END LICENSE BLOCK ------------------------------------ |
---|
14 | |
---|
15 | if (!defined('DC_RC_PATH')) { |
---|
16 | |
---|
17 | return null; |
---|
18 | } |
---|
19 | |
---|
20 | $core->addBehavior( |
---|
21 | 'initWidgets', |
---|
22 | array('cinecturlink2Widget', 'adminLinks') |
---|
23 | ); |
---|
24 | $core->addBehavior( |
---|
25 | 'initWidgets', |
---|
26 | array('cinecturlink2Widget', 'adminCats') |
---|
27 | ); |
---|
28 | |
---|
29 | class cinecturlink2Widget |
---|
30 | { |
---|
31 | public static function adminLinks($w) |
---|
32 | { |
---|
33 | global $core; |
---|
34 | |
---|
35 | $C2 = new cinecturlink2($core); |
---|
36 | |
---|
37 | $categories_combo = array('' => '', __('Uncategorized') => 'null'); |
---|
38 | $categories = $C2->getCategories(); |
---|
39 | while($categories->fetch()) { |
---|
40 | $cat_title = html::escapeHTML($categories->cat_title); |
---|
41 | $categories_combo[$cat_title] = $categories->cat_id; |
---|
42 | } |
---|
43 | |
---|
44 | $sortby_combo = array( |
---|
45 | __('Update date') => 'link_upddt', |
---|
46 | __('My rating') => 'link_note', |
---|
47 | __('Title') => 'link_title', |
---|
48 | __('Random') => 'RANDOM', |
---|
49 | __('Number of views') => 'COUNTER' |
---|
50 | ); |
---|
51 | $order_combo = array( |
---|
52 | __('Ascending') => 'asc', |
---|
53 | __('Descending') => 'desc' |
---|
54 | ); |
---|
55 | |
---|
56 | $w->create( |
---|
57 | 'cinecturlink2links', |
---|
58 | __('My cinecturlink'), |
---|
59 | array('cinecturlink2Widget', 'publicLinks'), |
---|
60 | 'null', |
---|
61 | __('Show selection of cinecturlinks') |
---|
62 | ); |
---|
63 | $w->cinecturlink2links->setting( |
---|
64 | 'title', |
---|
65 | __('Title:'), |
---|
66 | __('My cinecturlink'), |
---|
67 | 'text' |
---|
68 | ); |
---|
69 | $w->cinecturlink2links->setting( |
---|
70 | 'category', |
---|
71 | __('Category:'), |
---|
72 | '', |
---|
73 | 'combo', |
---|
74 | $categories_combo |
---|
75 | ); |
---|
76 | $w->cinecturlink2links->setting( |
---|
77 | 'sortby', |
---|
78 | __('Order by:'), |
---|
79 | 'link_upddt', |
---|
80 | 'combo', |
---|
81 | $sortby_combo |
---|
82 | ); |
---|
83 | $w->cinecturlink2links->setting( |
---|
84 | 'sort', |
---|
85 | __('Sort: (only for date, note and title)'), |
---|
86 | 'desc', |
---|
87 | 'combo', |
---|
88 | $order_combo |
---|
89 | ); |
---|
90 | $w->cinecturlink2links->setting( |
---|
91 | 'limit', |
---|
92 | __('Limit:'), |
---|
93 | 10, |
---|
94 | 'text' |
---|
95 | ); |
---|
96 | $w->cinecturlink2links->setting( |
---|
97 | 'withlink', |
---|
98 | __('Enable link'), |
---|
99 | 1, |
---|
100 | 'check' |
---|
101 | ); |
---|
102 | $w->cinecturlink2links->setting( |
---|
103 | 'showauthor', |
---|
104 | __('Show author'), |
---|
105 | 1, |
---|
106 | 'check' |
---|
107 | ); |
---|
108 | $w->cinecturlink2links->setting( |
---|
109 | 'shownote', |
---|
110 | __('Show my rating'), |
---|
111 | 0, |
---|
112 | 'check' |
---|
113 | ); |
---|
114 | $w->cinecturlink2links->setting( |
---|
115 | 'showdesc', |
---|
116 | __('Show description'), |
---|
117 | 0, |
---|
118 | 'check' |
---|
119 | ); |
---|
120 | $w->cinecturlink2links->setting( |
---|
121 | 'showpagelink', |
---|
122 | __('Show a link to cinecturlink page'), |
---|
123 | 0, |
---|
124 | 'check' |
---|
125 | ); |
---|
126 | $w->cinecturlink2links->setting( |
---|
127 | 'homeonly', |
---|
128 | __('Display on:'), |
---|
129 | 1, |
---|
130 | 'combo', |
---|
131 | array( |
---|
132 | __('All pages') => 0, |
---|
133 | __('Home page only') => 1, |
---|
134 | __('Except on home page') => 2 |
---|
135 | ) |
---|
136 | ); |
---|
137 | } |
---|
138 | |
---|
139 | public static function adminCats($w) |
---|
140 | { |
---|
141 | $w->create( |
---|
142 | 'cinecturlink2cats', |
---|
143 | __('List of categories of cinecturlink'), |
---|
144 | array('cinecturlink2Widget', 'publicCats'), |
---|
145 | null, |
---|
146 | __('List of categories of cinecturlink') |
---|
147 | ); |
---|
148 | $w->cinecturlink2cats->setting( |
---|
149 | 'title', |
---|
150 | __('Title:'), |
---|
151 | __('My cinecturlink by categories'), |
---|
152 | 'text' |
---|
153 | ); |
---|
154 | $w->cinecturlink2cats->setting( |
---|
155 | 'shownumlink', |
---|
156 | __('Show number of links'), |
---|
157 | 0, |
---|
158 | 'check' |
---|
159 | ); |
---|
160 | $w->cinecturlink2cats->setting( |
---|
161 | 'homeonly', |
---|
162 | __('Display on:'), |
---|
163 | 1, |
---|
164 | 'combo', |
---|
165 | array( |
---|
166 | __('All pages') => 0, |
---|
167 | __('Home page only') => 1, |
---|
168 | __('Except on home page') => 2 |
---|
169 | ) |
---|
170 | ); |
---|
171 | } |
---|
172 | |
---|
173 | public static function publicLinks($w) |
---|
174 | { |
---|
175 | global $core; |
---|
176 | |
---|
177 | $core->blog->settings->addNamespace('cinecturlink2'); |
---|
178 | |
---|
179 | if (!$core->blog->settings->cinecturlink2->cinecturlink2_active |
---|
180 | || $w->homeonly == 1 && $core->url->type != 'default' |
---|
181 | || $w->homeonly == 2 && $core->url->type == 'default' |
---|
182 | ) { |
---|
183 | return null; |
---|
184 | } |
---|
185 | |
---|
186 | $C2 = new cinecturlink2($core); |
---|
187 | |
---|
188 | if ($w->category) { |
---|
189 | if ($w->category == 'null') { |
---|
190 | $params['sql'] = ' AND L.cat_id IS NULL '; |
---|
191 | } |
---|
192 | elseif (is_numeric($w->category)) { |
---|
193 | $params['cat_id'] = (integer) $w->category; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | $limit = abs((integer) $w->limit); |
---|
198 | |
---|
199 | # Tirage aléatoire |
---|
200 | # Consomme beaucoup de ressources! |
---|
201 | if ($w->sortby == 'RANDOM') { |
---|
202 | $big_rs = $C2->getLinks($params); |
---|
203 | |
---|
204 | if ($big_rs->isEmpty()) { |
---|
205 | |
---|
206 | return null; |
---|
207 | } |
---|
208 | |
---|
209 | $ids= array(); |
---|
210 | while($big_rs->fetch()) { |
---|
211 | $ids[] = $big_rs->link_id; |
---|
212 | } |
---|
213 | shuffle($ids); |
---|
214 | $ids = array_slice($ids, 0, $limit); |
---|
215 | |
---|
216 | $params['link_id'] = array(); |
---|
217 | foreach($ids as $id) { |
---|
218 | $params['link_id'][] = $id; |
---|
219 | } |
---|
220 | } |
---|
221 | elseif ($w->sortby == 'COUNTER') { |
---|
222 | $params['order'] = 'link_count asc'; |
---|
223 | $params['limit'] = $limit; |
---|
224 | } |
---|
225 | else { |
---|
226 | $params['order'] = $w->sortby; |
---|
227 | $params['order'] .= $w->sort == 'asc' ? ' asc' : ' desc'; |
---|
228 | $params['limit'] = $limit; |
---|
229 | } |
---|
230 | |
---|
231 | $rs = $C2->getLinks($params); |
---|
232 | |
---|
233 | if ($rs->isEmpty()) { |
---|
234 | |
---|
235 | return null; |
---|
236 | } |
---|
237 | |
---|
238 | $widthmax = (integer) $core->blog->settings->cinecturlink2->cinecturlink2_widthmax; |
---|
239 | $style = $widthmax ? ' style="width:'.$widthmax.'px;"' : ''; |
---|
240 | |
---|
241 | $entries = array(); |
---|
242 | while($rs->fetch()) { |
---|
243 | $url = $rs->link_url; |
---|
244 | $img = $rs->link_img; |
---|
245 | $title = html::escapeHTML($rs->link_title); |
---|
246 | $author = html::escapeHTML($rs->link_author); |
---|
247 | $cat = html::escapeHTML($rs->cat_title); |
---|
248 | $note = $w->shownote ? ' <em>('.$rs->link_note.'/20)</em>' : ''; |
---|
249 | $desc = $w->showdesc ? '<br /><em>'.html::escapeHTML($rs->link_desc).'</em>' : ''; |
---|
250 | $lang = $rs->link_lang ? ' hreflang="'.$rs->link_lang.'"' : ''; |
---|
251 | $count = abs((integer) $rs->link_count); |
---|
252 | |
---|
253 | # --BEHAVIOR-- cinecturlink2WidgetLinks |
---|
254 | $bhv = $core->callBehavior('cinecturlink2WidgetLinks', $rs->link_id); |
---|
255 | |
---|
256 | $entries[] = |
---|
257 | '<p style="text-align:center;">'. |
---|
258 | ($w->withlink && !empty($url) ? '<a href="'.$url.'"'.$lang.' title="'.$cat.'">' : ''). |
---|
259 | '<strong>'.$title.'</strong>'.$note.'<br />'. |
---|
260 | ($w->showauthor ? $author.'<br />' : '').'<br />'. |
---|
261 | '<img src="'.$img.'" alt="'.$title.' - '.$author.'"'.$style.' />'. |
---|
262 | $desc. |
---|
263 | ($w->withlink && !empty($url) ? '</a>' : ''). |
---|
264 | '</p>'.$bhv; |
---|
265 | |
---|
266 | try { |
---|
267 | $cur = $core->con->openCursor($C2->table); |
---|
268 | $cur->link_count = ($count + 1); |
---|
269 | $C2->updLink($rs->link_id, $cur, false); |
---|
270 | } |
---|
271 | catch (Exception $e) { |
---|
272 | |
---|
273 | } |
---|
274 | } |
---|
275 | # Tirage aléatoire |
---|
276 | if ($w->sortby == 'RANDOM' |
---|
277 | || $w->sortby == 'COUNTER' |
---|
278 | ) { |
---|
279 | shuffle($entries); |
---|
280 | if ($core->blog->settings->cinecturlink2->cinecturlink2_triggeronrandom) { |
---|
281 | $core->blog->triggerBlog(); |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | return |
---|
286 | '<div class="cinecturlink2list">'. |
---|
287 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
288 | implode(' ',$entries). |
---|
289 | ($w->showpagelink && $core->blog->settings->cinecturlink2->cinecturlink2_public_active ? |
---|
290 | '<p><a href="'.$core->blog->url.$core->url->getBase('cinecturlink2').'" title="'.__('view all links').'">'.__('More links').'</a></p>' : '' |
---|
291 | ). |
---|
292 | '</div>'; |
---|
293 | } |
---|
294 | |
---|
295 | public static function publicCats($w) |
---|
296 | { |
---|
297 | global $core; |
---|
298 | |
---|
299 | $core->blog->settings->addNamespace('cinecturlink2'); |
---|
300 | |
---|
301 | if (!$core->blog->settings->cinecturlink2->cinecturlink2_active |
---|
302 | || !$core->blog->settings->cinecturlink2->cinecturlink2_public_active |
---|
303 | || $w->homeonly == 1 && $core->url->type != 'default' |
---|
304 | || $w->homeonly == 2 && $core->url->type == 'default' |
---|
305 | ) { |
---|
306 | return null; |
---|
307 | } |
---|
308 | |
---|
309 | $C2 = new cinecturlink2($core); |
---|
310 | |
---|
311 | $rs = $C2->getCategories(array()); |
---|
312 | |
---|
313 | if ($rs->isEmpty()) { |
---|
314 | |
---|
315 | return null; |
---|
316 | } |
---|
317 | |
---|
318 | $res = |
---|
319 | '<li><a href="'. |
---|
320 | $core->blog->url.$core->url->getBase('cinecturlink2'). |
---|
321 | '" title="'.__('view all links').'">'.__('all links'). |
---|
322 | '</a>'; |
---|
323 | if ($w->shownumlink) { |
---|
324 | $res .= ' ('.($C2->getLinks(array(), true)->f(0)).')'; |
---|
325 | } |
---|
326 | $res .= '</li>'; |
---|
327 | |
---|
328 | while($rs->fetch()) { |
---|
329 | $res .= |
---|
330 | '<li><a href="'. |
---|
331 | $core->blog->url.$core->url->getBase('cinecturlink2').'/'.$core->blog->settings->cinecturlink2->cinecturlink2_public_caturl.'/'.urlencode($rs->cat_title). |
---|
332 | '" title="'.__('view links of this category').'">'. |
---|
333 | html::escapeHTML($rs->cat_title). |
---|
334 | '</a>'; |
---|
335 | if ($w->shownumlink) { |
---|
336 | $res .= ' ('.($C2->getLinks(array('cat_id'=>$rs->cat_id), true)->f(0)).')'; |
---|
337 | } |
---|
338 | $res .= '</li>'; |
---|
339 | } |
---|
340 | |
---|
341 | return |
---|
342 | '<div class="cinecturlink2cat">'. |
---|
343 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
344 | '<ul>'.$res.'</ul>'. |
---|
345 | '</div>'; |
---|
346 | } |
---|
347 | } |
---|