forked from habari-extras/eventscontent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventscontent.plugin.php
314 lines (286 loc) · 10.4 KB
/
eventscontent.plugin.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
<?php
class EventsContent extends Plugin
{
/**
* Add update beacon support
**/
public function action_update_check()
{
Update::add( $this->info->name, 'c330c3fe-3f34-47ff-b5c7-51b2269cfaed', $this->info->version );
}
/**
* Register content type
**/
public function action_plugin_activation( $plugin_file )
{
// add the content type.
Post::add_new_type( 'event' );
// Give anonymous users access
$group = UserGroup::get_by_name('anonymous');
$group->grant('post_event', 'read');
}
public function action_plugin_deactivation( $plugin_file )
{
Post::deactivate_post_type( 'event' );
}
/**
* Register templates
**/
public function action_init()
{
// Create templates
// $this->add_template('event.single', dirname(__FILE__) . '/event.single.php');
}
/**
* Create name string. This is where you make what it displays pretty.
**/
public function filter_post_type_display($type, $foruse)
{
$names = array(
'event' => array(
'singular' => _t('Event'),
'plural' => _t('Events'),
)
);
return isset($names[$type][$foruse]) ? $names[$type][$foruse] : $type;
}
/**
* Format the unix timestamp as readable date.
**/
public static function format_date_out($value)
{
if(isset($value) && !empty($value))
{
// Get the user's preferred format or, if none set, the system's default format
$user_format = User::identify()->info->locale_date_format;
if(!isset($user_format) || empty($user_format)) $user_format = Options::get('dateformat');
if(strstr($value, ";"))
{ // Output formatting for dates with start and end
list($start, $end) = explode(";", $value, 2);
$startformatter = HabariDateTime::date_create($start);
$endformatter = HabariDateTime::date_create($end);
(isset($user_format) && !empty($user_format)) ? $out = $startformatter->format($user_format) . " - " . $endformatter->format($user_format) : $out = $startformatter->format() . " - " . $endformatter->format();
}
else
{ // Output formatting for single dates
$formatter = HabariDateTime::date_create($value);
(isset($user_format) && !empty($user_format)) ? $out = $formatter->format($user_format) : $out = $formatter->format();
}
return $out;
}
else return $value;
}
/**
* Format the unix timestamp as readable time.
**/
public static function format_time_out($value)
{
if(isset($value) && !empty($value))
{
// Get the user's preferred format or, if none set, the system's default format
$user_format = User::identify()->info->locale_time_format;
if(!isset($user_format) || empty($user_format)) $user_format = Options::get('timeformat');
// Format the time
$hdteventtime = HabariDateTime::date_create($value);
if(isset($user_format) && !empty($user_format))
$out = $hdteventtime->format($user_format);
else
$out = $hdteventtime->format();
return $out;
}
else return $value;
}
/**
* Modify publish form. We're going to add the custom 'eventdate' field, as we like
* to hold our events at specific dates. We'll also add fields for the time, the location
* and a field called "eventtag" which will help us group multiple posts for the same event.
* We store the time separately to let the user decide in his theme what he wants to display.
* Also, if we stored both together, the time would be 00:00 for events with no specific time.
**/
public function action_form_publish($form, $post, $context)
{
// only edit the form if it's an event
if ($form->content_type->value == Post::type('event'))
{
// add text fields
$form->insert('tags', 'text', 'eventtag', 'null:null', _t('Event Tag (for event grouping)'), 'admincontrol_textArea');
$form->insert('tags', 'text', 'location', 'null:null', _t('Event Location'), 'admincontrol_textArea');
$form->insert('tags', 'text', 'eventdate', 'null:null', _t('Event Date'), 'admincontrol_textArea');
$form->insert('tags', 'text', 'eventtime', 'null:null', _t('Event Time'), 'admincontrol_textArea');
// load values and display the fields
$form->eventtag->value = $post->info->eventtag;
$form->eventtag->template = 'admincontrol_text';
$form->location->value = $post->info->location;
$form->location->template = 'admincontrol_text';
// use the same function for displaying the date we use for displaying the date in the theme
$form->eventdate->value = EventsContent::format_date_out($post->info->eventdate);
$form->eventdate->template = 'admincontrol_text';
// the same for the time
$form->eventtime->value = EventsContent::format_time_out($post->info->eventtime);
$form->eventtime->template = 'admincontrol_text';
}
}
/**
* Save our data to the database
**/
public function action_publish_post( $post, $form )
{
if ($post->content_type == Post::type('event'))
{
$post->info->eventtag = $form->eventtag->value;
$post->info->location = $form->location->value;
// Save date and time as unix timestamp so we can order by date.
// Multiday events are supported by the following function, but first we do some formatting preparation
// This actually reverts what format_date_out() did before
$eventdate = $form->eventdate->value;
if(!empty($eventdate))
{
$post->info->eventdate = EventsContent::eventdate_to_unix($eventdate);
}
else $post->info->eventdate = "";
// For the time, the reverting is simpler as multiple times are not yet supported
// If the field is empty, we remove the time from the database
// That's necessary because otherwise format_time_out would use the current time
$eventtime = $form->eventtime->value;
if(isset($eventtime) && !empty($eventtime))
{
$post->info->eventtime = HabariDateTime::date_create($eventtime)->int;
}
else
{
unset($post->info->eventtime);
}
}
}
/**
* Converts dates provided by the user to unix timestamps
* This function is necessary for multiple dates
**/
public static function eventdate_to_unix($eventdatestring)
{
$eventdatestring = str_replace("-", ";", $eventdatestring);
if(strpos($eventdatestring, ";") === false)
return HabariDateTime::date_create($eventdatestring)->int;
$dates = explode(";", $eventdatestring);
foreach($dates as $datestring)
{
try
{
$unixdates[] = HabariDateTime::date_create($datestring)->int;
} catch(Exception $crap) {return false;}
}
if(count($unixdates)) return implode(";", $unixdates);
else return false;
}
/**
* Add the posts to the blog home and it's pagination pages
* Thanks to lildude for the fix included in this function
*/
public function filter_template_user_filters( $filters )
{
// Cater for the home page which uses presets as of d918a831
if ( isset( $filters['preset'] ) ) {
$filters['preset'] = 'events';
} else {
// Cater for other pages like /page/1 which don't use presets yet
if ( isset( $filters['content_type'] ) ) {
$filters['content_type'] = Utils::single_array( $filters['content_type'] );
$filters['content_type'][] = Post::type( 'event' );
}
}
return $filters;
}
/**
* Modify output in the rss feed (include post info metadata)
**/
public function action_rss_add_post( $feed_entry, $post )
{
$info = $post->info->get_url_args();
foreach( $info as $key => $value ) {
if( is_array( $value ) && isset( $value['enclosure'] ) ) {
$enclosure = $feed_entry->addChild( 'enclosure' );
$enclosure->addAttribute( 'url', $value['enclosure'] );
$enclosure->addAttribute( 'length', $value['size'] );
$enclosure->addAttribute( 'type', 'text' );
}
}
}
/**
* Modify output in the atom feed (include post info metadata)
**/
public function action_atom_add_post( $feed_entry, $post )
{
// $info = $post->info->get_url_args();
// foreach( $info as $key => $value ) {
// if( is_array( $value ) && isset( $value['enclosure'] ) ) {
// $enclosure = $feed_entry->addChild( 'link' );
// $enclosure->addAttribute( 'rel', 'enclosure' );
// $enclosure->addAttribute( 'href', $value['enclosure'] );
// $enclosure->addAttribute( 'length', $value['size'] );
// $enclosure->addAttribute( 'type', 'text' );
// }
// }
if(Post::type("event")==$post->content_type)
$feed_entry->content[0] = "<strong>Event @ ".$post->info->location.", ".$post->info->eventdate_out.":</strong> ".$feed_entry->content[0];
}
/**
* Add events to the global posts atom feed
**/
public function filter_atom_get_collection_content_type( $content_type )
{
$content_type = Utils::single_array( $content_type );
$content_type[] = Post::type( 'event' );
return $content_type;
}
/**
* Make posts searchable by locations and eventtags on the admin manage page
**/
public function filter_posts_search_to_get ( $arguments, $flag, $value, $match, $search_string)
{
if($flag == 'location') {
$arguments['info'] = array('location'=>$value);
}
else if($flag == 'eventtag') {
$arguments['info'] = array('eventtag'=>$value);
}
return $arguments;
}
/**
* Add rewrite rules to handle locations and eventtags
**/
public function filter_rewrite_rules($rules)
{
$rules[] = RewriteRule::create_url_rule('"location"/location_name', 'PluginHandler', 'location');
$rules[] = RewriteRule::create_url_rule('"eventtag"/eventtag_value', 'PluginHandler', 'eventtag');
return $rules;
}
/**
* Implement the output for posts by location
**/
public function action_plugin_act_location($handler)
{
$handler->theme->act_display_entries(array('info' => array('location' => $handler->handler_vars['location_name'])));
}
/**
* Implement the output for posts by eventtag
**/
public function action_plugin_act_eventtag($handler)
{
$handler->theme->act_display_entries(array('info'=>array('eventtag'=>$handler->handler_vars['eventtag_value'])));
}
/**
* Convert the date to the user's preferred date format when it is requested by the theme with eventdate_out.
**/
public function filter_post_info_eventdate_out($value)
{
return EventsContent::format_date_out($value);
}
/**
* Convert the time to the user's preferred time format when it is requested by the theme with eventtime_out.
**/
public function filter_post_info_eventtime_out($value)
{
return EventsContent::format_time_out($value);
}
}
?>