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 | |
---|
13 | class 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, 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 | } |
---|
92 | |
---|
93 | return $rs; |
---|
94 | } |
---|
95 | |
---|
96 | public function getPostsByEventdata($params=array(),$count_only=false) |
---|
97 | { |
---|
98 | if (!isset($params['columns'])) $params['columns'] = array(); |
---|
99 | $params['columns'][] = 'EV.eventdata_start'; |
---|
100 | $params['columns'][] = 'EV.eventdata_end'; |
---|
101 | |
---|
102 | if (!isset($params['from'])) $params['from'] = ''; |
---|
103 | $params['from'] .= ', '.$this->table.' EV '; |
---|
104 | |
---|
105 | if (!isset($params['sql'])) $params['sql'] = ''; |
---|
106 | $params['sql'] .= " AND EV.post_id = P.post_id "; |
---|
107 | |
---|
108 | if (isset($params['period'])) { |
---|
109 | switch($params['period']) { |
---|
110 | case 'ongoing': |
---|
111 | $params['sql'] .= "AND TIMESTAMP(EV.eventdata_start) < NOW() ". |
---|
112 | " AND TIMESTAMP(EV.eventdata_end) > NOW() "; break; |
---|
113 | case 'outgoing': |
---|
114 | $params['sql'] .= "AND (TIMESTAMP(EV.eventdata_start) > NOW() ". |
---|
115 | " OR TIMESTAMP(EV.eventdata_end) < NOW()) "; break; |
---|
116 | case 'notstarted': |
---|
117 | $params['sql'] .= "AND TIMESTAMP(EV.eventdata_start) > NOW() "; break; |
---|
118 | case 'scheduled': |
---|
119 | $params['sql'] .= "AND TIMESTAMP(EV.eventdata_start) > NOW() "; break; |
---|
120 | case 'started': |
---|
121 | $params['sql'] .= "AND TIMESTAMP(EV.eventdata_start) < NOW() "; break; |
---|
122 | case 'notfinished': |
---|
123 | $params['sql'] .= "AND TIMESTAMP(EV.eventdata_end) > NOW() "; break; |
---|
124 | case 'finished': |
---|
125 | $params['sql'] .= "AND TIMESTAMP(EV.eventdata_end) < NOW() "; break; |
---|
126 | } |
---|
127 | unset($params['period']); |
---|
128 | } |
---|
129 | if (!empty($params['eventdata_type'])) { |
---|
130 | $params['sql'] .= "AND EV.eventdata_type = '".$this->con->escape($params['eventdata_type'])."' "; |
---|
131 | unset($params['eventdata_type']); |
---|
132 | } |
---|
133 | if (!empty($params['eventdata_start'])) { |
---|
134 | $params['sql'] .= "AND EV.eventdata_start = '".$this->con->escape($params['eventdata_start'])."' "; |
---|
135 | unset($params['eventdata_start']); |
---|
136 | } |
---|
137 | if (!empty($params['eventdata_end'])) { |
---|
138 | $params['sql'] .= "AND EV.eventdata_end = '".$this->con->escape($params['eventdata_end'])."' "; |
---|
139 | unset($params['eventdata_end']); |
---|
140 | } |
---|
141 | |
---|
142 | # Metadata |
---|
143 | if (isset($params['meta_id'])) { |
---|
144 | $params['from'] .= ', '.$this->core->prefix.'meta META '; |
---|
145 | $params['sql'] .= 'AND META.post_id = P.post_id '; |
---|
146 | $params['sql'] .= "AND META.meta_id = '".$this->con->escape($params['meta_id'])."' "; |
---|
147 | |
---|
148 | if (!empty($params['meta_type'])) { |
---|
149 | $params['sql'] .= "AND META.meta_type = '".$this->con->escape($params['meta_type'])."' "; |
---|
150 | unset($params['meta_type']); |
---|
151 | } |
---|
152 | unset($params['meta_id']); |
---|
153 | } |
---|
154 | |
---|
155 | return $this->core->blog->getPosts($params,$count_only); |
---|
156 | } |
---|
157 | |
---|
158 | public function setEventdata($type,$post_id,$start,$end) |
---|
159 | { |
---|
160 | $post_id = (integer) $post_id; |
---|
161 | |
---|
162 | $cur = $this->con->openCursor($this->table); |
---|
163 | |
---|
164 | $cur->post_id = (integer) $post_id; |
---|
165 | $cur->eventdata_type = (string) $type; |
---|
166 | $cur->eventdata_start = (string) $start; |
---|
167 | $cur->eventdata_end = (string) $end; |
---|
168 | |
---|
169 | $cur->insert(); |
---|
170 | } |
---|
171 | |
---|
172 | public function delEventdata($type,$post_id,$start=null,$end=null) |
---|
173 | { |
---|
174 | $post_id = (integer) $post_id; |
---|
175 | |
---|
176 | $strReq = 'DELETE FROM '.$this->table.' '. |
---|
177 | 'WHERE post_id = '.$post_id.' '. |
---|
178 | "AND eventdata_type = '".$this->con->escape($type)."' "; |
---|
179 | |
---|
180 | if ($start !== null) $strReq .= "AND eventdata_start = '".$this->con->escape($start)."' "; |
---|
181 | if ($end !== null) $strReq .= "AND eventdata_end = '".$this->con->escape($end)."' "; |
---|
182 | |
---|
183 | $this->con->execute($strReq); |
---|
184 | } |
---|
185 | } |
---|
186 | ?> |
---|