dashboard.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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) 2019 - 2021
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. /**
  22. * dashboard class
  23. *
  24. * @method null delete
  25. * @method null toggle
  26. * @method null copy
  27. */
  28. if (!class_exists('dashboard')) {
  29. class dashboard {
  30. /**
  31. * declare the variables
  32. */
  33. private $app_name;
  34. private $app_uuid;
  35. private $name;
  36. private $table;
  37. private $toggle_field;
  38. private $toggle_values;
  39. private $description_field;
  40. private $location;
  41. /**
  42. * called when the object is created
  43. */
  44. public function __construct() {
  45. //assign the variables
  46. $this->app_name = 'dashboard';
  47. $this->app_uuid = '55533bef-4f04-434a-92af-999c1e9927f7';
  48. $this->name = 'dashboard';
  49. $this->table = 'dashboard';
  50. $this->toggle_field = 'dashboard_enabled';
  51. $this->toggle_values = ['true','false'];
  52. $this->description_field = 'dashboard_description';
  53. $this->location = 'dashboard.php';
  54. }
  55. /**
  56. * delete rows from the database
  57. */
  58. public function delete($records) {
  59. if (permission_exists($this->name.'_delete')) {
  60. //add multi-lingual support
  61. $language = new text;
  62. $text = $language->get();
  63. //validate the token
  64. $token = new token;
  65. if (!$token->validate($_SERVER['PHP_SELF'])) {
  66. message::add($text['message-invalid_token'],'negative');
  67. header('Location: '.$this->location);
  68. exit;
  69. }
  70. //delete multiple records
  71. if (is_array($records) && @sizeof($records) != 0) {
  72. //build the delete array
  73. $x = 0;
  74. foreach ($records as $record) {
  75. //add to the array
  76. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['dashboard_uuid'])) {
  77. $array[$this->table][$x]['dashboard_uuid'] = $record['dashboard_uuid'];
  78. $array[$this->table.'_groups'][$x]['dashboard_uuid'] = $record['dashboard_uuid'];
  79. }
  80. //increment the id
  81. $x++;
  82. }
  83. //delete the checked rows
  84. if (is_array($array) && @sizeof($array) != 0) {
  85. //execute delete
  86. $database = new database;
  87. $database->app_name = $this->app_name;
  88. $database->app_uuid = $this->app_uuid;
  89. $database->delete($array);
  90. unset($array);
  91. //set message
  92. message::add($text['message-delete']);
  93. }
  94. unset($records);
  95. }
  96. }
  97. }
  98. /**
  99. * toggle a field between two values
  100. */
  101. public function toggle($records) {
  102. if (permission_exists($this->name.'_edit')) {
  103. //add multi-lingual support
  104. $language = new text;
  105. $text = $language->get();
  106. //validate the token
  107. $token = new token;
  108. if (!$token->validate($_SERVER['PHP_SELF'])) {
  109. message::add($text['message-invalid_token'],'negative');
  110. header('Location: '.$this->location);
  111. exit;
  112. }
  113. //toggle the checked records
  114. if (is_array($records) && @sizeof($records) != 0) {
  115. //get current toggle state
  116. foreach($records as $record) {
  117. if (isset($record['checked']) && $record['checked'] == 'true' && is_uuid($record['dashboard_uuid'])) {
  118. $uuids[] = "'".$record['dashboard_uuid']."'";
  119. }
  120. }
  121. if (is_array($uuids) && @sizeof($uuids) != 0) {
  122. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  123. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  124. $database = new database;
  125. $rows = $database->select($sql, $parameters ?? null, 'all');
  126. if (is_array($rows) && @sizeof($rows) != 0) {
  127. foreach ($rows as $row) {
  128. $states[$row['uuid']] = $row['toggle'];
  129. }
  130. }
  131. unset($sql, $parameters, $rows, $row);
  132. }
  133. //build update array
  134. $x = 0;
  135. foreach($states as $uuid => $state) {
  136. //create the array
  137. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  138. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  139. //increment the id
  140. $x++;
  141. }
  142. //save the changes
  143. if (is_array($array) && @sizeof($array) != 0) {
  144. //save the array
  145. $database = new database;
  146. $database->app_name = $this->app_name;
  147. $database->app_uuid = $this->app_uuid;
  148. $database->save($array);
  149. unset($array);
  150. //set message
  151. message::add($text['message-toggle']);
  152. }
  153. unset($records, $states);
  154. }
  155. }
  156. }
  157. /**
  158. * copy rows from the database
  159. */
  160. public function copy($records) {
  161. if (permission_exists($this->name.'_add')) {
  162. //add multi-lingual support
  163. $language = new text;
  164. $text = $language->get();
  165. //validate the token
  166. $token = new token;
  167. if (!$token->validate($_SERVER['PHP_SELF'])) {
  168. message::add($text['message-invalid_token'],'negative');
  169. header('Location: '.$this->location);
  170. exit;
  171. }
  172. //copy the checked records
  173. if (is_array($records) && @sizeof($records) != 0) {
  174. //get checked records
  175. foreach($records as $record) {
  176. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['dashboard_uuid'])) {
  177. $uuids[] = "'".$record['dashboard_uuid']."'";
  178. }
  179. }
  180. //create the array from existing data
  181. if (is_array($uuids) && @sizeof($uuids) != 0) {
  182. $sql = "select * from v_".$this->table." ";
  183. $sql .= "where dashboard_uuid in (".implode(', ', $uuids).") ";
  184. $database = new database;
  185. $rows = $database->select($sql, $parameters ?? null, 'all');
  186. if (is_array($rows) && @sizeof($rows) != 0) {
  187. $x = 0;
  188. foreach ($rows as $row) {
  189. //copy data
  190. $array[$this->table][$x] = $row;
  191. //add copy to the description
  192. $array[$this->table][$x]['dashboard_uuid'] = uuid();
  193. $array[$this->table][$x][$this->description_field] = trim($row[$this->description_field]).' ('.$text['label-copy'].')';
  194. //increment the id
  195. $x++;
  196. }
  197. }
  198. unset($sql, $parameters, $rows, $row);
  199. }
  200. //save the changes and set the message
  201. if (is_array($array) && @sizeof($array) != 0) {
  202. //save the array
  203. $database = new database;
  204. $database->app_name = $this->app_name;
  205. $database->app_uuid = $this->app_uuid;
  206. $database->save($array);
  207. unset($array);
  208. //set message
  209. message::add($text['message-copy']);
  210. }
  211. unset($records);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. ?>