contact_relation_edit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2024
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. Luis Daniel Lucio Quiroz <[email protected]>
  21. */
  22. //includes files
  23. require_once dirname(__DIR__, 2) . "/resources/require.php";
  24. require_once "resources/check_auth.php";
  25. //check permissions
  26. if (permission_exists('contact_relation_edit') || permission_exists('contact_relation_add')) {
  27. //access granted
  28. }
  29. else {
  30. echo "access denied";
  31. exit;
  32. }
  33. //add multi-lingual support
  34. $language = new text;
  35. $text = $language->get();
  36. //action add or update
  37. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  38. $action = "update";
  39. $contact_relation_uuid = $_REQUEST["id"];
  40. }
  41. else {
  42. $action = "add";
  43. }
  44. //get the contact uuid
  45. if (!empty($_GET["contact_uuid"]) && is_uuid($_GET["contact_uuid"])) {
  46. $contact_uuid = $_GET["contact_uuid"];
  47. }
  48. //get http post variables and set them to php variables
  49. if (!empty($_POST)) {
  50. $relation_label = $_POST["relation_label"];
  51. $relation_label_custom = $_POST["relation_label_custom"];
  52. $relation_contact_uuid = $_POST["relation_contact_uuid"];
  53. $relation_reciprocal = $_POST["relation_reciprocal"];
  54. $relation_reciprocal_label = $_POST["relation_reciprocal_label"];
  55. $relation_reciprocal_label_custom = $_POST["relation_reciprocal_label_custom"];
  56. //use custom label(s), if set
  57. $relation_label = ($relation_label_custom != '') ? $relation_label_custom : $relation_label;
  58. $relation_reciprocal_label = ($relation_reciprocal_label_custom != '') ? $relation_reciprocal_label_custom : $relation_reciprocal_label;
  59. }
  60. //process the form data
  61. if (!empty($_POST) && empty($_POST["persistformvar"])) {
  62. //set the uuid
  63. if ($action == "update") {
  64. $contact_relation_uuid = $_POST["contact_relation_uuid"];
  65. }
  66. //validate the token
  67. $token = new token;
  68. if (!$token->validate($_SERVER['PHP_SELF'])) {
  69. message::add($text['message-invalid_token'],'negative');
  70. header('Location: contacts.php');
  71. exit;
  72. }
  73. //check for all required data
  74. $msg = '';
  75. if (!empty($msg) && empty($_POST["persistformvar"])) {
  76. require_once "resources/header.php";
  77. require_once "resources/persist_form_var.php";
  78. echo "<div align='center'>\n";
  79. echo "<table><tr><td>\n";
  80. echo $msg."<br />";
  81. echo "</td></tr></table>\n";
  82. persistformvar($_POST);
  83. echo "</div>\n";
  84. require_once "resources/footer.php";
  85. return;
  86. }
  87. //add or update the database
  88. if (empty($_POST["persistformvar"])) {
  89. //update last modified
  90. $array['contacts'][0]['contact_uuid'] = $contact_uuid;
  91. $array['contacts'][0]['domain_uuid'] = $domain_uuid;
  92. $array['contacts'][0]['last_mod_date'] = 'now()';
  93. $array['contacts'][0]['last_mod_user'] = $_SESSION['username'];
  94. $p = permissions::new();
  95. $p->add('contact_edit', 'temp');
  96. $database = new database;
  97. $database->app_name = 'contacts';
  98. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  99. $database->save($array);
  100. unset($array);
  101. $p->delete('contact_edit', 'temp');
  102. //add the relation
  103. if ($action == "add" && permission_exists('contact_relation_add')) {
  104. $contact_relation_uuid = uuid();
  105. $array['contact_relations'][0]['contact_relation_uuid'] = $contact_relation_uuid;
  106. if ($relation_reciprocal) {
  107. $contact_relation_uuid = uuid();
  108. $array['contact_relations'][1]['contact_relation_uuid'] = $contact_relation_uuid;
  109. $array['contact_relations'][1]['domain_uuid'] = $_SESSION['domain_uuid'];
  110. $array['contact_relations'][1]['contact_uuid'] = $relation_contact_uuid;
  111. $array['contact_relations'][1]['relation_label'] = $relation_reciprocal_label;
  112. $array['contact_relations'][1]['relation_contact_uuid'] = $contact_uuid;
  113. }
  114. message::add($text['message-add']);
  115. }
  116. //update the relation
  117. if ($action == "update" && permission_exists('contact_relation_edit')) {
  118. $array['contact_relations'][0]['contact_relation_uuid'] = $contact_relation_uuid;
  119. message::add($text['message-update']);
  120. }
  121. //execute
  122. if (!empty($array)) {
  123. $array['contact_relations'][0]['contact_uuid'] = $contact_uuid;
  124. $array['contact_relations'][0]['domain_uuid'] = $_SESSION['domain_uuid'];
  125. $array['contact_relations'][0]['relation_label'] = $relation_label;
  126. $array['contact_relations'][0]['relation_contact_uuid'] = $relation_contact_uuid;
  127. $database = new database;
  128. $database->app_name = 'contacts';
  129. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  130. $database->save($array);
  131. unset($array);
  132. }
  133. //redirect
  134. header("Location: contact_edit.php?id=".escape($contact_uuid));
  135. exit;
  136. }
  137. }
  138. //pre-populate the form
  139. if (!empty($_GET) && empty($_POST["persistformvar"])) {
  140. $contact_relation_uuid = $_GET["id"] ?? '';
  141. $sql = "select * from v_contact_relations ";
  142. $sql .= "where domain_uuid = :domain_uuid ";
  143. $sql .= "and contact_relation_uuid = :contact_relation_uuid ";
  144. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  145. $parameters['contact_relation_uuid'] = $contact_relation_uuid;
  146. $database = new database;
  147. $row = $database->select($sql, $parameters, 'row');
  148. if (is_array($row) && @sizeof($row) != 0) {
  149. $relation_label = $row["relation_label"];
  150. $relation_contact_uuid = $row["relation_contact_uuid"];
  151. }
  152. unset($sql, $parameters, $row);
  153. }
  154. //get contact details and contact_name
  155. $sql = "select contact_uuid, contact_organization, contact_name_given, contact_name_family, contact_nickname ";
  156. $sql .= "from v_contacts ";
  157. $sql .= "where domain_uuid = :domain_uuid ";
  158. $sql .= "and contact_uuid <> :contact_uuid ";
  159. $sql .= "order by contact_organization desc, contact_name_given asc, contact_name_family asc ";
  160. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  161. $parameters['contact_uuid'] = $contact_relation_uuid;
  162. $database = new database;
  163. $contacts = $database->select($sql, $parameters, 'all');
  164. if (!empty($contacts) && is_uuid($row['relation_contact_uuid'])) {
  165. foreach($contacts as $field) {
  166. if ($field['contact_uuid'] == $row['relation_contact_uuid']) {
  167. $name = array();
  168. if (!empty($field['contact_organization'])) { $name[] = $field['contact_organization']; }
  169. if (!empty($field['contact_name_family'])) { $name[] = $field['contact_name_family']; }
  170. if (!empty($field['contact_name_given'])) { $name[] = $field['contact_name_given']; }
  171. if (empty($field['contact_name_family']) && empty($field['contact_name_given']) && !empty($field['contact_nickname'])) { $name[] = $field['contact_nickname']; }
  172. $contact_name = implode(', ', $name);
  173. break;
  174. }
  175. }
  176. }
  177. //create token
  178. $object = new token;
  179. $token = $object->create($_SERVER['PHP_SELF']);
  180. //show the header
  181. $document['title'] = $text['title-contact_relation'];
  182. require_once "resources/header.php";
  183. ?>
  184. <script type="text/javascript">
  185. function get_contacts(element_id, id, search) {
  186. var xhttp = new XMLHttpRequest();
  187. xhttp.onreadystatechange = function() {
  188. if (this.readyState == 4 && this.status == 200) {
  189. //create a handle for the contact select object
  190. select = document.getElementById(element_id);
  191. //remove current options
  192. while (select.options.length > 0) {
  193. select.remove(0);
  194. }
  195. //add an empty row
  196. //select.add(new Option('', ''));
  197. //add new options from the json results
  198. obj = JSON.parse(this.responseText);
  199. for (var i=0; i < obj.length; i++) {
  200. select.add(new Option(obj[i].name, obj[i].id));
  201. }
  202. }
  203. };
  204. if (search) {
  205. xhttp.open("GET", "/app/contacts/contact_json.php?search="+search, true);
  206. }
  207. else {
  208. xhttp.open("GET", "/app/contacts/contact_json.php", true);
  209. }
  210. xhttp.send();
  211. }
  212. </script>
  213. <?php
  214. //javascript to toggle input/select boxes
  215. echo "<script type='text/javascript'>";
  216. echo " function toggle_custom(field) {";
  217. echo " $('#'+field).toggle();";
  218. echo " document.getElementById(field).selectedIndex = 0;";
  219. echo " document.getElementById(field+'_custom').value = '';";
  220. echo " $('#'+field+'_custom').toggle();";
  221. echo " if ($('#'+field+'_custom').is(':visible')) { $('#'+field+'_custom').trigger('focus'); } else { $('#'+field).trigger('focus'); }";
  222. echo " }";
  223. echo "</script>";
  224. //show the content
  225. echo "<form method='post' name='frm'>\n";
  226. echo "<div class='action_bar' id='action_bar'>\n";
  227. echo " <div class='heading'><b>".$text['header-contact_relation']."</b></div>\n";
  228. echo " <div class='actions'>\n";
  229. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'contact_edit.php?id='.urlencode($contact_uuid)]);
  230. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save']);
  231. echo " </div>\n";
  232. echo " <div style='clear: both;'></div>\n";
  233. echo "</div>\n";
  234. echo "<div class='card'>\n";
  235. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  236. echo "<tr>\n";
  237. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  238. echo " ".$text['label-contact_relation_label']."\n";
  239. echo "</td>\n";
  240. echo "<td width='70%' class='vtable' align='left'>\n";
  241. if (!empty($_SESSION["contact"]["relation_label"])) {
  242. sort($_SESSION["contact"]["relation_label"]);
  243. foreach($_SESSION["contact"]["relation_label"] as $row) {
  244. $relation_label_options[] = "<option value='".$row."' ".(($row == $relation_label) ? "selected='selected'" : null).">".$row."</option>";
  245. }
  246. $relation_label_found = (in_array($relation_label, $_SESSION["contact"]["relation_label"])) ? true : false;
  247. }
  248. else {
  249. $selected[!empty($relation_label)] = "selected";
  250. $default_labels[] = $text['label-contact_relation_option_parent'];
  251. $default_labels[] = $text['label-contact_relation_option_child'];
  252. $default_labels[] = $text['label-contact_relation_option_employee'];
  253. $default_labels[] = $text['label-contact_relation_option_member'];
  254. $default_labels[] = $text['label-contact_relation_option_associate'];
  255. $default_labels[] = $text['label-contact_relation_option_other'];
  256. foreach ($default_labels as $default_label) {
  257. $relation_label_options[] = "<option value='".$default_label."' ".!empty($selected[$default_label]).">".$default_label."</option>";
  258. }
  259. $relation_label_found = (in_array(!empty($relation_label), $default_labels)) ? true : false;
  260. }
  261. echo " <select class='formfld' ".((!empty($relation_label) && !$relation_label_found) ? "style='display: none;'" : null)." name='relation_label' id='relation_label' onchange=\"getElementById('relation_label_custom').value='';\">\n";
  262. echo " <option value=''></option>\n";
  263. echo (is_array($relation_label_options)) ? implode("\n", $relation_label_options) : null;
  264. echo " </select>\n";
  265. echo " <input type='text' class='formfld' ".((empty($relation_label) || $relation_label_found) ? "style='display: none;'" : null)." name='relation_label_custom' id='relation_label_custom' value=\"".((!$relation_label_found) ? htmlentities($relation_label ?? '') : null)."\">\n";
  266. echo " <input type='button' id='btn_toggle_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('relation_label');\">\n";
  267. echo "<br />\n";
  268. echo !empty($text['description-relation_label'])."\n";
  269. echo "</td>\n";
  270. echo "</tr>\n";
  271. echo "<tr>\n";
  272. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  273. echo " ".$text['label-contact_relation_contact']."\n";
  274. echo "</td>\n";
  275. echo "<td class='vtable' align='left'>\n";
  276. echo " <input class=\"formfld\" type=\"text\" name=\"contact_search\" placeholder=\"search\" style=\"width: 80px;\" onkeyup=\"get_contacts('contact_select', 'contact_uuid', this.value);\" maxlength=\"255\" value=\"\">\n";
  277. echo " <select class='formfld' style=\"width: 150px;\" id=\"contact_select\" name=\"relation_contact_uuid\" >\n";
  278. echo " <option value='".escape($relation_contact_uuid ?? '')."'>".escape($contact_name ?? '')."</option>\n";
  279. echo " </select>\n";
  280. echo "</td>\n";
  281. echo "</tr>\n";
  282. if ($action == 'add') {
  283. echo "<tr>\n";
  284. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  285. echo " ".$text['label-contact_relation_reciprocal']."\n";
  286. echo "</td>\n";
  287. echo "<td class='vtable' align='left'>\n";
  288. echo " <select class='formfld' name='relation_reciprocal' id='relation_reciprocal' onchange=\"$('#reciprocal_label').slideToggle(400);\">\n";
  289. echo " <option value='0'>".$text['option-false']."</option>\n";
  290. echo " <option value='1'>".$text['option-true']."</option>\n";
  291. echo " </select>\n";
  292. echo "<br />\n";
  293. echo $text['description-contact_relation_reciprocal']."\n";
  294. echo "</td>\n";
  295. echo "</tr>\n";
  296. echo "</table>\n";
  297. echo "<div id='reciprocal_label' style='display: none;'>\n";
  298. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  299. echo "<tr>\n";
  300. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  301. echo " ".$text['label-contact_relation_reciprocal_label']."\n";
  302. echo "</td>\n";
  303. echo "<td width='70%' class='vtable' align='left'>\n";
  304. echo " <select class='formfld' name='relation_reciprocal_label' id='relation_reciprocal_label' onchange=\"getElementById('relation_reciprocal_label_custom').value='';\">\n";
  305. echo " <option value=''></option>\n";
  306. echo (!empty($relation_label_options)) ? implode("\n", $relation_label_options) : null;
  307. echo " </select>\n";
  308. echo " <input type='text' class='formfld' style='display: none;' name='relation_reciprocal_label_custom' id='relation_reciprocal_label_custom' value=''>\n";
  309. echo " <input type='button' id='btn_toggle_reciprocal_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('relation_reciprocal_label');\">\n";
  310. echo "<br />\n";
  311. echo $text['description-contact_relation_reciprocal_label']."\n";
  312. echo "</td>\n";
  313. echo "</tr>\n";
  314. echo "</table>\n";
  315. echo "</div>\n";
  316. }
  317. else {
  318. echo "</table>\n";
  319. }
  320. echo "</div>\n";
  321. echo "<br><br>";
  322. echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid)."'>\n";
  323. if ($action == "update") {
  324. echo "<input type='hidden' name='contact_relation_uuid' value='".escape($contact_relation_uuid)."'>\n";
  325. }
  326. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  327. echo "</form>";
  328. //include the footer
  329. require_once "resources/footer.php";
  330. ?>