contact_email_edit.php 11 KB

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