attribute.php (14011B)
1 <?php 2 class ControllerCatalogAttribute extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('catalog/attribute'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('catalog/attribute'); 11 12 $this->getList(); 13 } 14 15 public function add() { 16 $this->load->language('catalog/attribute'); 17 18 $this->document->setTitle($this->language->get('heading_title')); 19 20 $this->load->model('catalog/attribute'); 21 22 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 23 $this->model_catalog_attribute->addAttribute($this->request->post); 24 25 $this->session->data['success'] = $this->language->get('text_success'); 26 27 $url = ''; 28 29 if (isset($this->request->get['sort'])) { 30 $url .= '&sort=' . $this->request->get['sort']; 31 } 32 33 if (isset($this->request->get['order'])) { 34 $url .= '&order=' . $this->request->get['order']; 35 } 36 37 if (isset($this->request->get['page'])) { 38 $url .= '&page=' . $this->request->get['page']; 39 } 40 41 $this->response->redirect($this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url, true)); 42 } 43 44 $this->getForm(); 45 } 46 47 public function edit() { 48 $this->load->language('catalog/attribute'); 49 50 $this->document->setTitle($this->language->get('heading_title')); 51 52 $this->load->model('catalog/attribute'); 53 54 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 55 $this->model_catalog_attribute->editAttribute($this->request->get['attribute_id'], $this->request->post); 56 57 $this->session->data['success'] = $this->language->get('text_success'); 58 59 $url = ''; 60 61 if (isset($this->request->get['sort'])) { 62 $url .= '&sort=' . $this->request->get['sort']; 63 } 64 65 if (isset($this->request->get['order'])) { 66 $url .= '&order=' . $this->request->get['order']; 67 } 68 69 if (isset($this->request->get['page'])) { 70 $url .= '&page=' . $this->request->get['page']; 71 } 72 73 $this->response->redirect($this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url, true)); 74 } 75 76 $this->getForm(); 77 } 78 79 public function delete() { 80 $this->load->language('catalog/attribute'); 81 82 $this->document->setTitle($this->language->get('heading_title')); 83 84 $this->load->model('catalog/attribute'); 85 86 if (isset($this->request->post['selected']) && $this->validateDelete()) { 87 foreach ($this->request->post['selected'] as $attribute_id) { 88 $this->model_catalog_attribute->deleteAttribute($attribute_id); 89 } 90 91 $this->session->data['success'] = $this->language->get('text_success'); 92 93 $url = ''; 94 95 if (isset($this->request->get['sort'])) { 96 $url .= '&sort=' . $this->request->get['sort']; 97 } 98 99 if (isset($this->request->get['order'])) { 100 $url .= '&order=' . $this->request->get['order']; 101 } 102 103 if (isset($this->request->get['page'])) { 104 $url .= '&page=' . $this->request->get['page']; 105 } 106 107 $this->response->redirect($this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url, true)); 108 } 109 110 $this->getList(); 111 } 112 113 protected function getList() { 114 if (isset($this->request->get['sort'])) { 115 $sort = $this->request->get['sort']; 116 } else { 117 $sort = 'ad.name'; 118 } 119 120 if (isset($this->request->get['order'])) { 121 $order = $this->request->get['order']; 122 } else { 123 $order = 'ASC'; 124 } 125 126 if (isset($this->request->get['page'])) { 127 $page = $this->request->get['page']; 128 } else { 129 $page = 1; 130 } 131 132 $url = ''; 133 134 if (isset($this->request->get['sort'])) { 135 $url .= '&sort=' . $this->request->get['sort']; 136 } 137 138 if (isset($this->request->get['order'])) { 139 $url .= '&order=' . $this->request->get['order']; 140 } 141 142 if (isset($this->request->get['page'])) { 143 $url .= '&page=' . $this->request->get['page']; 144 } 145 146 $data['breadcrumbs'] = array(); 147 148 $data['breadcrumbs'][] = array( 149 'text' => $this->language->get('text_home'), 150 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 151 ); 152 153 $data['breadcrumbs'][] = array( 154 'text' => $this->language->get('heading_title'), 155 'href' => $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url, true) 156 ); 157 158 $data['add'] = $this->url->link('catalog/attribute/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 159 $data['delete'] = $this->url->link('catalog/attribute/delete', 'user_token=' . $this->session->data['user_token'] . $url, true); 160 161 $data['attributes'] = array(); 162 163 $filter_data = array( 164 'sort' => $sort, 165 'order' => $order, 166 'start' => ($page - 1) * $this->config->get('config_limit_admin'), 167 'limit' => $this->config->get('config_limit_admin') 168 ); 169 170 $attribute_total = $this->model_catalog_attribute->getTotalAttributes(); 171 172 $results = $this->model_catalog_attribute->getAttributes($filter_data); 173 174 foreach ($results as $result) { 175 $data['attributes'][] = array( 176 'attribute_id' => $result['attribute_id'], 177 'name' => $result['name'], 178 'attribute_group' => $result['attribute_group'], 179 'sort_order' => $result['sort_order'], 180 'edit' => $this->url->link('catalog/attribute/edit', 'user_token=' . $this->session->data['user_token'] . '&attribute_id=' . $result['attribute_id'] . $url, true) 181 ); 182 } 183 184 if (isset($this->error['warning'])) { 185 $data['error_warning'] = $this->error['warning']; 186 } else { 187 $data['error_warning'] = ''; 188 } 189 190 if (isset($this->session->data['success'])) { 191 $data['success'] = $this->session->data['success']; 192 193 unset($this->session->data['success']); 194 } else { 195 $data['success'] = ''; 196 } 197 198 if (isset($this->request->post['selected'])) { 199 $data['selected'] = (array)$this->request->post['selected']; 200 } else { 201 $data['selected'] = array(); 202 } 203 204 $url = ''; 205 206 if ($order == 'ASC') { 207 $url .= '&order=DESC'; 208 } else { 209 $url .= '&order=ASC'; 210 } 211 212 if (isset($this->request->get['page'])) { 213 $url .= '&page=' . $this->request->get['page']; 214 } 215 216 $data['sort_name'] = $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . '&sort=ad.name' . $url, true); 217 $data['sort_attribute_group'] = $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . '&sort=attribute_group' . $url, true); 218 $data['sort_sort_order'] = $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . '&sort=a.sort_order' . $url, true); 219 220 $url = ''; 221 222 if (isset($this->request->get['sort'])) { 223 $url .= '&sort=' . $this->request->get['sort']; 224 } 225 226 if (isset($this->request->get['order'])) { 227 $url .= '&order=' . $this->request->get['order']; 228 } 229 230 $pagination = new Pagination(); 231 $pagination->total = $attribute_total; 232 $pagination->page = $page; 233 $pagination->limit = $this->config->get('config_limit_admin'); 234 $pagination->url = $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true); 235 236 $data['pagination'] = $pagination->render(); 237 238 $data['results'] = sprintf($this->language->get('text_pagination'), ($attribute_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($attribute_total - $this->config->get('config_limit_admin'))) ? $attribute_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $attribute_total, ceil($attribute_total / $this->config->get('config_limit_admin'))); 239 240 $data['sort'] = $sort; 241 $data['order'] = $order; 242 243 $data['header'] = $this->load->controller('common/header'); 244 $data['column_left'] = $this->load->controller('common/column_left'); 245 $data['footer'] = $this->load->controller('common/footer'); 246 247 $this->response->setOutput($this->load->view('catalog/attribute_list', $data)); 248 } 249 250 protected function getForm() { 251 $data['text_form'] = !isset($this->request->get['attribute_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); 252 253 if (isset($this->error['warning'])) { 254 $data['error_warning'] = $this->error['warning']; 255 } else { 256 $data['error_warning'] = ''; 257 } 258 259 if (isset($this->error['name'])) { 260 $data['error_name'] = $this->error['name']; 261 } else { 262 $data['error_name'] = array(); 263 } 264 265 if (isset($this->error['attribute_group'])) { 266 $data['error_attribute_group'] = $this->error['attribute_group']; 267 } else { 268 $data['error_attribute_group'] = ''; 269 } 270 271 $url = ''; 272 273 if (isset($this->request->get['sort'])) { 274 $url .= '&sort=' . $this->request->get['sort']; 275 } 276 277 if (isset($this->request->get['order'])) { 278 $url .= '&order=' . $this->request->get['order']; 279 } 280 281 if (isset($this->request->get['page'])) { 282 $url .= '&page=' . $this->request->get['page']; 283 } 284 285 $data['breadcrumbs'] = array(); 286 287 $data['breadcrumbs'][] = array( 288 'text' => $this->language->get('text_home'), 289 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 290 ); 291 292 $data['breadcrumbs'][] = array( 293 'text' => $this->language->get('heading_title'), 294 'href' => $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url, true) 295 ); 296 297 if (!isset($this->request->get['attribute_id'])) { 298 $data['action'] = $this->url->link('catalog/attribute/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 299 } else { 300 $data['action'] = $this->url->link('catalog/attribute/edit', 'user_token=' . $this->session->data['user_token'] . '&attribute_id=' . $this->request->get['attribute_id'] . $url, true); 301 } 302 303 $data['cancel'] = $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url, true); 304 305 if (isset($this->request->get['attribute_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 306 $attribute_info = $this->model_catalog_attribute->getAttribute($this->request->get['attribute_id']); 307 } 308 309 $this->load->model('localisation/language'); 310 311 $data['languages'] = $this->model_localisation_language->getLanguages(); 312 313 if (isset($this->request->post['attribute_description'])) { 314 $data['attribute_description'] = $this->request->post['attribute_description']; 315 } elseif (isset($this->request->get['attribute_id'])) { 316 $data['attribute_description'] = $this->model_catalog_attribute->getAttributeDescriptions($this->request->get['attribute_id']); 317 } else { 318 $data['attribute_description'] = array(); 319 } 320 321 if (isset($this->request->post['attribute_group_id'])) { 322 $data['attribute_group_id'] = $this->request->post['attribute_group_id']; 323 } elseif (!empty($attribute_info)) { 324 $data['attribute_group_id'] = $attribute_info['attribute_group_id']; 325 } else { 326 $data['attribute_group_id'] = ''; 327 } 328 329 $this->load->model('catalog/attribute_group'); 330 331 $data['attribute_groups'] = $this->model_catalog_attribute_group->getAttributeGroups(); 332 333 if (isset($this->request->post['sort_order'])) { 334 $data['sort_order'] = $this->request->post['sort_order']; 335 } elseif (!empty($attribute_info)) { 336 $data['sort_order'] = $attribute_info['sort_order']; 337 } else { 338 $data['sort_order'] = ''; 339 } 340 341 $data['header'] = $this->load->controller('common/header'); 342 $data['column_left'] = $this->load->controller('common/column_left'); 343 $data['footer'] = $this->load->controller('common/footer'); 344 345 $this->response->setOutput($this->load->view('catalog/attribute_form', $data)); 346 } 347 348 protected function validateForm() { 349 if (!$this->user->hasPermission('modify', 'catalog/attribute')) { 350 $this->error['warning'] = $this->language->get('error_permission'); 351 } 352 353 if (!$this->request->post['attribute_group_id']) { 354 $this->error['attribute_group'] = $this->language->get('error_attribute_group'); 355 } 356 357 foreach ($this->request->post['attribute_description'] as $language_id => $value) { 358 if ((utf8_strlen($value['name']) < 1) || (utf8_strlen($value['name']) > 64)) { 359 $this->error['name'][$language_id] = $this->language->get('error_name'); 360 } 361 } 362 363 return !$this->error; 364 } 365 366 protected function validateDelete() { 367 if (!$this->user->hasPermission('modify', 'catalog/attribute')) { 368 $this->error['warning'] = $this->language->get('error_permission'); 369 } 370 371 $this->load->model('catalog/product'); 372 373 foreach ($this->request->post['selected'] as $attribute_id) { 374 $product_total = $this->model_catalog_product->getTotalProductsByAttributeId($attribute_id); 375 376 if ($product_total) { 377 $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total); 378 } 379 } 380 381 return !$this->error; 382 } 383 384 public function autocomplete() { 385 $json = array(); 386 387 if (isset($this->request->get['filter_name'])) { 388 $this->load->model('catalog/attribute'); 389 390 $filter_data = array( 391 'filter_name' => $this->request->get['filter_name'], 392 'start' => 0, 393 'limit' => 5 394 ); 395 396 $results = $this->model_catalog_attribute->getAttributes($filter_data); 397 398 foreach ($results as $result) { 399 $json[] = array( 400 'attribute_id' => $result['attribute_id'], 401 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')), 402 'attribute_group' => $result['attribute_group'] 403 ); 404 } 405 } 406 407 $sort_order = array(); 408 409 foreach ($json as $key => $value) { 410 $sort_order[$key] = $value['name']; 411 } 412 413 array_multisort($sort_order, SORT_ASC, $json); 414 415 $this->response->addHeader('Content-Type: application/json'); 416 $this->response->setOutput(json_encode($json)); 417 } 418 }