This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec_db.class.php
373 lines (344 loc) · 12.2 KB
/
ec_db.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* This file contains WP Events Calendar plugin.
*
* This is the main WPEC file.
* @internal Complete the description.
*
* @package WP-Events-Calendar
* @since 1.0
*
* @autbor Luke Howell <[email protected]>
*
* @copyright Copyright (c) 2007-2009 Luke Howell
*
* @license GPLv3 {@link http://www.gnu.org/licenses/gpl}
* @filesource
*/
/*
--------------------------------------------------------------------------
$Id$
--------------------------------------------------------------------------
This file is part of the WordPress Events Calendar plugin project.
For questions, help, comments, discussion, etc., please join our
forum at {@link http://www.wp-eventscalendar.com/forum}. You can
also go to Luke's ({@link http://www.lukehowelll.com}) blog.
WP Events Calendar is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
if(!class_exists('EC_DB')):
/**
* This class is used by WPEC to access and modify the database.
*
* All the DB work needed by WPEC is done through this class.
* Internally, it uses the $wpdb global object provided by WordPress.
*
* If installing the plugin, the class will be called upon to create
* or update the WPEC database table.
*
* tHIS IS WHY IT IS IMPORTANT TO DEACTIVATE AND THEN REACTIVATE
* THE PLUGIN WHEN UPGRADING TO A NEW VERSION.
*
* Later on, EC_DB is used to read, create, modify and delete events.
*
* @package WP-Events-Calendar
* @since 6.0
*/
class EC_DB
{
/**
* Holds an instance of the $wpdb object.
* @var object
* @access private
*/
var $db;
/**
* Name of the WPEC table where events are stored.
* @var string
* @access private
*/
var $mainTable;
/**
* Name of the posts table with its prefix.
* @var string
* @access private
*/
var $postsTable;
/**
* Holds the main WPEC table version.
* @var int
* @access private
*/
var $dbVersion;
/**
* Constructor.
* Loads the $wpdb global object and makes sure we have the good table name
*/
function __construct()
{
global $wpdb;
$this->dbVersion = "108";
$this->db = $wpdb;
$this->mainTable = $this->db->prefix . 'eventscalendar_main';
// FIXME why is this needed? Is it for backward compatibility?
$this->mainTableCaps = $this->db->prefix . 'EventsCalendar_main';
if ($this->db->get_var("show tables like '$this->mainTableCaps'") == $this->mainTableCaps)
$this->mainTable = $this->mainTableCaps;
$this->postsTable = $this->db->prefix . 'posts';
}
/**
* Called on plugin activation to create or upgrade the WPEC table.
* FIXME I don't think we need that much code here. get_option will
* return false if an option does not exist. This means that
* if the eventscalendar_db_version is false or different
* from the new version, we just execute the SQL.
*/
function createTable()
{
if ($this->db->get_var("show tables like '$this->mainTable'") != $this->mainTable )
{
$sql = "CREATE TABLE " . $this->mainTable . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
eventTitle varchar(255) CHARACTER SET utf8 NOT NULL,
eventDescription text CHARACTER SET utf8 NOT NULL,
eventLocation varchar(255) CHARACTER SET utf8 default NULL,
eventLinkout varchar(255) CHARACTER SET utf8 default NULL,
eventStartDate date NOT NULL,
eventStartTime time default NULL,
eventEndDate date NOT NULL,
eventEndTime time default NULL,
accessLevel varchar(255) CHARACTER SET utf8 NOT NULL default 'public',
postID mediumint(9) NULL DEFAULT NULL,
PRIMARY KEY id (id)
);";
require_once(ABSPATH . "wp-admin/upgrade-functions.php");
dbDelta($sql);
// Request without CHARACTER SET utf8 if the CREATE TABLE failed
if ($this->db->get_var("show tables like '$this->mainTable'") != $this->mainTable )
{
$sql = str_replace("CHARACTER SET utf8 ","",$sql);
dbDelta($sql);
}
add_option("events_calendar_db_version", $this->dbVersion);
}
$installed_ver = get_option( "events_calendar_db_version" );
if ($installed_ver != $this->dbVersion)
{
$sql = "CREATE TABLE " . $this->mainTable . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
eventTitle varchar(255) CHARACTER SET utf8 NOT NULL,
eventDescription text CHARACTER SET utf8 NOT NULL,
eventLocation varchar(255) CHARACTER SET utf8 default NULL,
eventLinkout varchar(255) CHARACTER SET utf8 default NULL,
eventStartDate date NOT NULL,
eventStartTime time default NULL,
eventEndDate date NOT NULL,
eventEndTime time default NULL,
accessLevel varchar(255) CHARACTER SET utf8 NOT NULL default 'public',
postID mediumint(9) NULL DEFAULT NULL,
PRIMARY KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
dbDelta($sql);
$this->db->query("UPDATE " . $this->mainTable . " SET `eventLocation` = REPLACE(`eventLocation`,' ','');");
$this->db->query("UPDATE " . $this->mainTable . " SET `eventLocation` = REPLACE(`eventLocation`,'',NULL);");
$this->db->query("UPDATE " . $this->mainTable . " SET `eventStartTime` = REPLACE(`eventStartTime`,'00:00:00',NULL);");
$this->db->query("UPDATE " . $this->mainTable . " SET `eventEndTime` = REPLACE(`eventEndTime`,'00:00:00',NULL);");
update_option( "events_calendar_db_version", $this->dbVersion);
}
}
/**
* Initializes the WPEC options.
*
* This makes sure our options are in database with sensible values.
*
* There are two sets of options, the Events Calendar general options
* and the widget options.
*/
function initOptions()
{
$options = get_option('optionsEventsCalendar');
if(!is_array($options)) $options = array();
if (!isset($options['dateFormatWidget'])) $options['dateFormatWidget'] = 'm-d';
if (!isset($options['timeFormatWidget'])) $options['timeFormatWidget'] = 'g:i a';
if (!isset($options['dateFormatLarge'])) $options['dateFormatLarge'] = 'n/j/Y';
if (!isset($options['timeFormatLarge'])) $options['timeFormatLarge'] = 'g:i a';
if (!isset($options['timeStep'])) $options['timeStep'] = '30';
if (!isset($options['adaptedCSS'])) $options['adaptedCSS'] = ''; //TODO: remove this option
if (!isset($options['jqueryextremstatus'])) $options['jqueryextremstatus'] = 'false';
if (!isset($options['dayHasEventCSS'])) $options['dayHasEventCSS'] = 'color:red;'; //TODO: remove this option
if (!isset($options['daynamelength'])) $options['daynamelength'] = '3';
if (!isset($options['daynamelengthLarge'])) $options['daynamelengthLarge'] = '3';
if (!isset($options['accessLevel'])) $options['accessLevel'] = 'level_10';
update_option('optionsEventsCalendar', $options);
$widget_options = get_option('widgetEventsCalendar');
if (!is_array($widget_options) || empty($widget_options))
$widget_options = array();
if (!isset($widget_options['title']))
$widget_options['title'] = __('Events Calendar', 'events-calendar');
if (!isset($widget_options['type']))
$widget_options['type'] = 'calendar';
if (!isset($widget_options['listCount']))
$widget_options['listCount'] = 5;
update_option('widgetEventsCalendar', $widget_options);
}
/**
* Adds a new event into database.
*
* @param int $id the event id
* @param string $title the event title
* @param string $location the event location
* @param string $linkout URL to an external web site
* @param string $description description of the event
* @param date $startDate date of the event. If empty, will be today.
* @param time $startTime start time of the event.
* @param date $endDate end date. if empty, will be same as start date.
* @param time $endTime end time
* @param int $accessLevel who has access to this event
* @param int $postId post id if use activated it
*/
function addEvent($title, $location, $linkout, $description, $startDate, $startTime, $endDate, $endTime, $accessLevel, $postID)
{
global $wpdb;
/*
$postID = is_null($postID) ? null : $postID;
$location = is_null($location) ? null : $location;
$description = is_null($description) ? null : $description;
$startDate = is_null($startDate) ? null : $startDate;
$endDate = is_null($endDate) ? null : $endDate;
$linkout = is_null($linkout) ? null : $linkout;
$startTime = is_null($startTime) ? null : $startTime;
$accessLevel = is_null($accessLevel) ? null : $accessLevel;
$endTime = is_null($endTime) ? null : $endTime;
*/
$wpdb->insert(
$this->mainTable,
array(
'eventTitle' => $title,
'eventDescription' => $description,
'eventLocation' => $location,
'eventLinkout' => $linkout,
'eventStartDate' => $startDate,
'eventStartTime' => $startTime,
'eventEndDate' => $endDate,
'eventEndTime' => $endTime,
'accessLevel' => $accessLevel,
'postID' => $postID
)
);
}
/**
* Updates an already existing event.
*
* @param int $id the event id
* @param string $title the event title
* @param string $location the event location
* @param string $linkout URL to an external web site
* @param string $description description of the event
* @param date $startDate date of the event. If empty, will be today.
* @param time $startTime start time of the event.
* @param date $endDate end date. if empty, will be same as start date.
* @param time $endTime end time
* @param int $accessLevel who can access this event
* @param int $postId post id if use activated it
*/
function editEvent($id, $title, $location, $linkout, $description, $startDate, $startTime, $endDate, $endTime, $accessLevel, $postID)
{
global $wpdb;
// just to make sure
if (empty ($id))
return;
/*
$postID = is_null($postID) ? null : $postID;
$location = is_null($location) ? null : $location;
$description = is_null($description) ? null : $description;
$startDate = is_null($startDate) ? null : $startDate;
$endDate = is_null($endDate) ? null : $endDate;
$linkout = is_null($linkout) ? null : $linkout;
$startTime = is_null($startTime) ? null : $startTime;
$accessLevel = is_null($accessLevel) ? null : $accessLevel;
$endTime = is_null($endTime) ? null : $endTime;
*/
$wpdb->update(
$this->mainTable,
array(
'eventTitle' => $title,
'eventDescription' => $description,
'eventLocation' => $location,
'eventLinkout' => $linkout,
'eventStartDate' => $startDate,
'eventStartTime' => $startTime,
'eventEndDate' => $endDate,
'eventEndTime' => $endTime,
'accessLevel' => $accessLevel,
'postID' => $postID
),
array(
'id' => $id
)
);
}
/**
* Deletes an event.
* @param int $id ID of the event to delete.
*/
function deleteEvent($id)
{
if (empty($id))
return;
$sql = "DELETE FROM `" . $this->mainTable . "` WHERE `id` = %d";
$this->db->query($this->db->prepare($sql, (int) $id));
}
/**
* Returns the events for a specified date.
*
* @param date $d
* @return array
*/
function getDaysEvents($d)
{
$sql = "SELECT *"
. " FROM `" . $this->mainTable . "`"
. " WHERE `eventStartDate` <= '" . $d . "'"
. " AND `eventEndDate` >= '" . $d . "'"
. " ORDER BY `eventStartTime`, `eventEndTime`;";
return $this->db->get_results($sql);
}
/**
* Returns a specific event.
*
* @param int $id
* @return array
*/
function getEvent($id)
{
$sql = "SELECT * FROM `" . $this->mainTable . "` WHERE `id` = " . $id . " LIMIT 1;";
return $this->db->get_results($sql);
}
/**
* Returns upcoming events.
* @param int $num number of events to retrieve
* @return array
*/
function getUpcomingEvents($num = 5)
{
$dt = date('Y-m-d');
$sql = "SELECT *"
. " FROM `$this->mainTable`"
. " WHERE `eventStartDate` >= '$dt'"
. " OR `eventEndDate` >= '$dt'"
. " ORDER BY eventStartDate, eventStartTime LIMIT $num";
return $this->db->get_results($sql);
}
}
endif;
?>