PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.35
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.35
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.5.35, at WPDataProjects/Project/WPDP_Project_Page_Form.php

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