Dotclear

source: plugins/pollsFactory/inc/class.postoption.php @ 2146

Revision 2146, 8.7 KB checked in by JcDenis, 14 years ago (diff)

pollsFactory 1.1:

  • Fixed order of queries and selections
  • Fixed admin posts list actions
  • Fixed adding polls from new post
  • Fixed pgSQL bugs
Line 
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
13if (!defined('DC_RC_PATH')){return;}
14
15class 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          if ($count_only) {
113               $q = 'SELECT count(O.option_id) ';
114          }
115          else {
116               $q = 'SELECT O.option_id, O.post_id, O.option_meta, ';
117
118               if (!empty($params['columns']) && is_array($params['columns'])) {
119                    $q .= implode(', ',$params['columns']).', ';
120               }
121               $q .= 
122               'O.option_creadt, O.option_upddt, O.option_type, O.option_format, '.
123               'O.option_title, O.option_content, O.option_content_xhtml, '.
124               'O.option_selected, O.option_position, '.
125               'P.blog_id, P.post_type, P.post_title ';
126          }
127
128          $q .= 
129          'FROM '.$this->table.' O '.
130          'LEFT JOIN '.$this->core->prefix.'post P ON P.post_id = O.post_id ';
131
132          if (!empty($params['from'])) {
133               $q .= $params['from'].' ';
134          }
135         
136          $q .= "WHERE P.blog_id = '".$this->blog."' ";
137
138          # option_type
139          if (isset($params['option_type'])) {
140               if (is_array($params['option_type']) && !empty($params['option_type'])) {
141                    $q .= 'AND O.option_type '.$this->con->in($params['option_type']);
142               } elseif ($params['option_type'] != '') {
143                    $q .= "AND O.option_type = '".$this->con->escape($params['option_type'])."' ";
144               } else {
145                    $q .= "AND O.option_type != '' ";
146               }
147          }
148          else {
149               $q .= "AND O.option_type = '' ";
150          }
151          # option_id
152          if (!empty($params['option_id'])) {
153               if (is_array($params['option_id'])) {
154                    array_walk($params['option_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
155               } else {
156                    $params['option_id'] = array((integer) $params['option_id']);
157               }
158               $q .= 'AND O.option_id '.$this->con->in($params['option_id']);
159          }
160          # post_id
161          if (!empty($params['post_id'])) {
162               if (is_array($params['post_id'])) {
163                    array_walk($params['post_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
164               } else {
165                    $params['post_id'] = array((integer) $params['post_id']);
166               }
167               $q .= 'AND O.post_id '.$this->con->in($params['post_id']);
168          }
169          # option_meta
170          if (isset($params['option_meta'])) {
171               if (is_array($params['option_meta']) && !empty($params['option_meta'])) {
172                    $q .= 'AND O.option_meta '.$this->con->in($params['option_meta']);
173               } elseif ($params['option_meta'] != '') {
174                    $q .= "AND O.option_meta = '".$this->con->escape($params['option_meta'])."' ";
175               } else {
176                    $q .= "AND O.option_meta IS NULL ";
177               }
178          }
179          # option_selected
180          if (isset($params['option_selected'])) {
181               $q .= 'AND O.option_selected = '.(integer) $params['option_selected'].' ';
182          }
183          # option_title
184          if (!empty($params['option_title'])) {
185               $q .= "AND O.option_title = '".$this->con->escape($params['option_title'])."' ";
186          }
187          # sql
188          if (!empty($params['sql'])) {
189               $q .= $params['sql'].' ';
190          }
191          # order
192          if (!$count_only) {
193               if (!empty($params['order'])) {
194                    $q .= 'ORDER BY '.$this->con->escape($params['order']).' ';
195               }
196               else {
197                    $q .= 'ORDER BY O.option_id ASC ';
198               }
199          }
200          # limit
201          if (!$count_only && !empty($params['limit'])) {
202               $q .= $this->con->limit($params['limit']);
203          }
204
205          $rs = $this->con->select($q);
206          $rs->postOption = $this;
207
208          return $rs;
209     }
210
211     public function addOption($cur)
212     {
213          $this->lock();
214          try
215          {
216               $cur->option_id = $this->nextID();
217               $cur->option_creadt = date('Y-m-d H:i:s');
218               $cur->option_upddt = date('Y-m-d H:i:s');
219
220               $this->getOptionContent($cur,$cur->option_id);
221               $this->checkAuth($cur);
222
223               $cur->insert();
224               $this->unlock();
225          }
226          catch (Exception $e)
227          {
228               $this->unlock();
229               throw $e;
230          }
231
232          $this->trigger();
233          return $cur->option_id;
234     }
235     
236     public function updOption($id,$cur)
237     {
238          $id = (integer) $id;
239         
240          if (empty($id)) {
241               throw new Exception(__('No such option ID'));
242          }
243         
244          $this->getOptionContent($cur,$id);
245          $this->checkAuth($cur);
246         
247          $cur->option_upddt = date('Y-m-d H:i:s');
248         
249          $cur->update('WHERE option_id = '.$id.' ');
250          $this->trigger();
251     }
252
253     private function updOptionField($id,$field,$value)
254     {
255          $id = (integer) $id;
256          if (empty($id)) {
257               throw new Exception(__('No such option ID'));
258          }
259
260          $cur = $this->open();
261          $cur->setField($field,$value);
262          $cur->option_upddt = date('Y-m-d H:i:s');
263          $cur->update("WHERE option_id = ".$id);
264
265          $this->trigger();
266     }
267
268     public function updOptionPosition($id,$val)
269     {
270          $val = (integer) $val;
271          $this->updOptionField($id,'option_position',$val);
272     }
273
274     public function updOptionSelected($id,$val)
275     {
276          $val = (integer) $val;
277          $this->updOptionField($id,'option_selected',$val);
278     }
279
280     public function delOption($id=null,$type=null,$post_id=null,$meta=null)
281     {
282          $q = array();
283          if ($id != '') {
284               $id = (integer) $id;
285               $q[] = "option_id = '".$id."'";
286          }
287          if ($type != '') {
288               $type = $this->con->escape((string) $type);
289               $q[] = "option_type = '".$type."'";
290          }
291          if ($post_id != '') {
292               $post_id = (integer) $post_id;
293               $q[] = "post_id = '".$post_id."'";
294          }
295          if ($meta != '') {
296               $meta = $this->con->escape((string) $meta);
297               $q[] = "option_meta = '".$meta."'";
298          }
299
300          if (empty($q)) {
301               throw new Exception(__('Invalid request'));
302          }
303
304          $this->con->execute(
305               'DELETE FROM '.$this->table.' WHERE '.implode(' AND ',$q)
306          );
307
308          $this->trigger();
309     }
310
311     private function getOptionContent(&$cur,$option_id)
312     {
313          $option_content = $cur->option_content;
314          $option_content_xhtml = $cur->option_content_xhtml;
315
316          $this->setOptionContent(
317               $option_id,$cur->option_format,$cur->option_lang,
318               $option_content,$option_content_xhtml
319          );
320
321          $cur->option_content = $option_content;
322          $cur->option_content_xhtml = $option_content_xhtml;
323     }
324
325     public function setOptionContent($option_id,$format,$lang,&$content,&$content_xhtml)
326     {
327          if ($format == 'wiki')
328          {
329               $this->core->initWikiPost();
330               if (strpos($lang,'fr') === 0) {
331                    $this->core->wiki2xhtml->setOpt('active_fr_syntax',1);
332               }
333          }
334
335          if ($content) {
336               $content_xhtml = $this->core->callFormater($format,$content);
337               $content_xhtml = $this->core->HTMLfilter($content_xhtml);
338          } else {
339               $content_xhtml = '';
340          }
341
342          # --BEHAVIOR-- coreAfterPostOptionContentFormat
343          $this->core->callBehavior('coreAfterPostOptionContentFormat',array(
344               'content' => &$content,
345               'content_xhtml' => &$content_xhtml
346          ));
347     }
348}
349?>
Note: See TracBrowser for help on using the repository browser.

Sites map