tiki-calendar_edit_item.php 24.8 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
6
//
7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
changi67's avatar
changi67 committed
9
// $Id$
10 11

$section = 'calendar';
rjsmelo's avatar
rjsmelo committed
12
require_once('tiki-setup.php');
13

14 15
$access->check_feature('feature_calendar');

lphuberdeau's avatar
lphuberdeau committed
16
$calendarlib = TikiLib::lib('calendar');
rjsmelo's avatar
rjsmelo committed
17 18
include_once('lib/newsletters/nllib.php');
include_once('lib/calendar/calrecurrence.php');
19
if ($prefs['feature_groupalert'] == 'y') {
lphuberdeau's avatar
lphuberdeau committed
20
	$groupalertlib = TikiLib::lib('groupalert');
21
}
rjsmelo's avatar
rjsmelo committed
22
$auto_query_args = ['calitemId', 'viewcalitemId'];
23

rjsmelo's avatar
rjsmelo committed
24 25 26
$daysnames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
$daysnames_abr = ["Su","Mo","Tu","We","Th","Fr","Sa"];
$monthnames = ["","January","February","March","April","May","June","July","August","September","October","November","December"];
changi67's avatar
changi67 committed
27 28 29
$smarty->assign('daysnames', $daysnames);
$smarty->assign('daysnames_abr', $daysnames_abr);
$smarty->assign('monthnames', $monthnames);
30

changi67's avatar
changi67 committed
31
$smarty->assign('edit', false);
32
$smarty->assign('recurrent', '');
33
$hour_minmax = '';
rjsmelo's avatar
rjsmelo committed
34
$recurrence = [
35 36 37 38 39 40 41 42 43 44 45
	'id'				=> '',
	'weekly'			=> '',
	'weekday'			=> '',
	'monthly'			=> '',
	'dayOfMonth'		=> '',
	'yearly'			=> '',
	'dateOfYear_day'	=> '',
	'dateOfYear_month'	=> '',
	'startPeriod'		=> '',
	'nbRecurrences'		=> '',
	'endPeriod'			=> ''
rjsmelo's avatar
rjsmelo committed
46
];
47
$smarty->assign('recurrence', $recurrence);
48

rjsmelo's avatar
rjsmelo committed
49
$caladd = [];
50
$rawcals = $calendarlib->list_calendars();
51
if ($rawcals['cant'] == 0 && $tiki_p_admin_calendar == 'y') {
52
	$smarty->assign('msg', tra('You need to <a href="tiki-admin_calendars.php?cookietab=2">create a calendar</a>'));
53 54 55
	$smarty->display("error.tpl");
	die;
}
56

rjsmelo's avatar
rjsmelo committed
57
$rawcals['data'] = Perms::filter([ 'type' => 'calendar' ], 'object', $rawcals['data'], [ 'object' => 'calendarId' ], 'view_calendar');
58

59
foreach ($rawcals["data"] as $cal_data) {
rjsmelo's avatar
rjsmelo committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	$cal_id = $cal_data['calendarId'];
	$calperms = Perms::get([ 'type' => 'calendar', 'object' => $cal_id ]);
	if ($cal_data["personal"] == "y") {
		if ($user) {
			$cal_data["tiki_p_view_calendar"] = 'y';
			$cal_data["tiki_p_view_events"] = 'y';
			$cal_data["tiki_p_add_events"] = 'y';
			$cal_data["tiki_p_change_events"] = 'y';
		} else {
			$cal_data["tiki_p_view_calendar"] = 'n';
			$cal_data["tiki_p_view_events"] = 'y';
			$cal_data["tiki_p_add_events"] = 'n';
			$cal_data["tiki_p_change_events"] = 'n';
		}
	} else {
		$cal_data["tiki_p_view_calendar"] = $calperms->view_calendar ? "y" : "n";
		$cal_data["tiki_p_view_events"] = $calperms->view_events ? "y" : "n";
		$cal_data["tiki_p_add_events"] = $calperms->add_events ? "y" : "n";
		$cal_data["tiki_p_change_events"] = $calperms->change_events ? "y" : "n";
	}
80
	$caladd["$cal_id"] = $cal_data;
81 82 83
	if ($cal_data['tiki_p_add_events'] == 'y' && empty($calID)) {
		$calID = $cal_id;
	}
84
}
changi67's avatar
changi67 committed
85
$smarty->assign('listcals', $caladd);
86

rjsmelo's avatar
rjsmelo committed
87
if (! isset($_REQUEST["calendarId"])) {
sylvieg's avatar
sylvieg committed
88 89
	if (isset($_REQUEST['calitemId'])) {
		$calID = $calendarlib->get_calendarid($_REQUEST['calitemId']);
sylvieg's avatar
sylvieg committed
90
	} elseif (isset($_REQUEST['viewcalitemId'])) {
sylvieg's avatar
sylvieg committed
91 92 93
		$calID = $calendarlib->get_calendarid($_REQUEST['viewcalitemId']);
	}
} elseif (isset($_REQUEST['calendarId'])) {
94 95
	$calID = $_REQUEST['calendarId'];
} elseif (isset($_REQUEST['save']) && isset($_REQUEST['save']['calendarId'])) {
96
	$calID = $_REQUEST['save']['calendarId'];
sylvieg's avatar
sylvieg committed
97
}
98

rjsmelo's avatar
rjsmelo committed
99 100
if ($prefs['feature_groupalert'] == 'y' && ! empty($calID)) {
	$groupforalert = $groupalertlib->GetGroup('calendar', $calID);
101
	$showeachuser = '';
rjsmelo's avatar
rjsmelo committed
102 103 104
	if ($groupforalert != '') {
		$showeachuser = $groupalertlib->GetShowEachUser('calendar', $calID, $groupforalert);
		$listusertoalert = $userlib->get_users(0, -1, 'login_asc', '', '', false, $groupforalert, '');
changi67's avatar
changi67 committed
105
		$smarty->assign_by_ref('listusertoalert', $listusertoalert['data']);
106
	}
changi67's avatar
changi67 committed
107 108
	$smarty->assign_by_ref('groupforalert', $groupforalert);
	$smarty->assign_by_ref('showeachuser', $showeachuser);
109
}
110

changi67's avatar
changi67 committed
111
$tikilib->get_perm_object($calID, 'calendar');
112
$access->check_permission('tiki_p_view_calendar');
113

rjsmelo's avatar
rjsmelo committed
114 115
$calitemId = ! empty($_REQUEST['save']['calitemId']) ? $_REQUEST['save']['calitemId'] : (! empty($_REQUEST['calitemId']) ? $_REQUEST['calitemId'] : (! empty($_REQUEST['viewcalitemId']) ? $_REQUEST['viewcalitemId'] : 0));
if (! empty($calitemId) && ! empty($user)) {
116 117 118 119
	$calitem = $calendarlib->get_item($calitemId);
	if ($calitem['user'] == $user) {
		$smarty->assign('tiki_p_change_events', 'y');
		$tiki_p_change_events = 'y';
rjsmelo's avatar
rjsmelo committed
120
		if (! empty($_REQUEST['save']['calendarId'])) {
121 122 123
			$caladd[$_REQUEST['save']['calendarId']]['tiki_p_change_events'] = $caladd[$_REQUEST['save']['calendarId']]['tiki_p_add_events'];
		}
		$caladd[$calitem['calendarId']]['tiki_p_change_events'] = 'y';
changi67's avatar
changi67 committed
124
	}
125 126
}

rjsmelo's avatar
rjsmelo committed
127
if (isset($_REQUEST['save']) && ! isset($_REQUEST['preview']) && ! isset($_REQUEST['act'])) {
128
	$_REQUEST['changeCal'] = true;
129
}
130

131
if (isset($_REQUEST['act']) || isset($_REQUEST['preview']) || isset($_REQUEST['changeCal'])) {
132
	$save = $_POST['save'];
133
	// Take care of timestamps dates coming from jscalendar
rjsmelo's avatar
rjsmelo committed
134 135
	if (isset($save['date_start']) || isset($save['date_end'])) {
		if (isset($_REQUEST['tzoffset'])) {
136 137 138 139 140
			$browser_offset = 0 - intval($_REQUEST['tzoffset']) * 60;
			$server_offset = TikiDate::tzServerOffset(TikiLib::lib('tiki')->get_display_timezone());
			$save['date_start'] = $save['date_start'] - $server_offset + $browser_offset;
			$save['date_end'] = $save['date_end'] - $server_offset + $browser_offset;
		}
141 142 143 144 145 146 147
		$_REQUEST['start_date_Month'] = TikiLib::date_format("%m", $save['date_start']);
		$_REQUEST['start_date_Day'] = TikiLib::date_format("%d", $save['date_start']);
		$_REQUEST['start_date_Year'] = TikiLib::date_format("%Y", $save['date_start']);
		$_REQUEST['end_date_Month'] = TikiLib::date_format("%m", $save['date_end']);
		$_REQUEST['end_date_Day'] = TikiLib::date_format("%d", $save['date_end']);
		$_REQUEST['end_date_Year'] = TikiLib::date_format("%Y", $save['date_end']);
	}
148

149
	$save['allday'] = (isset($_REQUEST['allday']) && $_REQUEST['allday'] == 'true') ? 1 : 0;
sylvieg's avatar
sylvieg committed
150
	if (isset($_REQUEST['allday']) && $_REQUEST['allday'] == 'true') {
151
		$save['start'] = $tikilib->make_time(
152 153 154 155 156 157
			0,
			0,
			0,
			$_REQUEST['start_date_Month'],
			$_REQUEST['start_date_Day'],
			$_REQUEST['start_date_Year']
158 159 160 161 162
		);
		if ($save['end_or_duration'] == 'duration') {
			$save['duration'] = 86399;
			$save['end'] = $save['start'] + $save['duration'];
		} else {
163
			$save['end'] = $tikilib->make_time(
164 165 166 167 168 169
				23,
				59,
				59,
				$_REQUEST['end_date_Month'],
				$_REQUEST['end_date_Day'],
				$_REQUEST['end_date_Year']
170 171 172
			);
			$save['duration'] = max(0, $save['end'] - $save['start']);
		}
173
	} else {
174
		//Convert 12-hour clock hours to 24-hour scale to compute time
rjsmelo's avatar
rjsmelo committed
175
		if (! empty($_REQUEST['start_Meridian'])) {
176
			$_REQUEST['start_Hour'] = date('H', strtotime($_REQUEST['start_Hour'] . ':00 ' . $_REQUEST['start_Meridian']));
177
		}
178
		$save['start'] = $tikilib->make_time(
179 180 181 182 183 184
			$_REQUEST['start_Hour'],
			$_REQUEST['start_Minute'],
			0,
			$_REQUEST['start_date_Month'],
			$_REQUEST['start_date_Day'],
			$_REQUEST['start_date_Year']
185
		);
186 187

		if ($save['end_or_duration'] == 'duration') {
rjsmelo's avatar
rjsmelo committed
188
			$save['duration'] = max(0, $_REQUEST['duration_Hour'] * 60 * 60 + $_REQUEST['duration_Minute'] * 60);
189 190
			$save['end'] = $save['start'] + $save['duration'];
		} else {
191
			//Convert 12-hour clock hours to 24-hour scale to compute time
rjsmelo's avatar
rjsmelo committed
192
			if (! empty($_REQUEST['end_Meridian'])) {
193
				$_REQUEST['end_Hour'] = date('H', strtotime($_REQUEST['end_Hour'] . ':00 ' . $_REQUEST['end_Meridian']));
194
			}
195
			$save['end'] = $tikilib->make_time(
196 197 198 199 200 201
				$_REQUEST['end_Hour'],
				$_REQUEST['end_Minute'],
				0,
				$_REQUEST['end_date_Month'],
				$_REQUEST['end_date_Day'],
				$_REQUEST['end_date_Year']
202 203 204
			);
			$save['duration'] = max(0, $save['end'] - $save['start']);
		}
205 206
	}
}
207

208 209
$impossibleDates = false;
if (isset($save['start']) && isset($save['end'])) {
rjsmelo's avatar
rjsmelo committed
210
	if (($save['end'] - $save['start']) < 0) {
211
		$impossibleDates = true;
rjsmelo's avatar
rjsmelo committed
212
	}
213
}
214 215

if (isset($_POST['act'])) {
216
	// Check antibot code if anonymous and allowed
rjsmelo's avatar
rjsmelo committed
217
	if (empty($user) && $prefs['feature_antibot'] == 'y' && (! $captchalib->validate())) {
218
		$smarty->assign('msg', $captchalib->getErrors());
219 220 221 222
		$smarty->assign('errortype', 'no_redirect_login');
		$smarty->display("error.tpl");
		die;
	}
rjsmelo's avatar
rjsmelo committed
223 224 225
	if (empty($save['user'])) {
		$save['user'] = $user;
	}
226
	$newcalid = $save['calendarId'];
227
	if ((empty($save['calitemId']) and $caladd["$newcalid"]['tiki_p_add_events'] == 'y')
rjsmelo's avatar
rjsmelo committed
228 229 230 231 232 233 234 235
	or (! empty($save['calitemId']) and $caladd["$newcalid"]['tiki_p_change_events'] == 'y')) {
		if (empty($save['name'])) {
			$save['name'] = tra("event without name");
		}
		if (empty($save['priority'])) {
			$save['priority'] = 1;
		}
		if (! isset($save['status'])) {
236 237 238 239 240 241
			if (empty($calendar['defaulteventstatus'])) {
				$save['status'] = 1; // Confirmed
			} else {
				$save['status'] = $calendar['defaulteventstatus'];
			}
		}
242

rjsmelo's avatar
rjsmelo committed
243
		if (array_key_exists('recurrent', $_POST) && ($_POST['recurrent'] == 1) && $_POST['affect'] != 'event') {
244 245 246 247 248
			$impossibleDates = false;
			if ($_POST['end_Hour'] < $_POST['start_Hour']) {
				$impossibleDates = true;
			} elseif (($_POST['end_Hour'] == $_POST['start_Hour']) && ($_POST['end_Minute'] < $_POST['start_Minute'])) {
				$impossibleDates = true;
rjsmelo's avatar
rjsmelo committed
249
			} else {
250
				$impossibleDates = false;
rjsmelo's avatar
rjsmelo committed
251 252
			}
			if (! $impossibleDates) {
253 254
				$calRecurrence = new CalRecurrence($_POST['recurrenceId'] ? $_POST['recurrenceId'] : -1);
				$calRecurrence->setCalendarId($save['calendarId']);
changi67's avatar
changi67 committed
255 256
				$calRecurrence->setStart($_POST['start_Hour'] . str_pad($_POST['start_Minute'], 2, '0', STR_PAD_LEFT));
				$calRecurrence->setEnd($_POST['end_Hour'] . str_pad($_POST['end_Minute'], 2, '0', STR_PAD_LEFT));
257
				$calRecurrence->setAllday($save['allday']);
258 259 260 261 262 263 264 265 266
				$calRecurrence->setLocationId($save['locationId']);
				$calRecurrence->setCategoryId($save['categoryId']);
				$calRecurrence->setNlId(0); //TODO : What id nlId ?
				$calRecurrence->setPriority($save['priority']);
				$calRecurrence->setStatus($save['status']);
				$calRecurrence->setUrl($save['url']);
				$calRecurrence->setLang(strLen($save['lang']) > 0 ? $save['lang'] : 'en');
				$calRecurrence->setName($save['name']);
				$calRecurrence->setDescription($save['description']);
rjsmelo's avatar
rjsmelo committed
267
				switch ($_POST['recurrenceType']) {
268 269
					case "weekly":
						$calRecurrence->setWeekly(true);
270 271 272
						$calRecurrence->setWeekday($_POST['weekday']);
						$calRecurrence->setMonthly(false);
						$calRecurrence->setYearly(false);
rjsmelo's avatar
rjsmelo committed
273
						break;
274 275
					case "monthly":
						$calRecurrence->setWeekly(false);
276 277 278
						$calRecurrence->setMonthly(true);
						$calRecurrence->setDayOfMonth($_POST['dayOfMonth']);
						$calRecurrence->setYearly(false);
279
						break;
280 281
					case "yearly":
						$calRecurrence->setWeekly(false);
282 283
						$calRecurrence->setMonthly(false);
						$calRecurrence->setYearly(true);
changi67's avatar
changi67 committed
284
						$calRecurrence->setDateOfYear(str_pad($_POST['dateOfYear_month'], 2, '0', STR_PAD_LEFT) . str_pad($_POST['dateOfYear_day'], 2, '0', STR_PAD_LEFT));
rjsmelo's avatar
rjsmelo committed
285
						break;
286 287
				}
				$calRecurrence->setStartPeriod($_POST['startPeriod']);
rjsmelo's avatar
rjsmelo committed
288
				if ($_POST['endType'] == "dt") {
289
					$calRecurrence->setEndPeriod($_POST['endPeriod']);
rjsmelo's avatar
rjsmelo committed
290
				} else {
291 292 293 294
					$calRecurrence->setNbRecurrences($_POST['nbRecurrences']);
				}
				$calRecurrence->setUser($save['user']);
				$calRecurrence->save($_POST['affect'] == 'all');
295 296
				// Save the ip at the log for the addition of new calendar items
				if ($prefs['feature_actionlog'] == 'y' && empty($save['calitemId']) && $caladd["$newcalid"]['tiki_p_add_events']) {
rjsmelo's avatar
rjsmelo committed
297
					$logslib->add_action('Created', 'recurrent event starting on ' . $_POST['startPeriod'] . ' in calendar ' . $save['calendarId'], 'calendar event');
changi67's avatar
changi67 committed
298
				}
rjsmelo's avatar
rjsmelo committed
299 300
				if ($prefs['feature_actionlog'] == 'y' && ! empty($save['calitemId']) and $caladd["$newcalid"]['tiki_p_change_events']) {
					$logslib->add_action('Updated', 'recurrent event starting on ' . $_POST['startPeriod'] . ' in calendar ' . $save['calendarId'], 'calendar event');
changi67's avatar
changi67 committed
301
				}
rjsmelo's avatar
rjsmelo committed
302
				header('Location: tiki-calendar.php?todate=' . $save['start']);
303
				die;
304
			}
305
		} else {
rjsmelo's avatar
rjsmelo committed
306
			if (! $impossibleDates) {
changi67's avatar
changi67 committed
307
				if (array_key_exists('recurrenceId', $_POST)) {
308 309 310
					$save['recurrenceId'] = $_POST['recurrenceId'];
					$save['changed'] = true;
				}
changi67's avatar
changi67 committed
311
				$calitemId = $calendarlib->set_item($user, $save['calitemId'], $save);
312 313
				// Save the ip at the log for the addition of new calendar items
				if ($prefs['feature_actionlog'] == 'y' && empty($save['calitemId']) && $caladd["$newcalid"]['tiki_p_add_events']) {
rjsmelo's avatar
rjsmelo committed
314
					$logslib->add_action('Created', 'event ' . $calitemId . ' in calendar ' . $save['calendarId'], 'calendar event');
changi67's avatar
changi67 committed
315
				}
rjsmelo's avatar
rjsmelo committed
316 317
				if ($prefs['feature_actionlog'] == 'y' && ! empty($save['calitemId']) and $caladd["$newcalid"]['tiki_p_change_events']) {
					$logslib->add_action('Updated', 'event ' . $calitemId . ' in calendar ' . $save['calendarId'], 'calendar event');
changi67's avatar
changi67 committed
318 319
				}
				if ($prefs['feature_groupalert'] == 'y') {
rjsmelo's avatar
rjsmelo committed
320
					$groupalertlib->Notify($_REQUEST['listtoalert'], "tiki-calendar_edit_item.php?viewcalitemId=" . $calitemId);
changi67's avatar
changi67 committed
321
				}
rjsmelo's avatar
rjsmelo committed
322
				header('Location: tiki-calendar.php?todate=' . $save['start']);
changi67's avatar
changi67 committed
323
				die;
324
			}
325
		}
326 327 328
	}
}

rjsmelo's avatar
rjsmelo committed
329 330
if (! empty($_REQUEST['viewcalitemId']) && isset($_REQUEST['del_me']) && $tiki_p_calendar_add_my_particip == 'y') {
	$calendarlib->update_participants($_REQUEST['viewcalitemId'], null, [$user]);
331 332
}

rjsmelo's avatar
rjsmelo committed
333 334
if (! empty($_REQUEST['viewcalitemId']) && isset($_REQUEST['add_me']) && $tiki_p_calendar_add_my_particip == 'y') {
	$calendarlib->update_participants($_REQUEST['viewcalitemId'], [['name' => $user]], null);
335 336
}

rjsmelo's avatar
rjsmelo committed
337
if (! empty($_REQUEST['viewcalitemId']) && ! empty($_REQUEST['guests']) && isset($_REQUEST['add_guest']) && $tiki_p_calendar_add_guest_particip == 'y') {
338
	$guests = preg_split('/ *, */', $_REQUEST['guests']);
rjsmelo's avatar
rjsmelo committed
339 340
	foreach ($guests as $i => $guest) {
		$guests[$i] = ['name' => $guest];
341 342 343 344
	}
	$calendarlib->update_participants($_REQUEST['viewcalitemId'], $guests);
}

345
if (isset($_REQUEST["delete"]) and ($_REQUEST["delete"]) and isset($_REQUEST["calitemId"]) and $tiki_p_change_events == 'y') {
346
	// There is no check for valid antibot code if anonymous allowed to delete events since this comes from a JS button at the tpl and bots are not know to use JS
347
	$access->check_authenticity();
348
	$calitem = $calendarlib->get_item($_REQUEST['calitemId']);
349
	$calendarlib->drop_item($user, $_REQUEST["calitemId"]);
350
	if ($prefs['feature_actionlog'] == 'y') {
rjsmelo's avatar
rjsmelo committed
351
		$logslib->add_action('Removed', 'event ' . $_REQUEST['calitemId'], 'calendar event');
352
	}
353
	$_REQUEST["calitemId"] = 0;
rjsmelo's avatar
rjsmelo committed
354
	header('Location: tiki-calendar.php?todate=' . $calitem['start']);
355
	exit;
356
} elseif (isset($_REQUEST["delete"]) and ($_REQUEST["delete"]) and isset($_REQUEST["recurrenceId"]) and $tiki_p_change_events == 'y') {
357
	// There is no check for valid antibot code if anonymous allowed to delete events since this comes from a JS button at the tpl and bots are not know to use JS
358 359
	$calRec = new CalRecurrence($_REQUEST['recurrenceId']);
	$calRec->delete();
360
	if ($prefs['feature_actionlog'] == 'y') {
rjsmelo's avatar
rjsmelo committed
361
		$logslib->add_action('Removed', 'recurrent event (recurrenceId = ' . $_REQUEST["recurrenceId"] . ')', 'calendar event');
362
	}
rjsmelo's avatar
rjsmelo committed
363 364
	$_REQUEST["recurrenceTypeId"] = 0;
	$_REQUEST["calitemId"] = 0;
365 366
	header('Location: tiki-calendar.php');
	die;
367
} elseif (isset($_REQUEST['drop']) and $tiki_p_change_events == 'y') {
rjsmelo's avatar
rjsmelo committed
368 369 370 371 372 373 374 375
	check_ticket('calendar');
	if (is_array($_REQUEST['drop'])) {
		foreach ($_REQUEST['drop'] as $dropme) {
			$calendarlib->drop_item($user, $dropme);
		}
	} else {
		$calendarlib->drop_item($user, $_REQUEST['drop']);
	}
376
	if ($prefs['feature_actionlog'] == 'y') {
rjsmelo's avatar
rjsmelo committed
377
		$logslib->add_action('Removed (dropped)', 'event/s ' . $_REQUEST['calitemId'], 'calendar event');
378
	}
379 380
	header('Location: tiki-calendar.php');
	die;
changi67's avatar
changi67 committed
381
} elseif (isset($_REQUEST['duplicate']) and $tiki_p_add_events == 'y') {
382
	// Check antibot code if anonymous and allowed
rjsmelo's avatar
rjsmelo committed
383
	if (empty($user) && $prefs['feature_antibot'] == 'y' && (! $captchalib->validate())) {
384
		$smarty->assign('msg', $captchalib->getErrors());
385 386 387 388
		$smarty->assign('errortype', 'no_redirect_login');
		$smarty->display("error.tpl");
		die;
	}
389
	$calitem = $calendarlib->get_item($_REQUEST['calitemId']);
390
	$calitem['calendarId'] = $calID;
391
	$calitem['calitemId'] = 0;
changi67's avatar
changi67 committed
392
	$calendarlib->set_item($user, 0, $calitem);
393 394 395
	$id = 0;
	if (isset($_REQUEST['calId'])) {
		$calendar = $calendarlib->get_calendar($_REQUEST['calId']);
changi67's avatar
changi67 committed
396
	} else {
397
		$calendar = $calendarlib->get_calendar($calitem['calendarId']);
changi67's avatar
changi67 committed
398 399
	}
	$smarty->assign('edit', true);
rjsmelo's avatar
rjsmelo committed
400
	$hour_minmax = abs(ceil(($calendar['startday'] - 1) / (60 * 60))) . '-' . ceil(($calendar['endday']) / (60 * 60));
401
} elseif (isset($_REQUEST['preview']) || $impossibleDates) {
rjsmelo's avatar
rjsmelo committed
402
	$save['parsed'] = TikiLib::lib('parser')->parse_data($save['description'], ['is_html' => $prefs['calendar_description_is_html'] === 'y']);
403
	$save['parsedName'] = TikiLib::lib('parser')->parse_data($save['name']);
404
	$id = isset($save['calitemId']) ? isset($save['calitemId']) : '';
405
	$calitem = $save;
406
	$calitem['recurrenceId'] = '';
407

rjsmelo's avatar
rjsmelo committed
408
	$recurrence = [
409 410
		'id'	=> '',
		'weekly' => isset($_POST['recurrenceType']) && $_POST['recurrenceType'] == 'weekly',
411
		'weekday' => isset($_POST['weekday']) ? $_POST['weekday'] : '',
412
		'monthly' => isset($_POST['recurrenceType']) && $_POST['recurrenceType'] == 'monthly',
413
		'dayOfMonth' => isset($_POST['dayOfMonth']) ? $_POST['dayOfMonth'] : '',
414
		'yearly' => isset($_POST['recurrenceType']) && $_POST['recurrenceType'] == 'yearly',
415 416 417 418 419
		'dateOfYear_day' => isset($_POST['dateOfYear_day']) ? $_POST['dateOfYear_day'] : '',
		'dateOfYear_month' => isset($_POST['dateOfYear_month']) ? $_POST['dateOfYear_month'] : '',
		'startPeriod' => isset($_POST['startPeriod']) ? $_POST['startPeriod'] : '',
		'nbRecurrences' => isset($_POST['nbRecurrences']) ? $_POST['nbRecurrences'] : '',
		'endPeriod' => isset($_POST['endPeriod']) ? $_POST['endPeriod'] : ''
rjsmelo's avatar
rjsmelo committed
420 421
	];
	if (isset($_POST['recurrent']) && $_POST['recurrent'] == 1) {
422 423 424
		$smarty->assign('recurrent', $_POST['recurrent']);
	}
	$smarty->assign_by_ref('recurrence', $recurrence);
425

426
	$calendar = $calendarlib->get_calendar($calitem['calendarId']);
changi67's avatar
changi67 committed
427
	$smarty->assign('edit', true);
428 429
} elseif (isset($_REQUEST['changeCal'])) {
	$calitem = $save;
430
	$calendar = $calendarlib->get_calendar($calitem['calendarId']);
rjsmelo's avatar
rjsmelo committed
431 432 433
	if (empty($save['calitemId'])) {
		$calitem['allday'] = $calendar['allday'] == 'y' ? true : false;
	}
changi67's avatar
changi67 committed
434
	$smarty->assign('edit', true);
rjsmelo's avatar
rjsmelo committed
435 436
	$id = isset($save['calitemId']) ? $save['calitemId'] : 0;
	$hour_minmax = ceil(($calendar['startday']) / (60 * 60)) . '-' . ceil(($calendar['endday']) / (60 * 60));
437
	$smarty->assign('changeCal', isset($_REQUEST['changeCal']));
438
} elseif (isset($_REQUEST['viewcalitemId']) and $tiki_p_view_events == 'y') {
439 440 441
	$calitem = $calendarlib->get_item($_REQUEST['viewcalitemId']);
	$id = $_REQUEST['viewcalitemId'];
	$calendar = $calendarlib->get_calendar($calitem['calendarId']);
rjsmelo's avatar
rjsmelo committed
442
	$hour_minmax = ceil(($calendar['startday']) / (60 * 60)) . '-' . ceil(($calendar['endday']) / (60 * 60));
443
} elseif (isset($_REQUEST['calitemId']) and ($tiki_p_change_events == 'y' or $tiki_p_view_events == 'y')) {
444 445 446
	$calitem = $calendarlib->get_item($_REQUEST['calitemId']);
	$id = $_REQUEST['calitemId'];
	$calendar = $calendarlib->get_calendar($calitem['calendarId']);
changi67's avatar
changi67 committed
447
	$smarty->assign('edit', true);
rjsmelo's avatar
rjsmelo committed
448
	$hour_minmax = ceil(($calendar['startday']) / (60 * 60)) . '-' . ceil(($calendar['endday']) / (60 * 60));
449
//Add event buttons - either button on top of page or one of the buttons on a specific day
450
} elseif (isset($calID) and $tiki_p_add_events == 'y') {
451
	$calendar = $calendarlib->get_calendar($calID);
452 453
	if (isset($_REQUEST['todate'])) {
		$now = $_REQUEST['todate'];
rjsmelo's avatar
rjsmelo committed
454
		if (isset($_REQUEST['tzoffset'])) {
455 456 457 458
			$browser_offset = 0 - intval($_REQUEST['tzoffset']) * 60;
			$server_offset = TikiDate::tzServerOffset(TikiLib::lib('tiki')->get_display_timezone());
			$now = $now - $server_offset + $browser_offset;
		}
459
	} else {
mose's avatar
mose committed
460
		$now = $tikilib->now;
461
	}
462
	//if current time of day is within the calendar day (between startday and endday), then use now as start, otherwise use beginning of calendar day
463
	$now_start = $tikilib->make_time(
rjsmelo's avatar
rjsmelo committed
464
		abs(ceil($calendar['startday'] / (60 * 60))),
465 466 467 468 469
		TikiLib::date_format('%M', $now),
		TikiLib::date_format('%S', $now),
		TikiLib::date_format('%m', $now),
		TikiLib::date_format('%d', $now),
		TikiLib::date_format('%Y', $now)
470
	);
471
	$now_end = $tikilib->make_time(
rjsmelo's avatar
rjsmelo committed
472
		abs(ceil($calendar['endday'] / (60 * 60))),
473 474 475 476 477
		TikiLib::date_format('%M', $now),
		TikiLib::date_format('%S', $now),
		TikiLib::date_format('%m', $now),
		TikiLib::date_format('%d', $now),
		TikiLib::date_format('%Y', $now)
478
	);
rjsmelo's avatar
rjsmelo committed
479
	$now_start = ($now_start <= $now && ($now_start + (60 * 60)) < $now_end) ? $now : $now_start;
480

481
	//if $now_end is midnight, make it one second before
rjsmelo's avatar
rjsmelo committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	$now_end = TikiLib::date_format('%H%M%s', $now_start + (60 * 60)) == '000000' ? $now_start + (60 * 60) - 1 : $now_start + (60 * 60);

	$calitem = [
		'calitemId' => 0,
		'user' => $user,
		'name' => '',
		'url' => '',
		'description' => '',
		'status' => $calendar['defaulteventstatus'],
		'priority' => 0,
		'locationId' => 0,
		'categoryId' => 0,
		'nlId' => 0,
		'start' => $now_start,
		'end' => $now_end,
		'duration' => (60 * 60),
		'recurrenceId' => 0,
		'allday' => $calendar['allday'] == 'y' ? true : false
		];
	$hour_minmax = abs(ceil(($calendar['startday'] - 1) / (60 * 60))) . '-' . ceil(($calendar['endday']) / (60 * 60));
502
	$id = 0;
changi67's avatar
changi67 committed
503
	$smarty->assign('edit', true);
504
} else {
rjsmelo's avatar
rjsmelo committed
505 506 507 508
	$smarty->assign('errortype', 401);
	$smarty->assign('msg', tra("You do not have permission to view this page"));
	$smarty->display("error.tpl");
	die;
509
}
rjsmelo's avatar
rjsmelo committed
510
if (! empty($id) && $calendar['personal'] == 'y' && $calitem['user'] != $user) {
511 512 513 514 515
	$smarty->assign('errortype', 401);
	$smarty->assign('msg', tra("You do not have permission to view this page"));
	$smarty->display("error.tpl");
	die;
}
516

rjsmelo's avatar
rjsmelo committed
517 518
if (! empty($calendar['eventstatus'])) {
	$calitem['status'] = $calendar['eventstatus'];
519
}
520

521
if ($calendar['customlocations'] == 'y') {
522
	$listlocs = $calendarlib->list_locations($calID);
523
} else {
rjsmelo's avatar
rjsmelo committed
524
	$listlocs = [];
525 526
}
$smarty->assign('listlocs', $listlocs);
527
$smarty->assign('changeCal', isset($_REQUEST['changeCal']));
528

529
$userprefslib = TikiLib::lib('userprefs');
530
$smarty->assign('use_24hr_clock', $userprefslib->get_user_clock_pref($user));
531

532
if ($calendar['customcategories'] == 'y') {
533
	$listcats = $calendarlib->list_categories($calID);
534
} else {
rjsmelo's avatar
rjsmelo committed
535
	$listcats = [];
536 537 538 539 540 541
}
$smarty->assign('listcats', $listcats);

if ($calendar["customsubscription"] == 'y') {
	$subscrips = $nllib->list_avail_newsletters();
} else {
rjsmelo's avatar
rjsmelo committed
542
	$subscrips = [];
543 544 545 546
}
$smarty->assign('subscrips', $subscrips);

if ($calendar["customlanguages"] == 'y') {
547 548
	$langLib = TikiLib::lib('language');
	$languages = $langLib->list_languages();
549
} else {
rjsmelo's avatar
rjsmelo committed
550
	$languages = [];
551 552 553
}
$smarty->assign('listlanguages', $languages);

rjsmelo's avatar
rjsmelo committed
554 555 556
$smarty->assign('listpriorities', ['0','1','2','3','4','5','6','7','8','9']);
$smarty->assign('listprioritycolors', ['fff','fdd','fcc','fbb','faa','f99','e88','d77','c66','b66','a66']);
$smarty->assign('listroles', ['0' => '','1' => tra('required'),'2' => tra('optional'),'3' => tra('non-participant')]);
557 558


559
if ($prefs['feature_theme_control'] == 'y') {
rjsmelo's avatar
rjsmelo committed
560 561 562
	$cat_type = "calendar";
	$cat_objid = $calID;
	include('tiki-tc.php');
563 564
}

565
$headerlib->add_cssfile('themes/base_files/feature_css/calendar.css', 20);
566

567
$smarty->assign('referer', empty($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], 'tiki-calendar_edit_item.php') !== false ? 'tiki-calendar.php' : $_SERVER['HTTP_REFERER']);
568 569
$smarty->assign('myurl', 'tiki-calendar_edit_item.php');
$smarty->assign('id', $id);
570
$smarty->assign('hour_minmax', $hour_minmax);
571
if (isset($calitem['recurrenceId']) && $calitem['recurrenceId'] > 0) {
572
	$cr = new CalRecurrence($calitem['recurrenceId']);
changi67's avatar
changi67 committed
573
	$smarty->assign('recurrence', $cr->toArray());
574
}
changi67's avatar
changi67 committed
575
$headerlib->add_js(
576
	'function checkDateOfYear(day,month) {
rjsmelo's avatar
rjsmelo committed
577
		var mName = new Array("-","' . tra('January') . '","' . tra('February') . '","' . tra('March') . '","' . tra('April') . '","' . tra('May') . '","' . tra('June') . '","' . tra('July') . '","' . tra('August') . '","' . tra('September') . '","' . tra('October') . '","' . tra('November') . '","' . tra('December') . '}");
578 579 580 581 582 583 584 585
		var error = false;
		if (month == 4 || month == 6 || month == 9 || month == 11)
			if (day == 31)
				error = true;
			if (month == 2)
				if (day > 29)
					error = true;
			if (error) {
rjsmelo's avatar
rjsmelo committed
586
				document.getElementById("errorDateOfYear").innerHTML = "<em>' . tra('There\'s no such date as') . ' " + day + " ' . tra('of') . ' " + mName[month] + "</em>";
587 588 589 590
			} else {
				document.getElementById("errorDateOfYear").innerHTML = "";
			}
		}'
changi67's avatar
changi67 committed
591
);
592 593
$smarty->assign('calitem', $calitem);
$smarty->assign('calendar', $calendar);
594
$smarty->assign('calendarId', $calID);
595
$smarty->assign('preview', isset($_REQUEST['preview']));
596 597 598 599 600 601
if ($calitem['allday']) {
	$smarty->assign('hidden_if_all_day', ' style="display:none;"');
} else {
	$smarty->assign('hidden_if_all_day', '');
}

rjsmelo's avatar
rjsmelo committed
602
if (array_key_exists('CalendarViewGroups', $_SESSION) && count($_SESSION['CalendarViewGroups']) == 1) {
changi67's avatar
changi67 committed
603
	$smarty->assign('calendarView', $_SESSION['CalendarViewGroups'][0]);
rjsmelo's avatar
rjsmelo committed
604
}
605

lphuberdeau's avatar
lphuberdeau committed
606
$wikilib = TikiLib::lib('wiki');
607 608
$plugins = $wikilib->list_plugins(true, 'editwiki');
$smarty->assign_by_ref('plugins', $plugins);
changi67's avatar
changi67 committed
609
$smarty->assign('impossibleDates', $impossibleDates);
rjsmelo's avatar
rjsmelo committed
610
if (! empty($_REQUEST['fullcalendar'])) {
611 612 613 614 615
	$smarty->display('calendar.tpl');
} else {
	$smarty->assign('mid', 'tiki-calendar_edit_item.tpl');
	$smarty->display('tiki.tpl');
}