Skip to content

Commit

Permalink
Changed/fixed check for "only future events"
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGausi committed Dec 1, 2018
1 parent f3e57d1 commit b3d207e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
50 changes: 48 additions & 2 deletions src/Resources/contao/modules/CEAuthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,48 @@
* (c) Leo Feyer, LGPL-3.0-or-later
*
*/

// Get a timestamp for midnight, today
function MidnightTime() {
return mktime(0,0,0,date("m"),date("d"),date("Y"));
}

// Check, whether the event time (defined by start/end date) is "in the past"
// this means here: It ends before the current day
// Therefore, if an event ends at 1:00am, it can be edited up to 23 hours after it expires
// Dates are timestamps here, not Strings
// returns TRUE if the Dates are not elapsed (that long) and therfore can be edited by the user,
// even if "only future events" is enabled
//
// used in the editor to validate Userinput
function DateIsNotElapsed($StartDate, $EndDate) {
if (!$EndDate) {
$EndDate = $StartDate;
}
return MidnightTime() <= $EndDate;
}

// the same as above, but with events, not single dates
//
// used in GetAllEvents Hook,
// "Midnight" is required often there, therefore an extra parameter for this
function EventIsNotElapsed($aEvent, $currentMidnight) {
if (!$aEvent['endTime']) {
return $currentMidnight <= $aEvent['startTime'];
} else {
return $currentMidnight <= $aEvent['endTime'];
}
}

// used in Module ModuleEventReaderEdit
// midnight is used only once, therefore no extra parameter for this.
function EventIsNotElapsed2($objEvent) {
if (!$objEvent->endTime) {
return MidnightTime() <= $objEvent->startTime;
} else {
return MidnightTime() <= $objEvent->endTime;
}
}

// Check, whether the User is an Admin to add/edit Events in this objCalendar
function UserIsAdmin($objCalendar, $User){
Expand Down Expand Up @@ -94,7 +136,9 @@ function EditLinksAreAllowed ($objCalendar, $aEvent, $userID, $UserIsAdmin, $Use
// Allow only if FE User is logged in or the calendar does not requie login
&& ( FE_USER_LOGGED_IN || !$objCalendar->caledit_loginRequired)
// Allow only if CalendarEditing is not restricted to future events -OR- EventTime is later then CurrentTime,
&& ((!$objCalendar->caledit_onlyFuture) || ($currentTime <= $aEvent['startTime']) )
// && ((!$objCalendar->caledit_onlyFuture) || ($currentTime <= $aEvent['startTime']) )

&& ((!$objCalendar->caledit_onlyFuture) || (EventIsNotElapsed($aEvent, $currentTime)) )
// Allow only if CalendarEditing is not restricted to the Owner -OR- The Owner is currently logged in
&& ((!$objCalendar->caledit_onlyUser) || ($aEvent['fe_user'] == $userID))
);
Expand All @@ -119,7 +163,9 @@ function EditLinksAreAllowed2 ($objCalendar, $objEvent, $User, $UserIsAdmin, $Us
// Allow only if FE User is logged in or the calendar does not requie login
&& ( FE_USER_LOGGED_IN || !$objCalendar->caledit_loginRequired)
// Allow only if CalendarEditing is not restricted to future events -OR- EventTime is later then CurrentTime,
&& ((!$objCalendar->caledit_onlyFuture) || (time() <= $objEvent->startTime) )
//&& ((!$objCalendar->caledit_onlyFuture) || (time() <= $objEvent->startTime) )
&& ((!$objCalendar->caledit_onlyFuture) || (EventIsNotElapsed2($objEvent)) )

// Allow only if CalendarEditing is not restricted to the Owner -OR- The Owner is currently logged in
&& ((!$objCalendar->caledit_onlyUser) || ($objEvent->fe_user == $User->id))
);
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/contao/modules/ListAllEvents_Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function updateAllEvents($arrEvents, $arrCalendars, $intStart, $intEnd, $
}

// now: scan the events-array and add edit links where appropriate
$currentTime = time();
$currentTime = MidnightTime();
foreach ($arrEvents as &$intnext) {
foreach ($intnext as &$intdate) {
foreach ($intdate as &$aEvent){
Expand Down
32 changes: 23 additions & 9 deletions src/Resources/contao/modules/ModuleEventEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,29 @@ public function UserIsToAddCalendar($user, $pid) {
}
}

public function checkValidDate($calendarID, $newDate, $objWidget) {
public function checkValidDate($calendarID, $objStart, $objEnd) {
$objCalendar = $this->getCalendarObjectFromPID($calendarID);
if (NULL === $objCalendar) {
return false;
}
}
$tmpStartDate = strtotime($objStart->__get('value'));
$tmpEndDate = strtotime($objEnd->__get('value'));

if ((!$objCalendar->caledit_onlyFuture) || UserIsAdmin($objCalendar, $this->User)) {
// elapsed events can be edited, or user is an admin
return true;
} else {
// editing elapsed events is denied and user is not an admin
$isValid = ($newDate >= time());
//$isValid = ($newDate >= time());
$isValid = DateIsNotElapsed($tmpStartDate, $tmpEndDate);
if (!$isValid) {
$objWidget->addError($GLOBALS['TL_LANG']['MSC']['caledit_formErrorElapsedDate']);
if (!$tmpEndDate && (MidnightTime() > $tmpStartDate)) {
$objStart->addError($GLOBALS['TL_LANG']['MSC']['caledit_formErrorElapsedDate']);
}
if ($tmpEndDate && (MidnightTime() > $tmpEndDate)) {
$objEnd->addError($GLOBALS['TL_LANG']['MSC']['caledit_formErrorElapsedDate']);
}
//$objWidget->addError($GLOBALS['TL_LANG']['MSC']['caledit_formErrorElapsedDate']);
}
return $isValid;
}
Expand Down Expand Up @@ -240,7 +249,11 @@ public function checkUserEditRights($user, $eventID, $CurrentObjectData) {
}

$userIsAdmin = UserIsAdmin($objCalendar, $user);
if (!$userIsAdmin && ($CurrentObjectData->startTime <= time()) && ($objCalendar->caledit_onlyFuture)){
//if (!$userIsAdmin && ($CurrentObjectData->startTime <= time()) && ($objCalendar->caledit_onlyFuture)){
if (!$userIsAdmin
&& (!DateIsNotElapsed($CurrentObjectData->startTime, $CurrentObjectData->endTime ))
//($CurrentObjectData->startTime <= time())
&& ($objCalendar->caledit_onlyFuture)){
$this->ErrorString = $GLOBALS['TL_LANG']['MSC']['caledit_NoPast'];
return false;
}
Expand Down Expand Up @@ -886,12 +899,13 @@ protected function HandleEdit($editID, $currentEventObject) {
$arrWidgets['startDate']->parse();
$arrWidgets['endDate']->parse();
}



// Check, whether the user is allowed to edit past events
// or the date is in the future
$newDate = strtotime($arrWidgets['startDate']->__get('value'));
$validDate = $this->checkValidDate($NewEventData['pid'], $newDate, $arrWidgets['startDate']);
//$tmpStartDate = strtotime($arrWidgets['startDate']->__get('value'));
//$tmpEndDate = strtotime($arrWidgets['endDate']->__get('value'));

$validDate = $this->checkValidDate($NewEventData['pid'], $arrWidgets['startDate'], $arrWidgets['endDate']);
if (!$validDate) {
// modification of the widget is done in checkValidDate
$doNotSubmit = true;
Expand Down

0 comments on commit b3d207e

Please sign in to comment.