public function addNewTag($params)
 {
     global $user;
     $dt = new DateTime();
     $new_id = db_insert('cdb_tag')->fields(array("bezeichnung" => $params["bezeichnung"], "letzteaenderung" => $dt->format('Y-m-d H:i:s'), "aenderunguser" => $user->cmsuserid))->execute();
     return $new_id;
     cdb_log("addNewTag: " . $params["bezeichnung"], 2, $new_id, CDB_LOG_TAG);
     // never executed!
 }
/**
 * unsubscribe all persons in $batch from $list_id
 * 
 * @param string $api
 * @param string $list_id
 * @param array $batch
 * @param bool $send_goodbye
 * @param bool $send_notify
 */
function listBatchUnsubscribe($api, $list_id, $batch, $send_goodbye = false, $send_notify = false)
{
    if (count($batch) == 0) {
        return;
    }
    $delete_member = false;
    // flag to completely delete the member from your list instead of just unsubscribing, default to false
    $vals = $api->listBatchUnsubscribe($list_id, $batch, $delete_member, $send_goodbye, $send_notify);
    include_once "churchdb_db.php";
    if ($api->errorCode) {
        cdb_log("CRON - Fehler beim Unsubscribe zu MailChimp: Code=" . $api->errorCode . " Msg=" . $api->errorMessage, 2);
    } else {
        cdb_log("CRON - MailChimp-Liste {$list_id}: Entferne " . count($batch) . " Personen.", 2);
    }
}
/**
 * 
 * @param unknown $params
 * @throws CTFail
 */
function f_functions($params)
{
    $function = $params["func"];
    $fields = getAllFields("intern_code='{$function}'");
    // Prüfe, ob eine E_Mail-Adresse gesetz werden soll die es schon gibt ohne Schreibrechte.
    // Das soll verhindert werden, denn sonst kann sich jemand die E-Mail eines Admins geben...
    if (isset($params["email"]) && !user_access("write access", "churchdb")) {
        // Check, if the email address has changed
        $db = db_query("SELECT * FROM {cdb_person} p WHERE id=:id", array(":id" => $params["id"]))->fetch();
        if ($db->email != $params["email"]) {
            // Check, if another user has this email
            $db = db_query("SELECT * FROM {cdb_person} p WHERE email=:email AND id!=:id", array(":email" => $params["email"], ":id" => $params["id"]))->fetch();
            if ($db != false) {
                throw new CTFail("Die E-Mail-Adresse ist schon vergeben. Um eine vergebene E-Mail-Adresse zu setzen werden mehr Rechte gebraucht.");
            }
        }
    }
    if ($function == "f_group") {
        saveGeocodeGruppe($params["id"], "", "");
    }
    foreach ($fields["fields"] as $key => $value) {
        if (isset($params[$key])) {
            $arr[$key] = $params[$key];
        }
    }
    // Wenn die letzteaenderung mit �bergeben wird (z.B. bei Sync mit externen Tools)
    // Soll das hier mit gesetzt werden
    if (isset($params['letzteaenderung'])) {
        $arr['letzteaenderung'] = $params['letzteaenderung'];
    }
    $oldarr = saveDataArray($fields, $params["id"], $arr);
    if (is_string($oldarr)) {
        $res = $oldarr;
    } else {
        $txt = churchcore_getFieldChanges($fields["fields"], $oldarr, $arr);
        if ($txt) {
            if ($function == "f_group") {
                $txt = t("group") . ": " . $arr["bezeichnung"] . "\n" . $txt;
            } else {
                $details = churchdb_getPersonDetails($params["id"]);
                //var_dump($details);
                //        $txt = t("person").": ". $details->vorname. " ". $details->name. " (". $params["id"]. ")\n". $txt;
            }
        }
        sendFieldNotifications($function, $txt);
        if ($txt) {
            cdb_log("{$function} - " . $txt, 2, $params["id"], $function == "f_group" ? CDB_LOG_GROUP : CDB_LOG_PERSON, 1);
        }
    }
}
/**
 *
 * @param unknown $params
 * @throws CTFail
 */
function f_functions($params)
{
    $function = $params["func"];
    $fields = getAllFields("intern_code = '{$function}'");
    // Check if someone try to set an existing email, but have no administer persons
    // otherwise someone could use the email of an admin...
    if (isset($params["email"]) && !user_access("administer persons", "churchcore")) {
        // Check, if the email address has changed
        $db = db_query("SELECT id, email FROM {cdb_person} p\n                    WHERE id=:id", array(":id" => $params["id"]))->fetch();
        if ($db->email != $params["email"]) {
            // Check if other user have more permissions than this one, than don't allow to set email
            $newUserPerms = getUserAuthorization($params["id"]);
            $db = db_query("SELECT id FROM {cdb_person} p\n                    WHERE email=:email", array(":email" => $params["email"]));
            $morePermissions = false;
            foreach ($db as $p) {
                $otherUserPerms = getUserAuthorization($p->id);
                if (hasMorePerms($newUserPerms, $otherUserPerms)) {
                    $morePermissions = true;
                }
            }
            if ($morePermissions) {
                throw new CTFail(t('email.already.used.you.need.more.rights.to.change.this'));
            }
        }
    }
    if ($function == "f_group") {
        saveGeocodeGruppe($params["id"], "", "");
    }
    $arr = array();
    foreach ($fields["fields"] as $key => $value) {
        if (isset($params[$key])) {
            $arr[$key] = $params[$key];
        }
    }
    // Wenn die letzteaenderung mit �bergeben wird (z.B. bei Sync mit externen Tools)
    // Soll das hier mit gesetzt werden
    if (isset($params['letzteaenderung'])) {
        $arr['letzteaenderung'] = $params['letzteaenderung'];
    }
    $oldarr = saveDataArray($fields, $params["id"], $arr);
    if (is_string($oldarr)) {
        $res = $oldarr;
    } else {
        $txt = churchcore_getFieldChanges($fields["fields"], $oldarr, $arr);
        if ($txt) {
            if ($function == "f_group") {
                $txt = t("group") . ": " . $arr["bezeichnung"] . "\n" . $txt;
            } else {
                $details = churchdb_getPersonDetails($params["id"]);
                //var_dump($details);
                //        $txt = t("person").": ". $details->vorname. " ". $details->name. " (". $params["id"]. ")\n". $txt;
            }
        }
        sendFieldNotifications($function, $txt);
        if ($txt) {
            cdb_log("{$function} - " . $txt, 2, $params["id"], $function == "f_group" ? CDB_LOG_GROUP : CDB_LOG_PERSON, 1);
        }
    }
}
/**
 * save fact
 * @param unknown $params
 */
function churchservice_saveFact($params)
{
    global $user;
    $event_id = $params["event_id"];
    $fact_id = $params["fact_id"];
    $value = $params["value"];
    $dt = new datetime();
    if ($value == "") {
        db_query("DELETE FROM {cs_event_fact} WHERE event_id={$event_id} and fact_id={$fact_id}");
    } else {
        db_query("INSERT INTO {cs_event_fact} (event_id, fact_id, value, modified_date, modified_pid)\n       VALUES ({$event_id}, {$fact_id}, {$value}, '" . $dt->format('Y-m-d H:i:s') . "', {$user->id})\n       ON DUPLICATE KEY UPDATE value={$value}, modified_pid={$user->id}, modified_date='" . $dt->format('Y-m-d H:i:s') . "'");
    }
    cdb_log("[ChurchService] Speichere Fakt {$value} bei Fakt {$fact_id}, Event {$event_id}", 2, $fact_id, "fact");
}
/**
 * create group meetings
 *
 * TODO: check for success, maybe combine first two sqls into one request?
 *
 * @return string "ok"
 */
function createGroupMeetings()
{
    $res = db_query("SELECT id FROM {cdb_gruppe} WHERE treffen_yn=1");
    $sql = "SELECT * FROM {cdb_gruppentreffen} WHERE gruppe_id=:id AND datumbis>=CURDATE() \n          ORDER BY datumbis desc";
    foreach ($res as $meeting) {
        $res2 = db_query($sql, array(":id" => $meeting->id))->fetch();
        if ($res2 == null) {
            cdb_log("Erstelle Gruppentreffen fuer Gruppe " . $meeting->id, 3, -1, 'cron');
            db_query("INSERT INTO {cdb_gruppentreffen} (gruppe_id, datumvon, datumbis,eintragerfolgt_yn,ausgefallen_yn)\n   \t    VALUES ({$meeting->id}, CURDATE() - interval (dayofweek(CURDATE())-2) day, curdate() + interval (8-dayofweek(curdate())) day,0,0)");
        }
    }
    return "ok";
}