Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 60 additions & 25 deletions familyconnections/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ function displayAddSubmit ()
$repeat = null;
$private = 0;
$invite = 0;
$date = isset($_POST['date']) ? strip_tags($_POST['date']) : '';
$title = isset($_POST['title']) ? strip_tags($_POST['title']) : '';
$desc = isset($_POST['desc']) ? strip_tags($_POST['desc']) : '';
$category = isset($_POST['category']) ? strip_tags($_POST['category']) : '';
$frequency = '';
$endDate = isset($_POST['date-end']) ? strip_tags($_POST['date-end']) : '';
$untilDate = isset($_POST['repeat-until']) ? strip_tags($_POST['repeat-until']) : '';

if (isset($_POST['timestart']) and !isset($_POST['all-day']))
{
Expand All @@ -286,6 +293,10 @@ function displayAddSubmit ()
{
$repeat = 'yearly';
}
elseif (isset($_POST['repeat-frequency']))
{
$frequency = strip_tags($_POST['repeat-frequency']);
}
if (isset($_POST['private']))
{
$private = 1;
Expand All @@ -295,11 +306,26 @@ function displayAddSubmit ()
$invite = 1;
}

// Can't make a yearly event also an invitation
if ($repeat == 'yearly')
{
$endDate = $date;
$frequency = '';
}

$dates = $this->fcmsCalendar->getExpandedEventDates($date, $endDate, $frequency, $untilDate);
if (count($dates) <= 0)
{
$this->displayHeader();
loadTemplate('calendar', 'add', array('error' => T_('Invalid Date.')));
$this->displayFooter();
return;
}

// Can't make a repeating or multi-day event also an invitation.
$notify_user_changed_event = 0;
if ($repeat == 'yearly' && $invite == 1)
if (($repeat == 'yearly' || count($dates) > 1) && $invite == 1)
{
// Let's turn off the invitation, submit the event and tell the user what we did
// Let's turn off the invitation, submit the event and tell the user what we did.
$invite = 0;
$notify_user_changed_event = 1;
}
Expand All @@ -310,33 +336,42 @@ function displayAddSubmit ()
)
VALUES (?, ?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?)";

$params = array(
$_POST['date'],
$timeStart,
$timeEnd,
$_POST['title'],
$_POST['desc'],
$this->fcmsUser->id,
$_POST['category'],
$repeat,
$private,
$invite
);
$eventId = false;
foreach ($dates as $date)
{
$params = array(
$date,
$timeStart,
$timeEnd,
$title,
$desc,
$this->fcmsUser->id,
$category,
$repeat,
$private,
$invite
);

$id = $this->fcmsDatabase->insert($sql, $params);
$insertedId = $this->fcmsDatabase->insert($sql, $params);

if ($id === false)
{
$this->displayHeader();
$this->fcmsError->displayError();
$this->displayFooter();
return;
if ($insertedId === false)
{
$this->displayHeader();
$this->fcmsError->displayError();
$this->displayFooter();
return;
}

if ($eventId === false)
{
$eventId = $insertedId;
}
}

// Display the invitation screen
if ($invite == 1)
{
header("Location: calendar.php?invite=$id");
header("Location: calendar.php?invite=$eventId");
return;
}

Expand All @@ -353,7 +388,7 @@ function displayAddSubmit ()
'header' => T_('You cannot invite guests to a repeating event.'),
'errors' => array(
T_('Your event was created, but no invitations were sent.'),
T_('Please create a new non-repeating event and invite guests to that.'),
T_('Please create a new single-day non-repeating event and invite guests to that.'),
),
);
}
Expand All @@ -362,7 +397,7 @@ function displayAddSubmit ()
displayOkMessage();
}

$this->fcmsCalendar->displayEvent($id, $templateParams);
$this->fcmsCalendar->displayEvent($eventId, $templateParams);
$this->displayFooter();
}

Expand Down
99 changes: 99 additions & 0 deletions familyconnections/inc/calendar_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,105 @@ function getEvents ($month, $day, $year)
return $events;
}

/**
* getExpandedEventDates
*
* Builds the list of dates to create for multi-day and recurring events.
*
* @param string $startDate
* @param string $endDate
* @param string $repeat
* @param string $repeatUntil
*
* @return array
*/
function getExpandedEventDates ($startDate, $endDate = '', $repeat = '', $repeatUntil = '')
{
$start = $this->getCalendarTimestamp($startDate);
if ($start === false)
{
return array();
}

$end = empty($endDate) ? $start : $this->getCalendarTimestamp($endDate);
if ($end === false || $end < $start)
{
return array();
}

$repeat = strtolower(trim($repeat));
if (!in_array($repeat, array('daily', 'weekly', 'monthly')))
{
$repeat = '';
}

$durationDays = (int)(($end - $start) / 86400);
$dates = array();

if (empty($repeat))
{
for ($date = $start; $date <= $end; $date = strtotime('+1 day', $date))
{
$dates[] = date('Y-m-d', $date);
}

return $dates;
}

$until = $this->getCalendarTimestamp($repeatUntil);
if ($until === false || $until < $start)
{
return array();
}

$intervals = array(
'daily' => '+1 day',
'weekly' => '+1 week',
'monthly' => '+1 month',
);
$interval = $intervals[$repeat];
for ($date = $start; $date <= $until; $date = strtotime($interval, $date))
{
for ($i = 0; $i <= $durationDays; $i++)
{
$eventDate = strtotime("+$i day", $date);
if ($eventDate <= $until)
{
$dates[] = date('Y-m-d', $eventDate);
}
}
}

return array_values(array_unique($dates));
}

/**
* getCalendarTimestamp
*
* @param string $date
*
* @return int|boolean
*/
private function getCalendarTimestamp ($date)
{
if (!preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $date))
{
return false;
}

list($year, $month, $day) = explode('-', $date);
$year = (int)$year;
$month = (int)$month;
$day = (int)$day;

if (!checkdate($month, $day, $year))
{
return false;
}

return mktime(0, 0, 0, $month, $day, $year);
}

/**
* displayAddForm
*
Expand Down
23 changes: 23 additions & 0 deletions familyconnections/ui/themes/default/templates/calendar/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@

<div id="more-cal-info">
<div id="cal-details">
<div class="field-row">
<div class="field-label">
<label for="date-end"><b>End Date</b></label>
</div>
<div class="field-widget">
<input type="text" id="date-end" name="date-end" size="10">
</div>
</div>
<div class="field-row">
<div class="field-label">
<label for="category"><b><?php echo $TMPL['categoryText']; ?></b></label>
Expand All @@ -65,6 +73,21 @@
</select>
</div>
</div>
<div class="field-row">
<div class="field-label">
<label for="repeat-frequency"><b>Repeat</b></label>
</div>
<div class="field-widget">
<select id="repeat-frequency" name="repeat-frequency">
<option value="">None</option>
<option value="daily">Daily</option>
<option value="weekly">Weekly</option>
<option value="monthly">Monthly</option>
</select>
Repeat Until
<input type="text" id="repeat-until" name="repeat-until" size="10">
</div>
</div>
<div class="field-row">
<div class="field-widget">
<input type="checkbox" name="repeat-yearly" id="repeat-yearly"/>
Expand Down