1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of cinecturlink2, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009-2010 JC Denis and contributors |
---|
6 | # jcdenis@gdwd.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | if (!defined('DC_RC_PATH')){return;} |
---|
14 | |
---|
15 | class cinecturlink2 |
---|
16 | { |
---|
17 | public $core; |
---|
18 | public $con; |
---|
19 | public $table; |
---|
20 | public $blog; |
---|
21 | |
---|
22 | public function __construct($core) |
---|
23 | { |
---|
24 | $this->core = $core; |
---|
25 | $this->con = $core->con; |
---|
26 | $this->table = $core->prefix.'cinecturlink2'; |
---|
27 | $this->blog = $core->con->escape($core->blog->id); |
---|
28 | } |
---|
29 | |
---|
30 | public function getLinks($params=array(),$count_only=false) |
---|
31 | { |
---|
32 | if ($count_only) |
---|
33 | { |
---|
34 | $strReq = 'SELECT count(L.link_id) '; |
---|
35 | } |
---|
36 | else |
---|
37 | { |
---|
38 | $content_req = ''; |
---|
39 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
40 | $content_req .= implode(', ',$params['columns']).', '; |
---|
41 | } |
---|
42 | |
---|
43 | $strReq = |
---|
44 | 'SELECT L.link_id, L.blog_id, L.cat_id, L.user_id, L.link_type, '. |
---|
45 | $content_req. |
---|
46 | 'L.link_creadt, L.link_upddt, L.link_note, L.link_count, '. |
---|
47 | 'L.link_title, L.link_desc, L.link_author, '. |
---|
48 | 'L.link_lang, L.link_url, L.link_img, '. |
---|
49 | 'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '. |
---|
50 | 'U.user_url, '. |
---|
51 | 'C.cat_title, C.cat_desc '; |
---|
52 | } |
---|
53 | |
---|
54 | $strReq .= |
---|
55 | 'FROM '.$this->table.' L '. |
---|
56 | 'INNER JOIN '.$this->core->prefix.'user U ON U.user_id = L.user_id '. |
---|
57 | 'LEFT OUTER JOIN '.$this->table.'_cat C ON L.cat_id = C.cat_id '; |
---|
58 | |
---|
59 | if (!empty($params['from'])) { |
---|
60 | $strReq .= $params['from'].' '; |
---|
61 | } |
---|
62 | |
---|
63 | $strReq .= "WHERE L.blog_id = '".$this->blog."' "; |
---|
64 | |
---|
65 | if (isset($params['link_type'])) |
---|
66 | { |
---|
67 | if (is_array($params['link_type']) && !empty($params['link_type'])) { |
---|
68 | $strReq .= 'AND L.link_type '.$this->con->in($params['link_type']); |
---|
69 | } elseif ($params['link_type'] != '') { |
---|
70 | $strReq .= "AND L.link_type = '".$this->con->escape($params['link_type'])."' "; |
---|
71 | } |
---|
72 | } |
---|
73 | else |
---|
74 | { |
---|
75 | $strReq .= "AND L.link_type = 'cinecturlink' "; |
---|
76 | } |
---|
77 | |
---|
78 | if (!empty($params['link_id'])) { |
---|
79 | if (is_array($params['link_id'])) { |
---|
80 | array_walk($params['link_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
81 | } else { |
---|
82 | $params['link_id'] = array((integer) $params['link_id']); |
---|
83 | } |
---|
84 | $strReq .= 'AND L.link_id '.$this->con->in($params['link_id']); |
---|
85 | } |
---|
86 | |
---|
87 | if (!empty($params['cat_id'])) { |
---|
88 | if (is_array($params['cat_id'])) { |
---|
89 | array_walk($params['cat_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
90 | } else { |
---|
91 | $params['cat_id'] = array((integer) $params['cat_id']); |
---|
92 | } |
---|
93 | $strReq .= 'AND L.cat_id '.$this->con->in($params['cat_id']); |
---|
94 | } |
---|
95 | if (!empty($params['cat_title'])) { |
---|
96 | $strReq .= "AND C.cat_title = '".$this->con->escape($params['cat_title'])."' "; |
---|
97 | } |
---|
98 | |
---|
99 | if (!empty($params['link_title'])) { |
---|
100 | $strReq .= "AND L.link_title = '".$this->con->escape($params['link_title'])."' "; |
---|
101 | } |
---|
102 | |
---|
103 | if (!empty($params['link_lang'])) { |
---|
104 | $strReq .= "AND L.link_lang = '".$this->con->escape($params['link_lang'])."' "; |
---|
105 | } |
---|
106 | |
---|
107 | if (!empty($params['sql'])) { |
---|
108 | $strReq .= $params['sql'].' '; |
---|
109 | } |
---|
110 | |
---|
111 | if (!$count_only) |
---|
112 | { |
---|
113 | if (!empty($params['order'])) { |
---|
114 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
115 | } else { |
---|
116 | $strReq .= 'ORDER BY L.link_upddt DESC '; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | if (!$count_only && !empty($params['limit'])) { |
---|
121 | $strReq .= $this->con->limit($params['limit']); |
---|
122 | } |
---|
123 | |
---|
124 | return $this->con->select($strReq); |
---|
125 | } |
---|
126 | |
---|
127 | public function addLink($cur) |
---|
128 | { |
---|
129 | $this->con->writeLock($this->table); |
---|
130 | |
---|
131 | try |
---|
132 | { |
---|
133 | if ($cur->link_title == '') { |
---|
134 | throw new Exception(__('No link title')); |
---|
135 | } |
---|
136 | if ($cur->link_desc == '') { |
---|
137 | throw new Exception(__('No link description')); |
---|
138 | } |
---|
139 | if ('' == $cur->link_note) { |
---|
140 | $cur->link_note = 10; |
---|
141 | } |
---|
142 | if (0 > $cur->link_note || $cur->link_note > 20) { |
---|
143 | $cur->link_note = 10; |
---|
144 | } |
---|
145 | |
---|
146 | $cur->link_id = $this->getNextLinkId(); |
---|
147 | $cur->blog_id = $this->blog; |
---|
148 | $cur->user_id = $this->core->auth->userID(); |
---|
149 | $cur->link_creadt = date('Y-m-d H:i:s'); |
---|
150 | $cur->link_upddt = date('Y-m-d H:i:s'); |
---|
151 | $cur->link_pos = 0; |
---|
152 | $cur->link_count = 0; |
---|
153 | $cur->insert(); |
---|
154 | $this->con->unlock(); |
---|
155 | } |
---|
156 | catch (Exception $e) |
---|
157 | { |
---|
158 | $this->con->unlock(); |
---|
159 | throw $e; |
---|
160 | } |
---|
161 | $this->trigger(); |
---|
162 | |
---|
163 | # --BEHAVIOR-- cinecturlink2AfterAddLink |
---|
164 | $this->core->callBehavior('cinecturlink2AfterAddLink',$cur); |
---|
165 | |
---|
166 | return $cur->link_id; |
---|
167 | } |
---|
168 | |
---|
169 | public function updLink($id,$cur,$behavior=true) |
---|
170 | { |
---|
171 | $id = (integer) $id; |
---|
172 | |
---|
173 | if (empty($id)) { |
---|
174 | throw new Exception(__('No such link ID')); |
---|
175 | } |
---|
176 | |
---|
177 | $cur->link_upddt = date('Y-m-d H:i:s'); |
---|
178 | |
---|
179 | $cur->update("WHERE link_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
180 | $this->trigger(); |
---|
181 | |
---|
182 | if ($behavior) { |
---|
183 | # --BEHAVIOR-- cinecturlink2AfterUpdLink |
---|
184 | $this->core->callBehavior('cinecturlink2AfterUpdLink',$cur,$id); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | public function delLink($id) |
---|
189 | { |
---|
190 | $id = (integer) $id; |
---|
191 | |
---|
192 | if (empty($id)) { |
---|
193 | throw new Exception(__('No such link ID')); |
---|
194 | } |
---|
195 | |
---|
196 | # --BEHAVIOR-- cinecturlink2BeforeDelLink |
---|
197 | $this->core->callBehavior('cinecturlink2BeforeDelLink',$id); |
---|
198 | |
---|
199 | $this->con->execute( |
---|
200 | 'DELETE FROM '.$this->table.' '. |
---|
201 | 'WHERE link_id = '.$id.' '. |
---|
202 | "AND blog_id = '".$this->blog."' " |
---|
203 | ); |
---|
204 | |
---|
205 | $this->trigger(); |
---|
206 | } |
---|
207 | |
---|
208 | private function getNextLinkId() |
---|
209 | { |
---|
210 | return $this->con->select( |
---|
211 | 'SELECT MAX(link_id) FROM '.$this->table.' ' |
---|
212 | )->f(0) + 1; |
---|
213 | } |
---|
214 | |
---|
215 | public function getCategories($params=array(),$count_only=false) |
---|
216 | { |
---|
217 | if ($count_only) |
---|
218 | { |
---|
219 | $strReq = 'SELECT count(C.cat_id) '; |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | $content_req = ''; |
---|
224 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
225 | $content_req .= implode(', ',$params['columns']).', '; |
---|
226 | } |
---|
227 | |
---|
228 | $strReq = |
---|
229 | 'SELECT C.cat_id, C.blog_id, C.cat_title, C.cat_desc, '. |
---|
230 | $content_req. |
---|
231 | 'C.cat_pos, C.cat_creadt, C.cat_upddt '; |
---|
232 | } |
---|
233 | |
---|
234 | $strReq .= 'FROM '.$this->table.'_cat C '; |
---|
235 | |
---|
236 | if (!empty($params['from'])) { |
---|
237 | $strReq .= $params['from'].' '; |
---|
238 | } |
---|
239 | |
---|
240 | $strReq .= "WHERE C.blog_id = '".$this->blog."' "; |
---|
241 | |
---|
242 | if (!empty($params['cat_id'])) { |
---|
243 | if (is_array($params['cat_id'])) { |
---|
244 | array_walk($params['cat_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
245 | } else { |
---|
246 | $params['cat_id'] = array((integer) $params['cat_id']); |
---|
247 | } |
---|
248 | $strReq .= 'AND C.cat_id '.$this->con->in($params['cat_id']); |
---|
249 | } |
---|
250 | |
---|
251 | if (!empty($params['cat_title'])) { |
---|
252 | $strReq .= "AND C.cat_title = '".$this->con->escape($params['cat_title'])."' "; |
---|
253 | } |
---|
254 | |
---|
255 | if (!empty($params['sql'])) { |
---|
256 | $strReq .= $params['sql'].' '; |
---|
257 | } |
---|
258 | |
---|
259 | if (!$count_only) |
---|
260 | { |
---|
261 | if (!empty($params['order'])) { |
---|
262 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
263 | } else { |
---|
264 | $strReq .= 'ORDER BY cat_pos ASC '; |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | if (!$count_only && !empty($params['limit'])) { |
---|
269 | $strReq .= $this->con->limit($params['limit']); |
---|
270 | } |
---|
271 | |
---|
272 | return $this->con->select($strReq); |
---|
273 | } |
---|
274 | |
---|
275 | public function addCategory($cur) |
---|
276 | { |
---|
277 | $this->con->writeLock($this->table.'_cat'); |
---|
278 | |
---|
279 | try |
---|
280 | { |
---|
281 | if ($cur->cat_title == '') { |
---|
282 | throw new Exception(__('No category title')); |
---|
283 | } |
---|
284 | if ($cur->cat_desc == '') { |
---|
285 | throw new Exception(__('No category description')); |
---|
286 | } |
---|
287 | |
---|
288 | $cur->cat_id = $this->getNextCatId(); |
---|
289 | $cur->cat_pos = $this->getNextCatPos(); |
---|
290 | $cur->blog_id = $this->blog; |
---|
291 | $cur->cat_creadt = date('Y-m-d H:i:s'); |
---|
292 | $cur->cat_upddt = date('Y-m-d H:i:s'); |
---|
293 | $cur->insert(); |
---|
294 | $this->con->unlock(); |
---|
295 | } |
---|
296 | catch (Exception $e) |
---|
297 | { |
---|
298 | $this->con->unlock(); |
---|
299 | throw $e; |
---|
300 | } |
---|
301 | $this->trigger(); |
---|
302 | return $cur->cat_id; |
---|
303 | } |
---|
304 | |
---|
305 | public function updCategory($id,$cur) |
---|
306 | { |
---|
307 | $id = (integer) $id; |
---|
308 | |
---|
309 | if (empty($id)) { |
---|
310 | throw new Exception(__('No such category ID')); |
---|
311 | } |
---|
312 | |
---|
313 | $cur->cat_upddt = date('Y-m-d H:i:s'); |
---|
314 | |
---|
315 | $cur->update("WHERE cat_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
316 | $this->trigger(); |
---|
317 | } |
---|
318 | |
---|
319 | public function delCategory($id) |
---|
320 | { |
---|
321 | $id = (integer) $id; |
---|
322 | |
---|
323 | if (empty($id)) { |
---|
324 | throw new Exception(__('No such category ID')); |
---|
325 | } |
---|
326 | |
---|
327 | $this->con->execute( |
---|
328 | 'DELETE FROM '.$this->table.'_cat '. |
---|
329 | 'WHERE cat_id = '.$id.' '. |
---|
330 | "AND blog_id = '".$this->blog."' " |
---|
331 | ); |
---|
332 | |
---|
333 | # Update link cat to NULL |
---|
334 | $cur = $this->con->openCursor($this->table); |
---|
335 | $cur->cat_id = NULL; |
---|
336 | $cur->link_upddt = date('Y-m-d H:i:s'); |
---|
337 | $cur->update("WHERE cat_id = ".$id." AND blog_id = '".$this->blog."' "); |
---|
338 | |
---|
339 | $this->trigger(); |
---|
340 | } |
---|
341 | |
---|
342 | private function getNextCatId() |
---|
343 | { |
---|
344 | return $this->con->select( |
---|
345 | 'SELECT MAX(cat_id) FROM '.$this->table.'_cat ' |
---|
346 | )->f(0) + 1; |
---|
347 | } |
---|
348 | |
---|
349 | private function getNextCatPos() |
---|
350 | { |
---|
351 | return $this->con->select( |
---|
352 | 'SELECT MAX(cat_pos) FROM '.$this->table.'_cat '. |
---|
353 | "WHERE blog_id = '".$this->blog."' " |
---|
354 | )->f(0) + 1; |
---|
355 | } |
---|
356 | |
---|
357 | private function trigger() |
---|
358 | { |
---|
359 | $this->core->blog->triggerBlog(); |
---|
360 | } |
---|
361 | |
---|
362 | public static function test_folder($root,$folder,$throw=false) |
---|
363 | { |
---|
364 | if (!is_dir($root.'/'.$folder)) |
---|
365 | { |
---|
366 | if (!is_dir($root) || !is_writable($root) || !mkdir($root.'/'.$folder)) |
---|
367 | { |
---|
368 | if ($throw) |
---|
369 | { |
---|
370 | throw new Exception(__('Failed to create public folder for images.')); |
---|
371 | } |
---|
372 | return false; |
---|
373 | } |
---|
374 | } |
---|
375 | return true; |
---|
376 | } |
---|
377 | } |
---|
378 | ?> |
---|