-
Notifications
You must be signed in to change notification settings - Fork 0
/
emfu-functions.php
232 lines (201 loc) · 6.69 KB
/
emfu-functions.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
<?php
// TODO: make these field numbers flexible, depending the received columns
define('emfu_field_nr_event_start_date', 1);
define('emfu_field_nr_event_start_time', 2);
define('emfu_field_nr_event_end_date', 3);
define('emfu_field_nr_event_end_time', 4);
define('emfu_field_nr_event_name', 5);
define('emfu_field_nr_post_excerpt', 6);
define('emfu_field_nr_post_content', 7);
define('emfu_field_nr_location_slug', 8);
define('emfu_field_nr_category_slug', 9);
/**
* Create a EM_Event object from the input row with fields
* @param array $event_row
*
* @return EM_Event
* @throws Exception
*/
function emfu_create_EM_Event_from_row(array $event_row ){
$event = new EM_Event();
/*
* hard coded values
*/
$event->post_type = 'event';
$event->post_status = 'publish';
$event->comment_status = 'open';
$event->ping_status = 'closed';
$event->event_private = 0;
$event->event_status = 1;
/*
* Name, title are mandatory
*/
$event->event_name = emfu_not_empty($event_row[ emfu_field_nr_event_name ], "event_name");
$event->post_title = emfu_not_empty($event_row[ emfu_field_nr_event_name ], "event_name");
/*
* excerpt and content are optional
*/
$event->post_excerpt = $event_row[ emfu_field_nr_post_excerpt ];
$event->post_content = $event_row[ emfu_field_nr_post_content ];
/*
* start date and time
*/
$event_start_date = emfu_get_valid_date_string($event_row[ emfu_field_nr_event_start_date ], "start_date");
$event_start_time = emfu_get_valid_time_string($event_row[ emfu_field_nr_event_start_time ], "start_time");
$event->event_start_date = $event_start_date;
$event->event_start_time = $event_start_time;
$event->event_start = $event_start_date . ' ' . $event_start_time;
/*
* end data and time
*/
$event_end_date = $event_row[ emfu_field_nr_event_end_date ];
$event_end_time = $event_row[ emfu_field_nr_event_end_time ];
// if end date is empty, the start date is taken
if (empty($event_end_date) )
$event_end_date = $event_start_date;
// if end time is empty, the start time is taken
if (empty($event_end_time))
$event_end_time = $event_start_time;
$event->event_end_date = emfu_get_valid_date_string($event_end_date, "end_date");
$event->event_end_time = emfu_get_valid_time_string($event_end_time, "end_time");
/*
* Location
*/
$location_id = emfu_get_location_id( $event_row[ emfu_field_nr_location_slug ] );
$event->location_id = $location_id;
/*
* Set the Category according to the category slug
*/
$category_id = emfu_get_category_id( $event_row[ emfu_field_nr_category_slug ] );
$category = array( $category_id );
$event->categories = new EM_Categories( $category );
return $event;
}
/**
* Check on being not empty.
* When empty, an Exception is thrown with error message.
* When not empty, the value is returned.
* @param string $value
* @param string $name
*
* @return string $value when not empty
* @throws Exception
*/
function emfu_not_empty(string $value, string $name): string {
if (empty($value))
throw new Exception("{$name} has no value" );
return $value;
}
/**
* Check if a event already exists with event_status = 1 and the same:
* - location
* - name
* - start date
* - start time
* *
* @param EM_Event $event
*
* @return bool
* @throws Exception
*/
function emfu_event_exists(EM_Event $event): bool
{
global $wpdb;
if (empty($event))
throw new Exception("Event is empty.", "function event_exists()");
$location_id = $event->location_id;
$event_name = $event->event_name;
$event_start_date = $event->event_start_date;
$event_start_time = $event->event_start_time;
$query_string = $wpdb->prepare( "SELECT count(*) FROM " . EM_EVENTS_TABLE .
" where event_status = 1 " .
"and location_id = %d " .
"and event_name = %s " .
"and event_start_date = %s " .
"and event_start_time = %s",
array($location_id, $event_name, $event_start_date, $event_start_time)
);
$count = (int) $wpdb->get_var($query_string);
return ($count > 0);
}
/**
* Get the id of the Category with category_slug = $category_slug
* @param string $category_slug
* @return int term_id, being the category id
* @throws Exception when $category_slug is empty or no category can be found with that slug
*/
function emfu_get_category_id(string $category_slug): int
{
if (empty($category_slug))
throw new Exception("Category-slug is empty.");
$term = get_term_by( 'slug', $category_slug, 'event-categories' );
if (empty($term))
throw new Exception("Unknown category-slug '" . $category_slug . "'.");
return $term->term_id;
}
/**
* Get the id of the EM_location with location_slug = $location_slug
* @param string $location_slug
* @return int location_id
* @throws Exception when no location can be found or $location_slug was empty
*/
function emfu_get_location_id(string $location_slug): int
{
global $wpdb;
if (empty($location_slug))
throw new Exception("Location-slug is empty.");
$query_string = $wpdb->prepare( "SELECT location_id " .
"FROM " . EM_LOCATIONS_TABLE .
" WHERE location_slug = %s",
array( $location_slug));
$location_id = $wpdb->get_var($query_string);
if (is_null($location_id))
throw new Exception("Unknown location-slug '" . $location_slug . "'.");
return $location_id;
}
/**
* Checks if $date contains a valid date string YYYY-MM-DD.
* When not valid, an exception is thrown.
* When valid, $date is returned.
* @param string $date
* @param string $name
*
* @return string
* @throws Exception
*/
function emfu_get_valid_date_string(string $date, string $name): string
{
// first check on non empty, for a nice error message
emfu_not_empty($date, $name);
// then check the date
$d = DateTime::createFromFormat("Y-m-d", $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits
// so changing the comparison from == to === fixes the issue.
if ( $d && $d->format("Y-m-d") === $date)
return $date;
else
throw new Exception($name . " is not valid: " . $date);
}
/**
* Checks if $time contains a valid timestring HH:mm:ss.
* When not valid, an exception is thrown.
* When valid, $time is returned.
* @param string $time
* @param string $name
*
* @return string
* @throws Exception
*/
function emfu_get_valid_time_string(string $time, string $name): string
{
// first check on non empty, for a nice error message
emfu_not_empty($time, $name);
if (strlen($time) != 8)
throw new Exception($name . " is not valid time (hh:mm:ss): " . $time);
// then check the date
$t = strtotime($time);
if ( $t )
return $time;
else
throw new Exception($name . " is not valid: " . $time);
}