Dotclear

source: plugins/eventHandler/inc/class.eventhandler.php @ 2648

Revision 2648, 12.8 KB checked in by JcDenis, 13 years ago (diff)

eventHandler 1.0-RC2

  • Fixed some bugs and typo
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of eventHandler, 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 eventHandler
16{
17     public $core;
18     public $con;
19     
20     protected $type;
21     protected $table;
22     protected $blog;
23     
24     public function __construct($core,$type='eventhandler')
25     {
26          $this->core = $core;
27          $this->con = $core->con;
28          $this->type = (string) $type;
29          $this->table = $core->prefix.'eventhandler';
30          $this->blog = $core->con->escape($core->blog->id);
31         
32     }
33     
34     public static function cleanedParams($params)
35     {
36          # Prepare params
37          if (!isset($params['columns'])) $params['columns'] = array();
38          if (!isset($params['from'])) $params['from'] = '';
39          if (!isset($params['sql'])) $params['sql'] = '';
40         
41          return $params;
42     }
43     
44     # Get record of events
45     public function getEvents($params,$count_only=false)
46     {
47          $params = self::cleanedParams($params);
48         
49          # Regain post_id
50          if (isset($params['event_id']))
51          {
52               $params['post_id'] = $params['event_id'];
53               unset($params['event_id']);
54          }
55          # Regain post_type
56          if (isset($params['event_type']))
57          {
58               $params['post_type'] = $params['event_type'];
59               unset($params['event_type']);
60          }
61          # Default post_type
62          if (!isset($params['post_type']))
63          {
64               $params['post_type'] = $this->type;
65          }
66         
67          # Columns of table eventhandler
68          if (!isset($params['columns'])) {
69               $params['columns'] = array();
70          }
71          $params['columns'][] = 'event_startdt';
72          $params['columns'][] = 'event_enddt';
73          $params['columns'][] = 'event_address';
74          $params['columns'][] = 'event_latitude';
75          $params['columns'][] = 'event_longitude';
76         
77          # Tables       
78          $params['from'] = 'INNER JOIN '.$this->table.' EH ON  EH.post_id = P.post_id'.$params['from'];
79         
80          # Period
81          if (!empty($params['event_period']) && $params['event_period'] != 'all')
82          {
83               switch ($params['event_period'])
84               {
85                    case 'ongoing':
86                         $op = array('<','>','AND'); break;
87                    case 'outgoing':
88                         $op = array('>','<','OR'); break;
89                    case 'notstarted':
90                    case 'scheduled':
91                         $op = array('>','!'); break;
92                    case 'started':
93                         $op = array('<','!'); break;
94                    case 'notfinished':
95                         $op = array('!','>'); break;
96                    case 'finished':
97                         $op = array('!','<'); break;
98                    default:
99                         $op = array('=','=','AND'); break;
100               }
101               $now = date('Y-m-d H:i:s');
102               
103               $params['sql'] .= $op[0] != '!' && $op[1] != '!' ? 'AND (' : 'AND ';
104               
105               if (!empty($params['event_startdt']) && $op[0] != '!')
106               {
107                    $params['sql'] .= "EH.event_startdt ".$op[0]." TIMESTAMP '".$this->con->escape($params['event_startdt'])."'";
108               
109                    //unset($params['event_startdt']);
110               }
111               elseif (empty($params['event_startdt']) && $op[0] != '!')
112               {
113                    $params['sql'] .= "EH.event_startdt ".$op[0]." TIMESTAMP '".$now."'";
114               }
115               
116               $params['sql'] .= $op[0] != '!' && $op[1] != '!' ? ' '.$op[2].' ' : '';
117               
118               if (!empty($params['event_enddt']) && $op[1] != '!')
119               {
120                    $params['sql'] .= "EH.event_enddt ".$op[1]." TIMESTAMP '".$this->con->escape($params['event_enddt'])."'";
121               
122                    //unset($params['event_enddt']);
123               }
124               elseif (empty($params['event_enddt']) && $op[1] != '!')
125               {
126                    $params['sql'] .= "EH.event_enddt ".$op[1]." TIMESTAMP '".$now."'";
127               }
128               
129               $params['sql'] .= $op[0] != '!' && $op[1] != '!' ? ') ' : ' ';
130               
131               //unset($params['event_period']); // used on template
132          }
133         
134          # Cut start date
135          if (!empty($params['event_start_year']))
136          {
137               $params['sql'] .= 'AND '.$this->con->dateFormat('EH.event_startdt','%Y').' = '.
138               "'".sprintf('%04d',$params['event_start_year'])."' ";
139               
140               //unset($params['event_start_year']);
141          }
142          if (!empty($params['event_start_month']))
143          {
144               $params['sql'] .= 'AND '.$this->con->dateFormat('EH.event_startdt','%m').' = '.
145               "'".sprintf('%02d',$params['event_start_month'])."' ";
146               
147               //unset($params['event_start_month']);
148          }
149          if (!empty($params['event_start_day']))
150          {
151               $params['sql'] .= 'AND '.$this->con->dateFormat('EH.event_startdt','%d').' = '.
152               "'".sprintf('%02d',$params['event_start_day'])."' ";
153               
154               //unset($params['event_start_day']);
155          }
156         
157          # Cut end date
158          if (!empty($params['event_end_year']))
159          {
160               $params['sql'] .= 'AND '.$this->con->dateFormat('EH.event_enddt','%Y').' = '.
161               "'".sprintf('%04d',$params['event_end_year'])."' ";
162               
163               //unset($params['event_end_year']);
164          }
165          if (!empty($params['event_end_month']))
166          {
167               $params['sql'] .= 'AND '.$this->con->dateFormat('EH.event_enddt','%m').' = '.
168               "'".sprintf('%02d',$params['event_end_month'])."' ";
169               
170               //unset($params['event_end_month']);
171          }
172          if (!empty($params['event_endt_day']))
173          {
174               $params['sql'] .= 'AND '.$this->con->dateFormat('EH.event_enddt','%d').' = '.
175               "'".sprintf('%02d',$params['event_end_day'])."' ";
176               
177               //unset($params['event_endt_day']);
178          }
179         
180          # Localization
181          if (!empty($params['event_address']))
182          {
183               $params['sql'] .= "AND EH.event_address = '".$this->con->escape($params['event_address'])."' ";
184               
185               //unset($params['event_address']);
186          }
187         
188          $rs = $this->core->blog->getPosts($params,$count_only);
189          $rs->eventHandler = $this;
190          $rs->extend('rsExtEventHandlerPublic');
191         
192          # --BEHAVIOR-- coreEventHandlerGetEvents
193          $this->core->callBehavior('coreEventHandlerGetEvents',$rs);
194         
195          return $rs;
196     }
197     
198     # Get record of events linked to a "normal post"
199     public function getEventsByPost($params=array(),$count_only=false)
200     {
201          $params = self::cleanedParams($params);
202         
203          if (!isset($params['post_id']))
204          {
205               return null;
206          }
207          if (!isset($params['event_type']))
208          {
209               $params['event_type'] = $this->type;
210          }
211         
212          $params['from'] .= ', '.$this->core->prefix.'meta EM ';
213         
214          if ($this->con->driver() == 'mysql')
215          {
216               $params['sql'] .= 'AND EM.meta_id = CAST(P.post_id as char) ';
217          }
218          else
219          {
220               $params['sql'] .= 'AND CAST(EM.meta_id as int) = CAST(P.post_id as int) ';
221          }
222         
223          $params['sql'] .= "AND EM.post_id = '".$this->con->escape($params['post_id'])."' ";
224          $params['sql'] .= "AND EM.meta_type = '".$this->con->escape($params['event_type'])."' ";
225         
226          unset($params['post_id']);
227         
228          return $this->getEvents($params,$count_only);
229     }
230     
231     # Get record of "normal posts" linked to an event
232     public function getPostsByEvent($params=array(),$count_only=false)
233     {
234          $params = self::cleanedParams($params);
235         
236          if (!isset($params['event_id']))
237          {
238               return null;
239          }
240          if (!isset($params['event_type']))
241          {
242               $params['event_type'] = $this->type;
243          }
244          if(!isset($params['post_type']))
245          {
246               $params['post_type'] = '';
247          }
248          $params['from'] .= ', '.$this->core->prefix.'meta EM ';
249          $params['sql'] .= 'AND EM.post_id = P.post_id ';
250          $params['sql'] .= "AND EM.meta_id = '".$this->con->escape($params['event_id'])."' ";
251          $params['sql'] .= "AND EM.meta_type = '".$this->con->escape($params['event_type'])."' ";
252         
253          unset($params['event_id'],$params['event_type']);
254         
255          return $this->core->blog->getPosts($params,$count_only);
256     }
257     
258     # Add an event
259     public function addEvent($cur_post,$cur_event)
260     {
261          if (!$this->core->auth->check('usage,contentadmin',$this->blog))
262          {
263               throw new Exception(__('You are not allowed to create an event'));
264          }
265         
266          $this->con->begin();
267          $this->con->writeLock($this->table);
268          try
269          {
270               # Clean cursor
271               $this->getEventCursor(null,$cur_post,$cur_event);
272               
273               # Adding first part of event record
274               $cur_event->post_id = $this->core->blog->addPost($cur_post);
275               
276               # Create second part of event record
277               $cur_event->insert();
278               
279               $this->con->unlock();
280          }
281          catch (Exception $e)
282          {
283               $this->con->unlock();
284               $this->con->rollback();
285               throw $e;
286          }
287          $this->con->commit();
288         
289          return $cur_event->post_id;
290     }
291     
292     # Update an event
293     public function updEvent($post_id,$cur_post,$cur_event)
294     {
295          if (!$this->core->auth->check('usage,contentadmin',$this->blog))
296          {
297               throw new Exception(__('You are not allowed to update events'));
298          }
299         
300          $post_id = (integer) $post_id;
301         
302          if (empty($post_id))
303          {
304               throw new Exception(__('No such event ID'));
305          }
306         
307          $this->con->begin();
308          //$this->con->writeLock($this->table);
309          try
310          {
311               # Clean cursor
312               $this->getEventCursor($post_id,$cur_post,$cur_event);
313               
314               # Update first part of event record
315               $this->core->blog->updPost($post_id,$cur_post);
316               
317               # Set post_id
318               $cur_event->post_id = $post_id;
319               
320               # update second part of event record
321               $cur_event->update(
322                    "WHERE post_id = '".$post_id."' "
323               );
324               
325               //$this->con->unlock();
326          }
327          catch (Exception $e)
328          {
329               //$this->con->unlock();
330               $this->con->rollback();
331               throw $e;
332          }
333          $this->con->commit();
334     }
335     
336     # Delete an event
337     public function delEvent($post_id)
338     {
339          if (!$this->core->auth->check('delete,contentadmin',$this->blog))
340          {
341               throw new Exception(__('You are not allowed to delete events'));
342          }
343         
344          $post_id = (integer) $post_id;
345         
346          if (empty($post_id))
347          {
348               throw new Exception(__('No such event ID'));
349          }
350
351          # Delete first part of event record
352          $this->core->blog->delPost($post_id);
353         
354//what about reference key?
355          # Delete second part of event record
356          $this->con->execute(
357               'DELETE FROM '.$this->table.' '.
358               'WHERE post_id = '.$post_id.' '
359          );
360     }
361     
362     # Clean cursor
363     private function getEventCursor($post_id,$cur_post,$cur_event)
364     {
365          # Required a start date
366          if ($cur_event->event_startdt == '')
367          {
368               throw new Exception(__('No event start date'));
369          }
370          # Required an end date
371          if ($cur_event->event_enddt == '')
372          {
373               throw new Exception(__('No event end date'));
374          }
375          # Compare dates
376          if (strtotime($cur_event->event_enddt) < strtotime($cur_event->event_startdt))
377          {
378               throw new Exception(__('Start date greater than end date'));
379          }
380          # Full coordiantes or nothing
381          if(($cur_event->event_latitude != '' && $cur_event->event_longitude == '')
382          || ($cur_event->event_latitude == '' && $cur_event->event_longitude != ''))
383          {
384               throw new Exception(__('Not full coordinate'));
385          }
386          # Coordinates format
387          if ($cur_event->event_latitude != '')
388          {
389               if (!preg_match('/^(-|)[0-9.]+$/',$cur_event->event_latitude))
390               {
391                    throw new Exception(__('Wrong format of coordinate'));
392               }
393          }
394          # Coordinates format
395          if ($cur_event->event_longitude != '')
396          {
397               if (!preg_match('/^(-|)[0-9.]+$/',$cur_event->event_longitude))
398               {
399                    throw new Exception(__('Wrong format of coordinate'));
400               }
401          }
402          # Set post type
403          if (!$post_id && $cur_post->post_type == '')
404          {
405               $cur_post->post_type = $this->type;
406          }
407         
408          # Force no comment
409          $cur_post->unsetField('post_open_comment');
410          $cur_post->post_open_comment = 0;
411         
412          # Force no trackback
413          $cur_post->unsetField('post_open_tb');
414          $cur_post->post_open_tb = 0;
415         
416          # unset post_id
417          $cur_event->unsetField('post_id');
418     }
419     
420     # Get human readable duration from integer
421     public static function getReadableDuration($int,$format='second')
422     {
423         $int = (integer) $int;
424         $time = '';
425          //$sec = $min = $hou = $day = 0;
426
427//todo format
428
429         $sec = $int % 60; $int -= $sec; $int /= 60;
430         $min = $int % 60; $int -= $min; $int /= 60;
431         $hou = $int % 24; $int -= $hou; $int /= 24;
432         $day = $int;
433         
434         if ($day>1) $time .= sprintf(__('%s days'),$day).' ';
435         if ($day==1) $time .=__('one day').' ';
436         if ($hou>1) $time .= sprintf(__('%s hours'),$hou).' ';
437         if ($hou==1) $time .= __('one hour').' ';
438         if ($min>1) $time .= sprintf(__('%s minutes'),$min).' ';
439         if ($min==1) $time .= __('one minute').' ';
440         if (!$day && !$min && !$day && !$hou) $time .= __('instantaneous');
441
442         return $time;
443     }
444     
445     # Build HTML content for events maps
446     # markers are in lib.eventhandler.extension.php
447     public static function getGmapContent($width,$height,$type,$zoom,$info,$lat,$lng,$markers)
448     {
449          $style = '';
450          if ($width || $height) {
451               $style = 'style="';
452               if ($width) {
453                    $style .= 'width:'.$width.';';
454               }
455               if ($height) {
456                    $style .= 'height:'.$height.';';
457               }
458               $style .= '" ';
459          }
460         
461          return 
462          '<div style="display:none;" class="event-gmap">'.
463          '<div '.$style.'class="event-gmap-place"><p>'.__("Please wait, try to create map...").'</p></div>'.
464          '<div style="display:none;" class="event-gmap-info">'.
465          '<p class="event-gmap-info-zoom">'.$zoom.'</p>'.
466          '<p class="event-gmap-info-type">'.$type.'</p>'.
467          '<p class="event-gmap-info-info">'.$info.'</p>'.
468          '<p class="event-gmap-info-lat">'.$lat.'</p>'.
469          '<p class="event-gmap-info-lng">'.$lng.'</p>'.
470          '</div>'.
471          $markers.
472          '</div>';
473     }
474}
475?>
Note: See TracBrowser for help on using the repository browser.

Sites map