/**
  *
  * @param array $params
  * @return array with persons
  */
 public function getAllowedPeopleForCalender($params)
 {
     include_once './' . CHURCHDB . '/churchdb_db.php';
     $db = db_query("SELECT * FROM {cc_domain_auth}\n                    WHERE daten_id=:daten_id AND (auth_id=403 || auth_id=404)", array(":daten_id" => $params["category_id"]));
     $res = array();
     foreach ($db as $d) {
         if ($d->domain_type == "gruppe") {
             $g = array();
             $ids = churchdb_getAllPeopleIdsFromGroups(array($d->domain_id));
             if ($ids) {
                 foreach ($ids as $id) {
                     $p = churchdb_getPersonDetails($id);
                     if ($p != "no access") {
                         $g[] = $p;
                     }
                 }
             }
             if (count($g)) {
                 $gr = churchcore_getTableData("cdb_gruppe", null, "id=" . $d->domain_id);
                 if ($gr) {
                     $res[] = array("type" => "gruppe", "data" => $g, "bezeichnung" => $gr[$d->domain_id]->bezeichnung);
                 }
             }
         } else {
             if ($d->domain_type == "person") {
                 $p = churchdb_getPersonDetails($d->domain_id);
                 if ($p != "no access") {
                     $res[] = array("type" => "person", "data" => $p);
                 }
             }
         }
     }
     return $res;
 }
 public function getPersonDetails($params)
 {
     $this->logPerson($params, 3);
     return churchdb_getPersonDetails($params["id"]);
 }
/**
 * 
 * @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);
        }
    }
}
/**
 * prepare person data for export
 * 
 * @param string $ids; null for all or comma separated list
 * @param string $template; when null, export everything that is possible
 * @throws Exception
 * 
 * @return array
 */
function _getPersonDataForExport($person_ids = null, $template = null)
{
    global $user;
    $ids = null;
    if ($person_ids != null) {
        $ids = explode(",", $person_ids);
    }
    // Check allowed persons
    $ps = churchdb_getAllowedPersonData();
    $department = churchcore_getTableData("cdb_bereich");
    $status = churchcore_getTableData("cdb_status");
    $station = churchcore_getTableData("cdb_station");
    $export = array();
    foreach ($ps as $p) {
        if ($ids == null || in_array($p->p_id, $ids)) {
            $detail = churchdb_getPersonDetails($p->p_id, false);
            $detail->bereich = "";
            $departments = array();
            foreach ($p->access as $dep_id) {
                $departments[] = $department[$dep_id]->bezeichnung;
            }
            $detail->bereich_id = implode('::', $departments);
            $detail->station_id = $station[$detail->station_id]->bezeichnung;
            if (user_access("view alldetails", "churchdb")) {
                $detail->status_id = $status[$detail->status_id]->bezeichnung;
            } else {
                if ($status[$detail->status_id]->mitglied_yn == 1) {
                    $detail->status_id = "Mitglied";
                } else {
                    $detail->status_id = "Kein Mitglied";
                }
            }
            if ($detail->geschlecht_no == 1) {
                $detail->Anrede1 = "Herrn";
                $detail->Anrede2 = "Lieber";
            } else {
                if ($detail->geschlecht_no == 2) {
                    $detail->Anrede1 = "Frau";
                    $detail->Anrede2 = "Liebe";
                }
            }
            if (isset($detail->geburtsdatum)) {
                $detail->age = churchcore_getAge($detail->geburtsdatum);
            }
            // If template was selected
            if ($template != null) {
                $export_entry = array();
                foreach ($template as $key => $field) {
                    if (strpos($key, "f_") === 0) {
                        $key = substr($key, 2, 99);
                        if (isset($detail->{$key})) {
                            $export_entry[$key] = $detail->{$key};
                        }
                    }
                }
            } else {
                $export_entry = (array) $detail;
                if (!user_access("administer persons", "churchcore")) {
                    unset($export_entry["letzteaenderung"]);
                    unset($export_entry["aenderunguser"]);
                    unset($export_entry["einladung"]);
                    unset($export_entry["active_yn"]);
                    unset($export_entry["lastlogin"]);
                    unset($export_entry["createdate"]);
                    unset($export_entry["lat"]);
                    unset($export_entry["lng"]);
                    unset($export_entry["gp_id"]);
                    unset($export_entry["imageurl"]);
                }
                // Unset Array, cause this is not exportable
                unset($export_entry["auth"]);
            }
            $export[$p->p_id] = _export_optimzations($export_entry);
        }
    }
    return $export;
}
/**
 *
 * @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);
        }
    }
}