]> git.mxchange.org Git - friendica.git/blob - include/event.php
Rearranged the contact menu so that it fits the menu at the conversations.
[friendica.git] / include / event.php
1 <?php
2
3 require_once('include/bbcode.php');
4 require_once('include/map.php');
5
6 function format_event_html($ev) {
7
8
9
10         if(! ((is_array($ev)) && count($ev)))
11                 return '';
12
13         $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
14
15         $o = '<div class="vevent">' . "\r\n";
16
17
18         $o .= '<p class="summary event-summary">' . bbcode($ev['summary']) .  '</p>' . "\r\n";
19
20         $o .= '<p class="description event-description">' . bbcode($ev['desc']) .  '</p>' . "\r\n";
21
22         $o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
23                 . datetime_convert('UTC','UTC',$ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
24                 . '" >' 
25                 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(), 
26                         $ev['start'] , $bd_format ))
27                         :  day_translate(datetime_convert('UTC', 'UTC', 
28                         $ev['start'] , $bd_format)))
29                 . '</abbr></p>' . "\r\n";
30
31         if(! $ev['nofinish'])
32                 $o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
33                         . datetime_convert('UTC','UTC',$ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
34                         . '" >' 
35                         . (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(), 
36                                 $ev['finish'] , $bd_format ))
37                                 :  day_translate(datetime_convert('UTC', 'UTC', 
38                                 $ev['finish'] , $bd_format )))
39                         . '</abbr></p>'  . "\r\n";
40
41         if(strlen($ev['location'])){
42                 $o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">' 
43                         . bbcode($ev['location']) 
44                         . '</span></p>' . "\r\n";
45                 
46                 if (strpos($ev['location'], "[map")===False) {
47                         $map = generate_named_map($ev['location']);
48                         if ($map!==$ev['location']) $o.=$map;
49                 }
50                 
51         }
52
53         $o .= '</div>' . "\r\n";
54         return $o;
55 }
56
57 /*
58 function parse_event($h) {
59
60         require_once('include/Scrape.php');
61         require_once('library/HTMLPurifier.auto.php');
62         require_once('include/html2bbcode');
63
64         $h = '<html><body>' . $h . '</body></html>';
65
66         $ret = array();
67
68
69         try {
70                 $dom = HTML5_Parser::parse($h);
71         } catch (DOMException $e) {
72                 logger('parse_event: parse error: ' . $e);
73         }
74
75         if(! $dom)
76                 return $ret;
77
78         $items = $dom->getElementsByTagName('*');
79
80         foreach($items as $item) {
81                 if(attribute_contains($item->getAttribute('class'), 'vevent')) {
82                         $level2 = $item->getElementsByTagName('*');
83                         foreach($level2 as $x) {
84                                 if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
85                                         $ret['start'] = $x->getAttribute('title');
86                                         if(! strpos($ret['start'],'Z'))
87                                                 $ret['adjust'] = true;
88                                 }
89                                 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
90                                         $ret['finish'] = $x->getAttribute('title');
91
92                                 if(attribute_contains($x->getAttribute('class'),'description'))
93                                         $ret['desc'] = $x->textContent;
94                                 if(attribute_contains($x->getAttribute('class'),'location'))
95                                         $ret['location'] = $x->textContent;
96                         }
97                 }
98         }
99
100         // sanitise
101
102         if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
103                 $config = HTMLPurifier_Config::createDefault();
104                 $config->set('Cache.DefinitionImpl', null);
105                 $purifier = new HTMLPurifier($config);
106                 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
107         }
108
109         if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
110                 $config = HTMLPurifier_Config::createDefault();
111                 $config->set('Cache.DefinitionImpl', null);
112                 $purifier = new HTMLPurifier($config);
113                 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
114         }
115
116         if(x($ret,'start'))
117                 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
118         if(x($ret,'finish'))
119                 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
120
121         return $ret;
122 }
123 */
124
125 function format_event_bbcode($ev) {
126
127         $o = '';
128
129         if($ev['summary'])
130                 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
131
132         if($ev['desc'])
133                 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
134
135         if($ev['start'])
136                 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
137
138         if(($ev['finish']) && (! $ev['nofinish']))
139                 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
140  
141         if($ev['location'])
142                 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
143
144         if($ev['adjust'])
145                 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
146
147
148         return $o;
149
150 }
151
152 function bbtovcal($s) {
153         $o = '';
154         $ev = bbtoevent($s);
155         if($ev['desc'])
156                 $o = format_event_html($ev);
157         return $o;
158 }
159
160
161 function bbtoevent($s) {
162
163         $ev = array();
164
165         $match = '';
166         if(preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is",$s,$match))
167                 $ev['summary'] = $match[1];
168         $match = '';
169         if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
170                 $ev['desc'] = $match[1];
171         $match = '';
172         if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
173                 $ev['start'] = $match[1];
174         $match = '';
175         if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
176                 $ev['finish'] = $match[1];
177         $match = '';
178         if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
179                 $ev['location'] = $match[1];
180         $match = '';
181         if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
182                 $ev['adjust'] = $match[1];
183         $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
184         return $ev;
185
186 }
187
188
189 function sort_by_date($a) {
190
191         usort($a,'ev_compare');
192         return $a;
193 }
194
195
196 function ev_compare($a,$b) {
197
198         $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
199         $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
200
201         if($date_a === $date_b)
202                 return strcasecmp($a['desc'],$b['desc']);
203         
204         return strcmp($date_a,$date_b);
205 }
206
207
208
209 function event_store($arr) {
210
211         require_once('include/datetime.php');
212         require_once('include/items.php');
213         require_once('include/bbcode.php');
214
215         $a = get_app();
216
217         $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
218         $arr['edited']  = (($arr['edited']) ? $arr['edited'] : datetime_convert());
219         $arr['type']    = (($arr['type']) ? $arr['type'] : 'event' );
220         $arr['cid']     = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
221         $arr['uri']     = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
222         $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0);
223
224         if($arr['cid'])
225                 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
226                         intval($arr['cid']),
227                         intval($arr['uid'])
228                 );
229         else
230                 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
231                         intval($arr['uid'])
232                 );
233
234         if(count($c))
235                 $contact = $c[0];
236
237
238         // Existing event being modified
239
240         if($arr['id']) {
241
242                 // has the event actually changed?
243
244                 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
245                         intval($arr['id']),
246                         intval($arr['uid'])
247                 );
248                 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
249
250                         // Nothing has changed. Grab the item id to return.
251
252                         $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
253                                 intval($arr['id']),
254                                 intval($arr['uid'])
255                         );
256                         return((count($r)) ? $r[0]['id'] : 0);
257                 }
258
259                 // The event changed. Update it.
260
261                 $r = q("UPDATE `event` SET
262                         `edited` = '%s',
263                         `start` = '%s',
264                         `finish` = '%s',
265                         `summary` = '%s',
266                         `desc` = '%s',
267                         `location` = '%s',
268                         `type` = '%s',
269                         `adjust` = %d,
270                         `nofinish` = %d,
271                         `allow_cid` = '%s',
272                         `allow_gid` = '%s',
273                         `deny_cid` = '%s',
274                         `deny_gid` = '%s'
275                         WHERE `id` = %d AND `uid` = %d",
276
277                         dbesc($arr['edited']),
278                         dbesc($arr['start']),
279                         dbesc($arr['finish']),
280                         dbesc($arr['summary']),
281                         dbesc($arr['desc']),
282                         dbesc($arr['location']),
283                         dbesc($arr['type']),
284                         intval($arr['adjust']),
285                         intval($arr['nofinish']),
286                         dbesc($arr['allow_cid']),
287                         dbesc($arr['allow_gid']),
288                         dbesc($arr['deny_cid']),
289                         dbesc($arr['deny_gid']),
290                         intval($arr['id']),
291                         intval($arr['uid'])
292                 );
293                 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
294                         intval($arr['id']),
295                         intval($arr['uid'])
296                 );
297                 if(count($r)) {
298                         $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
299                         $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
300                         $object .= '</object>' . "\n";
301
302
303                         q("UPDATE `item` SET `body` = '%s', `object` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `edited` = '%s', `private` = %d WHERE `id` = %d AND `uid` = %d",
304                                 dbesc(format_event_bbcode($arr)),
305                                 dbesc($object),
306                                 dbesc($arr['allow_cid']),
307                                 dbesc($arr['allow_gid']),
308                                 dbesc($arr['deny_cid']),
309                                 dbesc($arr['deny_gid']),
310                                 dbesc($arr['edited']),
311                                 intval($arr['private']),
312                                 intval($r[0]['id']),
313                                 intval($arr['uid'])
314                         );
315
316                         $item_id = $r[0]['id'];
317                 }
318                 else
319                         $item_id = 0;
320
321                 call_hooks("event_updated", $arr['id']);
322
323                 return $item_id;
324         }
325         else {
326
327                 // New event. Store it. 
328
329                 $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
330                         `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
331                         VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
332                         intval($arr['uid']),
333                         intval($arr['cid']),
334                         dbesc($arr['uri']),
335                         dbesc($arr['created']),
336                         dbesc($arr['edited']),
337                         dbesc($arr['start']),
338                         dbesc($arr['finish']),
339                         dbesc($arr['summary']),
340                         dbesc($arr['desc']),
341                         dbesc($arr['location']),
342                         dbesc($arr['type']),
343                         intval($arr['adjust']),
344                         intval($arr['nofinish']),
345                         dbesc($arr['allow_cid']),
346                         dbesc($arr['allow_gid']),
347                         dbesc($arr['deny_cid']),
348                         dbesc($arr['deny_gid'])
349
350                 );
351
352                 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
353                         dbesc($arr['uri']),
354                         intval($arr['uid'])
355                 );
356                 if(count($r))
357                         $event = $r[0];
358
359                 $item_arr = array();
360
361                 $item_arr['uid']           = $arr['uid'];
362                 $item_arr['contact-id']    = $arr['cid'];
363                 $item_arr['uri']           = $arr['uri'];
364                 $item_arr['parent-uri']    = $arr['uri'];
365                 $item_arr['type']          = 'activity';
366                 $item_arr['wall']          = (($arr['cid']) ? 0 : 1);
367                 $item_arr['contact-id']    = $contact['id'];
368                 $item_arr['owner-name']    = $contact['name'];
369                 $item_arr['owner-link']    = $contact['url'];
370                 $item_arr['owner-avatar']  = $contact['thumb'];
371                 $item_arr['author-name']   = $contact['name'];
372                 $item_arr['author-link']   = $contact['url'];
373                 $item_arr['author-avatar'] = $contact['thumb'];
374                 $item_arr['title']         = '';
375                 $item_arr['allow_cid']     = $arr['allow_cid'];
376                 $item_arr['allow_gid']     = $arr['allow_gid'];
377                 $item_arr['deny_cid']      = $arr['deny_cid'];
378                 $item_arr['deny_gid']      = $arr['deny_gid'];
379                 $item_arr['private']       = $arr['private'];
380                 $item_arr['last-child']    = 1;
381                 $item_arr['visible']       = 1;
382                 $item_arr['verb']          = ACTIVITY_POST;
383                 $item_arr['object-type']   = ACTIVITY_OBJ_EVENT;
384                 $item_arr['origin']        = ((intval($arr['cid']) == 0) ? 1 : 0);
385                 $item_arr['body']          = format_event_bbcode($event);
386
387
388                 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
389                 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
390                 $item_arr['object'] .= '</object>' . "\n";
391
392                 $item_id = item_store($item_arr);
393
394                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
395                         intval($arr['uid'])
396                 );
397                 //if(count($r))
398                 //      $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
399
400
401                 if($item_id) {
402                         //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d  WHERE `uid` = %d AND `id` = %d",
403                         //      dbesc($plink),
404                         //      intval($event['id']),
405                         //      intval($arr['uid']),
406                         //      intval($item_id)
407                         //);
408                         q("UPDATE `item` SET `event-id` = %d  WHERE `uid` = %d AND `id` = %d",
409                                 intval($event['id']),
410                                 intval($arr['uid']),
411                                 intval($item_id)
412                         );
413                 }
414
415                 call_hooks("event_created", $event['id']);
416
417                 return $item_id;
418         }
419 }