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 / Simple_Form / WPDP_Simple_Form.php

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

280 lines 10.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\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 $args['hide_db_info'] = true;
53
54 parent::__construct( $schema_name, $table_name, $wpda_list_columns, $args );
55
56 $this->setname = $this->wpda_list_columns->get_setname();
57 }
58
59 /**
60 * Overwrites method prepare_items
61 *
62 * Uses table options to hide items and add lookups to data entry form
63 *
64 * @param bool $set_back_form_values
65 */
66 protected function prepare_items( $set_back_form_values = false ) {
67 parent::prepare_items( $set_back_form_values );
68
69 foreach ( $this->wpda_list_columns->get_table_columns() as $columns ) {
70 // Hide columns which have show attribute disabled.
71 if ( isset( $columns['show'] ) && ! $columns['show'] ) {
72 $item_index = $this->get_item_index( $columns['column_name'] );
73 if ( isset( $this->form_items[ $item_index ] ) ) {
74 $this->form_items[ $item_index ]->set_hide_item( true );
75 }
76 }
77
78 // Set default value if available
79 if ( isset( $columns['default'] ) && '' !== $columns['default'] ) {
80 $item_default_value = $columns['default'];
81 if ( '$$USERID$$' === $item_default_value ) {
82 $item_default_value = WPDA::get_current_user_id();
83 } elseif ( '$$USER$$' === $item_default_value ) {
84 $item_default_value = WPDA::get_current_user_login();
85 } elseif ( '$$EMAIL$$' === $item_default_value ) {
86 $item_default_value = WPDA::get_current_user_email();
87 } elseif ( '$$NOW$$' === $item_default_value || '$$NOWDT$$' === $item_default_value ) {
88 global $wpdb;
89 $now_db = $wpdb->get_var( 'select now()' );
90 $db_format = WPDA::DB_DATETIME_FORMAT;
91 $convert_date = \DateTime::createFromFormat( $db_format, $now_db );
92 if ( false !== $convert_date ) {
93 $date_format = WPDA::get_option( WPDA::OPTION_PLUGIN_DATE_FORMAT );
94 $time_format = WPDA::get_option( WPDA::OPTION_PLUGIN_TIME_FORMAT );
95 $item_format = $date_format;
96 if ( '$$NOWDT$$' === $item_default_value ) {
97 $item_format .= " {$time_format}";
98 }
99 $item_default_value = $convert_date->format( $item_format );
100 } else {
101 $item_default_value = '';
102 }
103 }
104 $item_index = $this->get_item_index( $columns['column_name'] );
105 if ( isset( $this->form_items[ $item_index ] ) ) {
106 $this->form_items[ $item_index ]->set_item_default_value( $item_default_value );
107 }
108 }
109 }
110
111 // Check if there are any lookup items defined for this table.
112 $lookup_column_name = array();
113 $tableform = WPDP_Project_Design_Table_Model::get_column_options( $this->table_name, 'tableform', $this->setname, $this->schema_name );
114
115 $i = 0;
116 if ( null !== $tableform ) {
117 foreach ( $tableform as $tableform_item ) {
118 // Process lookup items
119 if ( isset( $tableform_item->lookup ) && false !== $tableform_item->lookup ) {
120 $lookup_column_name[ $tableform_item->column_name ] = $tableform_item->lookup;
121 }
122
123 if ( is_admin() ) {
124 if ( isset( $tableform_item->item_type ) ) {
125 // Process images
126 if ( 'image' === $tableform_item->item_type ) {
127 $class_path = explode( '\\', get_class( $this->form_items[ $i ] ) );
128 $class_name = array_pop( $class_path );
129 if ( 'WPDA_Simple_Form_Item_Image' !== $class_name ) {
130 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Image( $this->form_items[ $i ] );
131 }
132 }
133
134 // Process attachmemts
135 if ( 'attachment' === $tableform_item->item_type ) {
136 $class_path = explode( '\\', get_class( $this->form_items[ $i ] ) );
137 $class_name = array_pop( $class_path );
138 if ( 'WPDA_Simple_Form_Item_Media' !== $class_name ) {
139 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Media( $this->form_items[ $i ] );
140 }
141 }
142 }
143 }
144
145 $i ++;
146 }
147 }
148 if ( sizeof( $lookup_column_name ) > 0 ) {
149 // Process lookup items and create listboxes.
150 $lookups = array();
151 $autocompletes = array();
152 $relationships = WPDP_Project_Design_Table_Model::get_column_options( $this->table_name, 'relationships', $this->setname, $this->schema_name );
153
154 if ( null !== $relationships ) {
155 if ( isset( $relationships['relationships'] ) ) {
156 foreach ( $relationships['relationships'] as $relationship ) {
157 if ( isset( $relationship->relation_type ) ) {
158 if ( 'lookup' === $relationship->relation_type ) {
159 array_push( $lookups, $relationship );
160 } elseif ( 'autocomplete' === $relationship->relation_type ) {
161 array_push( $autocompletes, $relationship );
162 }
163 }
164 }
165 }
166 }
167
168 $i = 0;
169 foreach ( $this->form_items as $item ) {
170 if ( isset( $lookup_column_name[ $item->get_item_name() ] ) ) {
171 foreach ( $autocompletes as $autocomplete ) {
172 $source_column_name = $autocomplete->source_column_name[0];
173 if ( $source_column_name === $item->get_item_name() ) {
174 // TODO Add autocomplete lookup
175 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Autocomplete( $item );
176 $this->form_items[ $i ]->set_autocomplete( $autocomplete, $tableform );
177 $this->form_items[ $i ]->set_item_class( 'hide_item' );
178 }
179 }
180 foreach ( $lookups as $lookup ) {
181 // Lookups are always based on a single column. Use first element of array.
182 $source_column_name = $lookup->source_column_name[0];
183 if ( $source_column_name === $item->get_item_name() ) {
184 // Add lookup listbox
185 $target_column_name = str_replace( '`', '', $lookup->target_column_name[0] );
186 $target_table_name = str_replace( '`', '', $lookup->target_table_name );
187
188 if ( isset( $lookup->target_schema_name ) ) {
189 $target_schema_name = $lookup->target_schema_name;
190 } else {
191 $target_schema_name = $this->schema_name;
192 }
193
194 if ( isset( $lookup_column_name[ $source_column_name ] ) ) {
195 $wpdadb = WPDADB::get_db_connection( $target_schema_name );
196 if ( null !== $wpdadb ) {
197 $where = '';
198 for ( $j = 1; $j < count( $lookup->source_column_name ); $j++ ) {
199 $item_index = $this->get_item_index( $lookup->source_column_name[ $j ] );
200 if ( false !== $item_index ) {
201 $item_value = $this->parent['parent_key_value'][ $this->parent['parent_key'][ $j - 1 ] ];
202 if ( 'number' == WPDA::get_type( $this->form_items[ $item_index ]->get_data_type() ) ) {
203 $where .= $wpdadb->prepare(
204 ( '' === $where ? 'where' : 'and' ) . " {$lookup->target_column_name[ $j ]} = %d ",
205 array( $item_value )
206 );
207 } else {
208 $where .= $wpdadb->prepare(
209 ( '' === $where ? 'where' : 'and' ) . " {$lookup->target_column_name[ $j ]} = %s ",
210 array( $item_value )
211 );
212 }
213 }
214 }
215
216 if ( '' === $target_schema_name ) {
217 $lookup_sql_table_name = "`$target_table_name`";
218 } else {
219 $lookup_sql_table_name = "`{$wpdadb->dbname}`.`$target_table_name`";
220 }
221 $lookup_sql =
222 'select `' . str_replace( '`', '', $lookup_column_name[ $source_column_name ] ) . "`, `$target_column_name` " .
223 "from $lookup_sql_table_name " .
224 $where .
225 'order by `' . str_replace( '`', '', $lookup_column_name[ $source_column_name ] ) . "`, `$target_column_name`";
226
227 $rows = $wpdadb->get_results( $lookup_sql, 'ARRAY_A' );
228
229 $lov_values = array();
230 $lov_options = array();
231
232 if ( isset( $relationships['table'] ) ) {
233 foreach ( $relationships['table'] as $table_column ) {
234 if ( isset( $table_column->column_name ) && isset( $table_column->mandatory ) ) {
235 if ( $table_column->column_name === $source_column_name && 'No' === $table_column->mandatory ) {
236 array_push( $lov_values, '' );
237 array_push( $lov_options, '' );
238 }
239 }
240 }
241 }
242
243 foreach ( $rows as $row ) {
244 $hide_id = false;
245 foreach ( $tableform as $tableformitem ) {
246 if ( isset( $tableformitem->column_name ) ) {
247 if ( $tableformitem->column_name === $source_column_name ) {
248 $hide_id = isset( $tableformitem->hide_lookup_key ) ?
249 'on' === $tableformitem->hide_lookup_key : false;
250 break;
251 }
252 }
253 }
254 if ( $hide_id ) {
255 $lov_value = $row[ $lookup_column_name[ $source_column_name ] ];
256 } else {
257 $lov_value = $row[ $lookup_column_name[ $source_column_name ] ] . ' (' . $row[ $target_column_name ] . ')';
258 }
259 array_push( $lov_values, $lov_value );
260 array_push( $lov_options, $row[ $target_column_name ] );
261 }
262
263 $item->set_enum( $lov_values );
264 $item->set_enum_options( $lov_options );
265 $this->form_items[ $i ] = new WPDA_Simple_Form_Item_Enum( $item );
266 }
267 }
268 }
269 }
270 }
271 $i ++;
272 }
273 }
274
275 }
276
277 }
278
279 }
280