contact_phone_edit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_phone_edit') || permission_exists('contact_phone_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_phone_uuid = $_REQUEST["id"];
  41. }
  42. else {
  43. $action = "add";
  44. }
  45. //get the 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 (is_array($_POST) && @sizeof($_POST) != 0) {
  51. $phone_label = $_POST["phone_label"];
  52. $phone_label_custom = $_POST["phone_label_custom"];
  53. $phone_type_voice = $_POST["phone_type_voice"];
  54. $phone_type_fax = $_POST["phone_type_fax"];
  55. $phone_type_video = $_POST["phone_type_video"];
  56. $phone_type_text = $_POST["phone_type_text"];
  57. $phone_speed_dial = $_POST["phone_speed_dial"];
  58. $phone_country_code = $_POST["phone_country_code"];
  59. $phone_number = $_POST["phone_number"];
  60. $phone_extension = $_POST["phone_extension"];
  61. $phone_primary = $_POST["phone_primary"];
  62. $phone_description = $_POST["phone_description"];
  63. //remove any phone number formatting
  64. $phone_number = preg_replace('{(?!^\+)[\D]}', '', $phone_number);
  65. //use custom label if set
  66. $phone_label = ($phone_label_custom != '') ? $phone_label_custom : $phone_label;
  67. }
  68. //process the form data
  69. if (is_array($_POST) && @sizeof($_POST) != 0 && strlen($_POST["persistformvar"]) == 0) {
  70. //set thge uuid
  71. if ($action == "update") {
  72. $contact_phone_uuid = $_POST["contact_phone_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 numbers
  111. if ($phone_primary) {
  112. $sql = "update v_contact_phones set phone_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. //add the phone
  122. if ($action == "add" && permission_exists('contact_phone_add')) {
  123. $contact_phone_uuid = uuid();
  124. $array['contact_phones'][0]['contact_phone_uuid'] = $contact_phone_uuid;
  125. message::add($text['message-add']);
  126. }
  127. //update the phone
  128. if ($action == "update" && permission_exists('contact_phone_edit')) {
  129. $array['contact_phones'][0]['contact_phone_uuid'] = $contact_phone_uuid;
  130. message::add($text['message-update']);
  131. }
  132. //execute
  133. if (is_array($array) && @sizeof($array) != 0) {
  134. $array['contact_phones'][0]['contact_uuid'] = $contact_uuid;
  135. $array['contact_phones'][0]['domain_uuid'] = $domain_uuid;
  136. $array['contact_phones'][0]['phone_label'] = $phone_label;
  137. $array['contact_phones'][0]['phone_type_voice'] = $phone_type_voice ? 1 : null;
  138. $array['contact_phones'][0]['phone_type_fax'] = $phone_type_fax ? 1 : null;
  139. $array['contact_phones'][0]['phone_type_video'] = $phone_type_video ? 1 : null;
  140. $array['contact_phones'][0]['phone_type_text'] = $phone_type_text ? 1 : null;
  141. $array['contact_phones'][0]['phone_speed_dial'] = $phone_speed_dial;
  142. $array['contact_phones'][0]['phone_country_code'] = $phone_country_code;
  143. $array['contact_phones'][0]['phone_number'] = $phone_number;
  144. $array['contact_phones'][0]['phone_extension'] = $phone_extension;
  145. $array['contact_phones'][0]['phone_primary'] = $phone_primary ? 1 : 0;
  146. $array['contact_phones'][0]['phone_description'] = $phone_description;
  147. $database = new database;
  148. $database->app_name = 'contacts';
  149. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  150. $database->save($array);
  151. unset($array);
  152. }
  153. //redirect
  154. header("Location: contact_edit.php?id=".escape($contact_uuid));
  155. exit;
  156. }
  157. }
  158. //pre-populate the form
  159. if (count($_GET)>0 && $_POST["persistformvar"] != "true") {
  160. $contact_phone_uuid = $_GET["id"];
  161. $sql = "select * from v_contact_phones ";
  162. $sql .= "where domain_uuid = :domain_uuid ";
  163. $sql .= "and contact_phone_uuid = :contact_phone_uuid ";
  164. $parameters['domain_uuid'] = $domain_uuid;
  165. $parameters['contact_phone_uuid'] = $contact_phone_uuid;
  166. $database = new database;
  167. $row = $database->select($sql, $parameters, 'row');
  168. if (is_array($row) && @sizeof($row) != 0) {
  169. $phone_label = $row["phone_label"];
  170. $phone_type_voice = $row["phone_type_voice"];
  171. $phone_type_fax = $row["phone_type_fax"];
  172. $phone_type_video = $row["phone_type_video"];
  173. $phone_type_text = $row["phone_type_text"];
  174. $phone_speed_dial = $row["phone_speed_dial"];
  175. $phone_country_code = $row["phone_country_code"];
  176. $phone_number = $row["phone_number"];
  177. $phone_extension = $row["phone_extension"];
  178. $phone_primary = $row["phone_primary"];
  179. $phone_description = $row["phone_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_phones-edit'];
  189. }
  190. else if ($action == "add") {
  191. $document['title'] = $text['title-contact_phones-add'];
  192. }
  193. require_once "resources/header.php";
  194. //javascript to toggle input/select boxes
  195. echo "<script type='text/javascript'>\n";
  196. echo " function toggle_custom(field) {\n";
  197. echo " $('#'+field).toggle();\n";
  198. echo " document.getElementById(field).selectedIndex = 0;\n";
  199. echo " document.getElementById(field+'_custom').value = '';\n";
  200. echo " $('#'+field+'_custom').toggle();\n";
  201. echo " if ($('#'+field+'_custom').is(':visible')) { $('#'+field+'_custom').trigger('focus'); } else { $('#'+field).trigger('focus'); }\n";
  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_phones-edit']."</b>";
  210. }
  211. else if ($action == "add") {
  212. echo "<b>".$text['header-contact_phones-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. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  222. echo "<tr>\n";
  223. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  224. echo " ".$text['label-phone_label']."\n";
  225. echo "</td>\n";
  226. echo "<td width='70%' class='vtable' align='left'>\n";
  227. if (is_array($_SESSION["contact"]["phone_label"])) {
  228. sort($_SESSION["contact"]["phone_label"]);
  229. foreach($_SESSION["contact"]["phone_label"] as $row) {
  230. $phone_label_options[] = "<option value='".$row."' ".(($row == $phone_label) ? "selected='selected'" : null).">".$row."</option>";
  231. }
  232. $phone_label_found = (in_array($phone_label, $_SESSION["contact"]["phone_label"])) ? true : false;
  233. }
  234. else {
  235. $selected[$phone_label] = "selected";
  236. $default_labels[] = $text['option-work'];
  237. $default_labels[] = $text['option-home'];
  238. $default_labels[] = $text['option-mobile'];
  239. $default_labels[] = $text['option-main'];
  240. $default_labels[] = $text['option-fax'];
  241. $default_labels[] = $text['option-pager'];
  242. $default_labels[] = $text['option-voicemail'];
  243. $default_labels[] = $text['option-text'];
  244. $default_labels[] = $text['option-other'];
  245. foreach ($default_labels as $default_label) {
  246. $phone_label_options[] = "<option value='".$default_label."' ".$selected[$default_label].">".$default_label."</option>";
  247. }
  248. $phone_label_found = (in_array($phone_label, $default_labels)) ? true : false;
  249. }
  250. echo " <select class='formfld' ".((!$phone_label_found && $phone_label != '') ? "style='display: none;'" : null)." name='phone_label' id='phone_label' onchange=\"getElementById('phone_label_custom').value='';\">\n";
  251. echo " <option value=''></option>\n";
  252. echo (is_array($phone_label_options)) ? implode("\n", $phone_label_options) : null;
  253. echo " </select>\n";
  254. echo " <input type='text' class='formfld' ".(($phone_label_found || $phone_label == '') ? "style='display: none;'" : null)." name='phone_label_custom' id='phone_label_custom' value=\"".((!$phone_label_found) ? htmlentities($phone_label) : null)."\">\n";
  255. echo " <input type='button' id='btn_toggle_type' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('phone_label');\">\n";
  256. echo "<br />\n";
  257. echo $text['description-phone_label']."\n";
  258. echo "</td>\n";
  259. echo "</tr>\n";
  260. echo "<tr>\n";
  261. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  262. echo " ".$text['label-phone_type']."\n";
  263. echo "</td>\n";
  264. echo "<td class='vtable' align='left'>\n";
  265. 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";
  266. 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";
  267. 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";
  268. 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";
  269. echo "<br />\n";
  270. echo $text['description-phone_type']."\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_speed_dial']."\n";
  276. echo "</td>\n";
  277. echo "<td class='vtable' align='left'>\n";
  278. echo " <input class='formfld' type='text' name='phone_speed_dial' maxlength='255' min='0' step='1' value=\"".escape($phone_speed_dial)."\">\n";
  279. echo "<br />\n";
  280. echo $text['description-phone_speed_dial']."\n";
  281. echo "</td>\n";
  282. echo "</tr>\n";
  283. echo "<tr>\n";
  284. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  285. echo " ".$text['label-phone_country_code']."\n";
  286. echo "</td>\n";
  287. echo "<td class='vtable' align='left'>\n";
  288. echo " <input class='formfld' type='text' name='phone_country_code' maxlength='6' min='0' step='1' value=\"".escape($phone_country_code)."\">\n";
  289. echo "<br />\n";
  290. echo $text['description-phone_country_code']."\n";
  291. echo "</td>\n";
  292. echo "</tr>\n";
  293. echo "<tr>\n";
  294. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  295. echo " ".$text['label-phone_number']."\n";
  296. echo "</td>\n";
  297. echo "<td class='vtable' align='left'>\n";
  298. echo " <input class='formfld' type='text' name='phone_number' maxlength='255' min='0' step='1' value=\"".escape($phone_number)."\">\n";
  299. echo "<br />\n";
  300. echo $text['description-phone_number']."\n";
  301. echo "</td>\n";
  302. echo "</tr>\n";
  303. echo "<tr>\n";
  304. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  305. echo " ".$text['label-phone_extension']."\n";
  306. echo "</td>\n";
  307. echo "<td class='vtable' align='left'>\n";
  308. echo " <input class='formfld' type='number' name='phone_extension' min='0' step='1' maxlength='255' value=\"".escape($phone_extension)."\">\n";
  309. echo "<br />\n";
  310. echo $text['description-phone_extension']."\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-primary']."\n";
  316. echo "</td>\n";
  317. echo "<td class='vtable' align='left'>\n";
  318. echo " <select class='formfld' name='phone_primary' id='phone_primary'>\n";
  319. echo " <option value='0'>".$text['option-false']."</option>\n";
  320. echo " <option value='1' ".(($phone_primary) ? "selected" : null).">".$text['option-true']."</option>\n";
  321. echo " </select>\n";
  322. echo "<br />\n";
  323. echo $text['description-phone_primary']."\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-phone_description']."\n";
  329. echo "</td>\n";
  330. echo "<td class='vtable' align='left'>\n";
  331. echo " <input class='formfld' type='text' name='phone_description' maxlength='255' value=\"".escape($phone_description)."\">\n";
  332. echo "<br />\n";
  333. echo $text['description-phone_description']."\n";
  334. echo "</td>\n";
  335. echo "</tr>\n";
  336. echo "</table>";
  337. echo "<br><br>";
  338. echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid)."'>\n";
  339. if ($action == "update") {
  340. echo "<input type='hidden' name='contact_phone_uuid' value='".escape($contact_phone_uuid)."'>\n";
  341. }
  342. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  343. echo "</form>";
  344. //include the footer
  345. require_once "resources/footer.php";
  346. ?>