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_Autocomplete.php

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

166 lines 5.3 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_Autocomplete;
12
13 /**
14 * Class WPDA_Simple_Form_Item_Autocomplete
15 *
16 * Adds autocomplete support to input field
17 *
18 * @author Peter Schulz
19 * @since 4.1.3
20 */
21 class WPDA_Simple_Form_Item_Autocomplete extends WPDA_Simple_Form_Item {
22
23 const AUTOCOMPLE_NONCE_ACTION = 'WPDA-AUTO-COMPLETE*';
24
25 protected $autocomplete_def = null;
26 protected $tableform_settings = null;
27
28 protected function show_item() {
29 $this->set_item_class( 'wpda_autocomplete_key' );
30
31 parent::show_item();
32
33 if ( null === $this->autocomplete_def ) {
34 wp_die( '<span style="font-weight: bold">' . __( 'ERROR: Invalid autocomplete lookup usage', 'wp-data-access' ) . '</span>' );
35 }
36
37 $lookup_value = '';
38
39 if ( null !== $this->item_value && '' !== $this->item_value ) {
40 // Get lookup value
41 $schema_name = $this->autocomplete_def->target_schema_name;
42 $table_name = $this->autocomplete_def->target_table_name;
43 $column_name = $this->autocomplete_def->target_column_name[0];
44 $lookup_column_name = '';
45 $lookup_column_value = $this->item_value;
46
47 foreach ( $this->tableform_settings as $tableform_setting ) {
48 if ( $tableform_setting->column_name === $this->autocomplete_def->source_column_name[0] ) {
49 $lookup_column_name = $tableform_setting->lookup;
50 }
51 }
52 if ( '' === $lookup_column_name ) {
53 wp_die( '<span style="font-weight: bold">' . __( 'ERROR: Invalid autocomplete lookup usage', 'wp-data-access' ) . '</span>' );
54 }
55
56 $wpda_autocomplete = new WPDA_Autocomplete();
57 $lookup_value = $wpda_autocomplete->autocomplete_lookup(
58 $schema_name,
59 $table_name,
60 $column_name,
61 $lookup_column_name,
62 $lookup_column_value
63 );
64
65 if ( false === $lookup_value ) {
66 echo '<div>Lookup value not found!</div>';
67 }
68 }
69
70 $placeholder = __( 'Start typing', 'wp-data-access' ) . ' ' . strtolower( $this->get_item_label() ) . '...';
71 echo "<input type='text' id='wpda_autocomplete_{$this->get_item_name()}' class='wpda_autocomplete' placeholder='{$placeholder}' value='{$lookup_value}'/>"; // phpcs:ignore WordPress.Security.EscapeOutput
72
73 $this->add_js();
74 }
75
76 protected function add_js() {
77 $wpnonce = wp_create_nonce( self::AUTOCOMPLE_NONCE_ACTION . $this->autocomplete_def->target_table_name );
78 ?>
79 <script type="text/javascript">
80 jQuery(function() {
81 let wpda_path = '<?php echo admin_url( 'admin-ajax.php' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>';
82 let item_name = '<?php echo esc_attr( $this->get_item_name() ); ?>';
83
84 let autocomplete_def = '<?php echo json_encode( $this->autocomplete_def ); ?>';
85 let tableform_settings = '<?php echo json_encode( $this->tableform_settings ); ?>';
86
87 let autocomplete_def_json = jQuery.parseJSON(autocomplete_def);
88 let tableform_settings_json = jQuery.parseJSON(tableform_settings);
89 let lookup_label_column = '';
90
91 for (i=0; i<tableform_settings_json.length; i++) {
92 if (tableform_settings_json[i]['column_name']===item_name) {
93 lookup_label_column = tableform_settings_json[i].lookup;
94 }
95 }
96
97 if (lookup_label_column==='') {
98 alert('ERROR: Cannot call autocomplete service [error in definition]');
99 } else {
100 jQuery('#wpda_autocomplete_' + item_name).autocomplete({
101 autoFocus: true,
102 source: function (name, response) {
103 jQuery.ajax({
104 method: 'POST',
105 url: wpda_path + '?action=wpda_autocomplete',
106 data: {
107 wpda_wpnonce: '<?php echo esc_attr( $wpnonce ); ?>',
108 wpda_source_column_value: jQuery('#wpda_autocomplete_' + item_name).val(),
109 wpda_source_column_name: autocomplete_def_json.source_column_name[0],
110 wpda_target_schema_name: autocomplete_def_json.target_schema_name,
111 wpda_target_table_name: autocomplete_def_json.target_table_name,
112 wpda_target_column_name: autocomplete_def_json.target_column_name[0],
113 wpda_lookup_label_column: lookup_label_column
114 }
115 }).done(
116 function (msg) {
117 if (msg.status === "error") {
118 alert(msg.message);
119 } else {
120 response(msg.rows);
121 }
122 }
123 ).error(
124 function (msg) {
125 alert('ERROR: Autocomplete service call return an invalid response');
126 }
127 );
128 },
129 select: function(e,ui) {
130 jQuery('#' + item_name).val(ui.item.lookup);
131 },
132 change: function(event){
133 if (jQuery('#wpda_autocomplete_' + item_name).val()=='') {
134 jQuery('#' + item_name).val('');
135 }
136 },
137 classes: {
138 "ui-autocomplete": "wpda-autocomplete",
139 }
140 });
141 }
142 });
143 </script>
144 <style type="text/css">
145 ul.wpda-autocomplete.ui-widget-content {
146 background: white;
147 color: inherit;
148 }
149 ul.wpda-autocomplete li.ui-menu-item .ui-menu-item-wrapper.ui-state-active {
150 background: #efefef;
151 color: unset;
152 border: none;
153 }
154 </style>
155 <?php
156 }
157
158 public function set_autocomplete( $autocomplete_def, $tableform_settings ) {
159 $this->autocomplete_def = $autocomplete_def;
160 $this->tableform_settings = $tableform_settings;
161 }
162
163 }
164
165 }
166