contact_phone_edit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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_phone_edit') || permission_exists('contact_phone_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. $phone_label = '';
  38. $phone_label_custom = '';
  39. $phone_type_voice = '';
  40. $phone_type_fax = '';
  41. $phone_type_video = '';
  42. $phone_type_text = '';
  43. $phone_speed_dial = '';
  44. $phone_country_code = '';
  45. $phone_number = '';
  46. $phone_extension = '';
  47. $phone_primary = '';
  48. $phone_description = '';
  49. //action add or update
  50. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  51. $action = "update";
  52. $contact_phone_uuid = $_REQUEST["id"];
  53. }
  54. else {
  55. $action = "add";
  56. }
  57. //get the 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. $phone_label = $_POST["phone_label"];
  64. $phone_label_custom = $_POST["phone_label_custom"];
  65. $phone_type_voice = $_POST["phone_type_voice"] ?? null;
  66. $phone_type_fax = $_POST["phone_type_fax"] ?? null;
  67. $phone_type_video = $_POST["phone_type_video"] ?? null;
  68. $phone_type_text = $_POST["phone_type_text"] ?? null;
  69. $phone_speed_dial = $_POST["phone_speed_dial"];
  70. $phone_country_code = $_POST["phone_country_code"];
  71. $phone_number = $_POST["phone_number"];
  72. $phone_extension = $_POST["phone_extension"];
  73. $phone_primary = $_POST["phone_primary"];
  74. $phone_description = $_POST["phone_description"];
  75. //remove any phone number formatting
  76. $phone_number = preg_replace('{(?!^\+)[\D]}', '', $phone_number);
  77. //use custom label if set
  78. $phone_label = ($phone_label_custom != '') ? $phone_label_custom : $phone_label;
  79. }
  80. //process the form data
  81. if (!empty($_POST) && empty($_POST["persistformvar"])) {
  82. //set thge uuid
  83. if ($action == "update") {
  84. $contact_phone_uuid = $_POST["contact_phone_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 numbers
  123. if ($phone_primary) {
  124. $sql = "update v_contact_phones set phone_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. //add the phone
  134. if ($action == "add" && permission_exists('contact_phone_add')) {
  135. $contact_phone_uuid = uuid();
  136. $array['contact_phones'][0]['contact_phone_uuid'] = $contact_phone_uuid;
  137. message::add($text['message-add']);
  138. }
  139. //update the phone
  140. if ($action == "update" && permission_exists('contact_phone_edit')) {
  141. $array['contact_phones'][0]['contact_phone_uuid'] = $contact_phone_uuid;
  142. message::add($text['message-update']);
  143. }
  144. //execute
  145. if (!empty($array)) {
  146. $array['contact_phones'][0]['contact_uuid'] = $contact_uuid;
  147. $array['contact_phones'][0]['domain_uuid'] = $domain_uuid;
  148. $array['contact_phones'][0]['phone_label'] = $phone_label;
  149. $array['contact_phones'][0]['phone_type_voice'] = $phone_type_voice ? 1 : null;
  150. $array['contact_phones'][0]['phone_type_fax'] = $phone_type_fax ? 1 : null;
  151. $array['contact_phones'][0]['phone_type_video'] = $phone_type_video ? 1 : null;
  152. $array['contact_phones'][0]['phone_type_text'] = $phone_type_text ? 1 : null;
  153. $array['contact_phones'][0]['phone_speed_dial'] = $phone_speed_dial;
  154. $array['contact_phones'][0]['phone_country_code'] = $phone_country_code;
  155. $array['contact_phones'][0]['phone_number'] = $phone_number;
  156. $array['contact_phones'][0]['phone_extension'] = $phone_extension;
  157. $array['contact_phones'][0]['phone_primary'] = $phone_primary ? 1 : 0;
  158. $array['contact_phones'][0]['phone_description'] = $phone_description;
  159. $database = new database;
  160. $database->app_name = 'contacts';
  161. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  162. $database->save($array);
  163. unset($array);
  164. }
  165. //redirect
  166. header("Location: contact_edit.php?id=".escape($contact_uuid));
  167. exit;
  168. }
  169. }
  170. //pre-populate the form
  171. if (!empty($_GET) && empty($_POST["persistformvar"])) {
  172. $contact_phone_uuid = $_GET["id"] ?? '';
  173. $sql = "select * from v_contact_phones ";
  174. $sql .= "where domain_uuid = :domain_uuid ";
  175. $sql .= "and contact_phone_uuid = :contact_phone_uuid ";
  176. $parameters['domain_uuid'] = $domain_uuid;
  177. $parameters['contact_phone_uuid'] = $contact_phone_uuid;
  178. $database = new database;
  179. $row = $database->select($sql, $parameters, 'row');
  180. if (!empty($row)) {
  181. $phone_label = $row["phone_label"];
  182. $phone_type_voice = $row["phone_type_voice"];
  183. $phone_type_fax = $row["phone_type_fax"];
  184. $phone_type_video = $row["phone_type_video"];
  185. $phone_type_text = $row["phone_type_text"];
  186. $phone_speed_dial = $row["phone_speed_dial"];
  187. $phone_country_code = $row["phone_country_code"];
  188. $phone_number = $row["phone_number"];
  189. $phone_extension = $row["phone_extension"];
  190. $phone_primary = $row["phone_primary"];
  191. $phone_description = $row["phone_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_phones-edit'];
  201. }
  202. else if ($action == "add") {
  203. $document['title'] = $text['title-contact_phones-add'];
  204. }
  205. require_once "resources/header.php";
  206. //javascript to toggle input/select boxes
  207. echo "<script type='text/javascript'>\n";
  208. echo " function toggle_custom(field) {\n";
  209. echo " $('#'+field).toggle();\n";
  210. echo " document.getElementById(field).selectedIndex = 0;\n";
  211. echo " document.getElementById(field+'_custom').value = '';\n";
  212. echo " $('#'+field+'_custom').toggle();\n";
  213. echo " if ($('#'+field+'_custom').is(':visible')) { $('#'+field+'_custom').trigger('focus'); } else { $('#'+field).trigger('focus'); }\n";
  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_phones-edit']."</b>";
  222. }
  223. else if ($action == "add") {
  224. echo "<b>".$text['header-contact_phones-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. echo "<div class='card'>\n";
  234. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  235. echo "<tr>\n";
  236. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  237. echo " ".$text['label-phone_label']."\n";
  238. echo "</td>\n";
  239. echo "<td width='70%' class='vtable' align='left'>\n";
  240. if (!empty($_SESSION["contact"]["phone_label"])) {
  241. sort($_SESSION["contact"]["phone_label"]);
  242. foreach($_SESSION["contact"]["phone_label"] as $row) {
  243. $phone_label_options[] = "<option value='".$row."' ".(($row == $phone_label) ? "selected='selected'" : null).">".$row."</option>";
  244. }
  245. $phone_label_found = (in_array($phone_label, $_SESSION["contact"]["phone_label"])) ? true : false;
  246. }
  247. else {
  248. $selected[$phone_label] = "selected";
  249. $default_labels[] = $text['option-work'];
  250. $default_labels[] = $text['option-home'];
  251. $default_labels[] = $text['option-mobile'];
  252. $default_labels[] = $text['option-main'];
  253. $default_labels[] = $text['option-fax'];
  254. $default_labels[] = $text['option-pager'];
  255. $default_labels[] = $text['option-voicemail'];
  256. $default_labels[] = $text['option-text'];
  257. $default_labels[] = $text['option-other'];
  258. foreach ($default_labels as $default_label) {
  259. $phone_label_options[] = "<option value='".$default_label."' ".!empty($selected[$default_label]).">".$default_label."</option>";
  260. }
  261. $phone_label_found = (in_array(!empty($phone_label), $default_labels)) ? true : false;
  262. }
  263. echo " <select class='formfld' ".((!empty($phone_label) && !$phone_label_found) ? "style='display: none;'" : null)." name='phone_label' id='phone_label' onchange=\"getElementById('phone_label_custom').value='';\">\n";
  264. echo " <option value=''></option>\n";
  265. echo (!empty($phone_label_options)) ? implode("\n", $phone_label_options) : null;
  266. echo " </select>\n";
  267. echo " <input type='text' class='formfld' ".((empty($phone_label) || $phone_label_found) ? "style='display: none;'" : null)." name='phone_label_custom' id='phone_label_custom' value=\"".((!$phone_label_found) ? htmlentities($phone_label ?? '') : null)."\">\n";
  268. echo " <input type='button' id='btn_toggle_type' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('phone_label');\">\n";
  269. echo "<br />\n";
  270. echo $text['description-phone_label']."\n";
  271. echo "</td>\n";
  272. echo "</tr>\n";
  273. echo "<tr>\n";
  274. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  275. echo " ".$text['label-phone_type']."\n";
  276. echo "</td>\n";
  277. echo "<td class='vtable' align='left'>\n";
  278. echo " <label><input type='checkbox' name='phone_type_voice' id='phone_type_voice' value='1' ".(($phone_type_voice) ? "checked='checked'" : null)."> ".$text['label-voice']."</label>&nbsp;\n";
  279. echo " <label><input type='checkbox' name='phone_type_fax' id='phone_type_fax' value='1' ".(($phone_type_fax) ? "checked='checked'" : null)."> ".$text['label-fax']."</label>&nbsp;\n";
  280. echo " <label><input type='checkbox' name='phone_type_video' id='phone_type_video' value='1' ".(($phone_type_video) ? "checked='checked'" : null)."> ".$text['label-video']."</label>&nbsp;\n";
  281. echo " <label><input type='checkbox' name='phone_type_text' id='phone_type_text' value='1' ".(($phone_type_text) ? "checked='checked'" : null)."> ".$text['label-text']."</label>\n";
  282. echo "<br />\n";
  283. echo $text['description-phone_type']."\n";
  284. echo "</td>\n";
  285. echo "</tr>\n";
  286. echo "<tr>\n";
  287. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  288. echo " ".$text['label-phone_speed_dial']."\n";
  289. echo "</td>\n";
  290. echo "<td class='vtable' align='left'>\n";
  291. echo " <input class='formfld' type='text' name='phone_speed_dial' maxlength='255' min='0' step='1' value=\"".escape($phone_speed_dial)."\">\n";
  292. echo "<br />\n";
  293. echo $text['description-phone_speed_dial']."\n";
  294. echo "</td>\n";
  295. echo "</tr>\n";
  296. echo "<tr>\n";
  297. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  298. echo " ".$text['label-phone_country_code']."\n";
  299. echo "</td>\n";
  300. echo "<td class='vtable' align='left'>\n";
  301. echo " <input class='formfld' type='text' name='phone_country_code' maxlength='6' min='0' step='1' value=\"".escape($phone_country_code)."\">\n";
  302. echo "<br />\n";
  303. echo $text['description-phone_country_code']."\n";
  304. echo "</td>\n";
  305. echo "</tr>\n";
  306. echo "<tr>\n";
  307. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  308. echo " ".$text['label-phone_number']."\n";
  309. echo "</td>\n";
  310. echo "<td class='vtable' align='left'>\n";
  311. echo " <input class='formfld' type='text' name='phone_number' maxlength='255' min='0' step='1' value=\"".escape($phone_number)."\">\n";
  312. echo "<br />\n";
  313. echo $text['description-phone_number']."\n";
  314. echo "</td>\n";
  315. echo "</tr>\n";
  316. echo "<tr>\n";
  317. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  318. echo " ".$text['label-phone_extension']."\n";
  319. echo "</td>\n";
  320. echo "<td class='vtable' align='left'>\n";
  321. echo " <input class='formfld' type='number' name='phone_extension' min='0' step='1' maxlength='255' value=\"".escape($phone_extension)."\">\n";
  322. echo "<br />\n";
  323. echo $text['description-phone_extension']."\n";
  324. echo "</td>\n";
  325. echo "</tr>\n";
  326. echo "<tr>\n";
  327. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  328. echo " ".$text['label-primary']."\n";
  329. echo "</td>\n";
  330. echo "<td class='vtable' align='left'>\n";
  331. echo " <select class='formfld' name='phone_primary' id='phone_primary'>\n";
  332. echo " <option value='0'>".$text['option-false']."</option>\n";
  333. echo " <option value='1' ".(($phone_primary) ? "selected" : null).">".$text['option-true']."</option>\n";
  334. echo " </select>\n";
  335. echo "<br />\n";
  336. echo $text['description-phone_primary']."\n";
  337. echo "</td>\n";
  338. echo "</tr>\n";
  339. echo "<tr>\n";
  340. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  341. echo " ".$text['label-phone_description']."\n";
  342. echo "</td>\n";
  343. echo "<td class='vtable' align='left'>\n";
  344. echo " <input class='formfld' type='text' name='phone_description' maxlength='255' value=\"".escape($phone_description)."\">\n";
  345. echo "<br />\n";
  346. echo $text['description-phone_description']."\n";
  347. echo "</td>\n";
  348. echo "</tr>\n";
  349. echo "</table>";
  350. echo "</div>\n";
  351. echo "<br><br>";
  352. echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid)."'>\n";
  353. if ($action == "update") {
  354. echo "<input type='hidden' name='contact_phone_uuid' value='".escape($contact_phone_uuid)."'>\n";
  355. }
  356. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  357. echo "</form>";
  358. //include the footer
  359. require_once "resources/footer.php";
  360. ?>