length_class.php (13181B)
1 <?php 2 class ControllerLocalisationLengthClass extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('localisation/length_class'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('localisation/length_class'); 11 12 $this->getList(); 13 } 14 15 public function add() { 16 $this->load->language('localisation/length_class'); 17 18 $this->document->setTitle($this->language->get('heading_title')); 19 20 $this->load->model('localisation/length_class'); 21 22 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 23 $this->model_localisation_length_class->addLengthClass($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('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . $url, true)); 42 } 43 44 $this->getForm(); 45 } 46 47 public function edit() { 48 $this->load->language('localisation/length_class'); 49 50 $this->document->setTitle($this->language->get('heading_title')); 51 52 $this->load->model('localisation/length_class'); 53 54 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 55 $this->model_localisation_length_class->editLengthClass($this->request->get['length_class_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('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . $url, true)); 74 } 75 76 $this->getForm(); 77 } 78 79 public function delete() { 80 $this->load->language('localisation/length_class'); 81 82 $this->document->setTitle($this->language->get('heading_title')); 83 84 $this->load->model('localisation/length_class'); 85 86 if (isset($this->request->post['selected']) && $this->validateDelete()) { 87 foreach ($this->request->post['selected'] as $length_class_id) { 88 $this->model_localisation_length_class->deleteLengthClass($length_class_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('localisation/length_class', '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 = 'title'; 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('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . $url, true) 156 ); 157 158 $data['add'] = $this->url->link('localisation/length_class/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 159 $data['delete'] = $this->url->link('localisation/length_class/delete', 'user_token=' . $this->session->data['user_token'] . $url, true); 160 161 $data['length_classes'] = 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 $length_class_total = $this->model_localisation_length_class->getTotalLengthClasses(); 171 172 $results = $this->model_localisation_length_class->getLengthClasses($filter_data); 173 174 foreach ($results as $result) { 175 $data['length_classes'][] = array( 176 'length_class_id' => $result['length_class_id'], 177 'title' => $result['title'] . (($result['length_class_id'] == $this->config->get('config_length_class_id')) ? $this->language->get('text_default') : null), 178 'unit' => $result['unit'], 179 'value' => $result['value'], 180 'edit' => $this->url->link('localisation/length_class/edit', 'user_token=' . $this->session->data['user_token'] . '&length_class_id=' . $result['length_class_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_title'] = $this->url->link('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . '&sort=title' . $url, true); 217 $data['sort_unit'] = $this->url->link('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . '&sort=unit' . $url, true); 218 $data['sort_value'] = $this->url->link('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . '&sort=value' . $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 = $length_class_total; 232 $pagination->page = $page; 233 $pagination->limit = $this->config->get('config_limit_admin'); 234 $pagination->url = $this->url->link('localisation/length_class', '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'), ($length_class_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($length_class_total - $this->config->get('config_limit_admin'))) ? $length_class_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $length_class_total, ceil($length_class_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('localisation/length_class_list', $data)); 248 } 249 250 protected function getForm() { 251 $data['text_form'] = !isset($this->request->get['length_class_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['title'])) { 260 $data['error_title'] = $this->error['title']; 261 } else { 262 $data['error_title'] = array(); 263 } 264 265 if (isset($this->error['unit'])) { 266 $data['error_unit'] = $this->error['unit']; 267 } else { 268 $data['error_unit'] = array(); 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('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . $url, true) 295 ); 296 297 if (!isset($this->request->get['length_class_id'])) { 298 $data['action'] = $this->url->link('localisation/length_class/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 299 } else { 300 $data['action'] = $this->url->link('localisation/length_class/edit', 'user_token=' . $this->session->data['user_token'] . '&length_class_id=' . $this->request->get['length_class_id'] . $url, true); 301 } 302 303 $data['cancel'] = $this->url->link('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . $url, true); 304 305 if (isset($this->request->get['length_class_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 306 $length_class_info = $this->model_localisation_length_class->getLengthClass($this->request->get['length_class_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['length_class_description'])) { 314 $data['length_class_description'] = $this->request->post['length_class_description']; 315 } elseif (isset($this->request->get['length_class_id'])) { 316 $data['length_class_description'] = $this->model_localisation_length_class->getLengthClassDescriptions($this->request->get['length_class_id']); 317 } else { 318 $data['length_class_description'] = array(); 319 } 320 321 if (isset($this->request->post['value'])) { 322 $data['value'] = $this->request->post['value']; 323 } elseif (!empty($length_class_info)) { 324 $data['value'] = $length_class_info['value']; 325 } else { 326 $data['value'] = ''; 327 } 328 329 $data['header'] = $this->load->controller('common/header'); 330 $data['column_left'] = $this->load->controller('common/column_left'); 331 $data['footer'] = $this->load->controller('common/footer'); 332 333 $this->response->setOutput($this->load->view('localisation/length_class_form', $data)); 334 } 335 336 protected function validateForm() { 337 if (!$this->user->hasPermission('modify', 'localisation/length_class')) { 338 $this->error['warning'] = $this->language->get('error_permission'); 339 } 340 341 foreach ($this->request->post['length_class_description'] as $language_id => $value) { 342 if ((utf8_strlen($value['title']) < 3) || (utf8_strlen($value['title']) > 32)) { 343 $this->error['title'][$language_id] = $this->language->get('error_title'); 344 } 345 346 if (!$value['unit'] || (utf8_strlen($value['unit']) > 4)) { 347 $this->error['unit'][$language_id] = $this->language->get('error_unit'); 348 } 349 } 350 351 return !$this->error; 352 } 353 354 protected function validateDelete() { 355 if (!$this->user->hasPermission('modify', 'localisation/length_class')) { 356 $this->error['warning'] = $this->language->get('error_permission'); 357 } 358 359 $this->load->model('catalog/product'); 360 361 foreach ($this->request->post['selected'] as $length_class_id) { 362 if ($this->config->get('config_length_class_id') == $length_class_id) { 363 $this->error['warning'] = $this->language->get('error_default'); 364 } 365 366 $product_total = $this->model_catalog_product->getTotalProductsByLengthClassId($length_class_id); 367 368 if ($product_total) { 369 $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total); 370 } 371 } 372 373 return !$this->error; 374 } 375 }