Exemplo n.º 1
0
/**
 * called by ChurchCal on changes in calendar events
 * it uses old_startdate to move all events, then checks if there are events to create or delete
 * from changes in repeats, exceptions or additions
 * @param array $params
 * @param string $source
 */
function FromChurchCal($params, $csevent)
{
    echo "NOT USED ANYMORE FromChurchCal ";
    $diff = null;
    if (isset($params["old_startdate"])) {
        // move events to new startdate
        $startdate = new DateTime($params["startdate"]);
        $old_startdate = new DateTime($params["old_startdate"]);
        $diff = $startdate->format("U") - $old_startdate->format("U");
        db_query("UPDATE {cs_event} SET startdate = DATEADD(startdate, INTERVAL :diff SECOND)\n              WHERE e.cc_cal_id = :cal_id", array(":cal_id" => $params["cal_id"]));
    }
    // without repeat_id, this is only a time shift, so we can end processing here.
    if (empty($params["repeat_id"])) {
        return;
    }
    // Collect events into array to collect the info which has to be created/deleted/updated
    $events = array();
    // Get all mapped events from DB
    $db = db_query("SELECT id, startdate FROM {cs_event} e\n                  WHERE e.cc_cal_id=:cal_id", array(":cal_id" => $params["event"]["id"]));
    foreach ($db as $e) {
        $sd = new DateTime($e->startdate);
        $events[$sd->format('Y-m-d')] = array("status" => "delete", "id" => $e->id);
    }
    $o = _convertCTDateTimeToObjects($params);
    foreach (getAllDatesWithRepeats($o, -1000, +1000) as $d) {
        $sd = $d->format('Y-m-d');
        // Event was already moved above through old_startdate
        if (isset($events[$sd])) {
            $events[$sd]["status"] = "ok";
        } else {
            $events[$sd] = array("status" => "create");
        }
        $events[$sd]["startdate"] = $d->format('Y-m-d H:i:s');
    }
    $template = null;
    if (isset($params["eventTemplate"])) {
        $template = $params["eventTemplate"];
    }
    foreach ($events as $key => $do) {
        if ($do["status"] == "delete") {
            $params["id"] = $do["id"];
            $params["informDeleteEvent"] = 1;
            $params["deleteCalEntry"] = 0;
            churchservice_deleteEvent($params, $source);
        } else {
            if ($do["status"] == "create" && $template != null) {
                $params["startdate"] = $do["startdate"];
                $params["event"]["cc_cal_id"] = $params["event"]["id"];
                $params["eventTemplate"] = $template;
                churchservice_createEvent($params, $source);
            }
        }
    }
}
function churchservice_updateEventFromChurchCal($params, $source = null)
{
    $diff = null;
    // When $params["old_startdate"] is set, first the events are moved
    // to the diff of startdate-old_startdate
    if (isset($params["old_startdate"])) {
        $startdate = new DateTime($params["startdate"]);
        $old_startdate = new DateTime($params["old_startdate"]);
        $diff = $startdate->format("U") - $old_startdate->format("U");
        $db = db_query("select id, startdate from {cs_event} e where e.cc_cal_id=:cal_id", array(":cal_id" => $params["cal_id"]));
        foreach ($db as $e) {
            $sd = new DateTime($e->startdate);
            $sd->modify("+{$diff} seconds");
            db_update("cs_event")->fields(array("startdate" => $sd->format('Y-m-d H:i:s')))->condition('id', $e->id, "=")->execute();
        }
    }
    // When repeat_id is not given, this is only a time shift. So we can end the processing here.
    if (!isset($params["repeat_id"])) {
        return;
    }
    // Collect events in one array to collect the info which has to be created/deleted/updated
    $events = array();
    // Get all mapped events from DB
    $db = db_query("select id, startdate from {cs_event} e where e.cc_cal_id=:cal_id", array(":cal_id" => $params["cal_id"]));
    foreach ($db as $e) {
        $sd = new DateTime($e->startdate);
        $events[$sd->format('Y-m-d')] = array("status" => "delete", "id" => $e->id);
    }
    $o = _convertCTDateTimeToObjects($params);
    foreach (getAllDatesWithRepeats($o, -1000, +1000) as $d) {
        $sd = $d->format('Y-m-d');
        if (isset($events[$sd])) {
            // Event was already moved above through old_startdate
            $events[$sd]["status"] = "ok";
        } else {
            $events[$sd] = array("status" => "create");
        }
        $events[$sd]["startdate"] = $d->format('Y-m-d H:i:s');
    }
    $template = null;
    if (isset($params["eventTemplate"])) {
        $template = $params["eventTemplate"];
    }
    foreach ($events as $key => $do) {
        if ($do["status"] == "delete") {
            $params["id"] = $do["id"];
            $params["informDeleteEvent"] = 1;
            $params["deleteCalEntry"] = 0;
            churchservice_deleteEvent($params, $source);
        } else {
            if ($do["status"] == "create" && $template != null) {
                $params["id"] = null;
                $params["startdate"] = $do["startdate"];
                $params["eventTemplate"] = $template;
                churchservice_saveEvent($params, $source);
            }
        }
    }
}