]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
Merge pull request #3789 from Alkarex/french_regions
[friendica.git] / mod / cal.php
1 <?php
2 /**
3  * @file mod/cal.php
4  * @brief The calendar module
5  *      This calendar is for profile visitors and contains only the events
6  *      of the profile owner
7  */
8
9 use Friendica\App;
10 use Friendica\Core\System;
11
12 require_once('include/event.php');
13 require_once('include/redir.php');
14
15 function cal_init(App $a) {
16         if($a->argc > 1)
17                 auto_redir($a, $a->argv[1]);
18
19         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
20                 return;
21         }
22
23         nav_set_selected('events');
24
25         $o = '';
26
27         if($a->argc > 1) {
28                 $nick = $a->argv[1];
29                 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
30                         dbesc($nick)
31                 );
32
33                 if(! count($user))
34                         return;
35
36                 $a->data['user'] = $user[0];
37                 $a->profile_uid = $user[0]['uid'];
38
39                 // if it's a json request abort here becaus we don't
40                 // need the widget data
41                 if ($a->argv[2] === 'json')
42                         return;
43
44                 $profile = get_profiledata_by_nick($nick, $a->profile_uid);
45
46                 $account_type = account_type($profile);
47
48                 $tpl = get_markup_template("vcard-widget.tpl");
49
50                 $vcard_widget .= replace_macros($tpl, array(
51                         '$name' => $profile['name'],
52                         '$photo' => $profile['photo'],
53                         '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
54                         '$account_type' => $account_type,
55                         '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
56                 ));
57
58                 $cal_widget = widget_events();
59
60                 if(! x($a->page,'aside'))
61                         $a->page['aside'] = '';
62
63                 $a->page['aside'] .= $vcard_widget;
64                 $a->page['aside'] .= $cal_widget;
65         }
66
67         return;
68 }
69
70 function cal_content(App $a) {
71         nav_set_selected('events');
72
73         // First day of the week (0 = Sunday)
74         $firstDay = get_pconfig(local_user(),'system','first_day_of_week');
75         if ($firstDay === false) $firstDay=0;
76
77         // get the translation strings for the callendar
78         $i18n = get_event_strings();
79
80         $htpl = get_markup_template('event_head.tpl');
81         $a->page['htmlhead'] .= replace_macros($htpl,array(
82                 '$baseurl' => System::baseUrl(),
83                 '$module_url' => '/cal/' . $a->data['user']['nickname'],
84                 '$modparams' => 2,
85                 '$i18n' => $i18n,
86         ));
87
88         $etpl = get_markup_template('event_end.tpl');
89         $a->page['end'] .= replace_macros($etpl,array(
90                 '$baseurl' => System::baseUrl(),
91         ));
92
93         $o ="";
94
95         $mode = 'view';
96         $y = 0;
97         $m = 0;
98         $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
99
100         if($a->argc == 4) {
101                 if($a->argv[2] == 'export') {
102                         $mode = 'export';
103                         $format = $a->argv[3];
104                 }
105         }
106
107         //
108         // Setup permissions structures
109         //
110
111         $contact = null;
112         $remote_contact = false;
113         $contact_id = 0;
114
115         $owner_uid = $a->data['user']['uid'];
116         $nick = $a->data['user']['nickname'];
117
118         if(is_array($_SESSION['remote'])) {
119                 foreach($_SESSION['remote'] as $v) {
120                         if($v['uid'] == $a->profile['profile_uid']) {
121                                 $contact_id = $v['cid'];
122                                 break;
123                         }
124                 }
125         }
126         if($contact_id) {
127                 $groups = init_groups_visitor($contact_id);
128                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
129                         intval($contact_id),
130                         intval($a->profile['profile_uid'])
131                 );
132                 if (dbm::is_result($r)) {
133                         $contact = $r[0];
134                         $remote_contact = true;
135                 }
136         }
137         if(! $remote_contact) {
138                 if(local_user()) {
139                         $contact_id = $_SESSION['cid'];
140                         $contact = $a->contact;
141                 }
142         }
143         $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
144
145         if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
146                 notice( t('Access to this profile has been restricted.') . EOL);
147                 return;
148         }
149
150         // get the permissions
151         $sql_perms = item_permissions_sql($owner_uid,$remote_contact,$groups);
152         // we only want to have the events of the profile owner
153         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
154
155         // get the tab navigation bar
156         $tabs .= profile_tabs($a,false, $a->data['user']['nickname']);
157
158         // The view mode part is similiar to /mod/events.php
159         if($mode == 'view') {
160
161
162                 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
163                 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
164                 if(! $y)
165                         $y = intval($thisyear);
166                 if(! $m)
167                         $m = intval($thismonth);
168
169                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
170                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
171
172                 if($y < 1901)
173                         $y = 1900;
174                 if($y > 2099)
175                         $y = 2100;
176
177                 $nextyear = $y;
178                 $nextmonth = $m + 1;
179                 if($nextmonth > 12) {
180                                 $nextmonth = 1;
181                         $nextyear ++;
182                 }
183
184                 $prevyear = $y;
185                 if($m > 1)
186                         $prevmonth = $m - 1;
187                 else {
188                         $prevmonth = 12;
189                         $prevyear --;
190                 }
191
192                 $dim    = get_dim($y,$m);
193                 $start  = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
194                 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
195
196
197                 if ($a->argv[2] === 'json'){
198                         if (x($_GET,'start'))   $start = $_GET['start'];
199                         if (x($_GET,'end'))     $finish = $_GET['end'];
200                 }
201
202                 $start  = datetime_convert('UTC','UTC',$start);
203                 $finish = datetime_convert('UTC','UTC',$finish);
204
205                 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
206                 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
207
208                 // put the event parametes in an array so we can better transmit them
209                 $event_params = array(
210                         'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
211                         'start' => $start,
212                         'finish' => $finish,
213                         'adjust_start' => $adjust_start,
214                         'adjust_finish' => $adjust_finish,
215                         'ignored' => $ignored,
216                 );
217
218                 // get events by id or by date
219                 if (x($_GET,'id')){
220                         $r = event_by_id($owner_uid, $event_params, $sql_extra);
221                 } else {
222                         $r = events_by_date($owner_uid, $event_params, $sql_extra);
223                 }
224
225                 $links = array();
226
227                 if (dbm::is_result($r)) {
228                         $r = sort_by_date($r);
229                         foreach ($r as $rr) {
230                                 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
231                                 if (! x($links,$j)) {
232                                         $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
233                                 }
234                         }
235                 }
236
237
238                 $events=array();
239
240                 // transform the event in a usable array
241                 if (dbm::is_result($r))
242                         $r = sort_by_date($r);
243                         $events = process_events($r);
244
245                 if ($a->argv[2] === 'json'){
246                         echo json_encode($events); killme();
247                 }
248
249                 // links: array('href', 'text', 'extra css classes', 'title')
250                 if (x($_GET,'id')){
251                         $tpl =  get_markup_template("event.tpl");
252                 } else {
253 //                      if (get_config('experimentals','new_calendar')==1){
254                                 $tpl = get_markup_template("events_js.tpl");
255 //                      } else {
256 //                              $tpl = get_markup_template("events.tpl");
257 //                      }
258                 }
259
260                 // Get rid of dashes in key names, Smarty3 can't handle them
261                 foreach($events as $key => $event) {
262                         $event_item = array();
263                         foreach($event['item'] as $k => $v) {
264                                 $k = str_replace('-','_',$k);
265                                 $event_item[$k] = $v;
266                         }
267                         $events[$key]['item'] = $event_item;
268                 }
269
270                 $o = replace_macros($tpl, array(
271                         '$baseurl'      => System::baseUrl(),
272                         '$tabs'         => $tabs,
273                         '$title'        => t('Events'),
274                         '$view'         => t('View'),
275                         '$previous'     => array(System::baseUrl()."/events/$prevyear/$prevmonth", t('Previous'),'',''),
276                         '$next'         => array(System::baseUrl()."/events/$nextyear/$nextmonth", t('Next'),'',''),
277                         '$calendar' => cal($y,$m,$links, ' eventcal'),
278
279                         '$events'       => $events,
280
281                         "today" => t("today"),
282                         "month" => t("month"),
283                         "week" => t("week"),
284                         "day" => t("day"),
285                         "list" => t("list"),
286                 ));
287
288                 if (x($_GET,'id')){ echo $o; killme(); }
289
290                 return $o;
291         }
292
293         if($mode == 'export') {
294                 if(! (intval($owner_uid))) {
295                         notice( t('User not found'));
296                         return;
297                 }
298
299                 // Test permissions
300                 // Respect the export feature setting for all other /cal pages if it's not the own profile
301                 if( ((local_user() !== intval($owner_uid))) && ! feature_enabled($owner_uid, "export_calendar")) {
302                         notice( t('Permission denied.') . EOL);
303                         goaway('cal/' . $nick);
304                 }
305
306                 // Get the export data by uid
307                 $evexport = event_export($owner_uid, $format);
308
309                 if (!$evexport["success"]) {
310                         if($evexport["content"])
311                                 notice( t('This calendar format is not supported') );
312                         else
313                                 notice( t('No exportable data found'));
314
315                         // If it the own calendar return to the events page
316                         // otherwise to the profile calendar page
317                         if (local_user() === intval($owner_uid))
318                                 $return_path = "events";
319                         else
320                                 $returnpath = "cal/".$nick;
321
322                         goaway($return_path);
323                 }
324
325                 // If nothing went wrong we can echo the export content
326                 if ($evexport["success"]) {
327                         header('Content-type: text/calendar');
328                         header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"' );
329                         echo $evexport["content"];
330                         killme();
331                 }
332
333                 return;
334         }
335 }