5 * @brief The calendar module
6 * This calendar is for profile visitors and contains only the events
10 use Friendica\Content\Feature;
11 use Friendica\Core\Config;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\System;
14 use Friendica\Database\DBM;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Group;
18 require_once 'include/event.php';
19 require_once 'include/redir.php';
21 function cal_init(App $a)
24 auto_redir($a, $a->argv[1]);
27 if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
31 nav_set_selected('events');
35 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
43 $a->data['user'] = $user[0];
44 $a->profile_uid = $user[0]['uid'];
46 // if it's a json request abort here becaus we don't
47 // need the widget data
48 if ($a->argv[2] === 'json') {
52 $profile = get_profiledata_by_nick($nick, $a->profile_uid);
54 $account_type = Contact::getAccountType($profile);
56 $tpl = get_markup_template("vcard-widget.tpl");
58 $vcard_widget = replace_macros($tpl, array(
59 '$name' => $profile['name'],
60 '$photo' => $profile['photo'],
61 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
62 '$account_type' => $account_type,
63 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
66 $cal_widget = widget_events();
68 if (!x($a->page, 'aside')) {
69 $a->page['aside'] = '';
72 $a->page['aside'] .= $vcard_widget;
73 $a->page['aside'] .= $cal_widget;
79 function cal_content(App $a)
81 nav_set_selected('events');
83 // get the translation strings for the callendar
84 $i18n = get_event_strings();
86 $htpl = get_markup_template('event_head.tpl');
87 $a->page['htmlhead'] .= replace_macros($htpl, array(
88 '$baseurl' => System::baseUrl(),
89 '$module_url' => '/cal/' . $a->data['user']['nickname'],
94 $etpl = get_markup_template('event_end.tpl');
95 $a->page['end'] .= replace_macros($etpl, array(
96 '$baseurl' => System::baseUrl(),
104 $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
107 if ($a->argc == 4 && $a->argv[2] == 'export') {
109 $format = $a->argv[3];
112 // Setup permissions structures
113 $remote_contact = false;
116 $owner_uid = $a->data['user']['uid'];
117 $nick = $a->data['user']['nickname'];
119 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
120 foreach ($_SESSION['remote'] as $v) {
121 if ($v['uid'] == $a->profile['profile_uid']) {
122 $contact_id = $v['cid'];
130 $groups = Group::getIdsByContactId($contact_id);
131 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
133 intval($a->profile['profile_uid'])
135 if (DBM::is_result($r)) {
136 $remote_contact = true;
140 $is_owner = local_user() == $a->profile['profile_uid'];
142 if ($a->profile['hidewall'] && (!$is_owner) && (!$remote_contact)) {
143 notice(t('Access to this profile has been restricted.') . EOL);
147 // get the permissions
148 $sql_perms = item_permissions_sql($owner_uid, $remote_contact, $groups);
149 // we only want to have the events of the profile owner
150 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
152 // get the tab navigation bar
153 $tabs = profile_tabs($a, false, $a->data['user']['nickname']);
155 // The view mode part is similiar to /mod/events.php
156 if ($mode == 'view') {
157 $thisyear = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
158 $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
160 $y = intval($thisyear);
164 $m = intval($thismonth);
167 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
168 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
180 if ($nextmonth > 12) {
193 $dim = get_dim($y, $m);
194 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
195 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
198 if ($a->argv[2] === 'json') {
199 if (x($_GET, 'start')) {
200 $start = $_GET['start'];
203 if (x($_GET, 'end')) {
204 $finish = $_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
225 if (x($_GET, 'id')) {
226 $r = event_by_id($owner_uid, $event_params, $sql_extra);
228 $r = events_by_date($owner_uid, $event_params, $sql_extra);
233 if (DBM::is_result($r)) {
234 $r = sort_by_date($r);
235 foreach ($r as $rr) {
236 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
237 if (!x($links, $j)) {
238 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
243 // transform the event in a usable array
244 $events = process_events($r);
246 if ($a->argv[2] === 'json') {
247 echo json_encode($events);
251 // links: array('href', 'text', 'extra css classes', 'title')
252 if (x($_GET, 'id')) {
253 $tpl = get_markup_template("event.tpl");
255 // if (Config::get('experimentals','new_calendar')==1){
256 $tpl = get_markup_template("events_js.tpl");
258 // $tpl = get_markup_template("events.tpl");
262 // Get rid of dashes in key names, Smarty3 can't handle them
263 foreach ($events as $key => $event) {
264 $event_item = array();
265 foreach ($event['item'] as $k => $v) {
266 $k = str_replace('-', '_', $k);
267 $event_item[$k] = $v;
269 $events[$key]['item'] = $event_item;
272 $o = replace_macros($tpl, array(
273 '$baseurl' => System::baseUrl(),
275 '$title' => t('Events'),
276 '$view' => t('View'),
277 '$previous' => array(System::baseUrl() . "/events/$prevyear/$prevmonth", t('Previous'), '', ''),
278 '$next' => array(System::baseUrl() . "/events/$nextyear/$nextmonth", t('Next'), '', ''),
279 '$calendar' => cal($y, $m, $links, ' eventcal'),
280 '$events' => $events,
281 "today" => t("today"),
282 "month" => t("month"),
288 if (x($_GET, 'id')) {
296 if ($mode == 'export') {
297 if (!(intval($owner_uid))) {
298 notice(t('User not found'));
303 // Respect the export feature setting for all other /cal pages if it's not the own profile
304 if (((local_user() !== intval($owner_uid))) && !Feature::isEnabled($owner_uid, "export_calendar")) {
305 notice(t('Permission denied.') . EOL);
306 goaway('cal/' . $nick);
309 // Get the export data by uid
310 $evexport = event_export($owner_uid, $format);
312 if (!$evexport["success"]) {
313 if ($evexport["content"]) {
314 notice(t('This calendar format is not supported'));
316 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 $return_path = "cal/" . $nick;
327 goaway($return_path);
330 // If nothing went wrong we can echo the export content
331 if ($evexport["success"]) {
332 header('Content-type: text/calendar');
333 header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
334 echo $evexport["content"];