PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.2
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.2
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataProjects / Project / WPDP_Project_Page_Form.php

WPDP_Project_Page_Form.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.2, at WPDataProjects/Project/WPDP_Project_Page_Form.php

294 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 *
6 * @package WPDataProjects\Project
7 */
8
9 namespace WPDataProjects\Project {
10
11 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists;
12 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model;
13 use WPDataAccess\Simple_Form\WPDA_Simple_Form_Item_Enum;
14 use WPDataAccess\Simple_Form\WPDA_Simple_Form_Item_Set;
15 use WPDataAccess\WPDA;
16 use WPDataProjects\Parent_Child\WPDP_Child_Form;
17
18 /**
19 * Class WPDP_Project_Page_Form extends WPDP_Child_Form
20 *
21 * @see WPDP_Child_Form
22 *
23 * @author Peter Schulz
24 * @since 2.0.0
25 */
26 class WPDP_Project_Page_Form extends WPDP_Child_Form {
27
28 protected $database_tables = array();
29
30 /**
31 * WPDP_Project_Page_Form constructor.
32 *
33 * @param $schema_name
34 * @param $table_name
35 * @param $wpda_list_columns
36 * @param array $args
37 */
38 public function __construct( $schema_name, $table_name, $wpda_list_columns, array $args = array() ) {
39 // Add column labels.
40 $args['column_headers'] = array(
41 'project_id' => __( 'Project ID', 'wp-data-access' ),
42 'page_id' => __( 'Page ID', 'wp-data-access' ),
43 'add_to_menu' => __( 'Add To Menu', 'wp-data-access' ),
44 'page_name' => __( 'Menu Name', 'wp-data-access' ),
45 'page_type' => __( 'Type', 'wp-data-access' ),
46 'page_schema_name' => __( 'Schema Name', 'wp-data-access' ),
47 'page_table_name' => __( 'Table Name', 'wp-data-access' ),
48 'page_setname' => __( 'Template Set Name', 'wp-data-access' ),
49 'page_mode' => __( 'Mode', 'wp-data-access' ),
50 'page_allow_insert' => __( 'Allow insert?', 'wp-data-access' ),
51 'page_allow_delete' => __( 'Allow delete?', 'wp-data-access' ),
52 'page_allow_import' => __( 'Allow import?', 'wp-data-access' ),
53 'page_allow_bulk' => __( 'Allow bulk actions?', 'wp-data-access' ),
54 'page_allow_full_export' => __( 'Allow full table export?', 'wp-data-access' ),
55 'page_content' => __( 'Post', 'wp-data-access' ),
56 'page_title' => __( 'Title', 'wp-data-access' ),
57 'page_subtitle' => __( 'Subtitle', 'wp-data-access' ),
58 'page_role' => __( 'Role', 'wp-data-access' ),
59 'page_where' => __( 'WHERE Clause', 'wp-data-access' ),
60 'page_orderby' => __( 'Default ORDER BY', 'wp-data-access' ),
61 'page_sequence' => __( 'Seq#', 'wp-data-access' ),
62 );
63
64 parent::__construct( $schema_name, $table_name, $wpda_list_columns, $args );
65 }
66
67 /**
68 * Overwrites method prepare_items for specific user interaction
69 *
70 * @param bool $set_back_form_values
71 */
72 protected function prepare_items( $set_back_form_values = false ) {
73 parent::prepare_items( $set_back_form_values );
74
75 global $wpdb;
76
77 // Get available databases
78 $schema_names = WPDA_Dictionary_Lists::get_db_schemas();
79 $databases = array();
80 foreach ( $schema_names as $schema_name ) {
81 array_push( $databases, $schema_name['schema_name'] );
82
83 // Check table access to prepare table listbox content
84 if ( $wpdb->dbname === $schema_name['schema_name'] ) {
85 $table_access = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS );
86 } else {
87 $table_access = get_option( WPDA::BACKEND_OPTIONNAME_DATABASE_ACCESS . $schema_name['schema_name'] );
88 if ( false === $table_access ) {
89 $table_access = 'show';
90 }
91 }
92 switch ( $table_access ) {
93 case 'show':
94 $tables = $this->get_all_db_tables( $schema_name['schema_name'] );
95 break;
96 case 'hide':
97 $tables = $this->get_all_db_tables( $schema_name['schema_name'] );
98 // Remove WordPress tables from listbox content
99 $tables_named = array();
100 foreach ( $tables as $table ) {
101 $tables_named[ $table ] = true;
102 }
103 foreach ( $wpdb->tables( 'all', true ) as $wp_table ) {
104 unset( $tables_named[ $wp_table ] );
105 }
106 $tables = array();
107 foreach ( $tables_named as $key => $value ) {
108 array_push( $tables, $key );
109 }
110 break;
111 default:
112 // Show only selected tables and views
113 if ( $wpdb->dbname === $schema_name['schema_name'] ) {
114 $tables = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS_SELECTED );
115 } else {
116 $tables = get_option( WPDA::BACKEND_OPTIONNAME_DATABASE_SELECTED . $schema_name['schema_name'] );
117 if ( false === $tables ) {
118 $tables = '';
119 }
120 }
121 }
122 $this->database_tables[ $schema_name['schema_name'] ] = $tables;
123 }
124
125 $tables = array();
126 $column_index = $this->get_item_index( 'page_schema_name' );
127 if ( false !== $column_index ) {
128 $pub_schema_name = $this->form_items[ $column_index ]->get_item_value();
129 if ( '' === $pub_schema_name || null === $pub_schema_name ) {
130 $pub_schema_name = WPDA::get_user_default_scheme();
131 }
132 if ( isset( $this->database_tables[ $pub_schema_name ] ) ) {
133 $tables = $this->database_tables[ $pub_schema_name ];
134 }
135 }
136
137 $i = 0;
138 foreach ( $this->form_items as $item ) {
139 if ( 'page_type' === $item->get_item_name() ) {
140 $item_js =
141 'function set_item_visibility(page_type) { ' .
142 ' if (page_type===\'static\') { ' .
143 ' jQuery(\'[name="page_content"]\').parent().parent().show(); ' .
144 ' jQuery(\'[name="page_table_name"]\').parent().parent().hide(); ' .
145 ' jQuery(\'[name="page_mode"]\').parent().parent().hide(); ' .
146 ' jQuery(\'[name="page_allow_insert"]\').parent().parent().hide(); ' .
147 ' jQuery(\'[name="page_allow_delete"]\').parent().parent().hide(); ' .
148 ' } else { ' .
149 ' jQuery(\'[name="page_table_name"]\').parent().parent().show(); ' .
150 ' jQuery(\'[name="page_mode"]\').parent().parent().show(); ' .
151 ' jQuery(\'[name="page_allow_insert"]\').parent().parent().show(); ' .
152 ' jQuery(\'[name="page_allow_delete"]\').parent().parent().show(); ' .
153 ' jQuery(\'[name="page_content"]\').parent().parent().hide(); ' .
154 ' } ' .
155 '} ' .
156 'jQuery(function () { ' .
157 ' jQuery(\'[name="page_type"]\').change(function() { ' .
158 ' set_item_visibility(jQuery(this).val()); ' .
159 ' }); ' .
160 ' set_item_visibility(jQuery(\'[name="page_type"]\').val()); ' .
161 '});';
162 $item->set_item_js( $item_js );
163 } elseif ( 'page_content' === $item->get_item_name() ) {
164 $posts = get_posts(
165 array(
166 'post_status' => '%',
167 'orderby' => 'ID',
168 )
169 );
170
171 $lov = array();
172 $lov_options = array();
173 // For some reason get_posts always sorts DESC on ID: reverse array.
174 $posts_reverse = array_reverse( $posts );
175 // Set first element to blank.
176 array_push( $lov, '' );
177 array_push( $lov_options, '0' );
178 foreach ( $posts_reverse as $post ) {
179 $post_element = $post->post_title . ' (ID=' . $post->ID . ')';
180 array_push( $lov, $post_element );
181 array_push( $lov_options, $post->ID );
182 }
183
184 $item->set_enum( $lov );
185 $item->set_enum_options( $lov_options );
186 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Enum( $item );
187 } elseif ( 'page_schema_name' === $item->get_item_name() ) {
188 // Prepare listbox for column pub_schema_name
189 if ( '' === $item->get_item_value() || null === $item->get_item_value() ) {
190 $item->set_item_value( WPDA::get_user_default_scheme() );
191 }
192 $item->set_enum( $databases );
193 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Enum( $item );
194 } elseif ( 'page_table_name' === $item->get_item_name() ) {
195 $item->set_enum( $tables );
196 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Enum( $item );
197 } elseif ( 'page_role' === $item->get_item_name() ) {
198 global $wp_roles;
199 $lov = array();
200 $lov_options = array();
201 foreach ( $wp_roles->roles as $role => $val ) {
202 array_push( $lov_options, $role );
203 array_push( $lov, isset( $val['name'] ) ? $val['name'] : $role );
204 }
205 $item->set_enum( $lov );
206 $item->set_enum_options( $lov_options );
207 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Set( $item );
208 } elseif ( 'page_setname' === $item->get_item_name() ) {
209 global $wpdb;
210 $setnames = $wpdb->get_results(
211 $wpdb->prepare(
212 'select distinct wpda_table_setname from `%1s` order by wpda_table_setname', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
213 array(
214 WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ),
215 )
216 ),
217 'ARRAY_A'
218 );
219 $lov = array();
220 foreach ( $setnames as $setname ) {
221 array_push( $lov, $setname['wpda_table_setname'] );
222 }
223 if ( 0 === count( $lov ) ) {
224 array_push( $lov, 'default' );
225 }
226 $item->set_enum( $lov );
227 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Enum( $item );
228 } elseif ( 'page_allow_full_export' === $item->get_item_name() ) {
229 if ( wpda_freemius()->is_free_plan() ) {
230 $item->set_hide_item( true );
231 }
232 }
233 $i ++;
234 }
235 }
236
237 /**
238 * Get all db tables and views
239 *
240 * @param string $database Database schema name
241 *
242 * @return array
243 */
244 protected function get_all_db_tables( $database ) {
245 $tables = array();
246 $db_tables = WPDA_Dictionary_Lists::get_tables( true, $database ); // select all db tables and views
247 foreach ( $db_tables as $db_table ) {
248 array_push( $tables, $db_table['table_name'] ); // add table or view to array
249 }
250
251 return $tables;
252 }
253
254 /**
255 * Overwrites method show
256 *
257 * @param bool $allow_save
258 * @param string $add_param
259 */
260 public function show( $allow_save = true, $add_param = '' ) {
261 parent::show( $allow_save, $add_param );
262
263 global $wpdb;
264 ?>
265 <script type='text/javascript'>
266 var database_tables = new Object();
267 <?php
268 foreach ( $this->database_tables as $key => $value ) {
269 echo "database_tables['$key'] = " . json_encode( $value ) . ';'; // phpcs:ignore WordPress.Security.EscapeOutput
270 }
271 ?>
272
273 jQuery(function () {
274 jQuery("#page_schema_name option[value='<?php echo esc_attr( $wpdb->dbname ); ?>']").text("WordPress database (<?php echo esc_attr( $wpdb->dbname ); ?>)");
275 jQuery('#page_allow_full_export').parent().parent().find('.icon').empty().append('<span title="Adds CSV and JSON full table export buttons to list tables\n\nWorks on back-end pages only" class="material-icons pointer wpda_tooltip">help</span>');
276 jQuery('[name="page_schema_name"]').on('change', function () {
277 jQuery('[name="page_table_name"]').empty();
278 var tables = database_tables[jQuery(this).val()];
279 for ( var i = 0; i < tables.length; i++ ) {
280 jQuery('<option/>', {
281 value: tables[i],
282 html: tables[i]
283 }).appendTo('[name="page_table_name"]');
284 }
285 });
286 });
287 </script>
288 <?php
289 }
290
291 }
292
293 }
294