contact_address_edit.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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_address_edit') || permission_exists('contact_address_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. //set the defaults
  37. $address_label = '';
  38. $address_label_custom = '';
  39. $address_street = '';
  40. $address_extended = '';
  41. $address_community = '';
  42. $address_locality = '';
  43. $address_region = '';
  44. $address_postal_code = '';
  45. $address_country = '';
  46. $address_latitude = '';
  47. $address_longitude = '';
  48. $address_description = '';
  49. //action add or update
  50. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  51. $action = "update";
  52. $contact_address_uuid = $_REQUEST["id"];
  53. }
  54. else {
  55. $action = "add";
  56. }
  57. //get the contact uuid
  58. if (!empty($_GET["contact_uuid"]) && is_uuid($_GET["contact_uuid"])) {
  59. $contact_uuid = $_GET["contact_uuid"];
  60. }
  61. //get http post variables and set them to php variables
  62. if (!empty($_POST)) {
  63. $address_type = $_POST["address_type"];
  64. $address_label = $_POST["address_label"];
  65. $address_label_custom = $_POST["address_label_custom"];
  66. $address_street = $_POST["address_street"];
  67. $address_extended = $_POST["address_extended"];
  68. $address_community = $_POST["address_community"];
  69. $address_locality = $_POST["address_locality"];
  70. $address_region = $_POST["address_region"];
  71. $address_postal_code = $_POST["address_postal_code"];
  72. $address_country = $_POST["address_country"];
  73. $address_latitude = $_POST["address_latitude"];
  74. $address_longitude = $_POST["address_longitude"];
  75. $address_primary = $_POST["address_primary"];
  76. $address_description = $_POST["address_description"];
  77. //use custom label if set
  78. $address_label = !empty($address_label_custom) ? $address_label_custom : $address_label;
  79. }
  80. //process the form data
  81. if (!empty($_POST) && empty($_POST["persistformvar"])) {
  82. //set the uuid
  83. if ($action == "update") {
  84. $contact_address_uuid = $_POST["contact_address_uuid"];
  85. }
  86. //validate the token
  87. $token = new token;
  88. if (!$token->validate($_SERVER['PHP_SELF'])) {
  89. message::add($text['message-invalid_token'],'negative');
  90. header('Location: contacts.php');
  91. exit;
  92. }
  93. //check for all required data
  94. $msg = '';
  95. if (!empty($msg) && empty($_POST["persistformvar"])) {
  96. require_once "resources/header.php";
  97. require_once "resources/persist_form_var.php";
  98. echo "<div align='center'>\n";
  99. echo "<table><tr><td>\n";
  100. echo $msg."<br />";
  101. echo "</td></tr></table>\n";
  102. persistformvar($_POST);
  103. echo "</div>\n";
  104. require_once "resources/footer.php";
  105. return;
  106. }
  107. //add or update the database
  108. if (empty($_POST["persistformvar"])) {
  109. //update last modified
  110. $array['contacts'][0]['contact_uuid'] = $contact_uuid;
  111. $array['contacts'][0]['domain_uuid'] = $domain_uuid;
  112. $array['contacts'][0]['last_mod_date'] = 'now()';
  113. $array['contacts'][0]['last_mod_user'] = $_SESSION['username'];
  114. $p = permissions::new();
  115. $p->add('contact_edit', 'temp');
  116. $database = new database;
  117. $database->app_name = 'contacts';
  118. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  119. $database->save($array);
  120. unset($array);
  121. $p->delete('contact_edit', 'temp');
  122. //if primary, unmark other primary addresses
  123. if ($email_primary) {
  124. $sql = "update v_contact_addresses set address_primary = 0 ";
  125. $sql .= "where domain_uuid = :domain_uuid ";
  126. $sql .= "and contact_uuid = :contact_uuid ";
  127. $parameters['domain_uuid'] = $domain_uuid;
  128. $parameters['contact_uuid'] = $contact_uuid;
  129. $database = new database;
  130. $database->execute($sql, $parameters);
  131. unset($sql, $parameters);
  132. }
  133. if ($action == "add" && permission_exists('contact_address_add')) {
  134. $contact_address_uuid = uuid();
  135. $array['contact_addresses'][0]['contact_address_uuid'] = $contact_address_uuid;
  136. message::add($text['message-add']);
  137. }
  138. if ($action == "update" && permission_exists('contact_address_edit')) {
  139. $array['contact_addresses'][0]['contact_address_uuid'] = $contact_address_uuid;
  140. message::add($text['message-update']);
  141. }
  142. if (!empty($array)) {
  143. $array['contact_addresses'][0]['domain_uuid'] = $_SESSION['domain_uuid'];
  144. $array['contact_addresses'][0]['contact_uuid'] = $contact_uuid;
  145. $array['contact_addresses'][0]['address_type'] = $address_type;
  146. $array['contact_addresses'][0]['address_label'] = $address_label;
  147. $array['contact_addresses'][0]['address_street'] = $address_street;
  148. $array['contact_addresses'][0]['address_extended'] = $address_extended;
  149. $array['contact_addresses'][0]['address_community'] = $address_community;
  150. $array['contact_addresses'][0]['address_locality'] = $address_locality;
  151. $array['contact_addresses'][0]['address_region'] = $address_region;
  152. $array['contact_addresses'][0]['address_postal_code'] = $address_postal_code;
  153. $array['contact_addresses'][0]['address_country'] = $address_country;
  154. $array['contact_addresses'][0]['address_latitude'] = $address_latitude;
  155. $array['contact_addresses'][0]['address_longitude'] = $address_longitude;
  156. $array['contact_addresses'][0]['address_primary'] = $address_primary ? 1 : 0;
  157. $array['contact_addresses'][0]['address_description'] = $address_description;
  158. $database = new database;
  159. $database->app_name = 'contacts';
  160. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  161. $database->save($array);
  162. unset($array);
  163. }
  164. header("Location: contact_edit.php?id=".$contact_uuid);
  165. exit;
  166. }
  167. }
  168. //pre-populate the form
  169. if (!empty($_GET) && empty($_POST["persistformvar"])) {
  170. $contact_address_uuid = $_GET["id"] ?? '';
  171. $sql = "select * from v_contact_addresses ";
  172. $sql .= "where domain_uuid = :domain_uuid ";
  173. $sql .= "and contact_address_uuid = :contact_address_uuid ";
  174. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  175. $parameters['contact_address_uuid'] = $contact_address_uuid;
  176. $database = new database;
  177. $row = $database->select($sql, $parameters, 'row');
  178. if (!empty($row)) {
  179. $address_type = $row["address_type"];
  180. $address_label = $row["address_label"];
  181. $address_street = $row["address_street"];
  182. $address_extended = $row["address_extended"];
  183. $address_community = $row["address_community"];
  184. $address_locality = $row["address_locality"];
  185. $address_region = $row["address_region"];
  186. $address_postal_code = $row["address_postal_code"];
  187. $address_country = $row["address_country"];
  188. $address_latitude = $row["address_latitude"];
  189. $address_longitude = $row["address_longitude"];
  190. $address_primary = $row["address_primary"];
  191. $address_description = $row["address_description"];
  192. }
  193. unset($sql, $parameters, $row);
  194. }
  195. //create token
  196. $object = new token;
  197. $token = $object->create($_SERVER['PHP_SELF']);
  198. //show the header
  199. if ($action == "update") {
  200. $document['title'] = $text['title-contact_addresses-edit'];
  201. }
  202. else if ($action == "add") {
  203. $document['title'] = $text['title-contact_addresses-add'];
  204. }
  205. require_once "resources/header.php";
  206. //javascript to toggle input/select boxes
  207. echo "<script type='text/javascript'>";
  208. echo " function toggle_custom(field) {";
  209. echo " $('#'+field).toggle();";
  210. echo " document.getElementById(field).selectedIndex = 0;";
  211. echo " document.getElementById(field+'_custom').value = '';";
  212. echo " $('#'+field+'_custom').toggle();";
  213. echo " if ($('#'+field+'_custom').is(':visible')) { $('#'+field+'_custom').trigger('focus'); } else { $('#'+field).trigger('focus'); }";
  214. echo " }";
  215. echo "</script>";
  216. //show the content
  217. echo "<form method='post' name='frm' id='frm'>\n";
  218. echo "<div class='action_bar' id='action_bar'>\n";
  219. echo " <div class='heading'>";
  220. if ($action == "update") {
  221. echo "<b>".$text['header-contact_addresses-edit']."</b>";
  222. }
  223. else if ($action == "add") {
  224. echo "<b>".$text['header-contact_addresses-add']."</b>";
  225. }
  226. echo " </div>\n";
  227. echo " <div class='actions'>\n";
  228. 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)]);
  229. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save']);
  230. echo " </div>\n";
  231. echo " <div style='clear: both;'></div>\n";
  232. echo "</div>\n";
  233. if ($action == "update") {
  234. echo $text['description-contact_addresses-edit'];
  235. }
  236. else if ($action == "add") {
  237. echo $text['description-contact_addresses-add'];
  238. }
  239. echo "<br /><br />\n";
  240. echo "<div class='card'>\n";
  241. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  242. echo "<tr>\n";
  243. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  244. echo " ".$text['label-address_label']."\n";
  245. echo "</td>\n";
  246. echo "<td width='70%' class='vtable' align='left'>\n";
  247. if (!empty($_SESSION["contact"]["address_label"])) {
  248. sort($_SESSION["contact"]["address_label"]);
  249. foreach($_SESSION["contact"]["address_label"] as $row) {
  250. $address_label_options[] = "<option value='".$row."' ".(($row == $address_label) ? "selected='selected'" : null).">".$row."</option>";
  251. }
  252. $address_label_found = (in_array($address_label, $_SESSION["contact"]["address_label"])) ? true : false;
  253. }
  254. else {
  255. $selected[$address_label] = "selected";
  256. $default_labels[] = $text['option-work'];
  257. $default_labels[] = $text['option-home'];
  258. $default_labels[] = $text['option-mailing'];
  259. $default_labels[] = $text['option-physical'];
  260. $default_labels[] = $text['option-shipping'];
  261. $default_labels[] = $text['option-billing'];
  262. $default_labels[] = $text['option-other'];
  263. foreach ($default_labels as $default_label) {
  264. $address_label_options[] = "<option value='".$default_label."' ".!empty($selected[$default_label]).">".$default_label."</option>";
  265. }
  266. $address_label_found = (in_array($address_label, $default_labels)) ? true : false;
  267. }
  268. echo " <select class='formfld' ".((!empty($address_label) && !$address_label_found) ? "style='display: none;'" : null)." name='address_label' id='address_label' onchange=\"getElementById('address_label_custom').value='';\">\n";
  269. echo " <option value=''></option>\n";
  270. echo (!empty($address_label_options)) ? implode("\n", $address_label_options) : null;
  271. echo " </select>\n";
  272. echo " <input type='text' class='formfld' ".((empty($address_label) || $address_label_found) ? "style='display: none;'" : null)." name='address_label_custom' id='address_label_custom' value=\"".((!$address_label_found) ? htmlentities($address_label) : null)."\">\n";
  273. echo " <input type='button' id='btn_toggle_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('address_label');\">\n";
  274. echo "<br />\n";
  275. echo $text['description-address_label']."\n";
  276. echo "</td>\n";
  277. echo "</tr>\n";
  278. echo "<tr>\n";
  279. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  280. echo " ".$text['label-address_type']."\n";
  281. echo "</td>\n";
  282. echo "<td class='vtable' align='left'>\n";
  283. echo " <select class='formfld' name='address_type' id='address_type'>\n";
  284. echo " <option value=''></option>\n";
  285. $vcard_address_types = array(
  286. 'work' => $text['option-work'],
  287. 'home' => $text['option-home'],
  288. 'dom' => $text['option-dom'],
  289. 'intl' => $text['option-intl'],
  290. 'postal' => $text['option-postal'],
  291. 'parcel' => $text['option-parcel'],
  292. 'pref' => $text['option-pref']
  293. );
  294. foreach ($vcard_address_types as $vcard_address_type_value => $vcard_address_type_label) {
  295. echo " <option value='".$vcard_address_type_value."' ".(!empty($address_type) && $address_type == $vcard_address_type_value ? "selected" : null).">".$vcard_address_type_label."</option>\n";
  296. }
  297. echo " </select>\n";
  298. echo "<br />\n";
  299. echo $text['description-address_type']."\n";
  300. echo "</td>\n";
  301. echo "</tr>\n";
  302. echo "<tr>\n";
  303. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  304. echo " ".$text['label-address_address']."\n";
  305. echo "</td>\n";
  306. echo "<td class='vtable' align='left'>\n";
  307. echo " <textarea class='formfld' name='address_street' style='margin-bottom: 3px;'>".$address_street."</textarea><br>\n";
  308. echo " <input class='formfld' type='text' name='address_extended' maxlength='255' value=\"".escape($address_extended)."\">\n";
  309. echo "<br />\n";
  310. echo $text['description-address_address']."\n";
  311. echo "</td>\n";
  312. echo "</tr>\n";
  313. echo "<tr>\n";
  314. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  315. echo " ".$text['label-address_community']."\n";
  316. echo "</td>\n";
  317. echo "<td class='vtable' align='left'>\n";
  318. echo " <input class='formfld' type='text' name='address_community' maxlength='255' value=\"".escape($address_community)."\">\n";
  319. echo "<br />\n";
  320. echo $text['description-address_community']."\n";
  321. echo "</td>\n";
  322. echo "</tr>\n";
  323. echo "<tr>\n";
  324. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  325. echo " ".$text['label-address_locality']."\n";
  326. echo "</td>\n";
  327. echo "<td class='vtable' align='left'>\n";
  328. echo " <input class='formfld' type='text' name='address_locality' maxlength='255' value=\"".escape($address_locality)."\">\n";
  329. echo "<br />\n";
  330. echo $text['description-address_locality']."\n";
  331. echo "</td>\n";
  332. echo "</tr>\n";
  333. echo "<tr>\n";
  334. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  335. echo " ".$text['label-address_region']."\n";
  336. echo "</td>\n";
  337. echo "<td class='vtable' align='left'>\n";
  338. echo " <input class='formfld' type='text' name='address_region' maxlength='255' value=\"".escape($address_region)."\">\n";
  339. echo "<br />\n";
  340. echo $text['description-address_region']."\n";
  341. echo "</td>\n";
  342. echo "</tr>\n";
  343. echo "<tr>\n";
  344. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  345. echo " ".$text['label-address_postal_code']."\n";
  346. echo "</td>\n";
  347. echo "<td class='vtable' align='left'>\n";
  348. echo " <input class='formfld' type='text' name='address_postal_code' maxlength='255' value=\"".escape($address_postal_code)."\">\n";
  349. echo "<br />\n";
  350. echo $text['description-address_postal_code']."\n";
  351. echo "</td>\n";
  352. echo "</tr>\n";
  353. echo "<tr>\n";
  354. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  355. echo " ".$text['label-address_country']."\n";
  356. echo "</td>\n";
  357. echo "<td class='vtable' align='left'>\n";
  358. echo " <input class='formfld' type='text' name='address_country' maxlength='255' value=\"".escape($address_country)."\">\n";
  359. echo "<br />\n";
  360. echo $text['description-address_country']."\n";
  361. echo "</td>\n";
  362. echo "</tr>\n";
  363. echo "<tr>\n";
  364. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  365. echo " ".$text['label-address_latitude']."\n";
  366. echo "</td>\n";
  367. echo "<td class='vtable' align='left'>\n";
  368. echo " <input class='formfld' type='number' name='address_latitude' maxlength='255' min='-90' max='90' step='0.0000001' value=\"".escape($address_latitude)."\">\n";
  369. echo "<br />\n";
  370. echo $text['description-address_latitude']."\n";
  371. echo "</td>\n";
  372. echo "</tr>\n";
  373. echo "<tr>\n";
  374. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  375. echo " ".$text['label-address_longitude']."\n";
  376. echo "</td>\n";
  377. echo "<td class='vtable' align='left'>\n";
  378. echo " <input class='formfld' type='number' name='address_longitude' maxlength='255' min='-180' max='180' step='0.0000001' value=\"".escape($address_longitude)."\">\n";
  379. echo "<br />\n";
  380. echo $text['description-address_longitude']."\n";
  381. echo "</td>\n";
  382. echo "</tr>\n";
  383. echo "<tr>\n";
  384. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  385. echo " ".$text['label-primary']."\n";
  386. echo "</td>\n";
  387. echo "<td class='vtable' align='left'>\n";
  388. echo " <select class='formfld' name='address_primary' id='address_primary'>\n";
  389. echo " <option value='0'>".$text['option-false']."</option>\n";
  390. echo " <option value='1' ".(!empty($address_primary) && $address_primary ? "selected" : null).">".$text['option-true']."</option>\n";
  391. echo " </select>\n";
  392. echo "<br />\n";
  393. echo $text['description-address_primary']."\n";
  394. echo "</td>\n";
  395. echo "</tr>\n";
  396. echo "<tr>\n";
  397. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  398. echo " ".$text['label-address_description']."\n";
  399. echo "</td>\n";
  400. echo "<td class='vtable' align='left'>\n";
  401. echo " <input class='formfld' type='text' name='address_description' maxlength='255' value=\"".escape($address_description)."\">\n";
  402. echo "<br />\n";
  403. echo $text['description-address_description']."\n";
  404. echo "</td>\n";
  405. echo "</tr>\n";
  406. echo "</table>";
  407. echo "</div>\n";
  408. echo "<br><br>";
  409. echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid)."'>\n";
  410. if ($action == "update") {
  411. echo "<input type='hidden' name='contact_address_uuid' value='".escape($contact_address_uuid)."'>\n";
  412. }
  413. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  414. echo "</form>";
  415. //include the footer
  416. require_once "resources/footer.php";
  417. ?>