PluginProbe
WP Directory Kit / 1.5.6
WP Directory Kit v1.5.6
1.5.9 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 All 37 releases
wpdirectorykit / application / controllers / Wdk_fields.php

Wdk_fields.php in WP Directory Kit 1.5.6, at application/controllers/Wdk_fields.php

308 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly;
3
4 class Wdk_fields extends Winter_MVC_Controller {
5
6 public function __construct(){
7
8 if ( ! current_user_can('administrator') ) {
9 wp_die( __('You do not have sufficient permissions to access this page.') );
10 }
11
12 parent::__construct();
13 }
14
15 public function index()
16 {
17
18
19 $this->load->model('field_m');
20
21 $this->data['fields'] = $this->field_m->get();
22
23 //wmvc_dump($this->data['fields']);
24
25
26 $this->load->view('wdk_fields/index', $this->data);
27 }
28
29 public function field_edit()
30 {
31
32 $this->load->model('field_m');
33 $this->load->model('listingfield_m');
34
35 $field_id = (int) $this->input->post_get('id');
36 $this->data['selected_section'] = '';
37 wdk_access_check('category_m', $field_id);
38 $this->data['field_types'] = array(
39 'INPUTBOX' => __('INPUTBOX', 'wpdirectorykit'),
40 'SECTION' => __('SECTION', 'wpdirectorykit'),
41 'TEXTAREA' => __('TEXTAREA', 'wpdirectorykit'),
42 'TEXTAREA_WYSIWYG' => __('TEXTAREA WYSIWYG', 'wpdirectorykit'),
43 'NUMBER' => __('NUMBER', 'wpdirectorykit'),
44 'DATE' => __('DATE', 'wpdirectorykit'),
45 'DROPDOWN' => __('DROPDOWN', 'wpdirectorykit'),
46 'DROPDOWNMULTIPLE' => __('DROPDOWN MULTIPLE', 'wpdirectorykit'),
47 'CHECKBOX' => __('CHECKBOX', 'wpdirectorykit'),
48 'FILEUPLOAD' => __('FILE UPLOAD', 'wpdirectorykit'),
49 );
50 $this->data['section_list'] = array(
51 '' => __('Not Selected', 'wpdirectorykit'),
52 );
53
54 $fields_list = $this->field_m->get();
55 $this->data['fields_tree'] = array();
56 $this->data['fields_categories'] = array();
57 $section = '';
58 foreach ($fields_list as $key => $field) {
59 if($field->field_type == "SECTION") {
60 $section = $field->idfield;
61 $this->data['fields_tree'][$section] = array();
62 } else {
63 $this->data['fields_tree'][$section][$field->idfield] =$field->field_label;
64 $this->data['fields_categories'][$field->idfield] = $section;
65 }
66 }
67
68 $this->db->where(array('field_type'=> 'SECTION'));
69 $section_list = $this->field_m->get();
70 foreach($section_list as $section) {
71 $this->data['section_list'][$section->idfield] = $section->field_label;
72 }
73
74 $this->data['db_data'] = NULL;
75
76 $this->data['form'] = &$this->form;
77
78 /* default data */
79 if(empty($field_id) && empty($_POST)) {
80 $this->data['db_data']['is_visible_frontend'] = 1;
81 $this->data['db_data']['is_visible_dashboard'] = 1;
82 }
83
84 $rules = array(
85 array(
86 'field' => 'field_type',
87 'label' => __('Field Type', 'wpdirectorykit'),
88 'rules' => 'required'
89 ),
90 array(
91 'field' => 'field_label',
92 'label' => __('Field Label', 'wpdirectorykit'),
93 'rules' => 'required'
94 ),
95 array(
96 'field' => 'hint',
97 'label' => __('Hint', 'wpdirectorykit'),
98 'rules' => ''
99 ),
100 array(
101 'field' => 'placeholder',
102 'label' => __('Placeholder', 'wpdirectorykit'),
103 'rules' => ''
104 ),
105 array(
106 'field' => 'columns_number',
107 'label' => __('Columns number/Width', 'wpdirectorykit'),
108 'rules' => ''
109 ),
110 array(
111 'field' => 'is_visible_frontend',
112 'label' => __('Visible on Frontend', 'wpdirectorykit'),
113 'rules' => ''
114 ),
115 array(
116 'field' => 'is_required',
117 'label' => __('Field is Required', 'wpdirectorykit'),
118 'rules' => ''
119 ),
120 array(
121 'field' => 'is_price_format',
122 'label' => __('Field is Price Format', 'wpdirectorykit'),
123 'rules' => ''
124 ),
125 array(
126 'field' => 'is_visible_dashboard',
127 'label' => __('Visible in Dashboard', 'wpdirectorykit'),
128 'rules' => ''
129 ),
130 array(
131 'field' => 'prefix',
132 'label' => __('Prefix', 'wpdirectorykit'),
133 'rules' => ''
134 ),
135 array(
136 'field' => 'suffix',
137 'label' => __('Suffix', 'wpdirectorykit'),
138 'rules' => ''
139 ),
140 array(
141 'field' => 'values_list',
142 'label' => __('Values', 'wpdirectorykit'),
143 'rules' => ''
144 ),
145 array(
146 'field' => 'empty_value',
147 'label' => __('Empty Value', 'wpdirectorykit'),
148 'rules' => ''
149 ),
150 array(
151 'field' => 'icon_id',
152 'label' => __('Parent', 'wpdirectorykit'),
153 'rules' => ''
154 ),
155 array(
156 'field' => 'validation',
157 'label' => __('Validation', 'wpdirectorykit'),
158 'rules' => ''
159 ),
160 array(
161 'field' => 'min_length',
162 'label' => __('Min Length', 'wpdirectorykit'),
163 'rules' => 'is_numerical'
164 ),
165 array(
166 'field' => 'max_length',
167 'label' => __('Max Length', 'wpdirectorykit'),
168 'rules' => 'is_numerical'
169 ),
170 array(
171 'field' => 'date_format',
172 'label' => __('Date Format', 'wpdirectorykit'),
173 'rules' => ''
174 ),
175 array(
176 'field' => 'autosuggestion',
177 'label' => __('Autosuggestion', 'wpdirectorykit'),
178 'rules' => ''
179 ),
180 );
181
182 if($this->form->run($rules))
183 {
184 // Check _wpnonce
185 check_admin_referer( 'wdk-fields-edit_'.$field_id, '_wpnonce' );
186
187 // Save procedure for basic data
188
189 $data = $this->field_m->prepare_data(wdk_get_post(), $rules);
190
191
192 $insert_id = $this->field_m->insert($data, $field_id);
193
194 // check if column exists, add if not exists
195
196 if(!empty($insert_id))
197 $this->listingfield_m->create_table_column($data, $insert_id);
198
199 //exit($this->db->last_error());
200
201 /* Change section */
202 if($this->input->post('section'))
203 if(empty($field_id) || ($field_id && $this->data['fields_categories'][$field_id] != $this->input->post('section'))) {
204 /* add section in new category */
205 $this->data['fields_tree'][$this->input->post('section')][$insert_id] = true;
206
207 /* remove section from old category */
208 if(isset($this->data['fields_categories'][$field_id]))
209 unset($this->data['fields_tree'][$this->data['fields_categories'][$field_id]][$field_id]);
210
211 /* indexes by new order and reorder */
212 $values = array();
213 $order_index = 1;
214 foreach( $this->data['fields_tree'] as $key_section => $section)
215 {
216 if(empty($key_section)) continue;
217
218 $values[] = array('order_index' => $order_index, 'idfield' => $key_section);
219 $order_index++;
220 if(is_array($section))
221 foreach($section as $key_field => $field)
222 {
223 $values[] = array('order_index' => $order_index, 'idfield' => $key_field);
224 $order_index++;
225 }
226 }
227 $this->db->updateBatch( $this->field_m->_table_name, $values, 'idfield');
228
229 }
230
231
232 // redirect
233
234 if(!empty($insert_id) && empty($field_id))
235 {
236 wp_redirect(admin_url("admin.php?page=wdk_fields&function=field_edit&id=$insert_id&is_updated=true"));
237 exit;
238 } else {
239 $fields_list = $this->field_m->get();
240 $this->data['fields_categories'] = array();
241 $section = '';
242 foreach ($fields_list as $key => $field) {
243 if($field->field_type == "SECTION") {
244 $section = $field->idfield;
245 } else {
246 $this->data['fields_categories'][$field->idfield] = $section;
247 }
248 }
249 }
250 }
251
252
253
254 if(!empty($field_id)) {
255 $this->data['db_data'] = $this->field_m->get($field_id, TRUE);
256 $this->data['selected_section'] = wmvc_show_data($field_id, $this->data['fields_categories']);
257 }
258
259 $this->load->view('wdk_fields/field_edit', $this->data);
260 }
261
262 public function ajax_save_order()
263 {
264 $this->load->model('field_m');
265
266 $data_fields_list = sanitize_text_field($this->input->post_get('data_fields_list'));
267
268 // Verify the nonce before proceeding
269 $nonce = $this->input->post_get('wdk_fields_nonce');
270 if (empty($nonce) || !wp_verify_nonce($nonce, 'wdk_fields_save_order')) {
271 wp_send_json_error(array('message' => __('Invalid security token (nonce).', 'wpdirectorykit')), 403);
272 exit();
273 }
274
275 $splitted = explode(';', preg_replace('/[^0-9;]/', '', $data_fields_list));
276 $values = array();
277 $order_index = 1;
278
279 foreach($splitted as $field_id)
280 {
281 if(!empty($field_id))
282 $values[] = array('order_index' => $order_index, 'idfield' => $field_id);
283
284 $order_index++;
285 }
286
287 echo esc_html($this->db->updateBatch( $this->field_m->_table_name, $values, 'idfield'));
288
289 //wmvc_dump($_POST);
290 exit();
291 }
292
293 public function delete()
294 {
295 $field_id = (int) $this->input->post_get('id');
296 wdk_access_check('field_m', $field_id);
297 // Check _wpnonce
298 check_admin_referer( 'wdk-fields-delete_'.$field_id, '_wpnonce' );
299
300 $this->load->model('field_m');
301
302 $this->field_m->delete($field_id);
303
304 wp_redirect(admin_url("admin.php?page=wdk_fields"));
305 }
306
307 }
308