Dotclear

source: plugins/eventdata/class.dc.eventdata.php @ 1015

Revision 1015, 6.8 KB checked in by JcDenis, 14 years ago (diff)

eventdata:

  • fix some bugs
  • Add ics feeds
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of eventdata, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009 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
13class dcEventdata
14{
15     private $core;
16     private $con;
17     private $table;
18     
19     public function __construct(&$core)
20     {
21          $this->core =& $core;
22          $this->con =& $this->core->con;
23          $this->table = $this->core->prefix.'eventdata';
24     }
25
26     public function getEventdata($type=null,$limit=null,$eventdata_start=null,$eventdata_end=null,$post_id=null,$period=null,$sort='desc')
27     {
28          $strReq = 'SELECT eventdata_start, eventdata_end, eventdata_type, eventdata_location, COUNT(EV.post_id) as count '.
29          'FROM '.$this->table.' EV LEFT JOIN '.$this->core->prefix.'post P '.
30          'ON EV.post_id = P.post_id '.
31          "WHERE P.blog_id = '".$this->con->escape($this->core->blog->id)."' ";
32
33          if ($type !== null)
34               $strReq .= "AND eventdata_type = '".$this->con->escape($type)."' ";
35
36          switch ($period) {
37               case 'ongoing':
38                    $op = array('<','>'); break;
39               case 'outgoing':
40                    $op = array('>','<'); break;
41               case 'notstarted':
42                    $op = array('>','!'); break;
43               case 'scheduled':
44                    $op = array('>','!'); break;
45               case 'started':
46                    $op = array('','!'); break;
47               case 'notfinished':
48                    $op = array('!','>'); break;
49               case 'finished':
50                    $op = array('!','<'); break;
51               default:
52                    $op = array('=','='); break;
53          }
54
55          if ($eventdata_start !== null && $op[0] != '!')
56               $strReq .= "AND eventdata_start = '".$this->con->escape($eventdata_start)."' ";
57         
58          if ($eventdata_end !== null && $op[1] != '!')
59               $strReq .= "AND eventdata_end = '".$this->con->escape($eventdata_end)."' ";
60
61          if ($post_id !== null)
62               $strReq .= 'AND P.post_id = '.(integer) $post_id.' ';
63         
64          if (!$this->core->auth->check('contentadmin',$this->core->blog->id)) {
65               $strReq .= 'AND ((post_status = 1 ';
66               
67               if ($this->core->blog->without_password)
68                    $strReq .= 'AND post_password IS NULL ';
69
70               $strReq .= ') ';
71
72               if ($this->core->auth->userID())
73                    $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."' ";
74
75                    $strReq .= ') ';
76          }
77          $sort = strtoupper($sort) == 'ASC' ? 'ASC' : 'DESC';
78          $strReq .= 'GROUP BY eventdata_start,eventdata_end,eventdata_type,P.blog_id ORDER BY eventdata_start '.$sort.' ';
79
80          if ($limit)
81               $strReq .= $this->con->limit($limit);
82
83          $rs = $this->con->select($strReq);
84          $rs = $rs->toStatic();
85         
86          while ($rs->fetch()) {
87               $rs->set('start_ts',strtotime($rs->eventdata_start));
88               $rs->set('end_ts',strtotime($rs->eventdata_end));
89               $rs->set('start_ym',date('Ym',strtotime($rs->eventdata_start)));
90               $rs->set('end_ym',date('Ym',strtotime($rs->eventdata_end)));
91               $rs->set('duration_ts',(strtotime($rs->eventdata_end) - strtotime($rs->eventdata_start)));
92          }
93
94          return $rs;
95     }
96
97     public function getPostsByEventdata($params=array(),$count_only=false)
98     {
99          if (!isset($params['columns'])) $params['columns'] = array();
100          $params['columns'][] = 'EV.eventdata_start';
101          $params['columns'][] = 'EV.eventdata_end';
102          $params['columns'][] = 'EV.eventdata_location';
103
104          if (!isset($params['from'])) $params['from'] = '';
105          $params['from'] .= ' LEFT OUTER JOIN '.$this->table.' EV ON P.post_id = EV.post_id ';
106
107          if (!isset($params['sql'])) $params['sql'] = '';
108
109          if (isset($params['period'])) {
110               $ts_format = '%Y-%m-%d %H:%M:%S';
111               $ts_now = "TIMESTAMP '".dt::str($ts_format)."'";
112               $ts_start = 'EV.eventdata_start';
113               $ts_end = 'EV.eventdata_end';
114
115               switch($params['period']) {
116                    case 'ongoing':
117                    $params['sql'] .= 'AND '.$ts_start.' < '.$ts_now.' '.
118                         ' AND '.$ts_end.' > '.$ts_now.' '; break;
119                    case 'outgoing':
120                    $params['sql'] .= 'AND '.$ts_start.' > '.$ts_now.' '.
121                         ' AND '.$ts_end.' < '.$ts_now.' '; break;
122                    case 'notstarted':
123                    $params['sql'] .= 'AND '.$ts_start.' > '.$ts_now.' '; break;
124                    case 'scheduled':
125                    $params['sql'] .= 'AND '.$ts_start.' > '.$ts_now.' '; break;
126                    case 'started':
127                    $params['sql'] .= 'AND '.$ts_start.' < '.$ts_now.' '; break;
128                    case 'notfinished':
129                    $params['sql'] .= 'AND '.$ts_end.' > '.$ts_now.' '; break;
130                    case 'finished':
131                    $params['sql'] .= 'AND '.$ts_end.' < '.$ts_now.' '; break;
132               }
133               unset($params['period'],$ts_start,$ts_end,$ts_format,$ts_now);
134          }
135          if (!empty($params['eventdata_type'])) {
136               $params['sql'] .= "AND EV.eventdata_type = '".$this->con->escape($params['eventdata_type'])."' ";
137               unset($params['eventdata_type']);
138          }
139          if (!empty($params['eventdata_start'])) {
140               $params['sql'] .= "AND EV.eventdata_start = '".$this->con->escape($params['eventdata_start'])."' ";
141               unset($params['eventdata_start']);
142          }
143          if (!empty($params['eventdata_end'])) {
144               $params['sql'] .= "AND EV.eventdata_end = '".$this->con->escape($params['eventdata_end'])."' ";
145               unset($params['eventdata_end']);
146          }
147          if (!empty($params['eventdata_location'])) {
148               $params['sql'] .= "AND EV.eventdata_location = '".$this->con->escape($params['eventdata_location'])."' ";
149               unset($params['eventdata_location']);
150          }
151
152          # Metadata
153          if (isset($params['meta_id'])) {
154               $params['from'] .= ', '.$this->core->prefix.'meta META ';
155               $params['sql'] .= 'AND META.post_id = P.post_id ';
156               $params['sql'] .= "AND META.meta_id = '".$this->con->escape($params['meta_id'])."' ";
157               
158               if (!empty($params['meta_type'])) {
159                    $params['sql'] .= "AND META.meta_type = '".$this->con->escape($params['meta_type'])."' ";
160                    unset($params['meta_type']);
161               }
162               unset($params['meta_id']);         
163          }
164
165          return $this->core->blog->getPosts($params,$count_only);
166     }
167
168     public function setEventdata($type,$post_id,$start,$end,$location='')
169     {
170          $post_id = (integer) $post_id;
171
172          $cur = $this->con->openCursor($this->table);
173
174          $cur->post_id = (integer) $post_id;
175          $cur->eventdata_type = (string) $type;
176          $cur->eventdata_start = (string) $start;
177          $cur->eventdata_end = (string) $end;
178          $cur->eventdata_location = (string) $location;
179
180          $cur->insert();
181     }
182
183     public function delEventdata($type,$post_id,$start=null,$end=null,$location=null)
184     {
185          $post_id = (integer) $post_id;
186
187          $strReq = 'DELETE FROM '.$this->table.' '.
188                    'WHERE post_id = '.$post_id.' '.
189                    "AND eventdata_type = '".$this->con->escape($type)."' ";
190
191          if ($start !== null) $strReq .= "AND eventdata_start = '".$this->con->escape($start)."' ";
192          if ($end !== null) $strReq .= "AND eventdata_end = '".$this->con->escape($end)."' ";
193          if ($location !== null) $strReq .= "AND eventdata_location = '".$this->con->escape($location)."' ";
194
195          $this->con->execute($strReq);
196     }
197}   
198?>
Note: See TracBrowser for help on using the repository browser.

Sites map