2
0

schema_data_view.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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-2012
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. require_once "root.php";
  22. require_once "resources/require.php";
  23. require_once "resources/check_auth.php";
  24. if (permission_exists('schema_data_view')) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //add multi-lingual support
  32. $language = new text;
  33. $text = $language->get();
  34. if (strlen($_GET["id"]) > 0) {
  35. $schema_uuid = check_str($_GET["id"]);
  36. if (strlen($_GET["data_parent_row_uuid"])>0) {
  37. $data_parent_row_uuid = $_GET["data_parent_row_uuid"];
  38. }
  39. $search_all = strtolower(check_str($_GET["search_all"]));
  40. }
  41. //used for changing the order
  42. $order_by = $_GET["order_by"];
  43. $order = $_GET["order"];
  44. //used to alternate colors when paging
  45. $c = 0;
  46. $row_style["0"] = "row_style0";
  47. $row_style["1"] = "row_style1";
  48. //show the header
  49. require_once "resources/header.php";
  50. $document['title'] = $text['title-data_view'];
  51. //get the information about the schema by using the id
  52. $sql = "select * from v_schemas ";
  53. $sql .= "where domain_uuid = '$domain_uuid' ";
  54. $sql .= "and schema_uuid = '$schema_uuid' ";
  55. $prep_statement = $db->prepare(check_sql($sql));
  56. $prep_statement->execute();
  57. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  58. foreach ($result as &$row) {
  59. $schema_category = $row["schema_category"];
  60. $schema_label = $row["schema_label"];
  61. $schema_name = $row["schema_name"];
  62. $schema_auth = $row["schema_auth"];
  63. $schema_captcha = $row["schema_captcha"];
  64. $schema_parent_uuid = $row["schema_parent_uuid"];
  65. $schema_description = $row["schema_description"];
  66. break; //limit to 1 row
  67. }
  68. unset ($prep_statement);
  69. //get the field information
  70. $db_field_name_array = array();
  71. $db_value_array = array();
  72. $db_names .= "<tr>\n";
  73. $sql = "select * from v_schema_fields ";
  74. $sql .= "where domain_uuid = '$domain_uuid' ";
  75. $sql .= "and schema_uuid = '$schema_uuid' ";
  76. $sql .= "order by field_order asc ";
  77. $prep_statement = $db->prepare($sql);
  78. $prep_statement->execute();
  79. $result_names = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  80. $result_count = count($result);
  81. foreach($result_names as $row) {
  82. $field_label = $row["field_label"];
  83. $field_name = $row["field_name"];
  84. $field_type = $row["field_type"];
  85. $field_value = $row["field_value"];
  86. $field_list_hidden = $row["field_list_hidden"];
  87. $field_column = $row["field_column"];
  88. $field_required = $row["field_required"];
  89. $field_order = $row["field_order"];
  90. $field_order_tab = $row["field_order_tab"];
  91. $field_description = $row["field_description"];
  92. $name_array[$field_name]['field_label'] = $row["field_label"];
  93. $name_array[$field_name]['field_type'] = $row["field_type"];
  94. $name_array[$field_name]['field_list_hidden'] = $row["field_list_hidden"];
  95. $name_array[$field_name]['field_column'] = $row["field_column"];
  96. $name_array[$field_name]['field_required'] = $row["field_required"];
  97. $name_array[$field_name]['field_order'] = $row["field_order"];
  98. $name_array[$field_name]['field_order_tab'] = $row["field_order_tab"];
  99. $name_array[$field_name]['field_description'] = $row["field_description"];
  100. }
  101. unset($sql, $prep_statement, $row);
  102. $fieldcount = count($name_array);
  103. //get the data
  104. $sql = "";
  105. $sql .= "select * from v_schema_data ";
  106. $sql .= "where domain_uuid = '".$domain_uuid."' ";
  107. if (strlen($search_all) == 0) {
  108. $sql .= "and schema_uuid = '$schema_uuid' ";
  109. if (strlen($data_parent_row_uuid) > 0) {
  110. $sql .= "and data_parent_row_uuid = '$data_parent_row_uuid' ";
  111. }
  112. }
  113. else {
  114. $sql .= "and data_row_uuid in (";
  115. $sql .= "select data_row_uuid from v_schema_data \n";
  116. $sql .= "where domain_uuid = '".$domain_uuid."' ";
  117. $sql .= "and schema_uuid = '$schema_uuid' ";
  118. if (strlen($data_parent_row_uuid) == 0) {
  119. $tmp_digits = preg_replace('{\D}', '', $search_all);
  120. if (is_numeric($tmp_digits) && strlen($tmp_digits) > 5) {
  121. if (strlen($tmp_digits) == '11' ) {
  122. $sql .= "and data_field_value like '%".substr($tmp_digits, -10)."%' \n";
  123. }
  124. else {
  125. $sql .= "and data_field_value like '%$tmp_digits%' \n";
  126. }
  127. }
  128. else {
  129. $sql .= "and lower(data_field_value) like '%$search_all%' \n";
  130. }
  131. }
  132. else {
  133. $sql .= "and data_parent_row_uuid = '$data_parent_row_uuid' ";
  134. }
  135. $sql .= ")\n";
  136. }
  137. $sql .= "limit 20000\n";
  138. $prep_statement = $db->prepare($sql);
  139. $prep_statement->execute();
  140. $result_values = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  141. foreach($result_values as $row) {
  142. //set a php variable
  143. $field_name = $row[field_name];
  144. $data_row_uuid = $row[data_row_uuid];
  145. //restructure the data by setting it the value_array
  146. $value_array[$data_row_uuid][$field_name] = $row[data_field_value];
  147. $value_array[$data_row_uuid]['schema_uuid'] = $row["schema_uuid"];
  148. $value_array[$data_row_uuid]['data_row_uuid'] = $row[data_row_uuid];
  149. $value_array[$data_row_uuid]['schema_parent_uuid'] = $row[schema_parent_uuid];
  150. $value_array[$data_row_uuid]['data_parent_row_uuid'] = $row[data_parent_row_uuid];
  151. }
  152. $num_rows = count($value_array);
  153. //create the connection to the memory dbase_add_record
  154. try {
  155. $db_memory = new PDO('sqlite::memory:'); //sqlite 3
  156. }
  157. catch (PDOException $error) {
  158. print "error: " . $error->getMessage() . "<br/>";
  159. die();
  160. }
  161. //create a memory database and add the fields to the table
  162. $sql = "CREATE TABLE memory_table ";
  163. $sql .= "(";
  164. $sql .= "'id' INTEGER PRIMARY KEY, ";
  165. $sql .= "'schema_uuid' TEXT, ";
  166. $sql .= "'data_row_uuid' TEXT, ";
  167. $sql .= "'schema_parent_uuid' TEXT, ";
  168. $sql .= "'data_parent_row_uuid' TEXT, ";
  169. foreach($result_names as $row) {
  170. if ($row["field_type"] != "label") {
  171. if ($row["field_name"] != "domain_uuid") {
  172. //$row["field_label"];
  173. //$row["field_name"];
  174. //$row["field_type"];
  175. if ($row["field_name"] == "number") {
  176. $sql .= "'".$row["field_name"]."' NUMERIC, ";
  177. }
  178. else {
  179. $sql .= "'".$row["field_name"]."' TEXT, ";
  180. }
  181. }
  182. }
  183. }
  184. $sql .= "'domain_uuid' TEXT ";
  185. $sql .= ");";
  186. $prep_statement = $db_memory->prepare($sql);
  187. $prep_statement->execute();
  188. unset ($prep_statement, $sql);
  189. //list the values from the array
  190. $x = 0;
  191. foreach($value_array as $array) {
  192. //insert the data into the memory table
  193. $sql = "insert into memory_table ";
  194. $sql .= "(";
  195. $sql .= "'schema_uuid', ";
  196. $sql .= "'data_row_uuid', ";
  197. $sql .= "'schema_parent_uuid', ";
  198. $sql .= "'data_parent_row_uuid', ";
  199. //foreach($array as $key => $value) {
  200. // $sql .= "'$key', ";
  201. foreach($result_names as $row) {
  202. $field_name = $row["field_name"];
  203. $sql .= "'$field_name', ";
  204. }
  205. $sql .= "'domain_uuid' ";
  206. $sql .= ")";
  207. $sql .= "values ";
  208. $sql .= "(";
  209. $sql .= "'".$array['schema_uuid']."', ";
  210. $sql .= "'".$array['data_row_uuid']."', ";
  211. $sql .= "'".$array['schema_parent_uuid']."', ";
  212. $sql .= "'".$array['data_parent_row_uuid']."', ";
  213. //foreach($array as $key => $value) {
  214. // $sql .= "'$value', ";
  215. foreach($result_names as $row) {
  216. $field_name = $row["field_name"];
  217. $sql .= "'".check_str($array[$field_name])."', ";
  218. }
  219. $sql .= "'$domain_uuid' ";
  220. $sql .= ");";
  221. //echo "$sql <br /><br />\n";
  222. $db_memory->exec(check_sql($sql));
  223. unset($sql);
  224. unset($array);
  225. //unset the row of data
  226. unset($value_array[$x]);
  227. //increment the value
  228. $x++;
  229. }
  230. //set the title and description of the table
  231. echo "<br />\n";
  232. echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
  233. echo " <tr>\n";
  234. echo " <td align='left' valign='top'><strong>$schema_label</strong><br>\n";
  235. echo " $schema_description\n";
  236. echo " </td>\n";
  237. echo " <td align='right' valign='top'>\n";
  238. if (strlen($data_parent_row_uuid) == 0) {
  239. $search_all = str_replace("''", "'", $search_all);
  240. echo "<form method='GET' name='frm_search' action=''>\n";
  241. echo " <input class='formfld' type='text' name='search_all' value=\"$search_all\">\n";
  242. echo " <input type='hidden' name='id' value='$schema_uuid'>\n";
  243. echo " <input type='hidden' name='data_parent_row_uuid' value='$data_parent_row_uuid'>\n";
  244. echo " <input class='btn' type='submit' name='submit' value='".$text['button-search_all']."'>\n";
  245. echo "</form>\n";
  246. }
  247. echo " </td>\n";
  248. echo " </tr>\n";
  249. echo "</table>\n";
  250. echo "<br />";
  251. //prepare for paging the results
  252. require_once "resources/paging.php";
  253. $rows_per_page = 100;
  254. $param = "";
  255. $page = $_GET['page'];
  256. if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; }
  257. if (strlen($schema_parent_uuid) > 0) {
  258. $param = "&id=$schema_parent_uuid&data_row_uuid=$data_row_uuid";
  259. }
  260. else {
  261. $param = "&id=$schema_uuid&data_row_uuid=$data_row_uuid";
  262. }
  263. list($paging_controls, $rows_per_page, $var_3) = paging($num_rows, $param, $rows_per_page);
  264. $offset = $rows_per_page * $page;
  265. //list the data in the database
  266. $sql = "select * from memory_table \n";
  267. $sql .= "where domain_uuid = '$domain_uuid' \n";
  268. $sql .= "limit $rows_per_page offset $offset \n";
  269. //$sql .= "order by field_order asc \n";
  270. $prep_statement = $db_memory->prepare($sql);
  271. $prep_statement->execute();
  272. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  273. //begin the list
  274. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  275. echo "<tr>\n";
  276. foreach($result_names as $row) {
  277. if ($row['field_list_hidden'] != "hide") {
  278. echo "<th valign='top' nowrap>&nbsp; ".$row['field_label']." &nbsp;</th>\n";
  279. }
  280. }
  281. echo "<td class='list_control_icons'>";
  282. if (permission_exists('schema_data_add')) {
  283. echo "<a href='schema_data_edit.php?schema_uuid=".$schema_uuid."&data_parent_row_uuid=$data_parent_row_uuid' alt='".$text['button-add']."'>$v_link_label_add</a>";
  284. }
  285. echo "</td>\n";
  286. echo "</tr>\n";
  287. $db_values = '';
  288. $x = 0;
  289. foreach ($result as &$row) {
  290. echo "<tr>\n";
  291. foreach($result_names as $row2) {
  292. $field_name = $row2[field_name];
  293. //get the values from the array and set as php variables
  294. $field_label = $name_array[$field_name]['field_label'];
  295. $field_type = $name_array[$field_name]['field_type'];
  296. $field_list_hidden = $name_array[$field_name]['field_list_hidden'];
  297. $field_column = $name_array[$field_name]['field_column'];
  298. $field_required = $name_array[$field_name]['field_required'];
  299. $field_order = $name_array[$field_name]['field_order'];
  300. $field_order_tab = $name_array[$field_name]['field_order_tab'];
  301. $field_description = $name_array[$field_name]['field_description'];
  302. if ($field_list_hidden != "hide") {
  303. switch ($field_type) {
  304. case "textarea":
  305. $tmp_value = str_replace("\n", "<br />\n", $row[$field_name]);
  306. echo "<td valign='top' class='".$row_style[$c]."'>".$tmp_value."&nbsp;</td>\n";
  307. unset($tmp_value);
  308. break;
  309. case "email":
  310. echo "<td valign='top' class='".$row_style[$c]."'><a href='mailto:".$row[$field_name]."'>".$row[$field_name]."</a>&nbsp;</td>\n";
  311. break;
  312. case "phone":
  313. $tmp_phone = $row[$field_name];
  314. $tmp_phone = format_phone($tmp_phone);
  315. echo "<td valign='top' class='".$row_style[$c]."'>".$tmp_phone."&nbsp;</td>\n";
  316. break;
  317. case "url":
  318. $url = $row[$field_name];
  319. if (substr($url,0,4) != "http") {
  320. $url = 'http://'.$url;
  321. }
  322. echo "<td valign='top' class='".$row_style[$c]."'><a href='".$url."' target='_blank'>".$row[$field_name]."</a>&nbsp;</td>\n";
  323. break;
  324. default:
  325. echo "<td valign='top' class='".$row_style[$c]."'>".$row[$field_name]."&nbsp;</td>\n";
  326. break;
  327. }
  328. }
  329. }
  330. echo "<td class='list_control_icons'>";
  331. if (permission_exists('schema_data_edit')) {
  332. if (strlen($data_parent_row_uuid) == 0) {
  333. echo "<a href='schema_data_edit.php?schema_uuid=".$row["schema_uuid"]."&data_parent_row_uuid=$data_parent_row_uuid&data_row_uuid=".$row['data_row_uuid']."&search_all=$search_all' alt='".$text['button-edit']."'>$v_link_label_edit</a>";
  334. }
  335. else {
  336. echo "<a href='schema_data_edit.php?schema_uuid=".$row["schema_uuid"]."&data_parent_row_uuid=$data_parent_row_uuid&data_row_uuid=".$row['data_row_uuid']."' alt='".$text['button-edit']."'>$v_link_label_edit</a>";
  337. }
  338. }
  339. if (permission_exists('schema_delete')) {
  340. echo"<a href='schema_delete.php?data_row_uuid=".$row['data_row_uuid']."&data_parent_row_uuid=$data_parent_row_uuid&schema_uuid=".$schema_uuid."' alt='".$text['button-delete']."' onclick=\"return confirm('".$text['confirm-delete']."')\">$v_link_label_delete</a>";
  341. }
  342. echo "</td>\n";
  343. echo "</tr>\n";
  344. if ($c==0) { $c=1; } else { $c=0; }
  345. }
  346. //show the paging tools and final add button
  347. echo "<tr>\n";
  348. echo "<td colspan='999' align='left'>\n";
  349. echo " <table border='0' width='100%' cellpadding='0' cellspacing='0'>\n";
  350. echo " <tr>\n";
  351. echo " <td width='33.3%' nowrap>&nbsp;</td>\n";
  352. echo " <td width='33.3%' align='center' nowrap>$paging_controls</td>\n";
  353. echo " <td class='list_control_icons'>";
  354. if (permission_exists('schema_data_add')) {
  355. echo "<a href='schema_data_edit.php?schema_uuid=".$schema_uuid."&data_parent_row_uuid=$data_parent_row_uuid' alt='".$text['button-add']."'>$v_link_label_add</a>";
  356. }
  357. echo " </td>\n";
  358. echo " </tr>\n";
  359. echo " </table>\n";
  360. echo "</td>\n";
  361. echo "</tr>\n";
  362. echo "</table>\n";
  363. echo "<br><br>\n";
  364. echo "</div>";
  365. //show the header
  366. echo "<br><br>";
  367. require_once "resources/footer.php";
  368. ?>