PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
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_Enum.php

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

148 lines 4.1 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_Enum
15 *
16 * Handles a database column of type enum and shows them as a listbox.
17 *
18 * @author Peter Schulz
19 * @since 2.5.0
20 */
21 class WPDA_Simple_Form_Item_Enum extends WPDA_Simple_Form_Item {
22
23 /**
24 * WPDA_Simple_Form_Item_Enum constructor.
25 *
26 * @param array $args
27 */
28 public function __construct( $args = array() ) {
29 parent::__construct( $args );
30
31 $this->data_type = 'enum';
32 $this->item_hide_icon = true;
33 }
34
35 /**
36 * Overwrite method show_item: create listbox from enum
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 class="<?php echo esc_attr( $this->show_context_class_primary_key ); ?> <?php echo esc_attr( $this->item_class ); ?>"
60 <?php echo $this->is_readonly ? ' disabled="disabled"' : ''; ?>
61 <?php echo esc_attr( $this->show_context_item_events ); ?>
62 >
63 <?php
64 $enum_options = $this->item_enum_options;
65 $i = 0;
66 $list_values = array();
67 $list_values[ $this->item_value ] = true;
68 if ( 'new' === $this->show_context_action ) {
69 // Check if there is a default value.
70 if ( $this->item_default_value !== null &&
71 strtolower( $this->item_default_value ) !== 'null' ) {
72 $list_values[ $this->item_default_value ] = true;
73 }
74 }
75
76 if ( is_array( $this->item_enum ) ) {
77 foreach ( $this->item_enum as $value ) {
78 $selected = isset( $list_values[ '' !== $enum_options ? $enum_options[ $i ] : $value ] ) ? ' selected' : '';
79 ?>
80 <option value="<?php echo esc_attr( '' !== $enum_options ? $enum_options[ $i ] : $value ); ?>"<?php echo esc_attr( $selected ); ?>><?php echo esc_attr( $value ); ?></option>
81 <?php
82 $i ++;
83 }
84 }
85 ?>
86 </select>
87 <?php
88 }
89
90 /**
91 * Overwrite method
92 *
93 * @param $pre_insert
94 *
95 * @return bool
96 */
97 public function is_valid( $pre_insert = false ) {
98 if ( ! parent::is_valid( $pre_insert ) ) {
99 return false;
100 }
101
102 if ( 'YES' === $this->is_nullable && ( '' === $this->item_value || null === $this->item_value ) ) {
103 return true;
104 }
105
106 if ( 'enum' === substr( $this->column_type, 0, 4 ) ) {
107 // Get enum from MySQL table.
108 $allowed_values = explode(
109 ',',
110 str_replace(
111 '\'',
112 '',
113 substr( substr( (string) $this->column_type, 5 ), 0, - 1 )
114 )
115 ); // phpcs:ignore -- 8.1 proof
116 $value_found = false;
117 // Check if value is in enum.
118 foreach ( $allowed_values as $allowed_value ) {
119 if ( $allowed_value === $this->item_value ) {
120 $value_found = true; // Value allowed.
121 }
122 }
123 if ( ! $value_found ) {
124 // Value not in enum: inform user and set validation to failed.
125 $msg = new WPDA_Message_Box(
126 array(
127 'message_text' => 'Value for ' .
128 str_replace( '_', ' ', $this->item_name ) .
129 ' ' . __( 'not allowed', 'wp-data-access' ),
130 'message_type' => 'error',
131 'message_is_dismissible' => false,
132 )
133 );
134 $msg->box();
135
136 return false;
137 }
138 } else {
139 // TODO: how can I test the values of a manually created enum?
140 }
141
142 return true;
143 }
144
145 }
146
147 }
148