contact_email_edit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_email_edit') || permission_exists('contact_email_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. $email_label = '';
  38. $email_address = '';
  39. $email_description = '';
  40. //action add or update
  41. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  42. $action = "update";
  43. $contact_email_uuid = $_REQUEST["id"];
  44. }
  45. else {
  46. $action = "add";
  47. }
  48. if (!empty($_GET["contact_uuid"]) && is_uuid($_GET["contact_uuid"])) {
  49. $contact_uuid = $_GET["contact_uuid"];
  50. }
  51. //get http post variables and set them to php variables
  52. if (!empty($_POST)) {
  53. $email_label = $_POST["email_label"];
  54. $email_label_custom = $_POST["email_label_custom"];
  55. $email_address = $_POST["email_address"];
  56. $email_primary = $_POST["email_primary"];
  57. $email_description = $_POST["email_description"];
  58. //use custom label if set
  59. $email_label = !empty($email_label_custom) ? $email_label_custom : $email_label;
  60. }
  61. //process the form data
  62. if (!empty($_POST) && empty($_POST["persistformvar"])) {
  63. //set the uuid
  64. if ($action == "update") {
  65. $contact_email_uuid = $_POST["contact_email_uuid"];
  66. }
  67. //validate the token
  68. $token = new token;
  69. if (!$token->validate($_SERVER['PHP_SELF'])) {
  70. message::add($text['message-invalid_token'],'negative');
  71. header('Location: contacts.php');
  72. exit;
  73. }
  74. //check for all required data
  75. $msg = '';
  76. if (!empty($msg) && empty($_POST["persistformvar"])) {
  77. require_once "resources/header.php";
  78. require_once "resources/persist_form_var.php";
  79. echo "<div align='center'>\n";
  80. echo "<table><tr><td>\n";
  81. echo $msg."<br />";
  82. echo "</td></tr></table>\n";
  83. persistformvar($_POST);
  84. echo "</div>\n";
  85. require_once "resources/footer.php";
  86. return;
  87. }
  88. //add or update the database
  89. if (empty($_POST["persistformvar"])) {
  90. //update last modified
  91. $array['contacts'][0]['contact_uuid'] = $contact_uuid;
  92. $array['contacts'][0]['domain_uuid'] = $domain_uuid;
  93. $array['contacts'][0]['last_mod_date'] = 'now()';
  94. $array['contacts'][0]['last_mod_user'] = $_SESSION['username'];
  95. $p = permissions::new();
  96. $p->add('contact_edit', 'temp');
  97. $database = new database;
  98. $database->app_name = 'contacts';
  99. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  100. $database->save($array);
  101. unset($array);
  102. $p->delete('contact_edit', 'temp');
  103. //if primary, unmark other primary emails
  104. if ($email_primary) {
  105. $sql = "update v_contact_emails set email_primary = 0 ";
  106. $sql .= "where domain_uuid = :domain_uuid ";
  107. $sql .= "and contact_uuid = :contact_uuid ";
  108. $parameters['domain_uuid'] = $domain_uuid;
  109. $parameters['contact_uuid'] = $contact_uuid;
  110. $database = new database;
  111. $database->execute($sql, $parameters);
  112. unset($sql, $parameters);
  113. }
  114. if ($action == "add" && permission_exists('contact_email_add')) {
  115. $contact_email_uuid = uuid();
  116. $array['contact_emails'][0]['contact_email_uuid'] = $contact_email_uuid;
  117. message::add($text['message-add']);
  118. }
  119. if ($action == "update" && permission_exists('contact_email_edit')) {
  120. $array['contact_emails'][0]['contact_email_uuid'] = $contact_email_uuid;
  121. message::add($text['message-update']);
  122. }
  123. if (!empty($array)) {
  124. $array['contact_emails'][0]['domain_uuid'] = $_SESSION['domain_uuid'];
  125. $array['contact_emails'][0]['contact_uuid'] = $contact_uuid;
  126. $array['contact_emails'][0]['email_label'] = $email_label;
  127. $array['contact_emails'][0]['email_address'] = $email_address;
  128. $array['contact_emails'][0]['email_primary'] = $email_primary ? 1 : 0;
  129. $array['contact_emails'][0]['email_description'] = $email_description;
  130. $database = new database;
  131. $database->app_name = 'contacts';
  132. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  133. $database->save($array);
  134. unset($array);
  135. }
  136. header("Location: contact_edit.php?id=".$contact_uuid);
  137. exit;
  138. }
  139. }
  140. //pre-populate the form
  141. if (!empty($_GET) && empty($_POST["persistformvar"])) {
  142. $contact_email_uuid = $_GET["id"] ?? '';
  143. $sql = "select * from v_contact_emails ";
  144. $sql .= "where domain_uuid = :domain_uuid ";
  145. $sql .= "and contact_email_uuid = :contact_email_uuid ";
  146. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  147. $parameters['contact_email_uuid'] = $contact_email_uuid;
  148. $database = new database;
  149. $row = $database->select($sql, $parameters, 'row');;
  150. if (!empty($row)) {
  151. $email_label = $row["email_label"];
  152. $email_address = $row["email_address"];
  153. $email_primary = $row["email_primary"];
  154. $email_description = $row["email_description"];
  155. }
  156. unset($sql, $parameters, $row);
  157. }
  158. //create token
  159. $object = new token;
  160. $token = $object->create($_SERVER['PHP_SELF']);
  161. //show the header
  162. if ($action == "update") {
  163. $document['title'] = $text['title-contact_email-edit'];
  164. }
  165. else if ($action == "add") {
  166. $document['title'] = $text['title-contact_email-add'];
  167. }
  168. require_once "resources/header.php";
  169. //javascript to toggle input/select boxes
  170. echo "<script type='text/javascript'>";
  171. echo " function toggle_custom(field) {";
  172. echo " $('#'+field).toggle();";
  173. echo " document.getElementById(field).selectedIndex = 0;";
  174. echo " document.getElementById(field+'_custom').value = '';";
  175. echo " $('#'+field+'_custom').toggle();";
  176. echo " if ($('#'+field+'_custom').is(':visible')) { $('#'+field+'_custom').trigger('focus'); } else { $('#'+field).trigger('focus'); }";
  177. echo " }";
  178. echo "</script>";
  179. //show the content
  180. echo "<form method='post' name='frm' id='frm'>\n";
  181. echo "<div class='action_bar' id='action_bar'>\n";
  182. echo " <div class='heading'>";
  183. if ($action == "update") {
  184. echo "<b>".$text['header-contact_email-edit']."</b>";
  185. }
  186. else if ($action == "add") {
  187. echo "<b>".$text['header-contact_email-add']."</b>";
  188. }
  189. echo " </div>\n";
  190. echo " <div class='actions'>\n";
  191. 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 ?? '')]);
  192. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save']);
  193. echo " </div>\n";
  194. echo " <div style='clear: both;'></div>\n";
  195. echo "</div>\n";
  196. if ($action == "update") {
  197. echo $text['description-contact_email-edit'];
  198. }
  199. else if ($action == "add") {
  200. echo $text['description-contact_email-add'];
  201. }
  202. echo "<br /><br />\n";
  203. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  204. echo "<tr>\n";
  205. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  206. echo " ".$text['label-email_label']."\n";
  207. echo "</td>\n";
  208. echo "<td width='70%' class='vtable' align='left'>\n";
  209. if (!empty($_SESSION["contact"]["email_label"])) {
  210. sort($_SESSION["contact"]["email_label"]);
  211. foreach($_SESSION["contact"]["email_label"] as $row) {
  212. $email_label_options[] = "<option value='".$row."' ".(($row == $email_label) ? "selected='selected'" : null).">".$row."</option>";
  213. }
  214. $email_label_found = (in_array($email_label, $_SESSION["contact"]["email_label"])) ? true : false;
  215. }
  216. else {
  217. $selected[$email_label] = "selected";
  218. $default_labels[] = $text['option-work'];
  219. $default_labels[] = $text['option-home'];
  220. $default_labels[] = $text['option-other'];
  221. foreach ($default_labels as $default_label) {
  222. $email_label_options[] = "<option value='".$default_label."' ".!empty($selected[$default_label]).">".$default_label."</option>";
  223. }
  224. $email_label_found = (in_array($email_label, $default_labels)) ? true : false;
  225. }
  226. echo " <select class='formfld' ".((!empty($email_label) && !$email_label_found) ? "style='display: none;'" : null)." name='email_label' id='email_label' onchange=\"getElementById('email_label_custom').value='';\">\n";
  227. echo " <option value=''></option>\n";
  228. echo (!empty($email_label_options)) ? implode("\n", $email_label_options) : null;
  229. echo " </select>\n";
  230. echo " <input type='text' class='formfld' ".((empty($email_label) || $email_label_found) ? "style='display: none;'" : null)." name='email_label_custom' id='email_label_custom' value=\"".((!$email_label_found) ? htmlentities($email_label ?? '') : null)."\">\n";
  231. echo " <input type='button' id='btn_toggle_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('email_label');\">\n";
  232. echo "<br />\n";
  233. echo !empty($text['description-email_label'])."\n";
  234. echo "</td>\n";
  235. echo "</tr>\n";
  236. echo "<tr>\n";
  237. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  238. echo " ".$text['label-email_address']."\n";
  239. echo "</td>\n";
  240. echo "<td class='vtable' align='left'>\n";
  241. echo " <input class='formfld' type='text' name='email_address' maxlength='255' value=\"".escape($email_address)."\">\n";
  242. echo "<br />\n";
  243. echo !empty($text['description-email_address'])."\n";
  244. echo "</td>\n";
  245. echo "</tr>\n";
  246. echo "<tr>\n";
  247. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  248. echo " ".$text['label-primary']."\n";
  249. echo "</td>\n";
  250. echo "<td class='vtable' align='left'>\n";
  251. echo " <select class='formfld' name='email_primary' id='email_primary'>\n";
  252. echo " <option value='0'>".$text['option-false']."</option>\n";
  253. echo " <option value='1' ".(!empty($email_primary) && $email_primary ? "selected" : null).">".$text['option-true']."</option>\n";
  254. echo " </select>\n";
  255. echo "<br />\n";
  256. echo $text['description-email_primary']."\n";
  257. echo "</td>\n";
  258. echo "</tr>\n";
  259. echo "<tr>\n";
  260. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  261. echo " ".$text['label-email_description']."\n";
  262. echo "</td>\n";
  263. echo "<td class='vtable' align='left'>\n";
  264. echo " <input class='formfld' type='text' name='email_description' maxlength='255' value=\"".escape($email_description)."\">\n";
  265. echo "<br />\n";
  266. echo !empty($text['description-email_description'])."\n";
  267. echo "</td>\n";
  268. echo "</tr>\n";
  269. echo "</table>";
  270. echo "<br><br>";
  271. echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid ?? '')."'>\n";
  272. if ($action == "update") {
  273. echo "<input type='hidden' name='contact_email_uuid' value='".escape($contact_email_uuid)."'>\n";
  274. }
  275. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  276. echo "</form>";
  277. //include the footer
  278. require_once "resources/footer.php";
  279. ?>