aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Cal.php
blob: 9049fe7d0cdf14d65c054742499f367d13dabf15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace Zotlabs\Module;


use App;
use Zotlabs\Web\Controller;

require_once('include/conversation.php');
require_once('include/bbcode.php');
require_once('include/datetime.php');
require_once('include/event.php');
require_once('include/items.php');
require_once('include/html2plain.php');


class Cal extends Controller {

	function init() {
		if(observer_prohibited()) {
			return;
		}

		if(argc() > 1) {
			$nick = argv(1);

			profile_load($nick);

			$channelx = channelx_by_nick($nick);

			if(! $channelx) {
				notice( t('Channel not found.') . EOL);
				return;
			}

			App::$data['channel'] = $channelx;

			$observer = App::get_observer();
			App::$data['observer'] = $observer;

			head_set_icon(App::$data['channel']['xchan_photo_s']);

			App::$page['htmlhead'] = "<script> var profile_uid = " . ((App::$data['channel']) ? App::$data['channel']['channel_id'] : 0) . "; </script>" ;

		}

		return;
	}



	function get() {

		if(observer_prohibited()) {
			return;
		}

		$channel = App::$data['channel'];

		// since we don't currently have an event permission - use the stream permission

		if(! perm_is_allowed($channel['channel_id'], get_observer_hash(), 'view_stream')) {
			notice( t('Permissions denied.') . EOL);
			return;
		}

		nav_set_selected('Calendar');

		head_add_css('/library/fullcalendar/packages/core/main.min.css');
		head_add_css('/library/fullcalendar/packages/daygrid/main.min.css');
		head_add_css('cdav_calendar.css');

		head_add_js('/library/fullcalendar/packages/core/main.min.js');
		head_add_js('/library/fullcalendar/packages/daygrid/main.min.js');

		$sql_extra = permissions_sql($channel['channel_id'], get_observer_hash(), 'event');

		if(! perm_is_allowed($channel['channel_id'], get_observer_hash(), 'view_contacts') || App::$profile['hide_friends'])
			$sql_extra .= " and etype != 'birthday' ";

		$first_day = feature_enabled($channel['channel_id'], 'cal_first_day');
		$first_day = (($first_day) ? $first_day : 0);

		$start = '';
		$finish = '';

		if (argv(2) === 'json') {
			if (x($_GET,'start'))	$start = $_GET['start'];
			if (x($_GET,'end'))	$finish = $_GET['end'];
		}

		$start  = datetime_convert('UTC','UTC',$start);
		$finish = datetime_convert('UTC','UTC',$finish);
		$adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
		$adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);

		if (x($_GET, 'id')) {
			$r = q("SELECT event.*, item.plink, item.item_flags, item.author_xchan, item.owner_xchan, item.id as item_id
                                from event left join item on item.resource_id = event.event_hash
				where item.resource_type = 'event' and event.uid = %d and event.id = %d $sql_extra limit 1",
				intval($channel['channel_id']),
				intval($_GET['id'])
			);
		}
		else {
			// fixed an issue with "nofinish" events not showing up in the calendar.
			// There's still an issue if the finish date crosses the end of month.
			// Noting this for now - it will need to be fixed here and in Friendica.
			// Ultimately the finish date shouldn't be involved in the query.
			$r = q("SELECT event.*, item.plink, item.item_flags, item.author_xchan, item.owner_xchan, item.id as item_id
				from event left join item on event.event_hash = item.resource_id
				where item.resource_type = 'event' and event.uid = %d and event.uid = item.uid
				AND (( event.adjust = 0 AND ( event.dtend >= '%s' or event.nofinish = 1 ) AND event.dtstart <= '%s' )
				OR (  event.adjust = 1 AND ( event.dtend >= '%s' or event.nofinish = 1 ) AND event.dtstart <= '%s' ))
				$sql_extra",
				intval($channel['channel_id']),
				dbesc($start),
				dbesc($finish),
				dbesc($adjust_start),
				dbesc($adjust_finish)
			);
		}

		if($r) {
			xchan_query($r);
			$r = fetch_post_tags($r,true);
			$r = sort_by_date($r);
		}

		$events = [];

		if($r) {

			foreach($r as $rr) {

				$start = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['dtstart'], 'c') : datetime_convert('UTC', 'UTC', $rr['dtstart'], 'c'));
				if ($rr['nofinish']){
					$end = null;
				} else {
					$end = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['dtend'], 'c') : datetime_convert('UTC', 'UTC', $rr['dtend'], 'c'));
				}

				$html = '';
				if (x($_GET,'id')) {
					$rr['timezone'] = $tz;
					$html = format_event_html($rr);
				}

				$tz = get_iconfig($rr, 'event', 'timezone');
				if(! $tz)
					$tz = 'UTC';

				$events[] = array(
					'calendar_id' => 'channel_calendar',
					'rw' => true,
					'id'=>$rr['id'],
					'uri' => $rr['event_hash'],
					'timezone' => $tz,
					'start'=> $start,
					'end' => $end,
					'drop' => false,
					'allDay' => (($rr['adjust']) ? 0 : 1),
					'title' => html_entity_decode($rr['summary'], ENT_COMPAT, 'UTF-8'),
					'editable' => false,
					'item' => $rr,
					'plink' => [$rr['plink'], t('Link to source')],
					'description' => html_entity_decode($rr['description'], ENT_COMPAT, 'UTF-8'),
					'location' => html_entity_decode($rr['location'], ENT_COMPAT, 'UTF-8'),
					'allow_cid' => expand_acl($rr['allow_cid']),
					'allow_gid' => expand_acl($rr['allow_gid']),
					'deny_cid' => expand_acl($rr['deny_cid']),
					'deny_gid' => expand_acl($rr['deny_gid']),
					'html' => $html
				);
			}
		}

		if (argv(2) === 'json') {
			echo json_encode($events);
			killme();
		}

		if (x($_GET,'id')) {
			$o = replace_macros(get_markup_template("cal_event.tpl"), [
				'$events' => $events
			]);
			echo $o;
			killme();
		}

		$nick = $channel['channel_address'];

		$sources = '{
			id: \'channel_calendar\',
			url: \'/cal/' . $nick . '/json/\',
			color: \'#3a87ad\'
		}';

		$o = replace_macros(get_markup_template("cal_calendar.tpl"), [
			'$sources' => $sources,
			'$lang' => App::$language,
			'$timezone' => date_default_timezone_get(),
			'$first_day' => $first_day,
			'$prev'	=> t('Previous'),
			'$next'	=> t('Next'),
			'$today' => t('Today'),
			'$title' => '',
			'$dtstart' => '',
			'$dtend' => '',
			'$nick' => $nick
		]);

		return $o;

	}

}