4 * @brief The calendar module
5 * This calendar is for profile visitors and contains only the events
9 require_once('include/event.php');
10 require_once('include/redir.php');
12 function cal_init(&$a) {
14 auto_redir($a, $a->argv[1]);
16 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
20 nav_set_selected('events');
26 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
33 $a->data['user'] = $user[0];
34 $a->profile_uid = $user[0]['uid'];
36 // if it's a json request abort here becaus we don't
37 // need the widget data
38 if ($a->argv[2] === 'json')
41 $profile = get_profiledata_by_nick($nick, $a->profile_uid);
43 if((intval($profile['page-flags']) == PAGE_COMMUNITY) || (intval($profile['page-flags']) == PAGE_PRVGROUP))
44 $account_type = t('Forum');
48 $tpl = get_markup_template("vcard-widget.tpl");
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'] : ""),
58 $cal_widget = widget_events();
60 if(! x($a->page,'aside'))
61 $a->page['aside'] = '';
63 $a->page['aside'] .= $vcard_widget;
64 $a->page['aside'] .= $cal_widget;
70 function cal_content(&$a) {
71 nav_set_selected('events');
74 if( feature_enabled(local_user(), 'richtext') )
75 $editselect = 'textareas';
77 // First day of the week (0 = Sunday)
78 $firstDay = get_pconfig(local_user(),'system','first_day_of_week');
79 if ($firstDay === false) $firstDay=0;
81 // get the translation strings for the callendar
82 $i18n = get_event_strings();
84 $htpl = get_markup_template('event_head.tpl');
85 $a->page['htmlhead'] .= replace_macros($htpl,array(
86 '$baseurl' => $a->get_baseurl(),
87 '$module_url' => '/cal/' . $a->data['user']['nickname'],
90 '$editselect' => $editselect
93 $etpl = get_markup_template('event_end.tpl');
94 $a->page['end'] .= replace_macros($etpl,array(
95 '$baseurl' => $a->get_baseurl(),
96 '$editselect' => $editselect
104 $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
107 if($a->argv[2] == 'export') {
109 $format = $a->argv[3];
114 // Setup permissions structures
118 $remote_contact = false;
121 $owner_uid = $a->data['user']['uid'];
122 $nick = $a->data['user']['nickname'];
124 if(is_array($_SESSION['remote'])) {
125 foreach($_SESSION['remote'] as $v) {
126 if($v['uid'] == $a->profile['profile_uid']) {
127 $contact_id = $v['cid'];
133 $groups = init_groups_visitor($contact_id);
134 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
136 intval($a->profile['profile_uid'])
140 $remote_contact = true;
143 if(! $remote_contact) {
145 $contact_id = $_SESSION['cid'];
146 $contact = $a->contact;
149 $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
151 if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
152 notice( t('Access to this profile has been restricted.') . EOL);
156 // get the permissions
157 $sql_perms = item_permissions_sql($owner_uid,$remote_contact,$groups);
158 // we only want to have the events of the profile owner
159 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
161 // get the tab navigation bar
162 $tabs .= profile_tabs($a,false, $a->data['user']['nickname']);
164 // The view mode part is similiar to /mod/events.php
165 if($mode == 'view') {
168 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
169 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
171 $y = intval($thisyear);
173 $m = intval($thismonth);
175 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
176 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
185 if($nextmonth > 12) {
198 $dim = get_dim($y,$m);
199 $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
200 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
203 if ($a->argv[2] === 'json'){
204 if (x($_GET,'start')) $start = date("Y-m-d h:i:s", $_GET['start']);
205 if (x($_GET,'end')) $finish = date("Y-m-d h:i:s", $_GET['end']);
208 $start = datetime_convert('UTC','UTC',$start);
209 $finish = datetime_convert('UTC','UTC',$finish);
211 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
212 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
214 // put the event parametes in an array so we can better transmit them
215 $event_params = array(
216 'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
219 'adjust_start' => $adjust_start,
220 'adjust_finish' => $adjust_finish,
221 'ignored' => $ignored,
224 // get events by id or by date
226 $r = event_by_id($owner_uid, $event_params, $sql_extra);
228 $r = events_by_date($owner_uid, $event_params, $sql_extra);
234 $r = sort_by_date($r);
236 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
238 $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
245 // transform the event in a usable array
247 $r = sort_by_date($r);
248 $events = process_events($r);
250 if ($a->argv[2] === 'json'){
251 echo json_encode($events); killme();
254 // links: array('href', 'text', 'extra css classes', 'title')
256 $tpl = get_markup_template("event.tpl");
258 // if (get_config('experimentals','new_calendar')==1){
259 $tpl = get_markup_template("events-js.tpl");
261 // $tpl = get_markup_template("events.tpl");
265 // Get rid of dashes in key names, Smarty3 can't handle them
266 foreach($events as $key => $event) {
267 $event_item = array();
268 foreach($event['item'] as $k => $v) {
269 $k = str_replace('-','_',$k);
270 $event_item[$k] = $v;
272 $events[$key]['item'] = $event_item;
275 $o = replace_macros($tpl, array(
276 '$baseurl' => $a->get_baseurl(),
278 '$title' => t('Events'),
279 '$view' => t('View'),
280 '$previus' => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
281 '$next' => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
282 '$calendar' => cal($y,$m,$links, ' eventcal'),
284 '$events' => $events,
286 "today" => t("today"),
287 "month" => t("month"),
294 if (x($_GET,'id')){ echo $o; killme(); }
299 if($mode == 'export') {
300 if(! (intval($owner_uid))) {
301 notice( t('User not found'));
306 if( ((local_user() !== $owner_uid)) && !(feature_enabled($owner_uid, "export_calendar"))) {
307 notice( t('Permission denied.') . EOL);
311 // Get the export data by uid
312 $evexport = event_export($owner_uid, $format);
314 if ($evexport["success"] == false ) {
315 if($evexport["content"])
316 notice( t('This calendar format is not supported') );
318 notice( t('No exportable data found'));
323 // If nothing went wrong we can echo the export content
324 if ($evexport["success"] == true ) {
325 header('Content-type: text/calendar');
326 header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"' );
327 echo $evexport["content"];