PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.81
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.81
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 / Simple_Form / WPDP_Simple_Form.php

WPDP_Simple_Form.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.81, at WPDataProjects/Simple_Form/WPDP_Simple_Form.php

268 lines 9.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\Simple_Form
7 */
8
9 namespace WPDataProjects\Simple_Form {
10
11 use WPDataAccess\Connection\WPDADB;
12 use WPDataAccess\Simple_Form\WPDA_Simple_Form;
13 use WPDataAccess\Simple_Form\WPDA_Simple_Form_Item_Autocomplete;
14 use WPDataAccess\Simple_Form\WPDA_Simple_Form_Item_Enum;
15 use WPDataAccess\Simple_Form\WPDA_Simple_Form_Item_Image;
16 use WPDataAccess\Simple_Form\WPDA_Simple_Form_Item_Media;
17 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model;
18 use WPDataAccess\WPDA;
19
20 /**
21 * Class WPDP_Simple_Form extends WPDA_Simple_Form
22 *
23 * Uses table options to hide items and add lookups to data entry form
24 *
25 * @see WPDA_Simple_Form
26 *
27 * @author Peter Schulz
28 * @since 2.0.0
29 */
30 class WPDP_Simple_Form extends WPDA_Simple_Form {
31
32 /**
33 * Options set name
34 *
35 * @var string
36 */
37 protected $setname = 'default';
38
39 /**
40 * WPDP_Simple_Form constructor.
41 *
42 * @param $schema_name
43 * @param $table_name
44 * @param $wpda_list_columns
45 * @param array $args
46 */
47 public function __construct( $schema_name, $table_name, &$wpda_list_columns, $args = array() ) {
48 if ( isset( $args['title'] ) ) {
49 $this->title = $args['title'];
50 }
51
52 parent::__construct( $schema_name, $table_name, $wpda_list_columns, $args );
53
54 $this->setname = $this->wpda_list_columns->get_setname();
55 }
56
57 /**
58 * Overwrites method prepare_items
59 *
60 * Uses table options to hide items and add lookups to data entry form
61 *
62 * @param bool $set_back_form_values
63 */
64 protected function prepare_items( $set_back_form_values = false ) {
65 parent::prepare_items( $set_back_form_values );
66
67 foreach ( $this->wpda_list_columns->get_table_columns() as $columns ) {
68 // Hide columns which have show attribute disabled.
69 if ( isset( $columns['show'] ) && ! $columns['show'] ) {
70 $item_index = $this->get_item_index( $columns['column_name'] );
71 if ( isset( $this->form_items[ $item_index ] ) ) {
72 $this->form_items[ $item_index ]->set_hide_item( true );
73 }
74 }
75
76 // Set default value if available
77 if ( isset( $columns['default'] ) && '' !== $columns['default'] ) {
78 $item_default_value = WPDA::convert_default_value( $columns['default'] );
79 $item_index = $this->get_item_index( $columns['column_name'] );
80 if ( isset( $this->form_items[ $item_index ] ) ) {
81 $this->form_items[ $item_index ]->set_item_default_value( $item_default_value );
82 }
83 }
84 }
85
86 // Check if there are any lookup items defined for this table.
87 $lookup_column_name = array();
88 $tableform = WPDP_Project_Design_Table_Model::get_column_options( $this->table_name, 'tableform', $this->setname, $this->schema_name );
89
90 $i = 0;
91 if ( null !== $tableform ) {
92 foreach ( $tableform as $tableform_item ) {
93 // Process lookup items
94 if ( isset( $tableform_item->lookup ) && false !== $tableform_item->lookup ) {
95 $lookup_column_name[ $tableform_item->column_name ] = $tableform_item->lookup;
96 }
97
98 if (
99 'edit' === $this->action ||
100 (
101 'new' === $this->action &&
102 isset( $this->form_items[ $i ] ) &&
103 'NO' !== $this->form_items[ $i ]->is_nullable()
104 )
105 ) {
106 // Check readonly items
107 if ( isset( $tableform_item->readonly ) && 'on' === $tableform_item->readonly ) {
108 $this->form_items[ $i ]->set_readonly( true );
109 }
110 }
111
112 if ( is_admin() ) {
113 if ( isset( $tableform_item->item_type, $this->form_items[ $i ] ) ) {
114 // Process images
115 if ( 'image' === $tableform_item->item_type ) {
116 $class_path = explode( '\\', get_class( $this->form_items[ $i ] ) ); // phpcs:ignore -- 8.1 proof
117 $class_name = array_pop( $class_path ); // phpcs:ignore -- 8.1 proof
118 if ( 'WPDA_Simple_Form_Item_Image' !== $class_name ) {
119 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Image( $this->form_items[ $i ] );
120 }
121 }
122
123 // Process attachments
124 if ( 'attachment' === $tableform_item->item_type ) {
125 $class_path = explode( '\\', get_class( $this->form_items[ $i ] ) ); // phpcs:ignore -- 8.1 proof
126 $class_name = array_pop( $class_path ); // phpcs:ignore -- 8.1 proof
127 if ( 'WPDA_Simple_Form_Item_Media' !== $class_name ) {
128 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Media( $this->form_items[ $i ] );
129 }
130 }
131 }
132 }
133
134 $i ++;
135 }
136 }
137 if ( count( $lookup_column_name ) > 0 ) { // phpcs:ignore -- 8.1 proof
138 // Process lookup items and create listboxes.
139 $lookups = array();
140 $autocompletes = array();
141 $relationships = WPDP_Project_Design_Table_Model::get_column_options( $this->table_name, 'relationships', $this->setname, $this->schema_name );
142
143 if ( null !== $relationships ) {
144 if ( isset( $relationships['relationships'] ) ) {
145 foreach ( $relationships['relationships'] as $relationship ) {
146 if ( isset( $relationship->relation_type ) ) {
147 if ( 'lookup' === $relationship->relation_type ) {
148 array_push( $lookups, $relationship );
149 } elseif ( 'autocomplete' === $relationship->relation_type ) {
150 array_push( $autocompletes, $relationship ); // phpcs:ignore -- 8.1 proof
151 }
152 }
153 }
154 }
155 }
156
157 $i = 0;
158 foreach ( $this->form_items as $item ) {
159 if ( isset( $lookup_column_name[ $item->get_item_name() ] ) ) {
160 foreach ( $autocompletes as $autocomplete ) {
161 $source_column_name = $autocomplete->source_column_name[0];
162 if ( $source_column_name === $item->get_item_name() ) {
163 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Autocomplete( $item );
164 $this->form_items[ $i ]->set_autocomplete( $autocomplete, $tableform );
165 $this->form_items[ $i ]->set_item_class( 'hide_item' );
166 }
167 }
168 foreach ( $lookups as $lookup ) {
169 // Lookups are always based on a single column. Use first element of array.
170 $source_column_name = $lookup->source_column_name[0];
171 if ( $source_column_name === $item->get_item_name() ) {
172 // Add lookup listbox
173 $target_column_name = str_replace( '`', '', $lookup->target_column_name[0] );
174 $target_table_name = str_replace( '`', '', $lookup->target_table_name );
175
176 if ( isset( $lookup->target_schema_name ) ) {
177 $target_schema_name = $lookup->target_schema_name;
178 } else {
179 $target_schema_name = $this->schema_name;
180 }
181
182 if ( isset( $lookup_column_name[ $source_column_name ] ) ) {
183 $wpdadb = WPDADB::get_db_connection( $target_schema_name );
184 if ( null !== $wpdadb ) {
185 $where = '';
186 for ( $j = 1; $j < count( $lookup->source_column_name ); $j++ ) { // phpcs:ignore -- 8.1 proof
187 $item_index = $this->get_item_index( $lookup->source_column_name[ $j ] );
188 if ( false !== $item_index ) {
189 $item_value = $this->parent['parent_key_value'][ $this->parent['parent_key'][ $j - 1 ] ];
190 if ( 'number' == WPDA::get_type( $this->form_items[ $item_index ]->get_data_type() ) ) {
191 $where .= $wpdadb->prepare(
192 ( '' === $where ? 'where' : 'and' ) . " {$lookup->target_column_name[ $j ]} = %d ",
193 array( $item_value )
194 );
195 } else {
196 $where .= $wpdadb->prepare(
197 ( '' === $where ? 'where' : 'and' ) . " {$lookup->target_column_name[ $j ]} = %s ",
198 array( $item_value )
199 );
200 }
201 }
202 }
203
204 if ( '' === $target_schema_name ) {
205 $lookup_sql_table_name = "`$target_table_name`";
206 } else {
207 $lookup_sql_table_name = "`{$wpdadb->dbname}`.`$target_table_name`";
208 }
209 $lookup_sql =
210 'select `' . str_replace( '`', '', $lookup_column_name[ $source_column_name ] ) . "`, `$target_column_name` " .
211 "from $lookup_sql_table_name " .
212 $where .
213 'order by `' . str_replace( '`', '', $lookup_column_name[ $source_column_name ] ) . "`, `$target_column_name`";
214
215 $rows = $wpdadb->get_results( $lookup_sql, 'ARRAY_A' );
216
217 $lov_values = array();
218 $lov_options = array();
219
220 if ( isset( $relationships['table'] ) ) {
221 foreach ( $relationships['table'] as $table_column ) {
222 if ( isset( $table_column->column_name ) && isset( $table_column->mandatory ) ) {
223 if ( $table_column->column_name === $source_column_name && 'No' === $table_column->mandatory ) {
224 array_push( $lov_values, '' ); // phpcs:ignore -- 8.1 proof
225 array_push( $lov_options, '' ); // phpcs:ignore -- 8.1 proof
226 }
227 }
228 }
229 }
230
231 foreach ( $rows as $row ) {
232 $hide_id = false;
233 foreach ( $tableform as $tableformitem ) {
234 if ( isset( $tableformitem->column_name ) ) {
235 if ( $tableformitem->column_name === $source_column_name ) {
236 $hide_id = isset( $tableformitem->hide_lookup_key ) ?
237 'on' === $tableformitem->hide_lookup_key : false;
238 break;
239 }
240 }
241 }
242 if ( $hide_id ) {
243 $lov_value = $row[ $lookup_column_name[ $source_column_name ] ];
244 } else {
245 $lov_value = $row[ $lookup_column_name[ $source_column_name ] ] . ' (' . $row[ $target_column_name ] . ')';
246 }
247 array_push( $lov_values, $lov_value ); // phpcs:ignore -- 8.1 proof
248 array_push( $lov_options, $row[ $target_column_name ] ); // phpcs:ignore -- 8.1 proof
249 }
250
251 $item->set_enum( $lov_values );
252 $item->set_enum_options( $lov_options );
253 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Enum( $item );
254 }
255 }
256 }
257 }
258 }
259 $i ++;
260 }
261 }
262
263 }
264
265 }
266
267 }
268