Portions created by the Initial Developer are Copyright (C) 2018-2024
the Initial Developer. All Rights Reserved.
*/
//includes files
require_once dirname(__DIR__, 2) . "/resources/require.php";
require_once "resources/check_auth.php";
//check permissions
if (permission_exists('contact_add') || permission_exists('contact_edit')) {
//access granted
}
else {
echo "access denied";
exit;
}
//connect to the database
$database = new database;
//add multi-lingual support
$language = new text;
$text = $language->get();
//set the defaults
$contact_organization = '';
$contact_name_prefix = '';
$contact_name_given = '';
$contact_name_middle = '';
$contact_name_family = '';
$contact_name_suffix = '';
$contact_nickname = '';
$contact_title = '';
$contact_role = '';
$contact_category = '';
$contact_note = '';
$url_label = '';
$relation_label = '';
$contact_attachments = [];
//set from session variables
$body_text_color = !empty($_SESSION['theme']['body_text_color']['text']) ? $_SESSION['theme']['body_text_color']['text'] : 'false';
//action add or update
if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
$action = "update";
$contact_uuid = $_REQUEST["id"];
$id = $_REQUEST["id"];
}
else {
$action = "add";
}
//get http post variables and set them to php variables
if (!empty($_POST)) {
$contact_organization = $_POST["contact_organization"];
$contact_name_prefix = $_POST["contact_name_prefix"];
$contact_name_given = $_POST["contact_name_given"];
$contact_name_middle = $_POST["contact_name_middle"];
$contact_name_family = $_POST["contact_name_family"];
$contact_name_suffix = $_POST["contact_name_suffix"];
$contact_nickname = $_POST["contact_nickname"];
$contact_type = $_POST["contact_type"];
$contact_title = $_POST["contact_title"];
$contact_role = $_POST["contact_role"];
$contact_category = $_POST["contact_category"];
$contact_time_zone = $_POST["contact_time_zone"];
$contact_note = $_POST["contact_note"];
$last_mod_date = $_POST["last_mod_date"] ?? null;
$last_mod_user = $_POST["last_mod_user"] ?? null;
//$contact_users = $_POST["contact_users"];
//$contact_groups = $_POST["contact_groups"];
$contact_user_uuid = ($_SESSION['contact']['permissions']['boolean'] == "true") ? ($_POST["contact_user_uuid"] ?? $_SESSION["user_uuid"]) : ($contact_user_uuid = $_POST["contact_user_uuid"] ?? null);
$contact_group_uuid = $_POST["contact_group_uuid"] ?? null;
$contact_phones = $_POST["contact_phones"];
$contact_addresses = $_POST["contact_addresses"];
$contact_emails = $_POST["contact_emails"];
$contact_urls = $_POST["contact_urls"];
$contact_relations = $_POST["contact_relations"];
$contact_settings = $_POST["contact_settings"];
$contact_attachments = $_POST["contact_attachments"] ?? null;
$contact_times = $_POST["contact_times"] ?? null;
$contact_notes = $_POST["contact_notes"] ?? null;
}
//process the user data and save it to the database
if (!empty($_POST) && empty($_POST["persistformvar"])) {
//debug info
//view_array($_POST, true);
//validate the token
$token = new token;
if (!$token->validate($_SERVER['PHP_SELF'])) {
message::add($text['message-invalid_token'],'negative');
header('Location: contacts.php');
exit;
}
//process the http post data by submitted action
if (!empty($_POST['action'])) {
//prepare the array(s)
$x = 0;
if (!empty($_POST['contact_users']) && is_array($_POST['contact_users']) && @sizeof($_POST['contact_users']) != 0) {
foreach ($_POST['contact_users'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_users'][]['contact_user_uuid'] = $row['contact_user_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_groups']) && is_array($_POST['contact_groups']) && @sizeof($_POST['contact_groups']) != 0) {
foreach ($_POST['contact_groups'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_groups'][]['contact_group_uuid'] = $row['contact_group_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_phones']) && is_array($_POST['contact_phones']) && @sizeof($_POST['contact_phones']) != 0) {
foreach ($_POST['contact_phones'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_phones'][]['contact_phone_uuid'] = $row['contact_phone_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_addresses']) && is_array($_POST['contact_addresses']) && @sizeof($_POST['contact_addresses']) != 0) {
foreach ($_POST['contact_addresses'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_addresses'][]['contact_address_uuid'] = $row['contact_address_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_emails']) && is_array($_POST['contact_emails']) && @sizeof($_POST['contact_emails']) != 0) {
foreach ($_POST['contact_emails'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_emails'][]['contact_email_uuid'] = $row['contact_email_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_urls']) && is_array($_POST['contact_urls']) && @sizeof($_POST['contact_urls']) != 0) {
foreach ($_POST['contact_urls'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_urls'][]['contact_url_uuid'] = $row['contact_url_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_relations']) && is_array($_POST['contact_relations']) && @sizeof($_POST['contact_relations']) != 0) {
foreach ($_POST['contact_relations'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_relations'][]['contact_relation_uuid'] = $row['contact_relation_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_settings']) && is_array($_POST['contact_settings']) && @sizeof($_POST['contact_settings']) != 0) {
foreach ($_POST['contact_settings'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_settings'][]['contact_setting_uuid'] = $row['contact_setting_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_attachments']) && is_array($_POST['contact_attachments']) && @sizeof($_POST['contact_attachments']) != 0) {
foreach ($_POST['contact_attachments'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_attachments'][]['contact_attachment_uuid'] = $row['contact_attachment_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_times']) && is_array($_POST['contact_times']) && @sizeof($_POST['contact_times']) != 0) {
foreach ($_POST['contact_times'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_times'][]['contact_time_uuid'] = $row['contact_time_uuid'];
$x++;
}
}
}
$x = 0;
if (!empty($_POST['contact_notes']) && is_array($_POST['contact_notes']) && @sizeof($_POST['contact_notes']) != 0) {
foreach ($_POST['contact_notes'] as $row) {
if (!empty($row['contact_uuid']) && is_uuid($row['contact_uuid']) && !empty($row['checked']) && $row['checked'] === 'true') {
$array['contacts'][$x]['checked'] = $row['checked'];
$array['contacts'][$x]['contact_notes'][]['contact_note_uuid'] = $row['contact_note_uuid'];
$x++;
}
}
}
//send the array to the database class
if (!empty($array)) {
switch ($_POST['action']) {
case 'copy':
if (permission_exists('contact_add')) {
$database->copy($array);
}
break;
case 'delete':
if (permission_exists('contact_delete')) {
$database->delete($array);
}
break;
case 'toggle':
if (permission_exists('contact_update')) {
$database->toggle($array);
}
break;
}
}
//redirect the user
if (in_array($_POST['action'], array('copy', 'delete', 'toggle'))) {
header('Location: contact_edit.php?id='.$id);
exit;
}
}
//check for all required data
$msg = '';
//if (empty($contact_type)) { $msg .= $text['message-required']." ".$text['label-contact_type']."
\n"; }
//if (empty($contact_title)) { $msg .= $text['message-required']." ".$text['label-contact_title']."
\n"; }
//if (empty($contact_role)) { $msg .= $text['message-required']." ".$text['label-contact_role']."
\n"; }
//if (empty($contact_category)) { $msg .= $text['message-required']." ".$text['label-contact_category']."
\n"; }
//if (empty($contact_organization)) { $msg .= $text['message-required']." ".$text['label-contact_organization']."
\n"; }
//if (empty($contact_name_prefix)) { $msg .= $text['message-required']." ".$text['label-contact_name_prefix']."
\n"; }
//if (empty($contact_name_given)) { $msg .= $text['message-required']." ".$text['label-contact_name_given']."
\n"; }
//if (empty($contact_name_middle)) { $msg .= $text['message-required']." ".$text['label-contact_name_middle']."
\n"; }
//if (empty($contact_name_family)) { $msg .= $text['message-required']." ".$text['label-contact_name_family']."
\n"; }
//if (empty($contact_name_suffix)) { $msg .= $text['message-required']." ".$text['label-contact_name_suffix']."
\n"; }
//if (empty($contact_nickname)) { $msg .= $text['message-required']." ".$text['label-contact_nickname']."
\n"; }
//if (empty($contact_time_zone)) { $msg .= $text['message-required']." ".$text['label-contact_time_zone']."
\n"; }
//if (empty($last_mod_date)) { $msg .= $text['message-required']." ".$text['label-last_mod_date']."
\n"; }
//if (empty($last_mod_user)) { $msg .= $text['message-required']." ".$text['label-last_mod_user']."
\n"; }
//if (empty($contact_phones)) { $msg .= $text['message-required']." ".$text['label-contact_phones']."
\n"; }
//if (empty($contact_addresses)) { $msg .= $text['message-required']." ".$text['label-contact_addresses']."
\n"; }
//if (empty($contact_emails)) { $msg .= $text['message-required']." ".$text['label-contact_emails']."
\n"; }
//if (empty($contact_urls)) { $msg .= $text['message-required']." ".$text['label-contact_urls']."
\n"; }
//if (empty($contact_settings)) { $msg .= $text['message-required']." ".$text['label-contact_settings']."
\n"; }
//if (empty($contact_user_uuid)) { $msg .= $text['message-required']." ".$text['label-contact_user_uuid']."
\n"; }
//if (empty($contact_group_uuid)) { $msg .= $text['message-required']." ".$text['label-contact_group_uuid']."
\n"; }
//if (empty($contact_note)) { $msg .= $text['message-required']." ".$text['label-contact_note']."
\n"; }
if (!empty($msg) && empty($_POST["persistformvar"])) {
require_once "resources/header.php";
require_once "resources/persist_form_var.php";
echo "
| \n";
echo $msg." "; echo " |