providers.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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) 2023
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. require_once "resources/paging.php";
  25. //check permissions
  26. if (permission_exists('provider_view')) {
  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. //connect to the database
  37. $database = database::new();
  38. //get the http post data
  39. if (!empty($_POST) && is_array($_POST['providers'])) {
  40. $action = $_POST['action'];
  41. $search = $_POST['search'] ?? null;
  42. $providers = $_POST['providers'];
  43. }
  44. //process the http post data by action
  45. if (!empty($action) && is_array($providers) && @sizeof($providers) != 0) {
  46. //validate the token
  47. $token = new token;
  48. if (!$token->validate($_SERVER['PHP_SELF'])) {
  49. message::add($text['message-invalid_token'],'negative');
  50. header('Location: providers.php');
  51. exit;
  52. }
  53. //prepare the array
  54. //foreach ($providers as $row) {
  55. // $array['providers'][$x]['checked'] = $row['checked'];
  56. // $array['providers'][$x]['provider_uuid'] = $row['provider_uuid'];
  57. // $array['providers'][$x]['provider_enabled'] = $row['provider_enabled'];
  58. // $x++;
  59. //}
  60. //prepare the array
  61. $x = 0;
  62. foreach ($providers as $row) {
  63. $array[$x]['checked'] = $row['checked'] ?? null;
  64. $array[$x]['uuid'] = $row['provider_uuid'];
  65. //$array[$x]['provider_enabled'] = $row['provider_enabled'];
  66. $x++;
  67. }
  68. //prepare the database object
  69. $database->app_name = 'providers';
  70. $database->app_uuid = '35187839-237e-4271-b8a1-9b9c45dc8833';
  71. //send the array to the database class
  72. switch ($action) {
  73. case 'copy':
  74. if (permission_exists('provider_add')) {
  75. $obj = new providers;
  76. $obj->copy($array);
  77. }
  78. break;
  79. case 'toggle':
  80. if (permission_exists('provider_edit')) {
  81. $obj = new providers;
  82. $obj->toggle($array);
  83. }
  84. break;
  85. case 'delete':
  86. if (permission_exists('provider_delete')) {
  87. $obj = new providers;
  88. $obj->delete($array);
  89. }
  90. break;
  91. }
  92. //redirect the user
  93. header('Location: providers.php'.(!empty($search) ? '?search='.urlencode($search) : null));
  94. exit;
  95. }
  96. //get order and order by
  97. $order_by = $_GET["order_by"] ?? null;
  98. $order = $_GET["order"] ?? null;
  99. //add the search
  100. if (isset($_GET["search"])) {
  101. $search = strtolower($_GET["search"]);
  102. }
  103. //get the count
  104. $sql = "select count(provider_uuid) ";
  105. $sql .= "from v_providers ";
  106. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  107. if (isset($search)) {
  108. $sql .= "and (";
  109. $sql .= " lower(provider_name) like :search ";
  110. $sql .= ") ";
  111. $parameters['search'] = '%'.$search.'%';
  112. }
  113. $parameters['domain_uuid'] = $domain_uuid;
  114. $num_rows = $database->select($sql, $parameters, 'column');
  115. unset($sql, $parameters);
  116. //prepare to page the results
  117. $rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50;
  118. $param = !empty($search) ? "&search=".$search : null;
  119. $page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 0;
  120. list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page);
  121. list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true);
  122. $offset = $rows_per_page * $page;
  123. //get the list
  124. $sql = "select ";
  125. $sql .= "provider_uuid, ";
  126. $sql .= "provider_name, ";
  127. $sql .= "domain_uuid, ";
  128. $sql .= "cast(provider_enabled as text), ";
  129. $sql .= "provider_description ";
  130. $sql .= "from v_providers ";
  131. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  132. if (isset($search)) {
  133. $sql .= "and (";
  134. $sql .= " lower(provider_name) like :search ";
  135. $sql .= ") ";
  136. $parameters['search'] = '%'.$search.'%';
  137. }
  138. if (isset($_GET["order_by"]) && isset($_GET["order"])) {
  139. $sql .= order_by($order_by, $order, 'provider_name', 'asc');
  140. }
  141. else {
  142. $sql .= "order by provider_name asc ";
  143. }
  144. $sql .= limit_offset($rows_per_page, $offset);
  145. $parameters['domain_uuid'] = $domain_uuid;
  146. $providers = $database->select($sql, $parameters, 'all');
  147. unset($sql, $parameters);
  148. //create token
  149. $object = new token;
  150. $token = $object->create($_SERVER['PHP_SELF']);
  151. //additional includes
  152. $document['title'] = $text['title-providers'];
  153. require_once "resources/header.php";
  154. //show the content
  155. echo "<div class='action_bar' id='action_bar'>\n";
  156. echo " <div class='heading'><b>".$text['title-providers']."</b><div class='count'>".number_format($num_rows)."</div></div>\n";
  157. echo " <div class='actions'>\n";
  158. if (permission_exists('provider_add')) {
  159. echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','name'=>'btn_add','link'=>'provider_setup.php']);
  160. }
  161. if (permission_exists('provider_add')) {
  162. echo button::create(['type'=>'button','label'=>$text['button-advanced'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','name'=>'btn_add','link'=>'provider_edit.php']);
  163. }
  164. //if (permission_exists('provider_add') && $providers) {
  165. // echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$_SESSION['theme']['button_icon_copy'],'id'=>'btn_copy','name'=>'btn_copy','style'=>'display:none;','onclick'=>"modal_open('modal-copy','btn_copy');"]);
  166. //}
  167. //if (permission_exists('provider_edit') && $providers) {
  168. // echo button::create(['type'=>'button','label'=>$text['button-toggle'],'icon'=>$_SESSION['theme']['button_icon_toggle'],'id'=>'btn_toggle','name'=>'btn_toggle','style'=>'display:none;','onclick'=>"modal_open('modal-toggle','btn_toggle');"]);
  169. //}
  170. if (permission_exists('provider_delete') && $providers) {
  171. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'id'=>'btn_delete','name'=>'btn_delete','style'=>'display:none;','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  172. }
  173. echo "<form id='form_search' class='inline' method='get'>\n";
  174. echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search ?? '')."\" placeholder=\"".$text['label-search']."\" onkeydown='list_search_reset();'>";
  175. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search','style'=>(!empty($search) ? 'display: none;' : null)]);
  176. echo button::create(['label'=>$text['button-reset'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'btn_reset','link'=>'providers.php','style'=>(empty($search) ? 'display: none;' : null)]);
  177. if ($paging_controls_mini != '') {
  178. echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>\n";
  179. }
  180. echo " </form>\n";
  181. echo " </div>\n";
  182. echo " <div style='clear: both;'></div>\n";
  183. echo "</div>\n";
  184. //if (permission_exists('provider_add') && $providers) {
  185. // echo modal::create(['id'=>'modal-copy','type'=>'copy','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_copy','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('copy'); list_form_submit('form_list');"])]);
  186. //}
  187. //if (permission_exists('provider_edit') && $providers) {
  188. // echo modal::create(['id'=>'modal-toggle','type'=>'toggle','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_toggle','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('toggle'); list_form_submit('form_list');"])]);
  189. //}
  190. if (permission_exists('provider_delete') && $providers) {
  191. echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('delete'); list_form_submit('form_list');"])]);
  192. }
  193. echo $text['description-providers']."\n";
  194. echo "<br><br>\n";
  195. echo "<form id='form_list' method='post'>\n";
  196. echo "<input type='hidden' id='action' name='action' value=''>\n";
  197. echo "<input type='hidden' name='search' value=\"".escape($search ?? '')."\">\n";
  198. echo "<div class='card'>\n";
  199. echo "<table class='list'>\n";
  200. echo "<tr class='list-header'>\n";
  201. if (permission_exists('provider_add') || permission_exists('provider_edit') || permission_exists('provider_delete')) {
  202. echo " <th class='checkbox'>\n";
  203. echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle(); checkbox_on_change(this);' ".(empty($providers) ? "style='visibility: hidden;'" : null).">\n";
  204. echo " </th>\n";
  205. }
  206. echo th_order_by('provider_name', $text['label-provider_name'], $order_by, $order);
  207. echo th_order_by('provider_enabled', $text['label-provider_enabled'], $order_by, $order, null, "class='center'");
  208. echo " <th class='hide-sm-dn'>".$text['label-provider_description']."</th>\n";
  209. if (permission_exists('provider_edit') && !empty($_SESSION['theme']['list_row_edit_button']['boolean']) && $_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') {
  210. echo " <td class='action-button'>&nbsp;</td>\n";
  211. }
  212. echo "</tr>\n";
  213. if (is_array($providers) && @sizeof($providers) != 0) {
  214. $x = 0;
  215. foreach ($providers as $row) {
  216. if (permission_exists('provider_edit')) {
  217. $list_row_url = "provider_edit.php?id=".urlencode($row['provider_uuid']);
  218. }
  219. echo "<tr class='list-row' href='".$list_row_url."'>\n";
  220. if (permission_exists('provider_add') || permission_exists('provider_edit') || permission_exists('provider_delete')) {
  221. echo " <td class='checkbox'>\n";
  222. echo " <input type='checkbox' name='providers[$x][checked]' id='checkbox_".$x."' value='true' onclick=\"checkbox_on_change(this); if (!this.checked) { document.getElementById('checkbox_all').checked = false; }\">\n";
  223. echo " <input type='hidden' name='providers[$x][provider_uuid]' value='".escape($row['provider_uuid'])."' />\n";
  224. echo " </td>\n";
  225. }
  226. echo " <td>\n";
  227. if (permission_exists('provider_edit')) {
  228. echo " <a href='".$list_row_url."' title=\"".$text['button-edit']."\">".escape($row['provider_name'])."</a>\n";
  229. }
  230. else {
  231. echo " ".escape($row['provider_name']);
  232. }
  233. echo " </td>\n";
  234. if (permission_exists('provider_edit')) {
  235. echo " <td class='no-link center'>\n";
  236. echo " <input type='hidden' name='number_translations[$x][provider_enabled]' value='".escape($row['provider_enabled'])."' />\n";
  237. echo button::create(['type'=>'submit','class'=>'link','label'=>$text['label-'.$row['provider_enabled']],'title'=>$text['button-toggle'],'onclick'=>"list_self_check('checkbox_".$x."'); list_action_set('toggle'); list_form_submit('form_list')"]);
  238. }
  239. else {
  240. echo " <td class='center'>\n";
  241. echo $text['label-'.$row['provider_enabled']];
  242. }
  243. echo " </td>\n";
  244. echo " <td class='description overflow hide-sm-dn'>".escape($row['provider_description'])."</td>\n";
  245. if (permission_exists('provider_edit') && !empty($_SESSION['theme']['list_row_edit_button']['boolean']) && $_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') {
  246. echo " <td class='action-button'>\n";
  247. echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]);
  248. echo " </td>\n";
  249. }
  250. echo "</tr>\n";
  251. $x++;
  252. }
  253. unset($providers);
  254. }
  255. echo "</table>\n";
  256. echo "</div>\n";
  257. echo "<br>\n";
  258. echo "<div align='center'>".$paging_controls."</div>\n";
  259. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  260. echo "</form>\n";
  261. //include the footer
  262. require_once "resources/footer.php";
  263. ?>