4 * @brief The calendar module
5 * This calendar is for profile visitors and contains only the events
9 use Friendica\Content\Feature;
10 use Friendica\Core\Config;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Group;
17 require_once 'include/event.php';
18 require_once 'include/redir.php';
20 function cal_init(App $a) {
22 auto_redir($a, $a->argv[1]);
24 if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
28 nav_set_selected('events');
34 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
41 $a->data['user'] = $user[0];
42 $a->profile_uid = $user[0]['uid'];
44 // if it's a json request abort here becaus we don't
45 // need the widget data
46 if ($a->argv[2] === 'json')
49 $profile = get_profiledata_by_nick($nick, $a->profile_uid);
51 $account_type = Contact::getAccountType($profile);
53 $tpl = get_markup_template("vcard-widget.tpl");
55 $vcard_widget .= replace_macros($tpl, array(
56 '$name' => $profile['name'],
57 '$photo' => $profile['photo'],
58 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
59 '$account_type' => $account_type,
60 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
63 $cal_widget = widget_events();
65 if(! x($a->page,'aside'))
66 $a->page['aside'] = '';
68 $a->page['aside'] .= $vcard_widget;
69 $a->page['aside'] .= $cal_widget;
75 function cal_content(App $a) {
76 nav_set_selected('events');
78 // First day of the week (0 = Sunday)
79 $firstDay = PConfig::get(local_user(),'system','first_day_of_week', 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' => System::baseUrl(),
87 '$module_url' => '/cal/' . $a->data['user']['nickname'],
92 $etpl = get_markup_template('event_end.tpl');
93 $a->page['end'] .= replace_macros($etpl,array(
94 '$baseurl' => System::baseUrl(),
102 $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
105 if($a->argv[2] == 'export') {
107 $format = $a->argv[3];
112 // Setup permissions structures
116 $remote_contact = false;
119 $owner_uid = $a->data['user']['uid'];
120 $nick = $a->data['user']['nickname'];
122 if(is_array($_SESSION['remote'])) {
123 foreach($_SESSION['remote'] as $v) {
124 if($v['uid'] == $a->profile['profile_uid']) {
125 $contact_id = $v['cid'];
131 $groups = Group::getIdsByContactId($contact_id);
132 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
134 intval($a->profile['profile_uid'])
136 if (DBM::is_result($r)) {
138 $remote_contact = true;
141 if(! $remote_contact) {
143 $contact_id = $_SESSION['cid'];
144 $contact = $a->contact;
147 $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
149 if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
150 notice( t('Access to this profile has been restricted.') . EOL);
154 // get the permissions
155 $sql_perms = item_permissions_sql($owner_uid,$remote_contact,$groups);
156 // we only want to have the events of the profile owner
157 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
159 // get the tab navigation bar
160 $tabs .= profile_tabs($a,false, $a->data['user']['nickname']);
162 // The view mode part is similiar to /mod/events.php
163 if($mode == 'view') {
166 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
167 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
169 $y = intval($thisyear);
171 $m = intval($thismonth);
173 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
174 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
183 if($nextmonth > 12) {
196 $dim = get_dim($y,$m);
197 $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
198 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
201 if ($a->argv[2] === 'json'){
202 if (x($_GET,'start')) $start = $_GET['start'];
203 if (x($_GET,'end')) $finish = $_GET['end'];
206 $start = datetime_convert('UTC','UTC',$start);
207 $finish = datetime_convert('UTC','UTC',$finish);
209 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
210 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
212 // put the event parametes in an array so we can better transmit them
213 $event_params = array(
214 'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
217 'adjust_start' => $adjust_start,
218 'adjust_finish' => $adjust_finish,
219 'ignored' => $ignored,
222 // get events by id or by date
224 $r = event_by_id($owner_uid, $event_params, $sql_extra);
226 $r = events_by_date($owner_uid, $event_params, $sql_extra);
231 if (DBM::is_result($r)) {
232 $r = sort_by_date($r);
233 foreach ($r as $rr) {
234 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
235 if (! x($links,$j)) {
236 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
244 // transform the event in a usable array
245 if (DBM::is_result($r))
246 $r = sort_by_date($r);
247 $events = process_events($r);
249 if ($a->argv[2] === 'json'){
250 echo json_encode($events); killme();
253 // links: array('href', 'text', 'extra css classes', 'title')
255 $tpl = get_markup_template("event.tpl");
257 // if (Config::get('experimentals','new_calendar')==1){
258 $tpl = get_markup_template("events_js.tpl");
260 // $tpl = get_markup_template("events.tpl");
264 // Get rid of dashes in key names, Smarty3 can't handle them
265 foreach($events as $key => $event) {
266 $event_item = array();
267 foreach($event['item'] as $k => $v) {
268 $k = str_replace('-','_',$k);
269 $event_item[$k] = $v;
271 $events[$key]['item'] = $event_item;
274 $o = replace_macros($tpl, array(
275 '$baseurl' => System::baseUrl(),
277 '$title' => t('Events'),
278 '$view' => t('View'),
279 '$previous' => array(System::baseUrl()."/events/$prevyear/$prevmonth", t('Previous'),'',''),
280 '$next' => array(System::baseUrl()."/events/$nextyear/$nextmonth", t('Next'),'',''),
281 '$calendar' => cal($y,$m,$links, ' eventcal'),
283 '$events' => $events,
285 "today" => t("today"),
286 "month" => t("month"),
292 if (x($_GET,'id')){ echo $o; killme(); }
297 if($mode == 'export') {
298 if(! (intval($owner_uid))) {
299 notice( t('User not found'));
304 // Respect the export feature setting for all other /cal pages if it's not the own profile
305 if( ((local_user() !== intval($owner_uid))) && ! Feature::isEnabled($owner_uid, "export_calendar")) {
306 notice( t('Permission denied.') . EOL);
307 goaway('cal/' . $nick);
310 // Get the export data by uid
311 $evexport = event_export($owner_uid, $format);
313 if (!$evexport["success"]) {
314 if($evexport["content"])
315 notice( t('This calendar format is not supported') );
317 notice( t('No exportable data found'));
319 // If it the own calendar return to the events page
320 // otherwise to the profile calendar page
321 if (local_user() === intval($owner_uid))
322 $return_path = "events";
324 $returnpath = "cal/".$nick;
326 goaway($return_path);
329 // If nothing went wrong we can echo the export content
330 if ($evexport["success"]) {
331 header('Content-type: text/calendar');
332 header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"' );
333 echo $evexport["content"];