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

WPDA_Simple_Form_Item_Set.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.41, at WPDataAccess/Simple_Form/WPDA_Simple_Form_Item_Set.php

153 lines 4.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 WPDataAccess\Simple_Form
7 */
8
9 namespace WPDataAccess\Simple_Form {
10
11 use WPDataAccess\Utilities\WPDA_Message_Box;
12
13 /**
14 * Class WPDA_Simple_Form_Item_Set
15 *
16 * Handles a database column of type set and shows them as a listbox which allows multiple selections.
17 *
18 * @author Peter Schulz
19 * @since 2.5.0
20 */
21 class WPDA_Simple_Form_Item_Set extends WPDA_Simple_Form_Item {
22
23 /**
24 * WPDA_Simple_Form_Item_Set constructor.
25 *
26 * @param array $args
27 */
28 public function __construct( $args = array() ) {
29 parent::__construct( $args );
30
31 $this->data_type = 'set';
32 $this->item_hide_icon = true;
33 }
34
35 /**
36 * Overwrite method show_item: create listbox from set
37 */
38 protected function show_item() {
39 if ( $this->is_key_column && ! $this->show_context_update_keys_allowed ) {
40 // PROBLEM
41 // Key columns are set to readonly. This will not work for listboxes.
42 // Therefor listboxes need to be set to disabled. Disabled values however
43 // are not available in a post ($_POST/$_REQUEST).
44 // SOLUTION
45 // Disable listbox (see JS when document is loaded) and add a hidden field
46 // holding the key value (HERE).
47 ?>
48 <input type="hidden"
49 name="<?php echo esc_attr( $this->item_name ); ?>"
50 value="<?php echo esc_attr( $this->item_value ); ?>"
51 />
52 <?php
53 }
54
55 // Enum column: show values in listbox.
56 ?>
57 <select name="<?php echo esc_attr( $this->item_name ); ?>[]"
58 id="<?php echo esc_attr( $this->item_name ); ?>"
59 <?php echo $this->is_readonly ? ' disabled="disabled"' : ''; ?>
60 class="<?php echo esc_attr( $this->show_context_class_primary_key ); ?><?php echo esc_attr( $this->item_class ); ?>"
61 multiple size=5
62 <?php echo esc_attr( $this->show_context_item_events ); ?>
63 >
64 <?php
65 $enum_options = $this->item_enum_options;
66 $i = 0;
67 $list_values = array();
68 if ( 'new' === $this->show_context_action ) {
69 $get_list_values = array( $this->item_default_value );
70 } else {
71 $get_list_values = explode( ',', ( string ) $this->item_value );//phpcs:ignore - 8.1 proof
72 }
73 foreach ( $get_list_values as $get_list_value ) {
74 $list_values[ $get_list_value ] = true;
75 }
76
77 if ( is_array( $this->item_enum ) ) {
78 foreach ( $this->item_enum as $value ) {
79 $selected = isset( $list_values[ '' !== $enum_options ? $enum_options[ $i ] : $value ] ) ? ' selected' : '';
80 ?>
81 <option value="<?php echo esc_attr( '' !== $enum_options ? $enum_options[ $i ] : $value ); ?>"<?php echo esc_attr( $selected ); ?>><?php echo esc_attr( $value ); ?></option>
82 <?php
83 $i ++;
84 }
85 }
86 ?>
87 </select>
88 <?php
89 }
90
91 /**
92 * Overwrite method
93 *
94 * @param $pre_insert
95 *
96 * @return bool
97 */
98 public function is_valid( $pre_insert = false ) {
99 if ( ! parent::is_valid( $pre_insert ) ) {
100 return false;
101 }
102
103 if ( 'YES' === $this->is_nullable && ( '' === $this->item_value || null === $this->item_value ) ) {
104 return true;
105 }
106
107 if ( 'set' === substr( $this->column_type, 0, 3 ) ) {
108 // Get set from MySQL table.
109 $allowed_values = explode(
110 ',',
111 str_replace(
112 '\'',
113 '',
114 substr( substr( (string) $this->column_type, 4 ), 0, - 1 )
115 )
116 );//phpcs:ignore - 8.1 proof
117 $entered_values = explode( ',', ( string ) $this->item_value );//phpcs:ignore - 8.1 proof
118
119 // Check if all values are in set
120 foreach ( $entered_values as $entered_value ) {
121 $value_found = false;
122 foreach ( $allowed_values as $allowed_value ) {
123 if ( $allowed_value === $entered_value ) {
124 $value_found = true; // Value allowed.
125 }
126 }
127 if ( ! $value_found ) {
128 // Value not in enum: inform user and set validation to failed.
129 $msg = new WPDA_Message_Box(
130 array(
131 'message_text' => 'Value for ' .
132 str_replace( '_', ' ', $this->item_name ) .
133 ' ' . __( 'not allowed', 'wp-data-access' ),
134 'message_type' => 'error',
135 'message_is_dismissible' => false,
136 )
137 );
138 $msg->box();
139
140 return false;
141 }
142 }
143 } else {
144 // TODO: how can I test the values of a manually created set?
145 }
146
147 return true;
148 }
149
150 }
151
152 }
153