Dotclear

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

Revision 3059, 12.8 KB checked in by JcDenis, 12 years ago (diff)

eventHanlder oldies

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

Sites map