1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of pollsFactory, 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 postOption |
---|
16 | { |
---|
17 | public $core; |
---|
18 | public $con; |
---|
19 | protected $table; |
---|
20 | protected $blog; |
---|
21 | |
---|
22 | public function __construct($core) |
---|
23 | { |
---|
24 | $this->core = $core; |
---|
25 | $this->con = $core->con; |
---|
26 | $this->blog = $core->con->escape($core->blog->id); |
---|
27 | $this->table = $core->con->escape($core->prefix.'post_option'); |
---|
28 | } |
---|
29 | |
---|
30 | public function table() |
---|
31 | { |
---|
32 | return $this->table; |
---|
33 | } |
---|
34 | |
---|
35 | public function open() |
---|
36 | { |
---|
37 | return $this->con->openCursor($this->table); |
---|
38 | } |
---|
39 | |
---|
40 | public function lock() |
---|
41 | { |
---|
42 | $this->con->writeLock($this->table); |
---|
43 | } |
---|
44 | |
---|
45 | public function unlock() |
---|
46 | { |
---|
47 | $this->con->unlock(); |
---|
48 | } |
---|
49 | |
---|
50 | public function trigger() |
---|
51 | { |
---|
52 | $this->core->blog->triggerBlog(); |
---|
53 | } |
---|
54 | |
---|
55 | public function nextID() |
---|
56 | { |
---|
57 | return (integer) $this->con->select( |
---|
58 | 'SELECT MAX(option_id) FROM '.$this->table |
---|
59 | )->f(0) + 1; |
---|
60 | } |
---|
61 | |
---|
62 | public function nextPosition($post_id=null,$option_meta=null,$option_type=null) |
---|
63 | { |
---|
64 | $q = array(); |
---|
65 | if ($post_id !== null) { |
---|
66 | $q[] = 'post_id = '.((integer) $post_id).' '; |
---|
67 | } |
---|
68 | if ($option_meta !== null) { |
---|
69 | $q[] = "option_meta = '".$this->con->escape($option_meta)."' "; |
---|
70 | } |
---|
71 | if ($option_type !== null) { |
---|
72 | $q[] = "option_type = '".$this->con->escape($option_type)."' "; |
---|
73 | } |
---|
74 | if (empty($q)) return 0; |
---|
75 | |
---|
76 | return $this->con->select( |
---|
77 | 'SELECT MAX(option_position) '. |
---|
78 | 'FROM '.$this->table.' '. |
---|
79 | 'WHERE '.implode('AND ',$q) |
---|
80 | )->f(0) + 1; |
---|
81 | } |
---|
82 | |
---|
83 | public function checkAuth(&$cur,$post_id=null) |
---|
84 | { |
---|
85 | if (!$this->core->auth->check('usage,contentadmin',$this->blog)) { |
---|
86 | throw new Exception(__('You are not allowed to edit post option')); |
---|
87 | } |
---|
88 | |
---|
89 | $post_id = is_int($post_id) ? $post_id : $cur->post_id; |
---|
90 | |
---|
91 | if (empty($post_id)) { |
---|
92 | throw new Exception(__('No such post ID')); |
---|
93 | } |
---|
94 | |
---|
95 | if (!$this->core->auth->check('contentadmin',$this->blog)) |
---|
96 | { |
---|
97 | $strReq = 'SELECT post_id '. |
---|
98 | 'FROM '.$this->core->prefix.'post '. |
---|
99 | 'WHERE post_id = '.$post_id.' '. |
---|
100 | "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
101 | |
---|
102 | $rs = $this->con->select($strReq); |
---|
103 | |
---|
104 | if ($rs->isEmpty()) { |
---|
105 | throw new Exception(__('You are not allowed to edit this post option')); |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | public function getOptions($params=array(),$count_only=false) |
---|
111 | { |
---|
112 | // This limit field to only one and group results on this field. |
---|
113 | $group = array(); |
---|
114 | if (!empty($params['group'])) { |
---|
115 | if (is_array($params['group'])) { |
---|
116 | foreach($params['group'] as $k => $v) { |
---|
117 | $group[] = $this->con->escape($v); |
---|
118 | } |
---|
119 | } |
---|
120 | else { |
---|
121 | $group[] = $this->con->escape($params['group']); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | if ($count_only) { |
---|
126 | if (!empty($group)) { |
---|
127 | $q = 'SELECT count('.$group[0].') '; |
---|
128 | } |
---|
129 | else { |
---|
130 | $q = 'SELECT count(O.option_id) '; |
---|
131 | } |
---|
132 | } |
---|
133 | else { |
---|
134 | if (!empty($group)) { |
---|
135 | $q = 'SELECT '.implode(', ',$group).' '; |
---|
136 | } |
---|
137 | else { |
---|
138 | $q = 'SELECT O.option_id, O.post_id, O.option_meta, '; |
---|
139 | |
---|
140 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
141 | $q .= implode(', ',$params['columns']).', '; |
---|
142 | } |
---|
143 | $q .= |
---|
144 | 'O.option_creadt, O.option_upddt, O.option_type, O.option_format, '. |
---|
145 | 'O.option_title, O.option_content, O.option_content_xhtml, '. |
---|
146 | 'O.option_selected, O.option_position, '. |
---|
147 | 'P.blog_id, P.post_type, P.post_title '; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | $q .= |
---|
152 | 'FROM '.$this->table.' O '. |
---|
153 | 'LEFT JOIN '.$this->core->prefix.'post P ON P.post_id = O.post_id '; |
---|
154 | |
---|
155 | if (!empty($params['from'])) { |
---|
156 | $q .= $params['from'].' '; |
---|
157 | } |
---|
158 | |
---|
159 | $q .= "WHERE P.blog_id = '".$this->blog."' "; |
---|
160 | |
---|
161 | # option_type |
---|
162 | if (isset($params['option_type'])) { |
---|
163 | if (is_array($params['option_type']) && !empty($params['option_type'])) { |
---|
164 | $q .= 'AND O.option_type '.$this->con->in($params['option_type']); |
---|
165 | } elseif ($params['option_type'] != '') { |
---|
166 | $q .= "AND O.option_type = '".$this->con->escape($params['option_type'])."' "; |
---|
167 | } else { |
---|
168 | $q .= "AND O.option_type != '' "; |
---|
169 | } |
---|
170 | } |
---|
171 | else { |
---|
172 | $q .= "AND O.option_type = '' "; |
---|
173 | } |
---|
174 | # option_id |
---|
175 | if (!empty($params['option_id'])) { |
---|
176 | if (is_array($params['option_id'])) { |
---|
177 | array_walk($params['option_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
178 | } else { |
---|
179 | $params['option_id'] = array((integer) $params['option_id']); |
---|
180 | } |
---|
181 | $q .= 'AND O.option_id '.$this->con->in($params['option_id']); |
---|
182 | } |
---|
183 | # post_id |
---|
184 | if (!empty($params['post_id'])) { |
---|
185 | if (is_array($params['post_id'])) { |
---|
186 | array_walk($params['post_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
187 | } else { |
---|
188 | $params['post_id'] = array((integer) $params['post_id']); |
---|
189 | } |
---|
190 | $q .= 'AND O.post_id '.$this->con->in($params['post_id']); |
---|
191 | } |
---|
192 | # option_meta |
---|
193 | if (isset($params['option_meta'])) { |
---|
194 | if (is_array($params['option_meta']) && !empty($params['option_meta'])) { |
---|
195 | $q .= 'AND O.option_meta '.$this->con->in($params['option_meta']); |
---|
196 | } elseif ($params['option_meta'] != '') { |
---|
197 | $q .= "AND O.option_meta = '".$this->con->escape($params['option_meta'])."' "; |
---|
198 | } else { |
---|
199 | $q .= "AND O.option_meta IS NULL "; |
---|
200 | } |
---|
201 | } |
---|
202 | # option_selected |
---|
203 | if (isset($params['option_selected'])) { |
---|
204 | $q .= 'AND O.option_selected = '.(integer) $params['option_selected'].' '; |
---|
205 | } |
---|
206 | # option_title |
---|
207 | if (!empty($params['option_title'])) { |
---|
208 | $q .= "AND O.option_title = '".$this->con->escape($params['option_title'])."' "; |
---|
209 | } |
---|
210 | # sql |
---|
211 | if (!empty($params['sql'])) { |
---|
212 | $q .= $params['sql'].' '; |
---|
213 | } |
---|
214 | # group |
---|
215 | if (!empty($group)) { |
---|
216 | if (!$count_only) { |
---|
217 | $q .= 'GROUP BY '.implode(', ',$group).' '; |
---|
218 | } |
---|
219 | else { |
---|
220 | $q .= 'GROUP BY '.$group[0].' '; |
---|
221 | } |
---|
222 | } |
---|
223 | # order |
---|
224 | if (!$count_only) { |
---|
225 | if (!empty($params['order'])) { |
---|
226 | $q .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
227 | } |
---|
228 | else { |
---|
229 | $q .= 'ORDER BY O.option_id ASC '; |
---|
230 | } |
---|
231 | } |
---|
232 | # limit |
---|
233 | if (!$count_only && !empty($params['limit'])) { |
---|
234 | $q .= $this->con->limit($params['limit']); |
---|
235 | } |
---|
236 | |
---|
237 | $rs = $this->con->select($q); |
---|
238 | $rs->postOption = $this; |
---|
239 | |
---|
240 | return $rs; |
---|
241 | } |
---|
242 | |
---|
243 | public function addOption($cur) |
---|
244 | { |
---|
245 | $this->lock(); |
---|
246 | try |
---|
247 | { |
---|
248 | $cur->option_id = $this->nextID(); |
---|
249 | $cur->option_creadt = date('Y-m-d H:i:s'); |
---|
250 | $cur->option_upddt = date('Y-m-d H:i:s'); |
---|
251 | |
---|
252 | $this->getOptionContent($cur,$cur->option_id); |
---|
253 | $this->checkAuth($cur); |
---|
254 | |
---|
255 | $cur->insert(); |
---|
256 | $this->unlock(); |
---|
257 | } |
---|
258 | catch (Exception $e) |
---|
259 | { |
---|
260 | $this->unlock(); |
---|
261 | throw $e; |
---|
262 | } |
---|
263 | |
---|
264 | $this->trigger(); |
---|
265 | return $cur->option_id; |
---|
266 | } |
---|
267 | |
---|
268 | public function updOption($id,$cur) |
---|
269 | { |
---|
270 | $id = (integer) $id; |
---|
271 | |
---|
272 | if (empty($id)) { |
---|
273 | throw new Exception(__('No such option ID')); |
---|
274 | } |
---|
275 | |
---|
276 | $this->getOptionContent($cur,$id); |
---|
277 | $this->checkAuth($cur); |
---|
278 | |
---|
279 | $cur->option_upddt = date('Y-m-d H:i:s'); |
---|
280 | |
---|
281 | $cur->update('WHERE option_id = '.$id.' '); |
---|
282 | $this->trigger(); |
---|
283 | } |
---|
284 | |
---|
285 | private function updOptionField($id,$field,$value) |
---|
286 | { |
---|
287 | $id = (integer) $id; |
---|
288 | if (empty($id)) { |
---|
289 | throw new Exception(__('No such option ID')); |
---|
290 | } |
---|
291 | |
---|
292 | $cur = $this->open(); |
---|
293 | $cur->setField($field,$value); |
---|
294 | $cur->option_upddt = date('Y-m-d H:i:s'); |
---|
295 | $cur->update("WHERE option_id = ".$id); |
---|
296 | |
---|
297 | $this->trigger(); |
---|
298 | } |
---|
299 | |
---|
300 | public function updOptionPosition($id,$val) |
---|
301 | { |
---|
302 | $val = (integer) $val; |
---|
303 | $this->updOptionField($id,'option_position',$val); |
---|
304 | } |
---|
305 | |
---|
306 | public function updOptionSelected($id,$val) |
---|
307 | { |
---|
308 | $val = (integer) $val; |
---|
309 | $this->updOptionField($id,'option_selected',$val); |
---|
310 | } |
---|
311 | |
---|
312 | public function delOption($id=null,$type=null,$post_id=null,$meta=null) |
---|
313 | { |
---|
314 | $q = array(); |
---|
315 | if ($id != '') { |
---|
316 | $id = (integer) $id; |
---|
317 | $q[] = "option_id = '".$id."'"; |
---|
318 | } |
---|
319 | if ($type != '') { |
---|
320 | $type = $this->con->escape((string) $type); |
---|
321 | $q[] = "option_type = '".$type."'"; |
---|
322 | } |
---|
323 | if ($post_id != '') { |
---|
324 | $post_id = (integer) $post_id; |
---|
325 | $q[] = "post_id = '".$post_id."'"; |
---|
326 | } |
---|
327 | if ($meta != '') { |
---|
328 | $meta = $this->con->escape((string) $meta); |
---|
329 | $q[] = "option_meta = '".$meta."'"; |
---|
330 | } |
---|
331 | |
---|
332 | if (empty($q)) { |
---|
333 | throw new Exception(__('Invalid request')); |
---|
334 | } |
---|
335 | |
---|
336 | $this->con->execute( |
---|
337 | 'DELETE FROM '.$this->table.' WHERE '.implode(' AND ',$q) |
---|
338 | ); |
---|
339 | |
---|
340 | $this->trigger(); |
---|
341 | } |
---|
342 | |
---|
343 | private function getOptionContent(&$cur,$option_id) |
---|
344 | { |
---|
345 | $option_content = $cur->option_content; |
---|
346 | $option_content_xhtml = $cur->option_content_xhtml; |
---|
347 | |
---|
348 | $this->setOptionContent( |
---|
349 | $option_id,$cur->option_format,$cur->option_lang, |
---|
350 | $option_content,$option_content_xhtml |
---|
351 | ); |
---|
352 | |
---|
353 | $cur->option_content = $option_content; |
---|
354 | $cur->option_content_xhtml = $option_content_xhtml; |
---|
355 | } |
---|
356 | |
---|
357 | public function setOptionContent($option_id,$format,$lang,&$content,&$content_xhtml) |
---|
358 | { |
---|
359 | if ($format == 'wiki') |
---|
360 | { |
---|
361 | $this->core->initWikiPost(); |
---|
362 | if (strpos($lang,'fr') === 0) { |
---|
363 | $this->core->wiki2xhtml->setOpt('active_fr_syntax',1); |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | if ($content) { |
---|
368 | $content_xhtml = $this->core->callFormater($format,$content); |
---|
369 | $content_xhtml = $this->core->HTMLfilter($content_xhtml); |
---|
370 | } else { |
---|
371 | $content_xhtml = ''; |
---|
372 | } |
---|
373 | |
---|
374 | # --BEHAVIOR-- coreAfterPostOptionContentFormat |
---|
375 | $this->core->callBehavior('coreAfterPostOptionContentFormat',array( |
---|
376 | 'content' => &$content, |
---|
377 | 'content_xhtml' => &$content_xhtml |
---|
378 | )); |
---|
379 | } |
---|
380 | } |
---|
381 | ?> |
---|