]> git.mxchange.org Git - friendica-addons.git/blob - cal/cal.php
cal: user settings info about supported formats
[friendica-addons.git] / cal / cal.php
1 <?php
2 /********************************************************************
3  * Name: Calendar Export
4  * Description: This addon exports the public events of your users as calendar files
5  * Version: 0.1
6  * Author: Tobias Diekershoff <https://f.diekershoff.de/profile/tobias>
7  * License: MIT
8  * ******************************************************************/
9
10
11 function cal_install()
12 {
13     register_hook('plugin_settings', 'addon/cal/cal.php', 'cal_addon_settings');
14     register_hook('plugin_settings_post', 'addon/cal/cal.php', 'cal_addon_settings_post');
15 }
16 function cal_uninstall()
17 {
18     unregister_hook('plugin_settings', 'addon/cal/cal.php', 'cal_addon_settings');
19     unregister_hook('plugin_settings_post', 'addon/cal/cal.php', 'cal_addon_settings_post');
20 }
21 function cal_module()
22 {
23 }
24 /*  pathes
25  *  /cal/$user/export/$
26  *  currently supported format is ical (iCalendar
27  */
28 function cal_content()
29 {
30     $a = get_app();
31     $o = "";
32     if ($a->argc == 1) {
33     $o .= "<h3>".t('Event Export')."</h3><p>".t('You can download public events from: ').$a->get_baseurl()."/cal/username/export/ical</p>";
34     } elseif ($a->argc==4) {
35         //  get the parameters from the request we just received
36         $username = $a->argv[1];
37         $do = $a->argv[2];
38         $format = $a->argv[3];
39         //  check that there is a user matching the requested profile
40         $r = q("SELECT uid FROM user WHERE nickname='".$username."' LIMIT 1;");
41         if (count($r)) 
42         {
43             $uid = $r[0]['uid'];
44         } else {
45             killme();
46         }
47         //  if we shall do anything other then export, end here
48         if (! $do == 'export' )
49             killme();
50         //  check if the user allows us to share the profile
51         $enable = get_pconfig( $uid, 'cal', 'enable');
52         if (! $enable == 1) {
53             info(t('The user does not export the calendar.'));
54             return;
55         }
56         //  we are allowed to show events
57         //  get the timezone the user is in
58         $r = q("SELECT timezone FROM user WHERE uid=".$uid." LIMIT 1;");
59         if (count($r))
60             $timezone = $r[0]['timezone'];
61         //  does the user who requests happen to be the owner of the events 
62         //  requested? then show all of your events, otherwise only those that 
63         //  don't have limitations set in allow_cid and allow_gid
64         if (local_user() == $uid) {
65             $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location` FROM `event` WHERE `uid`=".$uid.";");
66         } else {
67             $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location` FROM `event` WHERE `allow_cid`='' and `allow_gid`='' and `uid`='".$uid."';");
68         }
69         //  we have the events that are available for the requestor
70         //  now format the output according to the requested format
71         $res = cal_format_output($r, $format, $timezone);
72         if (! $res=='')
73             info($res);
74     } else {
75         //  wrong number of parameters
76         killme();
77     }
78     return $o;
79 }
80
81 function cal_format_output ($r, $f, $tz)
82 {
83     $res = t('This calendar format is not supported');;
84     switch ($f)
85     {
86         //  format the exported data as a CSV file
87         case "csv":
88             header("Content-type: text/csv");
89             $o = '"Subject", "Start Date", "Start Time", "Description", "End Date", "End Time", "Location"' . PHP_EOL;
90             foreach ($r as $rr) {
91                 $tmp1 = strtotime($rr['start']);
92                 $tmp2 = strtotime($rr['finish']);
93                 $time_format = "%H:%M:%S";
94                 $date_format = "%d.%m.%Y";
95                 $o .= '"'.$rr['summary'].'", "'.strftime($date_format, $tmp1) .
96                     '", "'.strftime($time_format, $tmp1).'", "'.$rr['desc'] .
97                     '", "'.strftime($date_format, $tmp2) .
98                     '", "'.strftime($time_format, $tmp2) . 
99                     '", "'.$rr['location'].'"' . PHP_EOL;
100             }
101             echo $o;
102             killme();
103
104         case "ical":
105             header("Content-type: text/ics");
106             $res = '';
107             $o = 'BEGIN:VCALENDAR'. PHP_EOL
108                 . 'PRODID:-//friendica calendar export//0.1//EN' . PHP_EOL
109                 . 'VERSION:2.0' . PHP_EOL;
110 //  TODO include timezone informations in cases were the time is not in UTC
111 //              . 'BEGIN:VTIMEZONE' . PHP_EOL
112 //              . 'TZID:' . $tz . PHP_EOL
113 //              . 'END:VTIMEZONE' . PHP_EOL;
114             foreach ($r as $rr) {
115                 if ($rr['adjust'] == 1) {
116                     $UTC = 'Z';
117                 } else { 
118                    $UTC = '';
119                 }
120                 $o .= 'BEGIN:VEVENT' . PHP_EOL;
121                 if ($rr[start]) {
122                     $tmp = strtotime($rr['start']);
123                     $dtformat = "%Y%m%dT%H%M%S".$UTC;
124                     $o .= 'DTSTART:'.strftime($dtformat, $tmp).PHP_EOL;
125                 }
126                 if ($rr['finish']) {
127                     $tmp = strtotime($rr['finish']);
128                     $dtformat = "%Y%m%dT%H%M%S".$UTC;
129                     $o .= 'DTEND:'.strftime($dtformat, $tmp).PHP_EOL;
130                 }
131                 if ($rr['summary'])
132                     $tmp = $rr['summary'];
133                     $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
134                     $o .= 'SUMMARY:' . $tmp . PHP_EOL;
135                 if ($rr['desc'])
136                     $tmp = $rr['desc'];
137                     $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
138                     $o .= 'DESCRIPTION:' . $tmp . PHP_EOL;
139                 if ($rr['location']) {
140                     $tmp = $rr['location'];
141                     $tmp = str_replace(PHP_EOL, PHP_EOL.' ',$tmp);
142                     $o .= 'LOCATION:' . $tmp . PHP_EOL;
143                 }
144                 $o .= 'END:VEVENT' . PHP_EOL;
145             }
146             $o .= 'END:VCALENDAR' . PHP_EOL;
147             echo $o;
148             killme();
149     }
150     return $res;
151 }
152
153 function cal_addon_settings_post ( &$a, &$b  )
154 {
155     if (! local_user())
156         return;
157
158     if (!x($_POST,'cal-submit'))
159         return;
160
161     set_pconfig(local_user(),'cal','enable',intval($_POST['cal-enable']));
162 }
163 function cal_addon_settings ( &$a, &$s  )
164 {
165     if (! local_user())
166         return;
167
168     $enabled = get_pconfig(local_user(), 'cal', 'enable');
169     $checked = (($enabled) ? ' checked="checked" ' : '');
170     $url = $a->get_baseurl().'/cal/'.$a->user['nickname'].'/export/<em>format</em>';
171
172     $s .= '<h3>'.t('Export Events').'</h3>';
173     $s .= '<p>'.t('If this is enabled, you public events will be available at').' <strong>'.$url.'</strong></p>';
174     $s .= '<p>'.t('Currently supported formats are ical and csv.').'</p>';
175     $s .= '<div id="cal-enable-wrapper">';
176     $s .= '<label id="cal-enable-label" for="cal-checkbox">'. t('Enable calendar export') .'</label>';
177     $s .= '<input id="cal-checkbox" type="checkbox" name="cal-enable" value="1" ' . $checked . '/>';
178     $s .= '</div><div class="clear"></div>';
179     $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="cal-submit" class="settings-submit" value="' . t('Submit') . '" /></div>'; 
180     $s .= '</div><div class="clear"></div>';
181
182 }
183
184 ?>