Esempio n. 1
0
function churchcal_getCCEventChangeImpact($newEvent, $pastEvent, $originEvent)
{
    $changes = array();
    $addCalChange = function (&$changes, $status, $startdate = null, $change = null) {
        $changes[] = array("status" => $status, "startdate" => $startdate->format("Y-m-d"), "changes" => $change);
    };
    $splitDate = new DateTime($newEvent["startdate"]);
    // 1. Get all Dates for the origin Event
    if ($originEvent != null) {
        $ds = getAllDatesWithRepeats((object) $originEvent, 0, 9999, $splitDate);
        if ($ds) {
            foreach ($ds as $d) {
                if (!dateInCCEvent($d, $newEvent)) {
                    // 1. Date is not in newEvent
                    if (!dateInCCEvent($d, $pastEvent)) {
                        // 2b. Deleted! Now for each booking make change entry
                        $addCalChange($changes, "deleted", $d);
                    }
                } else {
                    // 3. event is in newEvent, now check bookings!
                    $change = makeCCEventDiff(getOneEventOutOfSeries($originEvent, $d), getOneEventOutOfSeries($newEvent, $d));
                    if ($change != null) {
                        $addCalChange($changes, "updated", $d, $change);
                    }
                }
            }
        }
    }
    // Now do 4.
    $ds = getAllDatesWithRepeats((object) $newEvent, 0, 9999, $splitDate);
    if ($ds) {
        foreach ($ds as $d) {
            if (!dateInCCEvent($d, $originEvent)) {
                $change = makeCCEventDiff(null, $newEvent, $d);
                $addCalChange($changes, "new", $d, $change);
            }
        }
    }
    return $changes;
}
/**
 * Get booking changes
 * 1. Check from originEvent if this is in newEvent
 * 2a. If not in newEvent, if it is in pastEvent, everything fine. Nothing changed
 * 2b. If is not in newEvent and in pastEvent, then it was deleted!
 * 3. If is in newEvent, then diff the changes
 * 4. Check if newEvent is not in originEvent, then there is something new
 * @param [type] $originEvent
 * @param [type] $newEvent
 */
function churchresource_getEventChangeImpact($newEvent, $pastEvent, $originEvent)
{
    $changes = array();
    $addCRChange = function (&$changes, $booking, $status, $startdate = null, $change = null) {
        $resource = churchcore_getTableData("cr_resource", null, "id = " . $booking["resource_id"]);
        $booking["resource"] = $resource[$booking["resource_id"]]->bezeichnung;
        $changes[] = array("booking" => $booking, "status" => $status, "startdate" => $startdate->format("Y-m-d"), "changes" => $change);
    };
    $splitDate = new DateTime($newEvent["startdate"]);
    // 1. Get all Dates for the origin Event
    if ($originEvent != null) {
        $ds = getAllDatesWithRepeats((object) $originEvent, 0, 9999, $splitDate);
        if ($ds) {
            foreach ($ds as $d) {
                if (!dateInCCEvent($d, $newEvent)) {
                    // 1. Date is not in newEvent
                    if (dateInCCEvent($d, $pastEvent)) {
                        // 2a. in past Event, everything fine. Nothing to check, because in past is no changes in bookings!
                        if (isset($originEvent["bookings"])) {
                            foreach ($originEvent["bookings"] as $booking) {
                                //$addCRChange($changes, $booking, "no.change", $d);
                            }
                        }
                    } else {
                        // 2b. Deleted! Now for each booking make change entry
                        if (isset($originEvent["bookings"])) {
                            foreach ($originEvent["bookings"] as $booking) {
                                $addCRChange($changes, $booking, "deleted", $d);
                            }
                        }
                    }
                } else {
                    // 3. event is in newEvent, now check bookings!
                    if (isset($originEvent["bookings"])) {
                        foreach ($originEvent["bookings"] as $booking) {
                            if (empty($newEvent["bookings"])) {
                                $addCRChange($changes, $booking, "deleted");
                            } else {
                                $newBooking = findBookingInNewEvent($booking, $newEvent);
                                if ($newBooking != null) {
                                    $change = makeBookingDiff($booking, $newBooking, getOneEventOutOfSeries($originEvent, $d), getOneEventOutOfSeries($newEvent, $d));
                                    if ($change != null) {
                                        $addCRChange($changes, $booking, "updated", $d, $change);
                                    }
                                    //else $addCRChange($changes, $booking, "no.changes", $d, $change);
                                }
                                // This is currently not supported, cause delete is not possible. Only set Status to deleted.
                                //else $addCRChange($changes, $booking, "error", $d, $change);
                            }
                        }
                    }
                    // Perhaps new resource in booking
                    if (isset($newEvent["bookings"])) {
                        foreach ($newEvent["bookings"] as $booking) {
                            $originBooking = findBookingInOriginEvent($booking, $originEvent);
                            if ($originBooking == null) {
                                $addCRChange($changes, $booking, "new", $d);
                            }
                        }
                    }
                }
            }
        }
    }
    // Now do 4.
    if (isset($newEvent["bookings"])) {
        $ds = getAllDatesWithRepeats((object) $newEvent, 0, 9999, $splitDate);
        if ($ds) {
            foreach ($ds as $d) {
                if (!dateInCCEvent($d, $originEvent)) {
                    if (isset($newEvent["bookings"])) {
                        foreach ($newEvent["bookings"] as $booking) {
                            $addCRChange($changes, $booking, "new", $d);
                        }
                    }
                }
            }
        }
    }
    return $changes;
}