Dotclear

source: plugins/periodical/inc/class.periodical.php @ 2160

Revision 2160, 8.9 KB checked in by JcDenis, 14 years ago (diff)

periodical 0.1:

  • First lab release
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of periodical, 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
14class periodical
15{
16     public $core;
17     public $con;
18     
19     protected $table;
20     protected $blog;
21     
22     public function __construct($core)
23     {
24          $this->core = $core;
25          $this->con = $core->con;
26         
27          $this->table = $core->con->escape($core->prefix.'periodical');
28          $this->blog = $core->con->escape($core->blog->id);
29     }
30
31     public function openCursor()
32     {
33          return $this->con->openCursor($this->table);
34     }
35
36     public function nextID()
37     {
38          return $this->con->select(
39               'SELECT MAX(periodical_id) FROM '.$this->table
40          )->f(0) + 1;
41     }
42
43     # Get periods
44     public function getPeriods($params=array(),$count_only=false)
45     {
46          if ($count_only) {
47               $q = 'SELECT count(T.periodical_id) ';
48          }
49          else {
50
51               $q =
52               'SELECT T.periodical_id, T.periodical_type, '.
53               'T.periodical_title, T.periodical_tz, '.
54               'T.periodical_curdt, T.periodical_enddt, '.
55               'T.periodical_pub_int, T.periodical_pub_nb ';
56          }
57
58          $q .=
59          'FROM '.$this->table.' T ';
60
61          if (!empty($params['from'])) {
62               $q .= $params['from'].' ';
63          }
64          $q .= "WHERE blog_id = '".$this->blog."' ";
65
66          if (isset($params['periodical_type'])) {
67               if (is_array($params['periodical_type']) && !empty($params['periodical_type'])) {
68                    $q .= 'AND T.periodical_type '.$this->con->in($params['periodical_type']);
69               }
70               elseif ($params['periodical_type'] != '') {
71                    $q .= "AND T.periodical_type = '".$this->con->escape($params['periodical_type'])."' ";
72               }
73          }
74          else {
75               $q .= "AND T.periodical_type = 'post' ";
76          }
77          if (!empty($params['periodical_id'])) {
78               if (is_array($params['periodical_id'])) {
79                    array_walk($params['periodical_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
80               }
81               else {
82                    $params['periodical_id'] = array((integer) $params['periodical_id']);
83               }
84               $q .= 'AND T.periodical_id '.$this->con->in($params['periodical_id']);
85          }
86          if (!empty($params['periodical_title'])) {
87               $q .= "AND T.periodical_title = '".$this->con->escape($params['periodical_title'])."' ";
88          }
89          if (!empty($params['sql'])) {
90               $q .= $params['sql'].' ';
91          }
92          if (!$count_only) {
93               if (!empty($params['order'])) {
94                    $q .= 'ORDER BY '.$this->con->escape($params['order']).' ';
95               }
96               else {
97                    $q .= 'ORDER BY T.periodical_id ASC ';
98               }
99          }
100          if (!$count_only && !empty($params['limit'])) {
101               $q .= $this->con->limit($params['limit']);
102          }
103          $rs = $this->con->select($q);
104          $rs->core = $this->core;
105          $rs->periodical = $this;
106         
107          return $rs;
108     }
109
110     public function addPeriod($cur)
111     {
112          $this->con->writeLock($this->table);
113         
114          try {
115               $cur->periodical_id = $this->nextID();
116               $cur->blog_id = $this->blog;
117               $cur->periodical_type = 'post';
118               $cur->periodical_tz = $this->core->auth->getInfo('user_tz');
119               $cur->insert();
120               $this->con->unlock();
121          }
122          catch (Exception $e)
123          {
124               $this->con->unlock();
125               throw $e;
126          }
127          return $cur->periodical_id;
128     }
129
130     public function updPeriod($period_id,$cur)
131     {
132          $period_id = (integer) $period_id;
133
134          if ($cur->periodical_tz == '' 
135          && ($cur->periodical_curdt != '' || $cur->periodical_enddt != '')) {
136               $cur->periodical_tz = $this->core->auth->getInfo('user_tz');
137          }
138          $cur->update(
139               "WHERE blog_id = '".$this->blog."' ".
140               "AND periodical_id = ".$period_id." "
141          );
142     }
143
144     # Delete a period
145     public function delPeriod($period_id)
146     {
147          $period_id = (integer) $period_id;
148         
149          $params = array();
150          $params['periodical_id'] = $period_id;
151          $params['post_status'] = '';
152          $rs = $this->getPosts($params);
153
154          if (!$rs->isEmpty()) {
155               throw new Exception('Periodical is not empty');
156          }
157         
158          $this->con->execute(
159               'DELETE FROM '.$this->table.' '.
160               "WHERE blog_id = '".$this->blog."' ".
161               "AND periodical_id = ".$period_id." "
162          );
163     }
164
165     # Remove all posts related to a period
166     public function delPeriodPosts($period_id)
167     {
168          $params = array();
169          $params['post_status'] = '';
170          $params['periodical_id'] = (integer) $period_id;
171
172          $rs = $this->getPosts($params);
173
174          if ($rs->isEmpty()) return;
175
176          $ids = array();
177          while($rs->fetch())
178          {
179               $ids[] = $rs->post_id;
180          }
181
182          if (empty($ids)) return;
183
184          $this->con->execute(
185               'DELETE FROM '.$this->core->prefix.'meta '.
186               "WHERE meta_type = 'periodical' ".
187               "AND post_id ".$this->con->in($ids)
188          );
189     }
190
191     # Get posts related to periods
192     public function getPosts($params=array(),$count_only=false)
193     {
194          if (!isset($params['columns'])) $params['columns'] = array();
195          if (!isset($params['from'])) $params['from'] = '';
196          if (!isset($params['sql'])) $params['sql'] = '';
197
198          $params['columns'][] = 'T.periodical_id';
199          $params['columns'][] = 'T.periodical_title';
200          $params['columns'][] = 'T.periodical_type';
201          $params['columns'][] = 'T.periodical_tz';
202          $params['columns'][] = 'T.periodical_curdt';
203          $params['columns'][] = 'T.periodical_enddt';
204          $params['columns'][] = 'T.periodical_pub_int';
205          $params['columns'][] = 'T.periodical_pub_nb';
206
207          $params['from'] .= 'LEFT JOIN '.$this->core->prefix.'meta R ON P.post_id = R.post_id ';
208          $params['from'] .= 'LEFT JOIN '.$this->table.' T ON R.meta_id = T.periodical_id ';
209
210          $params['sql'] .= "AND R.meta_type = 'periodical' ";
211          $params['sql'] .= "AND T.periodical_type = 'post' ";
212
213          if (!empty($params['periodical_id'])) {
214               if (is_array($params['periodical_id'])) {
215                    array_walk($params['periodical_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
216               }
217               else {
218                    $params['periodical_id'] = array((integer) $params['periodical_id']);
219               }
220               $params['sql'] .= 'AND T.periodical_id '.$this->con->in($params['periodical_id']);
221               unset($params['periodical_id']);
222          }
223          if ($this->core->auth->check('admin',$this->core->blog->id)) {
224               if (isset($params['post_status'])) {
225                    if ($params['post_status'] != '') {
226                         $params['sql'] .= 'AND P.post_status = '.(integer) $params['post_status'].' ';
227                    }
228                    unset($params['post_status']);
229               }
230          }
231          else {
232               $params['sql'] .= 'AND P.post_status = -2 ';
233          }
234
235          $rs = $this->core->blog->getPosts($params,$count_only);
236          $rs->periodical = $this;
237
238          return $rs;
239     }
240
241     # Add post to periodical
242     public function addPost($period_id,$post_id)
243     {
244          $period_id = (integer) $period_id;
245          $post_id = (integer) $post_id;
246         
247          # Check if exists
248          $rs = $this->getPosts(array('post_id' => $post_id,'periodical_id' => $period_id));
249          if (!$rs->isEmpty()) return;
250
251          $cur = $this->con->openCursor($this->core->prefix.'meta');
252          $this->con->writeLock($this->core->prefix.'meta');
253         
254          try {
255               $cur->post_id = $post_id;
256               $cur->meta_id = $period_id;
257               $cur->meta_type = 'periodical';
258               $cur->insert();
259               $this->con->unlock();
260          }
261          catch (Exception $e)
262          {
263               $this->con->unlock();
264               throw $e;
265          }
266     }
267
268     # Delete post from periodical
269     public function delPost($post_id)
270     {
271          $post_id = (integer) $post_id;
272
273          $this->con->execute(
274               'DELETE FROM '.$this->core->prefix.'meta '.
275               "WHERE meta_type = 'periodical' ".
276               "AND post_id = '".$post_id."' "
277          );
278          return true;
279     }
280
281     # Remove all posts without pending status from periodical
282     public function cleanPosts($period_id=null)
283     {
284          $params = array();
285          $params['post_status'] = '';
286          $params['sql'] = 'AND post_status != -2 ';
287          if ($period_id !== null) {
288               $params['periodical_id'] = (integer) $period_id;
289          }
290          $rs = $this->getPosts($params);
291
292          if ($rs->isEmpty()) return;
293
294          $ids = array();
295          while($rs->fetch())
296          {
297               $ids[] = $rs->post_id;
298          }
299
300          if (empty($ids)) return;
301
302          $this->con->execute(
303               'DELETE FROM '.$this->core->prefix.'meta '.
304               "WHERE meta_type = 'periodical' ".
305               "AND post_id ".$this->con->in($ids)
306          );
307     }
308     
309     public static function getTimesCombo()
310     {
311          return array(
312               __('Hourly') => 'hour',
313               __('twice a day') => 'halfday',
314               __('Daily') => 'day',
315               __('Weekly') => 'week',
316               __('Monthly') => 'month'
317          );
318     }
319
320     public static function getNextTime($ts,$period)
321     {
322          $ts = (integer) $ts;
323          $e = explode(',',date('H,i,s,n,j,Y',$ts));
324          switch($period) {
325               case 'hour':
326               $new_ts = mktime($e[0] + 1,$e[1],$e[2],$e[3],$e[4],$e[5]);
327               break;
328
329               case 'halfday':
330               $new_ts = mktime($e[0],$e[1] + 12,$e[2],$e[3],$e[4],$e[5]);
331               break;
332
333               case 'day':
334               $new_ts = mktime($e[0],$e[1],$e[2],$e[3],$e[4] + 1,$e[5]);
335               break;
336
337               case 'week':
338               $new_ts = mktime($e[0],$e[1],$e[2],$e[3],$e[4] + 7,$e[5]);
339               break;
340
341               case 'month':
342               $new_ts = mktime($e[0],$e[1],$e[2],$e[3] + 1,$e[4],$e[5]);
343               break;
344
345               default:
346               $new_ts = 0;
347               throw new Exception(__('Unknow frequence'));
348               break;
349          }
350          return $new_ts;
351     }
352}
353?>
Note: See TracBrowser for help on using the repository browser.

Sites map