PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.76
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.76
5.5.84 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 All 160 releases
wp-data-access / WPDataProjects / Parent_Child / WPDP_Child_List_Table_Selection.php

WPDP_Child_List_Table_Selection.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.76, at WPDataProjects/Parent_Child/WPDP_Child_List_Table_Selection.php

271 lines 8.2 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\Parent_Child
7 */
8
9 namespace WPDataProjects\Parent_Child {
10
11 use WPDataAccess\Connection\WPDADB;
12 use WPDataAccess\Utilities\WPDA_Message_Box;
13
14 /**
15 * Class WPDP_Child_List_Table_Selection extends WPDP_Child_List_Table
16 *
17 * Provides a list table with lookup selection functionality. Rows can be selected from the list to be added to a
18 * relationship. This list table shows only rows that do not (yet) belong to the relationship.
19 *
20 * @see WPDP_Child_List_Table
21 *
22 * @author Peter Schulz
23 * @since 2.0.0
24 */
25 class WPDP_Child_List_Table_Selection extends WPDP_Child_List_Table {
26
27 /**
28 * Overwrites WPDP_Child_List_Table_Selection constructor
29 *
30 * Turns off all standard links (all links are overwritten in this class).
31 *
32 * @param array $args
33 */
34 public function __construct( $args = array() ) {
35 $this->page_number_item_name = 'child_selection_page_number';
36 $this->search_item_name = 'slov';
37
38 parent::__construct( $args );
39
40 $this->bulk_actions_enabled = true;
41 $this->show_view_link = 'off';
42 $this->allow_insert = 'off';
43 $this->allow_update = 'off';
44 $this->allow_delete = 'off';
45 $this->where_in = 'not in';
46 }
47
48 /**
49 * Overwrites method show to add a back button
50 */
51 public function show() {
52 parent::show();
53
54 if ( ! is_admin() ) {
55 return; // Cannot show back button on web page
56 }
57
58 // Prepare url back button
59 $url = '?page=' . esc_attr( $this->page );
60 $url .= '&' . esc_attr( $this->add_parent_args_to_back_button() ) . $this->page_number_link;
61 ?>
62 <div style="padding-top:5px;padding-left:3px;">
63 <button type="button"
64 onclick="javascript:window.location.href='<?php echo $url; // phpcs:ignore WordPress.Security.EscapeOutput ?>'"
65 class="button button-secondary">
66 <i class="fas fa-angle-left wpda_icon_on_button"></i>
67 <?php echo __( 'Child List', 'wp-data-access' ); ?>
68 </button>
69 </div>
70 <?php
71 }
72
73 /**
74 * Overwrites method add_parent_args to add argument list_table_selection
75 */
76 protected function add_parent_args() {
77 parent::add_parent_args();
78 ?>
79 <input type='hidden' name='list_table_selection' value='TRUE'>
80 <?php
81 }
82
83 /**
84 * Add parent args to back button as url argument string
85 *
86 * @return string
87 */
88 protected function add_parent_args_to_back_button() {
89 if ( isset( $_REQUEST['child_tab'] ) ) {
90 $child_tab = sanitize_text_field( wp_unslash( $_REQUEST['child_tab'] ) ); // input var okay.
91 } else {
92 $child_tab = '';
93 }
94 $args = "&action=list&mode=edit&child_request=TRUE&child_tab=$child_tab";
95 foreach ( $this->parent['parent_key'] as $parent_key ) {
96 $args .= '&' . esc_attr( $parent_key ) . '=' . esc_attr( $this->parent['parent_key_value'][ $parent_key ] );
97 }
98
99 return $args;
100 }
101
102 /**
103 * Overwrite method get_bulk_actions to add 'Add selected rows' to bulk selection
104 *
105 * @return array
106 */
107 public function get_bulk_actions() {
108 $actions = array(
109 'bulk-add' => __( 'Add selected rows', 'wp-data-access' ),
110 );
111
112 return $actions;
113 }
114
115 /**
116 * Overwrites method process_bulk_action to implement selection
117 */
118 public function process_bulk_action() {
119 if ( 'bulk-add' === $this->current_action() ) {
120 if ( ! isset( $_REQUEST['bulk-selected'] ) ) { // input var okay.
121 $msg = new WPDA_Message_Box(
122 array(
123 'message_text' => __( 'Nothing selected', 'wp-data-access' ),
124 )
125 );
126 $msg->box();
127
128 return;
129 }
130
131 $bulk_rows = (array) $_REQUEST['bulk-selected']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
132 //phpcs:ignore - 8.1 proof
133 $no_rows = count( $bulk_rows ); // # rows to be added.
134
135 $rows_to_be_added = array(); // Gonna hold rows to be added.
136
137 for ( $i = 0; $i < $no_rows; $i ++ ) {
138 // Write "json" to named array. Need to strip slashes twice. Once for the normal conversion
139 // and once extra for the pre-conversion of double quotes in method column_cb().
140 $row_object = json_decode( stripslashes( stripslashes( $bulk_rows[ $i ] ) ), true );
141 if ( $row_object ) {
142 $j = 0; // Index used to build array.
143
144 // Check all key columns.
145 foreach ( $this->wpda_list_columns->get_table_primary_key() as $key ) {
146 // Check if key is available.
147 if ( ! isset( $row_object[ $key ] ) ) {
148 wp_die( __( 'ERROR: Invalid URL', 'wp-data-access' ) );
149 }
150
151 // Write key value pair to array.
152 $rows_to_be_added[ $i ][ $j ]['key'] = $key;
153 $rows_to_be_added[ $i ][ $j ]['value'] = $row_object[ $key ];
154 $j ++;
155
156 }
157 }
158 }
159
160 // Looks like everything is there. Add relationship.
161 $no_key_cols = count( $this->wpda_list_columns->get_table_primary_key() );//phpcs:ignore - 8.1 proof
162 $rows_successfully_added = 0; // Number of rows successfully added.
163 $rows_with_errors = 0; // Number of rows that could not be added.
164 for ( $i = 0; $i < $no_rows; $i ++ ) {
165 // Prepare named array for delete operation.
166 $next_row_to_be_added = array();
167
168 $row_found = true;
169 for ( $j = 0; $j < $no_key_cols; $j ++ ) {
170 if ( isset( $rows_to_be_added[ $i ][ $j ]['key'] ) ) {
171 $next_row_to_be_added[ $rows_to_be_added[ $i ][ $j ]['key'] ] = $rows_to_be_added[ $i ][ $j ]['value'];
172 } else {
173 $row_found = false;
174 }
175 }
176
177 if ( $row_found ) {
178 if ( $this->add_row( $next_row_to_be_added ) ) {
179 // Row(s) successfully added.
180 $rows_successfully_added ++;
181 } else {
182 // An error occurred during the insert operation: increase error count.
183 $rows_with_errors ++;
184 }
185 } else {
186 // An error occurred during the insert operation: increase error count.
187 $rows_with_errors ++;
188 }
189 }
190
191 // Inform user about the results of the operation.
192 $message = '';
193 if ( 1 === $rows_successfully_added ) {
194 $message = __( 'Row added', 'wp-data-access' );
195 } elseif ( $rows_successfully_added > 1 ) {
196 $message = "$rows_successfully_added " . __( 'rows added', 'wp-data-access' );
197 }
198 if ( '' !== $message ) {
199 $msg = new WPDA_Message_Box(
200 array(
201 'message_text' => $message,
202 )
203 );
204 $msg->box();
205 }
206
207 if ( $rows_with_errors > 0 ) {
208 $msg = new WPDA_Message_Box(
209 array(
210 'message_text' => __( 'Not all rows have been added', 'wp-data-access' ),
211 'message_type' => 'error',
212 'message_is_dismissible' => false,
213 )
214 );
215 $msg->box();
216 }
217
218 // Records added, redirect to list table
219 if ( isset( $this->child['tab_label'] ) ) {
220 ?>
221 <script type='text/javascript'>
222 jQuery('#form_tab_<?php echo esc_attr( $this->child['tab_label'] ); ?>').submit();
223 </script>
224 <?php
225 }
226 }
227 }
228
229 /**
230 * Adds a relationship for every selected row
231 *
232 * @param array $next_row_to_be_added Rows to be added
233 *
234 * @return mixed Return value of SQL insert statement
235 */
236 protected function add_row( $next_row_to_be_added ) {
237 $wpdadb = WPDADB::get_db_connection( $this->schema_name );
238 if ( null === $wpdadb ) {
239 if ( is_admin() ) {
240 wp_die( sprintf( __( 'ERROR - Remote database %s not available', 'wp-data-access' ), esc_attr( $this->schema_name ) ) );
241 } else {
242 die( sprintf( __( 'ERROR - Remote database %s not available', 'wp-data-access' ), esc_attr( $this->schema_name ) ) );
243 }
244 }
245
246 $child_columns = array();
247 $index = 0;
248 foreach ( $next_row_to_be_added as $row ) {
249 $child_columns[ $this->child['relation_nm']['child_table_select'][ $index ] ] =
250 $row;
251 $child_columns[ $this->child['relation_nm']['child_table_where'][ $index ] ] =
252 $this->parent['parent_key_value'][ $this->parent['parent_key'][ $index ] ];
253 $index ++;
254 }
255
256 return $wpdadb->insert( $this->child['relation_nm']['child_table'], $child_columns );
257 }
258
259 /**
260 * Overwrite method column_default_add_action to prevent parent action being executed
261 *
262 * @param array $item
263 * @param string $column_name
264 * @param array $actions
265 */
266 protected function column_default_add_action( $item, $column_name, &$actions ) { }
267
268 }
269
270 }
271