/**
 * 
 * @param unknown $params
 * @param unknown $source
 * @param unknown $changes arr["add_exception"], ...
 */
function churchresource_updateResourcesFromChurchCal($params, $source, $changes = null)
{
    global $user;
    $newbookingstatus = 1;
    $resources = churchcore_getTableData("cr_resource");
    $db = db_query('select * from {cr_booking} where cc_cal_id=:cal_id', array(":cal_id" => $params["id"]));
    $params["location"] = "";
    $params["note"] = "";
    foreach ($db as $booking) {
        if (isset($params["bookings"]) && isset($params["bookings"][$booking->resource_id])) {
            $save = array_merge(array(), $params);
            $save["cc_cal_id"] = $params["id"];
            if (!isset($params["bookings"][$booking->resource_id]["status_id"])) {
                $save["status_id"] = $newbookingstatus;
            } else {
                $save["status_id"] = $params["bookings"][$booking->resource_id]["status_id"];
            }
            $save["id"] = $booking->id;
            $save["person_id"] = $user->id;
            $save["resource_id"] = $booking->resource_id;
            // Wenn es ein gro�es Update ist, also nicht nur eine Verschiebung auf dem Kalender
            if (isset($params["repeat_id"])) {
                $save["text"] = $params["bezeichnung"];
            }
            $save["startdate"] = _shiftDate($save["startdate"], -$params["bookings"][$booking->resource_id]["minpre"]);
            $save["enddate"] = _shiftDate($save["enddate"], $params["bookings"][$booking->resource_id]["minpost"]);
            // Wenn es keine L�schung ist
            if ($save["status_id"] != 99) {
                // Wenn es einen Unterschied im Datum gibt, setze den Status wieder auf zu genehmigen!
                if (strtotime($save["startdate"]) != strtotime($booking->startdate) || strtotime($save["enddate"]) != strtotime($booking->enddate)) {
                    // But only if I am not an admin and resource is not autoaccept!
                    if (!user_access("administer bookings", "churchresource") && $resources[$booking->resource_id]->autoaccept_yn == 0) {
                        $save["status_id"] = 1;
                    }
                }
            }
            churchresource_updateBooking($save, $changes);
            $params["bookings"][$booking->resource_id]["updated"] = true;
        } else {
            if (!isset($params["bookings"]) && isset($params["cal_id"])) {
            }
        }
    }
    // Gehe nun noch die neuen Bookings durch, die nicht in der DB sind
    if (!isset($params["bookings"])) {
        return;
    }
    foreach ($params["bookings"] as $booking) {
        if (!isset($booking["updated"])) {
            $save = array_merge(array(), $params);
            $save["cc_cal_id"] = $params["id"];
            if (!isset($booking["status_id"])) {
                $save["status_id"] = $newbookingstatus;
            } else {
                $save["status_id"] = $booking["status_id"];
            }
            $save["person_id"] = $user->id;
            $save["resource_id"] = $booking["resource_id"];
            $save["text"] = $params["bezeichnung"];
            $save["startdate"] = _shiftDate($save["startdate"], -$params["bookings"][$booking["resource_id"]]["minpre"]);
            $save["enddate"] = _shiftDate($save["enddate"], $params["bookings"][$booking["resource_id"]]["minpost"]);
            churchresource_createBooking($save);
        }
    }
}
/**
 *
 * @param array $params
 * @param string $source
 * @param array $changes arr["add_exception"], ...
 */
function churchresource_operateResourcesFromChurchCal($params)
{
    global $user;
    $newBookingStatus = 1;
    $resources = churchcore_getTableData("cr_resource");
    $bookings = db_query('SELECT * FROM {cr_booking}
                  WHERE cc_cal_id=:cal_id', array(":cal_id" => $params["id"]));
    $params["location"] = "";
    $params["note"] = "";
    foreach ($bookings as $booking) {
        if (isset($params["bookings"]) && isset($params["bookings"][$booking->id])) {
            $save = copyTypicalDateFields($params);
            foreach ($params["bookings"][$booking->id] as $key => $val) {
                $save[$key] = $val;
            }
            $save["cc_cal_id"] = $params["id"];
            if (!isset($params["bookings"][$booking->id]["status_id"])) {
                $save["status_id"] = $newbookingstatus;
            } else {
                $save["status_id"] = $params["bookings"][$booking->id]["status_id"];
            }
            $save["id"] = $booking->id;
            if (!empty($params["modified_pid"])) {
                $save["person_id"] = $params["modified_pid"];
            }
            if (!isset($save["resource_id"])) {
                $save["resource_id"] = $booking->resource_id;
            }
            $save["text"] = $params["bezeichnung"];
            $save["startdate"] = _shiftDate($save["startdate"], -$params["bookings"][$booking->id]["minpre"]);
            $save["enddate"] = _shiftDate($save["enddate"], $params["bookings"][$booking->id]["minpost"]);
            // if not to delete
            if ($save["status_id"] != CR_DELETED) {
                // on date change set status to need confirmation!
                if (strtotime($save["startdate"]) != strtotime($booking->startdate) || strtotime($save["enddate"]) != strtotime($booking->enddate)) {
                    // But only if I am not an admin and resource is not autoaccept!
                    if (!user_access("administer bookings", "churchresource") && $resources[$booking->id]->autoaccept_yn == 0) {
                        $save["status_id"] = CR_PENDING;
                    }
                }
            }
            churchresource_updateBooking($save, false);
            $params["bookings"][$booking->id]["updated"] = true;
        }
    }
    // Gehe nun noch die neuen Bookings durch, die nicht in der DB sind
    if (!isset($params["bookings"])) {
        return;
    }
    $newIds = array();
    foreach ($params["bookings"] as $oldbookingid => $booking) {
        if (!isset($booking["updated"])) {
            $save = array_merge(array(), $params);
            // Additions and exceptions will be handled from calendar
            unset($save["additions"]);
            unset($save["exceptions"]);
            foreach ($booking as $key => $val) {
                $save[$key] = $val;
            }
            $save["cc_cal_id"] = $params["id"];
            $save["status_id"] = isset($booking["status_id"]) ? $booking["status_id"] : $newBookingStatus;
            $save["person_id"] = empty($params["modified_pid"]) ? $user->id : $params["modified_pid"];
            $save["text"] = $params["bezeichnung"];
            $save["startdate"] = _shiftDate($save["startdate"], -$booking["minpre"]);
            $save["enddate"] = _shiftDate($save["enddate"], $booking["minpost"]);
            $arr = churchresource_createBooking($save, false);
            $newIds[$oldbookingid] = $arr["id"];
        }
    }
    return $newIds;
}
 public function createBooking($params)
 {
     include_once './' . CHURCHRESOURCE . '/churchresource_db.php';
     return churchresource_createBooking($params);
 }