/**
  * Load item $params["id"] with calcategory_id and template_yn from agenda
  *
  * @param array $params
  * @throws CTException - When item not found
  * @throws CTNoPermission - When not allowed to edit or view
  * @return item
  */
 public function loadItem($params)
 {
     $db = db_query("SELECT i.*, a.calcategory_id, a.template_yn FROM {cs_item} i, {cs_agenda} a WHERE i.agenda_id=a.id\n            and i.id=:id", array(":id" => $params["id"]))->fetch();
     if (!$db) {
         throw new CTException("Item not found!");
     }
     $auth = churchservice_getAuthorization();
     if (empty($auth["view agenda"][$db->calcategory_id]) && empty($auth["edit agenda"][$db->calcategory_id])) {
         throw new CTNoPermission("view agenda", "churchservice");
     }
     return $db;
 }
/**
 * update event service
 * 
 * @param array $params
 * @throws CTNoPermission
 * @return array
 */
function churchservice_updateEventService($params)
{
    global $user, $base_url;
    $id = $params["id"];
    $name = isset($params["name"]) ? $params["name"] : null;
    $cdb_person_id = isset($params["cdb_person_id"]) ? $params["cdb_person_id"] : null;
    $reason = isset($params["reason"]) ? $params["reason"] : null;
    $zugesagt_yn = $params["zugesagt_yn"];
    include_once CHURCHSERVICE . "/churchservice_db.php";
    $res = array();
    if ($name == "null") {
        $name = null;
    }
    if ($cdb_person_id == "null") {
        $cdb_person_id = null;
    }
    // look if event is still valid
    $arr = db_query("SELECT * FROM {cs_eventservice} WHERE id=:id", array(":id" => $id))->fetch();
    if (!$arr) {
        return "Entry not found, id not valid!";
    }
    if ($arr->valid_yn != 1 && !isset($params["valid_yn"])) {
        return "Eintrag konnte nicht angepasst werden, da veraltet. Bitte neu laden!";
    }
    // check auth
    $auth = churchservice_getAuthorization();
    // Es ist trotzdem erlaubt, wenn die PersonId eingetragen ist, dann wurde er ja angefragt
    if (!isset($auth["editservice"][$arr->service_id]) && !isset($auth["memberservice"][$arr->service_id]) && !churchService_adminOfEvent($arr->event_id) && $arr->cdb_person_id != $user->id) {
        throw new CTNoPermission("editservice", "churchservice");
    }
    // Wenn die neue �nderung vom gleichen User kommt und noch kein Cron gelaufen ist,
    // Oder wenn valid_yn valide ist, denn dann soll es upgedates werden!
    // brauchen wir kein neuen Insert, sondern machen nur ein Update.
    // Denn wahrscheinlich war es vorher nur ein Versehen.
    // TODO: translation correct?
    // if changing user is the same as last time and cron had not yet run
    // or if valid_yn is valide (update wished), we dont need an insert, only an update,
    // because the last edit probably was a mistake
    $dt = new datetime();
    if ($arr->modified_pid == $user->id && $arr->mailsenddate == null || isset($params["valid_yn"])) {
        $valid_yn = 1;
        if (isset($params["valid_yn"])) {
            $valid_yn = $params["valid_yn"];
        }
        db_update("cs_eventservice")->fields(array("name" => $name, "cdb_person_id" => $cdb_person_id, "valid_yn" => $valid_yn, "zugesagt_yn" => $zugesagt_yn, "reason" => $reason, "mailsenddate" => null, "modified_date" => $dt->format('Y-m-d H:i:s'), "modified_pid" => $user->id))->condition("id", $id, "=")->execute();
        $new_id = $id;
    } else {
        // new entry for edit
        $new_id = db_insert("cs_eventservice")->fields(array("event_id" => $arr->event_id, "service_id" => $arr->service_id, "valid_yn" => 1, "counter" => $arr->counter, "name" => $name, "cdb_person_id" => $cdb_person_id, "zugesagt_yn" => $zugesagt_yn, "reason" => $reason, "modified_date" => $dt->format('Y-m-d H:i:s'), "modified_pid" => $user->id))->execute();
        //if all ok set existing entry to old
        db_update("cs_eventservice")->fields(array("valid_yn" => 0))->condition("id", $id, "=")->execute();
    }
    include_once CHURCHCORE . "/churchcore_db.php";
    $leader = churchcore_getPersonById($arr->modified_pid);
    $event = db_query("SELECT e.startdate datum, c.bezeichnung FROM {cs_event} e, {cc_cal} c\n                     WHERE e.cc_cal_id=c.id and e.id=:event_id", array(":event_id" => $arr->event_id))->fetch();
    $service = churchcore_getTableData("cs_service", "", "id=" . $arr->service_id);
    if ($event && $service) {
        $service = $service[$arr->service_id];
        $subject = "[" . readConf('site_name', "ChurchTools") . "] ";
        $txt = "";
        // confirm
        if ($zugesagt_yn == 1) {
            $txt .= t("surname.name.has.approved.name.for.service.x.for.date.event", $user->vorname, $user->name, $service->bezeichnung, $event->datum, $event->bezeichnung, $name);
            $subject .= t("surname.name.has.approved.a.request", $user->vorname, $user->name);
        } else {
            if ($name) {
                $txt .= t("surname.name.has.proposed.name.for.service.x.for.date.event", $user->vorname, $user->name, $service->bezeichnung, $event->datum, $event->bezeichnung, $name);
                $subject .= t("surname.name.has.proposed.someone", $user->vorname, $user->name);
            } else {
                $txt .= t("surname.name.has.canceled.the.service.x.for.date.event", $user->vorname, $user->name, $service->bezeichnung, $event->datum, $event->bezeichnung);
                $subject .= t("surname.name.has.canceled.a.request", $user->vorname, $user->name);
            }
        }
        if ($reason != null) {
            $txt .= "<p>Folgendes wurde als Grund angegeben: " . $reason;
        }
        ct_notify("service", $arr->service_id, $txt);
        if ($leader != null) {
            // send mail, if someone other then the inquirer himself confirmed or canceled
            //TODO: maybe use asker, better to understand for nonenglish programmers
            //TODO: use email template
            if (!empty($leader->email) && $user != null && $leader->id != $user->id) {
                $setting = churchcore_getUserSettings("churchservice", $leader->id);
                if (isset($setting["informInquirer"]) && $setting["informInquirer"] == 1) {
                    $txt = "<h3>Hallo " . $leader->vorname . ",</h3><p>\n                 " . $txt;
                    $txt .= '<p><a href="' . $base_url . '?q=churchservice&id=' . $arr->event_id . '" class="btn btn-primary">Event aufrufen</a>';
                    churchservice_send_mail($subject, $txt, $leader->email);
                }
            }
            if (!isset($setting["informInquirer"])) {
                churchcore_saveUserSetting("churchservice", $leader->id, "informInquirer", 0);
            }
        }
    }
    $arr = db_query("SELECT es.*, concat(p.vorname,' ',p.name) as modifieduser FROM {cs_eventservice} es, {cdb_person} p \n                    WHERE p.id=es.modified_pid and es.id=:id", array(":id" => $new_id))->fetch();
    $res["eventservice"] = churchservice_extractEventServiceData($arr);
    $res["result"] = true;
    return $res;
}