/** * Returns a readable list with all changes or NULL * TODO: is != null needed or is if $oldVal sufficient? * * @param array $fields * @param array $oldArr * @param array $newArr * @param string $cutDates; default: true * @return string|NULL */ function churchcore_getFieldChanges($fields, $oldArr, $newArr, $cutDates = true) { $txt = ""; foreach ($newArr as $name => $value) { $oldVal = null; if (isset($fields[$name])) { if ($oldArr != null) { $oldVal = $oldArr->{$fields}[$name]["sql"]; } if ($fields[$name]["type"] == "date") { // only compare year and day of dates, time doesn't matter if ($cutDates && $fields[$name]["type"] == "date") { $oldVal = substr($oldVal, 0, 10); } $oldVal = churchcore_stringToDateDe($oldVal); $value = churchcore_stringToDateDe($value); } //TODO: != null probably can be omitted if ($oldVal != null && $value != $oldVal) { $txt .= $fields[$name]["text"] . ": {$value} (" . t('previously') . ": {$oldVal})\n"; } else { if ($oldVal == null && $value != null) { $txt .= $fields[$name]["text"] . ": {$value} (" . t('new') . ")\n"; } } } else { if ($oldArr != null && isset($oldArr->{$name})) { $oldVal = $oldArr->{$name}; } if ($oldVal == null && $value != null) { $txt .= "{$name}: {$value} (Neu)\n"; } else { if ($oldVal != $value) { $txt .= "{$name}: {$value} (" . t('previously') . ": {$oldVal})\n"; } } } } return $txt ? $txt : null; }
/** * * @return string; empty div */ function churchcal_getUserMyMeetings() { global $user; $db = db_query("SELECT count(*) count, c.id, c.category_id, c.startdate, c.bezeichnung,\n sum(if (mr.zugesagt_yn=1,1,0)) as zugesagt,\n sum(if (mr.zugesagt_yn is null AND mr.response_date is not null,1,0)) as perhaps,\n sum(if (mr.response_date is not null,1,0)) as response\n FROM {cc_cal} c, {cc_meetingrequest} mr\n WHERE c.id = mr.cal_id\n AND c.modified_pid={$user->id} AND DATEDIFF(c.startdate,now())>=0\n GROUP BY c.id, c.category_id, c.startdate, c.bezeichnung\n ORDER BY c.startdate"); $txt = ""; foreach ($db as $entry) { $txt .= '<li><a href="?q=churchcal&category_id=' . $entry->category_id . '&id=' . $entry->id . '">' . churchcore_stringToDateDe($entry->startdate) . " - " . $entry->bezeichnung . "</a>"; $txt .= '<p><small>'; $txt .= '<span class="badge badge-info">' . ($entry->count - $entry->response) . '</span> Offen '; if ($entry->zugesagt > 0) { $txt .= '<span class="badge badge-success">' . $entry->zugesagt . '</span> Zusage '; } if ($entry->perhaps > 0) { $txt .= '<span class="badge">' . $entry->perhaps . '</span> Vielleicht '; } if ($entry->response - $entry->perhaps - $entry->zugesagt > 0) { $txt .= '<span class="badge badge-important">' . ($entry->response - $entry->perhaps - $entry->zugesagt) . '</span> Absage '; } $txt .= "</small>"; } if ($txt != "") { $txt = "<ul>" . $txt . "</ul>"; } return $txt; }
/** * Returns a readable list with all changes or NULL * * @param unknown $fields * @param unknown $oldarr * @param unknown $newarr * @param string $cut_dates * @return string|NULL */ function churchcore_getFieldChanges($fields, $oldarr, $newarr, $cut_dates = true) { $txt = ""; foreach ($newarr as $name => $value) { $oldval = null; if (isset($fields[$name])) { if ($oldarr != null) { $oldval = $oldarr->{$fields}[$name]["sql"]; } if ($fields[$name]["type"] == "date") { // Beim Datum nur Jahr, Datum und Tag vergleichen, Uhrzeit egal if ($cut_dates && $fields[$name]["type"] == "date") { $oldval = substr($oldval, 0, 10); } $oldval = churchcore_stringToDateDe($oldval); $value = churchcore_stringToDateDe($value); } if ($oldval != null && $value != $oldval) { $txt = $txt . $fields[$name]["text"] . ": {$value} (Vorher: {$oldval})\n"; } else { if ($oldval == null && $value != null) { $txt = $txt . $fields[$name]["text"] . ": {$value} (Neu)\n"; } } } else { if ($oldarr != null && isset($oldarr->{$name})) { $oldval = $oldarr->{$name}; } if ($oldval == null && $value != null) { $txt = $txt . "{$name}: {$value} (Neu)\n"; } else { if ($oldval != $value) { $txt = $txt . "{$name}: {$value} (Vorher: {$oldval})\n"; } } } } if ($txt != "") { return $txt; } else { return null; } }
function makeCCEventDiff($originEvent, $newEvent) { $ret = array(); $internFields = array(); $internFields[] = "informMe"; $internFields[] = "informCreator"; $internFields[] = "modified_pid"; $internFields[] = "repeat_frequence"; $internFields[] = "allDay"; $internFields[] = "name"; $internFields[] = "func"; $internFields[] = "currentEvent_id"; $internFields[] = "id"; $internFields[] = "cal_id"; $internFields[] = "bookings"; $internFields[] = "csevents"; $internFields[] = "old_id"; $internFields[] = "old_category_id"; $internFields[] = "old_startdate"; $internFields[] = "copychurchservice"; $internFields[] = "newCSEventId"; $internFields[] = "exceptionids"; $internFields[] = "exceptions"; $internFields[] = "additions"; $internFields[] = "meetingRequest"; // From CR $internFields[] = "cc_cal_id"; $internFields[] = "person_id"; $internFields[] = "text"; $internFields[] = "show_in_churchcal_yn"; $internFields[] = "person_name"; $internFields[] = "cal_startdate"; $internFields[] = "cal_enddate"; $internFields[] = "booking_id"; $internFields[] = "neu"; foreach ($newEvent as $key => $newEntry) { if (!in_array($key, $internFields)) { if (!isset($originEvent[$key]) || $originEvent[$key] != $newEntry) { $k = $key; $einheit = ""; if (isset($originEvent[$key])) { $old = $originEvent[$key]; } else { $old = null; } $new = $newEntry; if ($key == "startdate") { $k = "start.date"; } else { if ($key == "enddate") { $k = "end.date"; } else { if ($key == "repeat_id") { $repeat_types = churchcore_getTableData("cc_repeat"); $k = "repeat.type"; if ($old != null) { $old = $repeat_types[$old]->bezeichnung; } $new = $repeat_types[$new]->bezeichnung; } else { if ($key == "repeat_until") { if ($old != null) { $old = substr($old, 0, 10); } $new = substr($old, 0, 10); $k = "repeat.to"; } else { if ($key == "modified_name") { $k = "creator"; } else { if ($key == "category_id") { $k = "category"; if ($old != null) { $old = churchcal_getCategory($old)->bezeichnung; } $new = churchcal_getCategory($new)->bezeichnung; } else { if ($key == "notizen") { $k = "more.information"; } else { if ($key == "ort") { $k = "note"; } else { if ($key == "intern_yn") { $k = "only.intern.visible"; if ($old != null) { if ($old == 1) { $old = t("yes"); } else { $old = t("no"); } } if ($new == 1) { $new = t("yes"); } else { $new = t("no"); } } else { if ($key == "modified_date") { $k = "modified.date"; } } } } } } } } } } if (strpos($key, "date") !== false || $key == "repeat_until") { if ($old != null && $old != "") { $old = churchcore_stringToDateDe($old); } if ($new != "") { $new = churchcore_stringToDateDe($new); } } if ($old != $new) { $ret[$k] = array("old" => $old . $einheit, "new" => $new . $einheit); } } } } return $ret; }
/** * TODO: too much code in churchresource_updateBooking, split it up * FIXME: the changes for using email template are breaking logging in case no email is send * otherwise logging of complete mails dont seems useful => only log important things in a short text? * * @param array $params * @return multitype:multitype:unknown */ function churchresource_updateBooking($params, $sendEMails = true) { global $base_url, $user; $oldBooking = getBooking($params["id"]); $bUser = churchcore_getPersonById($oldBooking->person_id); $ressources = churchcore_getTableData("cr_resource", "resourcetype_id,sortkey,bezeichnung"); $i = new CTInterface(); $i->setParam("resource_id"); $i->setParam("status_id"); $i->addTypicalDateFields(); $i->setParam("text", false); $i->setParam("location", false); $i->setParam("note", false); if (empty($params["text"])) { $res = db_query('SELECT text FROM {cr_booking} WHERE id=:id', array(":id" => $params["id"]))->fetch(); $params["text"] = $res->text; } $i->setParam("person_id", false); $id = db_update("cr_booking")->fields($i->getDBInsertArrayFromParams($params))->condition("id", $params["id"], "=")->execute(false); $changes = null; $exceptions = churchcore_getTableData("cr_exception", null, "booking_id=" . $params["id"]); // look which exceptions are already saved in DB. if (isset($params["exceptions"])) { foreach ($params["exceptions"] as $exception) { $current_exc = null; // It is not possible to search exceptions by id, because ChurchCal Exc have other IDs if ($exceptions) { foreach ($exceptions as $e) { if (churchcore_isSameDay($e->except_date_start, $exception["except_date_start"]) && churchcore_isSameDay($e->except_date_end, $exception["except_date_end"])) { $current_exc = $e; } } } if ($current_exc) { $exceptions[$current_exc->id]->exists = true; } else { $changes["add_exception"][] = $exception; } } } // delete removed exceptions from DB. if ($exceptions) { foreach ($exceptions as $e) { if (!isset($e->exists)) { $changes["del_exception"][] = (array) $e; } } } // get all additions $additions = churchcore_getTableData("cr_addition", null, "booking_id=" . $params["id"]); // look which additions are already saved in DB. if (isset($params["additions"])) { foreach ($params["additions"] as $addition) { $current_add = null; // It is not possible to search additions by id, because ChurchCal adds have other IDs if ($additions) { foreach ($additions as $a) { if (churchcore_isSameDay($a->add_date, $addition["add_date"]) && $a->with_repeat_yn == $addition["with_repeat_yn"]) { $current_add = $a; } } } if ($current_add) { $additions[$current_add->id]->exists = true; } else { $changes["add_addition"][] = $addition; } } } // delete removed additions from DB. if ($additions) { foreach ($additions as $a) { // churchresource_delAddition($a->id); if (!isset($a->exists)) { $changes["del_addition"][] = (array) $a; } } } // save new exceptions $res_exceptions = array(); $res_additions = array(); $days = array(); $resources = churchcore_getTableData("cr_resource"); //TODO: only get needed resource_id if ($changes) { if (isset($changes["add_exception"])) { foreach ($changes["add_exception"] as $exc) { // Check, if exception not alreay in DB (only possible when coming from Cal) $db = db_query("SELECT id FROM {cr_exception}\n WHERE booking_id=:booking_id AND except_date_start=:start", array(":booking_id" => $params["id"], ":start" => $exc["except_date_start"]))->fetch(); if (!$db) { $id = addException($params["id"], $exc["except_date_start"], $exc["except_date_end"], $user->id); if (isset($exc["id"])) { $res_exceptions[$exc["id"]] = $id; } $days[] = $exc["except_date_start"]; } } if ($sendEMails && getConf("churchresource_send_emails", true) && count($days) && $bUser) { $data = array('canceled' => true, 'surname' => $bUser->vorname, 'name' => $bUser->name, 'nickname' => $bUser->spitzname ? $bUser->spitzname : $bUser->vorname, 'user' => $user, 'resource' => $resources[$params["resource_id"]]->bezeichnung, 'booking' => $booking, 'days' => implode(", ", $days), 'person' => $bUser, 'contact' => getConf('site_mail')); $lang = getUserLanguage($oldBooking->person_id); $content = getTemplateContent('email/bookingRequest', 'churchresource', $data, null, $lang); churchresource_send_mail("[" . getConf('site_name') . "] " . t2($lang, 'updated.booking.request') . ": " . $params["text"], $content, $bUser->email); } } if (isset($changes["del_exception"])) { foreach ($changes["del_exception"] as $exc) { $db = db_query("SELECT id FROM {cr_exception}\n WHERE booking_id=:booking_id AND except_date_start=:start", array(":booking_id" => $params["id"], ":start" => $exc["except_date_start"]))->fetch(); if ($db) { churchresource_delException(array("id" => $db->id)); } } } if (isset($changes["add_addition"])) { foreach ($changes["add_addition"] as $add) { $db = db_query("SELECT id FROM {cr_addition}\n WHERE booking_id=:booking_id AND add_date=:date", array(":booking_id" => $params["id"], ":date" => $add["add_date"]))->fetch(); if (!$db) { $id = addAddition($params["id"], $add["add_date"], $add["with_repeat_yn"], $user->id); if (isset($add["id"])) { $res_additions[$add["id"]] = $id; } } } } if (isset($changes["del_addition"])) { foreach ($changes["del_addition"] as $add) { $db = db_query("SELECT id FROM {cr_addition}\n WHERE booking_id=:booking_id AND add_date=:date", array(":booking_id" => $params["id"], ":date" => $add["add_date"]))->fetch(); if ($db != false) { churchresource_delAddition($db->id); } } } } // FIXME: check logic for correct function; i am not sure what should happen exactly in which cases // TODO: maybe use $params as data and add further values $booking = getBooking($params["id"]); $changedFields = churchcore_getFieldChanges(getBookingFields(), $oldBooking, $booking, false); $data = array('enddate' => churchcore_stringToDateDe($params["enddate"]), 'startdate' => churchcore_stringToDateDe($params["startdate"]), 'resource' => $resources[$params["resource_id"]]->bezeichnung, 'changes' => str_replace("\n", "<br>", $changedFields), 'booking' => $booking, 'bookingUrl' => $base_url . "?q=churchresource&id=" . $params["id"], 'text' => $params['text'], 'note' => isset($params['location']) ? $params['location'] : "", 'pending' => $params["status_id"] == CR_PENDING, 'approved' => $params["status_id"] == CR_APPROVED && ($oldBooking->status_id != CR_APPROVED || $changedFields != null), 'canceled' => $params["status_id"] == CR_CANCELED, 'deleted' => $params["status_id"] == CR_DELETED, 'contact' => getConf('site_mail')); $logInfo = ' :: ' . t('bookingX.for.resource.on.datetime', $params["text"], $resources[$params["resource_id"]]->bezeichnung, $params["startdate"], isset($params['location']) ? $params['location'] : ""); $subject = t('booking.request.updated'); if ($data['pending']) { $logInfo = t('booking.updated') . $logInfo; } elseif ($data['approved']) { $logInfo = t('booking.approved') . $logInfo; } elseif ($data['canceled']) { $logInfo = t('booking.canceled') . $logInfo; } elseif ($data['deleted']) { $logInfo = t('booking.deleted') . $logInfo; } if ($sendEMails && getConf("churchresource_send_emails", true)) { if (($params["status_id"] != $oldBooking->status_id || $changedFields != null) && $bUser) { $adminmails = explode(",", $resources[$params["resource_id"]]->admin_person_ids); // if current user is not resource admin OR is not the booking creating user if (!in_array($user->id, $adminmails) || $user->id != $bUser->id) { $content = getTemplateContent('email/bookingUpdated', 'churchresource', $data); churchresource_send_mail("[" . getConf('site_name') . "] {$subject}: " . $params["text"], $content, $bUser->email); } } } if ($changedFields) { cr_log("UPDATE BOOKING\n" . $logInfo, 3, $booking->id); } return array("exceptions" => $res_exceptions, "additions" => $res_additions); }
function churchresource_createBooking($params) { global $base_url, $user; $i = new CTInterface(); $i->setParam("resource_id"); $i->addTypicalDateFields(); $i->setParam("person_id"); $i->setParam("status_id"); $i->setParam("text"); $i->setParam("location"); $i->setParam("note"); $i->setParam("cc_cal_id", false); $id = db_insert("cr_booking")->fields($i->getDBInsertArrayFromParams($params))->execute(false); $res = db_query("SELECT * from {cr_booking} where id={$id}")->fetch(); $res->ok = true; $exc_txt = ""; if (isset($params["exceptions"])) { foreach ($params["exceptions"] as $exception) { addException($res->id, $exception["except_date_start"], $exception["except_date_end"], $user->id); $exc_txt .= churchcore_stringToDateDe($exception["except_date_start"], false) + " "; } } $add_txt = ""; if (isset($params["additions"])) { $days = array(); foreach ($params["additions"] as $addition) { addAddition($res->id, $addition["add_date"], $addition["with_repeat_yn"], $user->id); $add_txt .= churchcore_stringToDateDe($addition["add_date"], false) + " "; if ($addition["with_repeat_yn"] == 1) { $add_txt .= "{R} "; } $add_txt .= " "; } } $info = churchcore_getTableData("cr_resource"); $txt = "Hier sind alle Buchungsinformationen zusammengefasst:<p><small>"; $txt .= '<table class="table table-condensed">'; $txt .= "<tr><td>Zweck<td>{$res->text}"; $txt .= "<tr><td>Ressource<td>" . $info[$params["resource_id"]]->bezeichnung; $txt .= "<tr><td>Start<td>" . churchcore_stringToDateDe($res->startdate); $txt .= "<tr><td>Ende<td>" . churchcore_stringToDateDe($res->enddate); $status = churchcore_getTableData("cr_status"); $txt .= "<tr><td>Status<td>" . $status[$res->status_id]->bezeichnung; if ($res->location != "") { $txt .= "<tr><td>Ort<td>{$res->location}"; } if ($res->repeat_id != "0") { $repeats = churchcore_getTableData("cc_repeat"); $txt .= "<tr><td>Wiederholungstyp<td>" . $repeats[$res->repeat_id]->bezeichnung; if ($res->repeat_id != 999) { $txt .= "<tr><td>Wiederholung bis<td>" . churchcore_stringToDateDe($res->repeat_until, false); } if ($exc_txt != "") { $txt .= "<tr><td>Ausnahmen<td>{$exc_txt}"; } } if ($add_txt != "") { $txt .= "<tr><td>Weitere Termine<td>{$add_txt}"; } if ($res->note != "") { $txt .= "<tr><td>Notiz<td>{$res->note}"; } if (isset($params["conflicts"]) && $params["conflicts"] != null) { $txt .= "<tr><td>Konflikte<td>" . $params["conflicts"]; } $txt .= "</table>"; $txt .= '</small><p><a class="btn" href="' . $base_url . "?q=churchresource&id=" . $res->id . '">Zur Buchungsanfrage »</a>'; if ($params["status_id"] == 1) { $txt_user = "******" . $user->vorname . "!</h3><p>Deine Buchungsanfrage '" . $params["text"] . "' ist bei uns eingegangen und wird bearbeitet. Sobald es einen neuen Status gibt, wirst Du informiert.<p>" . $txt; $txt_admin = "<h3>Hallo Admin!</h3><p>Eine neue Buchungsanfrage von <i>{$user->vorname} {$user->name}</i> wartet auf Genehmigung.<p>" . $txt; } else { $txt_user = "******" . $user->vorname . "!</h3><p>Deine Buchung '" . $params["text"] . "' war erfolgreich.<p>" . $txt; $txt_admin = "<h3>Hallo Admin!</h3><p>Eine neue Buchung von <i>{$user->vorname} {$user->name}</i> wurde erstellt und automatisch genehmigt.<p>" . $txt; } $istUserAdmin = false; if ($info[$params["resource_id"]]->admin_person_ids != -1) { $adminids = explode(',', $info[$params["resource_id"]]->admin_person_ids); foreach ($adminids as $adminid) { // Nur, wenn ich nicht selber der Admin bin if ($user->id != $adminid) { $p = churchcore_getPersonById($adminid); if ($p != null && $p->email != "") { churchresource_send_mail("[" . variable_get('site_name', 'ChurchTools') . "] Neue Buchungsanfrage: " . $params["text"], $txt_admin, $p->email); } } else { $istUserAdmin = true; } } } if (!$istUserAdmin) { churchresource_send_mail("[" . variable_get('site_name', 'ChurchTools') . "] Neue Buchungsanfrage: " . $params["text"], $txt_user, $user->email); } $txt = churchcore_getFieldChanges(getBookingFields(), null, $res); cr_log("CREATE BOOKING\n" . $txt, 3, $res->id); return $res; }