PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/custom-database-tables/templates/cdbt-public-edit.php

https://gitlab.com/oxidigitaluser/liguelista
PHP | 319 lines | 305 code | 10 blank | 4 comment | 106 complexity | a38732d9460b081c193b30a6e5c0a0c5 MD5 | raw file
  1. <?php
  2. function cdbt_render_edit_page($table=null, $mode=null, $_cdbt_token=null, $options=array()) {
  3. global $cdbt;
  4. foreach ($_POST as $k => $v) {
  5. ${$k} = $v;
  6. }
  7. if (!empty($options)) {
  8. $is_bootstrap_style = isset($options['bootstrap_style']) ? $options['bootstrap_style'] : false;
  9. $is_display_title = isset($options['display_title']) ? $options['display_title'] : false;
  10. $is_display_list_num = isset($options['display_list_num']) ? $options['display_list_num'] : true;
  11. $is_enable_sort = isset($options['enable_sort']) ? $options['enable_sort'] : false;
  12. $entry_page = isset($options['entry_page']) ? $options['entry_page'] : '';
  13. $exclude_cols = isset($options['exclude_cols']) ? (array)$options['exclude_cols'] : array();
  14. $add_class = isset($options['add_class']) ? $options['add_class'] : '';
  15. } else {
  16. $is_bootstrap_style = $is_display_title = $is_display_search = $is_enable_sort = false;
  17. $is_display_list_num = true;
  18. $exclude_cols = array();
  19. $entry_page = $add_class = '';
  20. }
  21. $is_entry_page = false;
  22. if (!empty($entry_page)) {
  23. if (intval($entry_page) > 0) {
  24. $post = get_post(intval($entry_page));
  25. } else {
  26. $post_types = get_post_types( array('public'=>true, '_builtin'=>false), 'names', 'and' );
  27. if (is_array($post_types)) {
  28. array_unshift($post_types, 'post', 'page');
  29. }
  30. $posts = get_posts( array('numberposts'=>-1, 'post_type'=>$post_types, 'orderby'=>'ID', 'order'=>'ASC') );
  31. foreach ($posts as $one_post) {
  32. if ($one_post->post_name == $entry_page) {
  33. $post = $one_post;
  34. break;
  35. }
  36. }
  37. }
  38. if (!empty($post)) {
  39. $pattern = get_shortcode_regex();
  40. if (preg_match_all('/'. $pattern .'/s', $post->post_content, $matches) && array_key_exists(2, $matches) && in_array('cdbt-entry', $matches[2])) {
  41. if (preg_match('/table=(\'|\")'. $table .'(\'|\")/iU', $matches[0][0])) {
  42. $is_entry_page = true;
  43. $entry_page_url = str_replace(get_option('siteurl'), '', get_permalink($post->ID));
  44. }
  45. }
  46. }
  47. } else {
  48. // if entry_page is undefined
  49. $post_types = get_post_types( array('public'=>true, '_builtin'=>false), 'names', 'and' );
  50. if (is_array($post_types)) {
  51. array_unshift($post_types, 'post', 'page');
  52. }
  53. $pattern = get_shortcode_regex();
  54. $posts = get_posts( array('numberposts'=>-1, 'post_type'=>$post_types, 'orderby'=>'ID', 'order'=>'DESC') );
  55. foreach ($posts as $one_post) {
  56. if (preg_match_all('/'. $pattern .'/s', $one_post->post_content, $matches) && array_key_exists(2, $matches) && in_array('cdbt-entry', $matches[2])) {
  57. if (preg_match('/table=(\'|\")'. $table .'(\'|\")/iU', $matches[0][0])) {
  58. $is_entry_page = true;
  59. $entry_page_url = str_replace(get_option('siteurl'), '', get_permalink($one_post->ID));
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. list($result, $table_name, $table_schema) = $cdbt->get_table_schema($table);
  66. if ($result && !empty($table_name) && !empty($table_schema)) {
  67. $page_num = (!isset($page_num) || empty($page_num)) ? 1 : intval($page_num);
  68. if (!isset($per_page) || empty($per_page)) {
  69. foreach ($cdbt->options['tables'] as $i => $table_opt) {
  70. if ($table_opt['table_name'] == $table_name) {
  71. $max_records = intval($table_opt['show_max_records']);
  72. break;
  73. }
  74. }
  75. $per_page = (!empty($max_records) && $max_records > 0) ? $max_records : intval(get_option('posts_per_page', 10));
  76. } else {
  77. $per_page = intval($per_page);
  78. }
  79. $table_class = $is_bootstrap_style ? 'table table-bordered table-striped table-hover ' : '';
  80. $title_attr = $is_bootstrap_style ? 'class="sr-only"' : 'style="display: none;"';
  81. if ($is_display_title) {
  82. $list_html = '<h3 class="dashboard-title">%s</h3>%s<div style="overflow-x: auto;"><table id="'. $table_name .'" class="'. $table_class . $add_class .'">%s%s</table></div>%s';
  83. } else {
  84. $list_html = '<span '. $title_attr .'>%s</span>%s<div style="overflow-x: auto;"><table id="'. $table_name .'" class="'. $table_class . $add_class .'" style="overflow-x: auto;">%s%s</table></div>%s';
  85. }
  86. list($result, $value) = $cdbt->get_table_comment($table_name);
  87. if ($result) {
  88. $title = sprintf(__('edit data in %s table (table comment: %s)', CDBT_PLUGIN_SLUG), $table_name, $value);
  89. } else {
  90. $title = sprintf(__('edit data in %s table', CDBT_PLUGIN_SLUG), $table_name);
  91. }
  92. $information_html = '';
  93. if (wp_verify_nonce($_cdbt_token, CDBT_PLUGIN_SLUG .'_'. $mode)) {
  94. if (isset($action) && $action == 'delete') {
  95. $IDs = explode(',', $ID);
  96. if (is_array($IDs) && !empty($IDs)) {
  97. $information_html_base = '<div class="alert alert-info"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><ul>%s</ul></div>';
  98. $deleted_IDs = array();
  99. foreach ($IDs as $ID) {
  100. $is_deleted = $cdbt->delete_data($table_name, intval($ID));
  101. $deleted_IDs[$ID] = ((bool)$is_deleted) ? true : false;
  102. }
  103. $delete_id_list = null;
  104. foreach ($deleted_IDs as $deleted_ID => $deleted_status) {
  105. if ($deleted_status) {
  106. $delete_id_list .= sprintf('<li><p class="text-success">%s %s.</p></li>', __('Deleted the data of ID:', CDBT_PLUGIN_SLUG), $deleted_ID);
  107. } else {
  108. $delete_id_list .= sprintf('<li><p class="text-warning">%s %s.</p></li>', __('Failed to delete data of ID:', CDBT_PLUGIN_SLUG), $deleted_ID);
  109. }
  110. }
  111. $information_html = sprintf($information_html_base, $delete_id_list);
  112. }
  113. }
  114. $list_index_row = $list_rows = $pagination = null;
  115. $nonce_field = wp_nonce_field(CDBT_PLUGIN_SLUG .'_'. $mode, '_cdbt_token', true, false);
  116. $limit = $per_page;
  117. $offset = ($page_num - 1) * $limit;
  118. $view_cols = null; // If this value is null, will be all columns display.
  119. $order_by = (isset($sort_by) && !empty($sort_by) && isset($sort_order) && !empty($sort_order)) ? array($sort_by => $sort_order) : null;
  120. if (isset($action) && $action == 'search') {
  121. if (isset($search_key) && !empty($search_key)) {
  122. $data = $cdbt->find_data($table_name, $table_schema, $search_key, $view_cols, $order_by);
  123. $total_data = count($data);
  124. if ($total_data > $limit) {
  125. $data = $cdbt->find_data($table_name, $table_schema, $search_key, $view_cols, $order_by, $limit, $offset);
  126. }
  127. }
  128. } else {
  129. $data = $cdbt->get_data($table_name, $view_cols, null, $order_by, $limit, $offset);
  130. $total_data = $cdbt->get_data($table_name, 'COUNT(*)', null, null);
  131. if (is_array($total_data) && !empty($total_data)) {
  132. $total_data = array_shift($total_data);
  133. foreach ($total_data as $key => $val) {
  134. if ($key == 'COUNT(*)') {
  135. $total_data = intval($val);
  136. break;
  137. }
  138. }
  139. } else {
  140. $total_data = 0;
  141. }
  142. $total_data_info = $total_data > 0 ? sprintf(__('Total %d items', CDBT_PLUGIN_SLUG), $total_data) : '';
  143. }
  144. $page_slug = CDBT_PLUGIN_SLUG;
  145. $controller_block_base = '<form method="post" class="controller-form" role="form">%s';
  146. $all_checkbox_button_label = __('Checked items delete', CDBT_PLUGIN_SLUG);
  147. $current_sort_by = (isset($sort_by) && !empty($sort_by)) ? $sort_by : '';
  148. $current_order_by = (isset($sort_order) && !empty($sort_order)) ? $sort_order : 'DESC';
  149. $data_info = (isset($total_data_info) && !empty($total_data_info)) ? '<div class="navbar-inherit edit-adjust"><span class="label label-info">'. $total_data_info .'</span></div>' : '';
  150. if (isset($action) && $action == 'search' && isset($total_data) && $total_data > 0) {
  151. $hits_message = $total_data == 1 ? __('1 row matched', CDBT_PLUGIN_SLUG) : sprintf(__('%d rows matched', CDBT_PLUGIN_SLUG), $total_data);
  152. $search_hits = <<<HITS
  153. <div class="search-hits tooltip left">
  154. <div class="tooltip-arrow"></div>
  155. <div class="tooltip-inner">$hits_message</div>
  156. </div>
  157. HITS;
  158. } else {
  159. $search_hits = '';
  160. }
  161. $search_key = (!isset($search_key)) ? '' : $search_key;
  162. $search_key_placeholder = __('Search keyword', CDBT_PLUGIN_SLUG);
  163. $search_button_label = __('Search', CDBT_PLUGIN_SLUG);
  164. $action = (isset($action) && !empty($action)) ? $action : '';
  165. $content = <<<NAV
  166. <nav class="navbar navbar-default" role="navigation">
  167. <div class="container-fluid">
  168. <input type="hidden" name="table" value="$table_name" />
  169. <input type="hidden" name="page" value="$page_slug" />
  170. <input type="hidden" name="page_num" value="$page_num" />
  171. <input type="hidden" name="mode" value="$mode" />
  172. <input type="hidden" name="action" value="$action" />
  173. <input type="hidden" name="ID" value="" />
  174. <input type="hidden" name="sort_by" value="$current_sort_by" />
  175. <input type="hidden" name="sort_order" value="$current_order_by" />
  176. $nonce_field
  177. </div>
  178. <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
  179. <button type="button" class="btn btn-default navbar-btn" style="position: absolute; left: 14px;" id="checked_items_delete" data-mode="edit" data-action="delete" data-toggle="modal" data-target=".confirmation">
  180. <span class="glyphicon glyphicon-check"></span> $all_checkbox_button_label</button>
  181. $data_info
  182. <div class="navbar-form navbar-right" role="search">
  183. $search_hits
  184. <div class="form-group">
  185. <input type="text" name="search_key" class="form-control" placeholder="$search_key_placeholder" value="$search_key" />
  186. </div>
  187. <button type="button" class="btn btn-default" id="search_items" data-mode="$mode" data-action="search"><span class="glyphicon glyphicon-search"></span> $search_button_label</button>
  188. </div>
  189. </div>
  190. </nav>
  191. NAV;
  192. $controller_block = sprintf($controller_block_base, $content);
  193. if (!empty($data) && is_array($data)) {
  194. $list_num = 1 + (($page_num - 1) * $per_page);
  195. foreach ($data as $record) {
  196. $primary_key_name = $primary_key_value = null;
  197. foreach ($record as $key => $val) {
  198. if (empty($primary_key) && $table_schema[$key]['primary_key']) {
  199. $primary_key_name = $key;
  200. $primary_key_value = $val;
  201. break;
  202. }
  203. }
  204. if ($list_num == (1 + (($page_num - 1) * $per_page))) {
  205. $list_index_row = '<thead><tr>';
  206. $list_index_row .= '<th><input type="checkbox" id="all_checkbox_controller" /></th>';
  207. $list_index_row .= ($is_display_list_num) ? '<th>'. __('No.', CDBT_PLUGIN_SLUG) .'</th>' : '';
  208. foreach ($record as $key => $val) {
  209. if (!empty($exclude_cols) && in_array($key, $exclude_cols)) {
  210. continue;
  211. } else {
  212. if (array_key_exists($key, $table_schema)) {
  213. if ($is_enable_sort) {
  214. $column_type = $table_schema[$key]['type'];
  215. if (preg_match('/^((|tiny|small|medium|big)int|float|double(| precision)|real|dec(|imal)|numeric|fixed|bool(|ean)|bit)$/i', $column_type)) {
  216. $icon_type = strtoupper($current_order_by) == 'DESC' ? 'sort-by-order' : 'sort-by-order-alt';
  217. } else if (preg_match('/^((|var|national |n)char(|acter)|(|tiny|medium|long)text|(|tiny|medium|long)blob|(|var)binary|enum|set)$/i', $column_type)) {
  218. $icon_type = strtoupper($current_order_by) == 'DESC' ? 'sort-by-alphabet' : 'sort-by-alphabet-alt';
  219. } else {
  220. $icon_type = strtoupper($current_order_by) == 'DESC' ? 'sort-by-attributes' : 'sort-by-attributes-alt';
  221. }
  222. $toggle_order_by = strtoupper($current_order_by) == 'DESC' ? 'ASC' : 'DESC';
  223. $sort_switch = '<a href="#" class="sort-switch btn btn-default btn-xs" data-sort-column="'. $key .'" data-toggle-order="'. $toggle_order_by .'"><span class="glyphicon glyphicon-'. $icon_type .'"></span></a>';
  224. } else {
  225. $sort_switch = '';
  226. }
  227. $display_name = !empty($table_schema[$key]['logical_name']) ? $table_schema[$key]['logical_name'] : $key;
  228. }
  229. $list_index_row .= '<th id="index-'. $key .'">'. $display_name . $sort_switch .'</th>';
  230. }
  231. }
  232. $list_index_row .= '<th>'. __('Controll', CDBT_PLUGIN_SLUG) .'</th>';
  233. $list_index_row .= '</tr></thead>';
  234. }
  235. $list_rows .= '<tr>';
  236. $list_rows .= '<td><input type="checkbox" id="checkbox_controller_'. $list_num .'" class="inherit_checkbox" value="'. $primary_key_value .'" /></td>';
  237. $list_rows .= ($is_display_list_num) ? '<td>'. $list_num .'</td>' : '';
  238. $is_include_binary_file = false;
  239. foreach ($record as $key => $val) {
  240. //if (strtoupper($key) == 'ID')
  241. // $data_id = intval($val);
  242. // strlen('a:*:{s:11:"origin_file";') = 24
  243. $is_binary = (preg_match('/^a:\d:\{s:11:\"origin_file\"\;$/i', substr($val, 0, 24))) ? true : false;
  244. $is_include_binary_file = ($is_binary) ? true : $is_include_binary_file;
  245. if ($is_binary) {
  246. eval('$tmp = array(' . trim(preg_replace('/(a:\d+:{|(|;)s:\d+:|(|;)i:|"$)/', ",", substr($val, 0, strpos($val, 'bin_data'))), ',,') . ');');
  247. foreach ($tmp as $i => $val) {
  248. if ($val == 'origin_file') $origin_file = $tmp[intval($i)+1];
  249. if ($val == 'mine_type') $mine_type = $tmp[intval($i)+1];
  250. if ($val == 'file_size') $file_size = $tmp[intval($i)+1];
  251. }
  252. }
  253. $val = ($is_binary) ? '<a href="#" class="binary-file" data-id="'. $primary_key_value .'" data-origin-file="'. $origin_file .'"><span class="glyphicon glyphicon-paperclip"></span> '. $mine_type .' ('. ceil($file_size/1024) .'KB)</a>' : cdbt_str_truncate($val, 40, '...', true);
  254. if (!empty($exclude_cols) && in_array($key, $exclude_cols)) {
  255. continue;
  256. } else {
  257. $list_rows .= '<td>'. $val .'</td>';
  258. }
  259. }
  260. $list_rows .= '<td><div class="btn-group-vertical">';
  261. if ($is_entry_page)
  262. $list_rows .= "\t" . '<button type="button" class="btn btn-default btn-sm edit-row" action-url="'. $entry_page_url .'" data-id="'. $primary_key_value .'" data-mode="input" data-action="update" data-token="'. wp_create_nonce(CDBT_PLUGIN_SLUG .'_input') .'"><span class="glyphicon glyphicon-edit"></span> '. __('Edit', CDBT_PLUGIN_SLUG) .'</button>';
  263. if ($is_include_binary_file)
  264. $list_rows .= "\t" . '<button type="button" class="btn btn-default btn-sm download-binary" data-id="'. $primary_key_value .'" data-mode="edit" data-action="download" data-loading-text="'. __('Downloading...', CDBT_PLUGIN_SLUG) .'"><span class="glyphicon glyphicon-download"></span> '. __('Download', CDBT_PLUGIN_SLUG) .'</button>';
  265. $list_rows .= "\t" . '<button type="button" class="btn btn-default btn-sm delete-row" data-id="'. $primary_key_value .'" data-mode="edit" data-action="delete" data-toggle="modal" data-target=".confirmation"><span class="glyphicon glyphicon-trash"></span> '. __('Delete', CDBT_PLUGIN_SLUG) .'</button>';
  266. $list_rows .= '</div></td>';
  267. $list_rows .= '</tr>';
  268. $list_num++;
  269. }
  270. $pagination = ($total_data > $per_page) ? cdbt_create_pagination(intval($page_num), intval($per_page), $total_data, $mode) : null;
  271. $btn_cancel = __('Cancel', CDBT_PLUGIN_SLUG);
  272. $btn_run = __('Yes, run', CDBT_PLUGIN_SLUG);
  273. $modal_container = <<<MODAL
  274. <!-- /* Modal */ -->
  275. <div class="modal fade confirmation" tabindex="-1" role="dialog" aria-labelledby="confirmation" aria-hidden="true">
  276. <div class="modal-dialog modal-sm">
  277. <div class="modal-content">
  278. <div class="modal-header">
  279. <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove"></span></button>
  280. <h4 class="modal-title" style="width: 100%; background: none;"></h4>
  281. </div>
  282. <div class="modal-body">
  283. </div>
  284. <div class="modal-footer">
  285. <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> <span class="cancel-close">$btn_cancel</span></button>
  286. <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-ok"></span> <span class="run-process">$btn_run</span></button>
  287. </div>
  288. </div><!-- /.modal-content -->
  289. </div><!-- /.modal-dialog -->
  290. </div><!-- /.modal -->
  291. MODAL;
  292. $display_html = sprintf($list_html, $title, $information_html.$controller_block, $list_index_row, '<tbody>' . $list_rows . '</tbody>', '</form>' . $pagination . $modal_container);
  293. } else {
  294. if (isset($action) && $action == 'search') {
  295. $msg_str = sprintf(__('No data to match for "%s".', CDBT_PLUGIN_SLUG), $search_key);
  296. } else {
  297. $msg_str = __('Data is none.', CDBT_PLUGIN_SLUG);
  298. $add_close_btn = false;
  299. }
  300. $close_btn = (isset($add_close_btn) && !$add_close_btn) ? '' : '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">'. __('Close', CDBT_PLUGIN_SLUG) .'</span></button>';
  301. $information_html = '<div class="alert alert-info">'. $close_btn . $msg_str .'</div>';
  302. $display_html = sprintf($list_html, $title, $controller_block, '', '', $information_html);
  303. }
  304. }
  305. } else {
  306. $display_html = '<div class="alert alert-info">'. __('The enabled tables is not exists currently.<br />Please create tables.', CDBT_PLUGIN_SLUG) .'</div>';
  307. }
  308. return $display_html;
  309. }