contact_address_edit.php 17 KB

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