| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly; |
| 3 |
|
| 4 |
class Wdk_location extends Winter_MVC_Controller { |
| 5 |
|
| 6 |
public function __construct(){ |
| 7 |
parent::__construct(); |
| 8 |
} |
| 9 |
|
| 10 |
public function index() |
| 11 |
{ |
| 12 |
$this->data['form'] = &$this->form; |
| 13 |
|
| 14 |
/* [Table Actions Bulk Form] */ |
| 15 |
$table_action = $this->input->post_get('table_action'); |
| 16 |
$action = $this->input->post_get('action'); |
| 17 |
$posts_selected = $this->input->post_get('post'); |
| 18 |
|
| 19 |
if(!empty($table_action)) |
| 20 |
{ |
| 21 |
switch ($action) { |
| 22 |
case 'delete': |
| 23 |
$this->bulk_delete($posts_selected); |
| 24 |
break; |
| 25 |
default: |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
$this->load->model('location_m'); |
| 30 |
|
| 31 |
$this->data['locations'] = $this->location_m->get_tree_table(); |
| 32 |
// Load view |
| 33 |
$this->load->view('wdk_location/index', $this->data); |
| 34 |
} |
| 35 |
|
| 36 |
public function delete() |
| 37 |
{ |
| 38 |
$id = (int) $this->input->post_get('id'); |
| 39 |
$paged = (int) $this->input->post_get('paged'); |
| 40 |
|
| 41 |
// Check _wpnonce |
| 42 |
check_admin_referer( 'wdk-location-delete_'.$id, '_wpnonce' ); |
| 43 |
wdk_access_check('location_m', $id); |
| 44 |
$this->load->model('location_m'); |
| 45 |
|
| 46 |
$this->location_m->delete($id); |
| 47 |
|
| 48 |
wp_redirect(admin_url("admin.php?page=wdk_location&paged=$paged")); |
| 49 |
} |
| 50 |
|
| 51 |
public function bulk_delete($posts_selected) |
| 52 |
{ |
| 53 |
// Check _wpnonce |
| 54 |
check_admin_referer( 'wdk-location-bulk', '_wpnonce'); |
| 55 |
|
| 56 |
$this->load->model('location_m'); |
| 57 |
foreach($posts_selected as $id) |
| 58 |
{ |
| 59 |
wdk_access_check('location_m', $id); |
| 60 |
$this->location_m->delete($id); |
| 61 |
} |
| 62 |
|
| 63 |
wp_redirect(admin_url("admin.php?page=wdk_location&is_updated=true&custom_message=".urlencode(esc_html__('Selected Locations removed', 'wpdirectorykit')))); |
| 64 |
exit; |
| 65 |
} |
| 66 |
|
| 67 |
public function edit() |
| 68 |
{ |
| 69 |
$this->load->model('location_m'); |
| 70 |
|
| 71 |
$id = (int) $this->input->post_get('id'); |
| 72 |
wdk_access_check('location_m', $id); |
| 73 |
|
| 74 |
$this->data['db_data'] = NULL; |
| 75 |
|
| 76 |
$this->data['form'] = &$this->form; |
| 77 |
|
| 78 |
$this->data['parents'] = $this->location_m->get_parents($id); |
| 79 |
|
| 80 |
//exit($this->db->last_query()); |
| 81 |
|
| 82 |
$rules = array( |
| 83 |
array( |
| 84 |
'field' => 'location_title', |
| 85 |
'label' => __('Title', 'wpdirectorykit'), |
| 86 |
'rules' => 'required' |
| 87 |
), |
| 88 |
array( |
| 89 |
'field' => 'order_index', |
| 90 |
'label' => __('Order Index', 'wpdirectorykit'), |
| 91 |
'rules' => '' |
| 92 |
), |
| 93 |
array( |
| 94 |
'field' => 'parent_id', |
| 95 |
'label' => __('Parent', 'wpdirectorykit'), |
| 96 |
'rules' => '' |
| 97 |
), |
| 98 |
array( |
| 99 |
'field' => 'icon_id', |
| 100 |
'label' => __('Parent', 'wpdirectorykit'), |
| 101 |
'rules' => '' |
| 102 |
), |
| 103 |
array( |
| 104 |
'field' => 'image_id', |
| 105 |
'label' => __('Parent', 'wpdirectorykit'), |
| 106 |
'rules' => '' |
| 107 |
), |
| 108 |
array( |
| 109 |
'field' => 'related_svg_map', |
| 110 |
'label' => __('Related SVG Map', 'wpdirectorykit'), |
| 111 |
'rules' => '' |
| 112 |
), |
| 113 |
array( |
| 114 |
'field' => 'related_svg_map_location', |
| 115 |
'label' => __('Related SVG Map Location', 'wpdirectorykit'), |
| 116 |
'rules' => '' |
| 117 |
), |
| 118 |
); |
| 119 |
|
| 120 |
global $wp_filesystem; |
| 121 |
// Initialize the WP filesystem, no more using 'file-put-contents' function |
| 122 |
if (empty($wp_filesystem)) { |
| 123 |
require_once (ABSPATH . '/wp-admin/includes/file.php'); |
| 124 |
WP_Filesystem(); |
| 125 |
} |
| 126 |
// @codingStandardsIgnoreEnd |
| 127 |
|
| 128 |
if(function_exists('run_wdk_svg_map')) { |
| 129 |
$maps_list_json = $wp_filesystem->get_contents(WDK_SVG_MAP_PATH.'resourse/maps-list-db.json'); |
| 130 |
$maps_list = json_decode($maps_list_json, JSON_OBJECT_AS_ARRAY); |
| 131 |
|
| 132 |
$maps_data_json = $wp_filesystem->get_contents(WDK_SVG_MAP_PATH.'resourse/maps-db.json'); |
| 133 |
$maps_data = json_decode($maps_data_json, JSON_OBJECT_AS_ARRAY); |
| 134 |
|
| 135 |
/* translate */ |
| 136 |
$this->data['maps_list'] = array(); |
| 137 |
foreach ($maps_list as $location_key => $location) { |
| 138 |
$this->data['maps_list'][str_replace('.svg', '', $location_key)] = esc_html__($location, 'wdk-svg-map'); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$this->data['map_related_locations'] = array(); |
| 143 |
|
| 144 |
if(!empty($id)) |
| 145 |
{ |
| 146 |
$this->data['db_data'] = $this->location_m->get($id, TRUE); |
| 147 |
} |
| 148 |
|
| 149 |
if(function_exists('run_wdk_svg_map')) { |
| 150 |
if(wmvc_show_data('related_svg_map', $this->data['db_data'], false) && isset($maps_data[wmvc_show_data('related_svg_map', $this->data['db_data'])])) { |
| 151 |
foreach ($maps_data[wmvc_show_data('related_svg_map', $this->data['db_data'])]['locations'] as $location_key => $location) { |
| 152 |
$this->data['map_related_locations'][$location_key] = esc_html__($location, 'wdk-svg-map'); |
| 153 |
} |
| 154 |
|
| 155 |
asort($this->data['map_related_locations']); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if($this->form->run($rules)) |
| 160 |
{ |
| 161 |
|
| 162 |
// Check _wpnonce |
| 163 |
check_admin_referer( 'wdk-location-edit_'.$id, '_wpnonce' ); |
| 164 |
|
| 165 |
// Save procedure for basic data |
| 166 |
$data = $this->location_m->prepare_data(wdk_get_post(), $rules); |
| 167 |
|
| 168 |
if(empty($id) && strpos($data['location_title'], ',') !== FALSE) { |
| 169 |
|
| 170 |
foreach (explode(',', $data['location_title']) as $title) { |
| 171 |
// Save standard wp post |
| 172 |
$insert_id = $this->location_m->insert(array_merge($data, array('location_title' => $title)), NULL); |
| 173 |
} |
| 174 |
wp_redirect(admin_url("admin.php?page=wdk_location&is_updated=true&custom_message=".urlencode(esc_html__('Bulk Locations added', 'wpdirectorykit')))); |
| 175 |
exit; |
| 176 |
} else { |
| 177 |
|
| 178 |
$data['icon_path'] = wdk_generate_path_image($data['icon_id']); |
| 179 |
$data['image_path'] = wdk_generate_path_image($data['image_id']); |
| 180 |
|
| 181 |
// Save standard wp post |
| 182 |
$insert_id = $this->location_m->insert($data, $id); |
| 183 |
|
| 184 |
//exit($this->db->last_error()); |
| 185 |
|
| 186 |
/* indexes by new order and reorder */ |
| 187 |
$results = $this->location_m->get_tree_table(); |
| 188 |
$values = array(); |
| 189 |
$order_index = 1; |
| 190 |
foreach( $results as $item) |
| 191 |
{ |
| 192 |
$values[] = array('order_index' => $order_index, 'idlocation' => $item->idlocation); |
| 193 |
$order_index++; |
| 194 |
} |
| 195 |
$this->db->updateBatch( $this->location_m->_table_name, $values, 'idlocation'); |
| 196 |
|
| 197 |
// redirect |
| 198 |
if(!empty($insert_id) && empty($id)) |
| 199 |
{ |
| 200 |
wp_redirect(admin_url("admin.php?page=wdk_location&id=$insert_id&is_updated=true")); |
| 201 |
exit; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
if(!empty($id)) |
| 208 |
{ |
| 209 |
$this->data['db_data'] = $this->location_m->get($id, TRUE); |
| 210 |
} |
| 211 |
|
| 212 |
$this->load->view('wdk_location/location_edit', $this->data); |
| 213 |
} |
| 214 |
|
| 215 |
public function import_from_svg() |
| 216 |
{ |
| 217 |
|
| 218 |
if(!function_exists('run_wdk_svg_map')) { |
| 219 |
exit(esc_html__('Addon WDK SVG Map missing', 'wdk-svg-map')); |
| 220 |
} |
| 221 |
|
| 222 |
$this->load->model('location_m'); |
| 223 |
|
| 224 |
$id = $this->input->post_get('id'); |
| 225 |
wdk_access_check('location_m', $id); |
| 226 |
|
| 227 |
global $wp_filesystem; |
| 228 |
// Initialize the WP filesystem, no more using 'file-put-contents' function |
| 229 |
if (empty($wp_filesystem)) { |
| 230 |
require_once (ABSPATH . '/wp-admin/includes/file.php'); |
| 231 |
WP_Filesystem(); |
| 232 |
} |
| 233 |
// @codingStandardsIgnoreEnd |
| 234 |
|
| 235 |
$maps_data_json = $wp_filesystem->get_contents(WDK_SVG_MAP_PATH.'resourse/maps-list-db.json'); |
| 236 |
$maps_data = json_decode($maps_data_json, JSON_OBJECT_AS_ARRAY); |
| 237 |
|
| 238 |
/* translate */ |
| 239 |
$this->data['maps_list'] = array(); |
| 240 |
foreach ($maps_data as $location_key => $location) { |
| 241 |
$this->data['maps_list'][str_replace('.svg', '', $location_key)] = esc_html__($location, 'wdk-svg-map'); |
| 242 |
} |
| 243 |
|
| 244 |
$this->data['db_data'] = NULL; |
| 245 |
|
| 246 |
$this->data['form'] = &$this->form; |
| 247 |
|
| 248 |
$this->data['location'] = $this->location_m->get($id, TRUE); |
| 249 |
$this->data['parents'] = $this->location_m->get_parents(); |
| 250 |
|
| 251 |
//exit($this->db->last_query()); |
| 252 |
|
| 253 |
$rules = array ( |
| 254 |
array ( |
| 255 |
'field' => 'related_svg_map', |
| 256 |
'label' => __('Related SVG Map', 'wpdirectorykit'), |
| 257 |
'rules' => 'required' |
| 258 |
), |
| 259 |
array ( |
| 260 |
'field' => 'related_svg_map_location', |
| 261 |
'label' => __('Related SVG Map Location', 'wpdirectorykit'), |
| 262 |
'rules' => '' |
| 263 |
), |
| 264 |
); |
| 265 |
|
| 266 |
if($this->form->run($rules)) |
| 267 |
{ |
| 268 |
|
| 269 |
// Check _wpnonce |
| 270 |
check_admin_referer( 'wdk-svg-import', '_wpnonce' ); |
| 271 |
|
| 272 |
// Save procedure for basic data |
| 273 |
$data = $this->location_m->prepare_data(wdk_get_post(), $rules); |
| 274 |
|
| 275 |
$maps_data_json = $wp_filesystem->get_contents(WDK_SVG_MAP_PATH.'resourse/maps-db.json'); |
| 276 |
$maps_data = json_decode($maps_data_json, JSON_OBJECT_AS_ARRAY); |
| 277 |
|
| 278 |
if(isset($maps_data[strtolower(wmvc_show_data('related_svg_map',$data))])) { |
| 279 |
$insert_data = array('parent_id' => wmvc_show_data('related_svg_map_location',$data, NULL)); |
| 280 |
foreach ($maps_data[strtolower(wmvc_show_data('related_svg_map',$data))]['locations'] as $location_key => $location) { |
| 281 |
if(!$this->location_m->get_by(array('parent_id'=>wmvc_show_data('related_svg_map_location',$data, NULL),'location_title'=>esc_html__($location, 'wdk-svg-map')), TRUE)) |
| 282 |
$insert_id = $this->location_m->insert(array_merge($insert_data, array('location_title' => esc_html__($location, 'wdk-svg-map'))), NULL); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
$this->load->view('wdk_location/import_from_svg', $this->data); |
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|