translation.php (16687B)
1 <?php 2 class ControllerDesignTranslation extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('design/translation'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('design/translation'); 11 12 $this->getList(); 13 } 14 15 public function add() { 16 $this->load->language('design/translation'); 17 18 $this->document->setTitle($this->language->get('heading_title')); 19 20 $this->load->model('design/translation'); 21 22 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 23 $this->model_design_translation->addTranslation($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('design/translation', 'user_token=' . $this->session->data['user_token'] . $url, true)); 42 } 43 44 $this->getForm(); 45 } 46 47 public function edit() { 48 $this->load->language('design/translation'); 49 50 $this->document->setTitle($this->language->get('heading_title')); 51 52 $this->load->model('design/translation'); 53 54 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 55 $this->model_design_translation->editTranslation($this->request->get['translation_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('design/translation', 'user_token=' . $this->session->data['user_token'] . $url, true)); 74 } 75 76 $this->getForm(); 77 } 78 79 public function delete() { 80 $this->load->language('design/translation'); 81 82 $this->document->setTitle($this->language->get('heading_title')); 83 84 $this->load->model('design/translation'); 85 86 if (isset($this->request->post['selected']) && $this->validateDelete()) { 87 foreach ($this->request->post['selected'] as $translation_id) { 88 $this->model_design_translation->deleteTranslation($translation_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('design/translation', '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 = 'store'; 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('design/translation', 'user_token=' . $this->session->data['user_token'], true) 156 ); 157 158 $this->load->model('localisation/language'); 159 160 $data['add'] = $this->url->link('design/translation/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 161 $data['delete'] = $this->url->link('design/translation/delete', 'user_token=' . $this->session->data['user_token'] . $url, true); 162 163 $data['translations'] = array(); 164 165 $filter_data = array( 166 'sort' => $sort, 167 'order' => $order, 168 'start' => ($page - 1) * $this->config->get('config_limit_admin'), 169 'limit' => $this->config->get('config_limit_admin') 170 ); 171 172 $translation_total = $this->model_design_translation->getTotalTranslations(); 173 174 $results = $this->model_design_translation->getTranslations($filter_data); 175 176 foreach ($results as $result) { 177 $data['translations'][] = array( 178 'translation_id' => $result['translation_id'], 179 'store' => ($result['store_id'] ? $result['store'] : $this->language->get('text_default')), 180 'route' => $result['route'], 181 'language' => $result['language'], 182 'key' => $result['key'], 183 'value' => $result['value'], 184 'edit' => $this->url->link('design/translation/edit', 'user_token=' . $this->session->data['user_token'] . '&translation_id=' . $result['translation_id'], true), 185 ); 186 } 187 188 $data['user_token'] = $this->session->data['user_token']; 189 190 if (isset($this->error['warning'])) { 191 $data['error_warning'] = $this->error['warning']; 192 } else { 193 $data['error_warning'] = ''; 194 } 195 196 if (isset($this->session->data['success'])) { 197 $data['success'] = $this->session->data['success']; 198 199 unset($this->session->data['success']); 200 } else { 201 $data['success'] = ''; 202 } 203 204 if (isset($this->request->post['selected'])) { 205 $data['selected'] = (array)$this->request->post['selected']; 206 } else { 207 $data['selected'] = array(); 208 } 209 210 $url = ''; 211 212 if ($order == 'ASC') { 213 $url .= '&order=DESC'; 214 } else { 215 $url .= '&order=ASC'; 216 } 217 218 if (isset($this->request->get['page'])) { 219 $url .= '&page=' . $this->request->get['page']; 220 } 221 222 $data['sort_store'] = $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . '&sort=store' . $url, true); 223 $data['sort_language'] = $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . '&sort=language' . $url, true); 224 $data['sort_route'] = $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . '&sort=route' . $url, true); 225 $data['sort_key'] = $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . '&sort=key' . $url, true); 226 $data['sort_value'] = $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . '&sort=value' . $url, true); 227 228 $pagination = new Pagination(); 229 $pagination->total = $translation_total; 230 $pagination->page = $page; 231 $pagination->limit = 10; 232 $pagination->url = $this->url->link('design/translation/history', 'user_token=' . $this->session->data['user_token'] . '&page={page}', true); 233 234 $data['pagination'] = $pagination->render(); 235 236 $data['results'] = sprintf($this->language->get('text_pagination'), ($translation_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($translation_total - $this->config->get('config_limit_admin'))) ? $translation_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $translation_total, ceil($translation_total / $this->config->get('config_limit_admin'))); 237 238 $data['sort'] = $sort; 239 $data['order'] = $order; 240 241 $data['header'] = $this->load->controller('common/header'); 242 $data['column_left'] = $this->load->controller('common/column_left'); 243 $data['footer'] = $this->load->controller('common/footer'); 244 245 $this->response->setOutput($this->load->view('design/translation_list', $data)); 246 } 247 248 protected function getForm() { 249 $data['text_form'] = !isset($this->request->get['translation_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); 250 251 if (isset($this->error['warning'])) { 252 $data['error_warning'] = $this->error['warning']; 253 } else { 254 $data['error_warning'] = ''; 255 } 256 257 if (isset($this->error['key'])) { 258 $data['error_key'] = $this->error['key']; 259 } else { 260 $data['error_key'] = ''; 261 } 262 263 $url = ''; 264 265 if (isset($this->request->get['sort'])) { 266 $url .= '&sort=' . $this->request->get['sort']; 267 } 268 269 if (isset($this->request->get['order'])) { 270 $url .= '&order=' . $this->request->get['order']; 271 } 272 273 if (isset($this->request->get['page'])) { 274 $url .= '&page=' . $this->request->get['page']; 275 } 276 277 $data['breadcrumbs'] = array(); 278 279 $data['breadcrumbs'][] = array( 280 'text' => $this->language->get('text_home'), 281 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 282 ); 283 284 $data['breadcrumbs'][] = array( 285 'text' => $this->language->get('heading_title'), 286 'href' => $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . $url, true) 287 ); 288 289 if (!isset($this->request->get['translation_id'])) { 290 $data['action'] = $this->url->link('design/translation/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 291 } else { 292 $data['action'] = $this->url->link('design/translation/edit', 'user_token=' . $this->session->data['user_token'] . '&translation_id=' . $this->request->get['translation_id'] . $url, true); 293 } 294 295 $data['cancel'] = $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . $url, true); 296 297 $data['user_token'] = $this->session->data['user_token']; 298 299 if (isset($this->request->get['translation_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 300 $translation_info = $this->model_design_translation->getTranslation($this->request->get['translation_id']); 301 } 302 303 $this->load->model('setting/store'); 304 305 $data['stores'] = $this->model_setting_store->getStores(); 306 307 if (isset($this->request->post['store_id'])) { 308 $data['store_id'] = $this->request->post['store_id']; 309 } elseif (!empty($translation_info)) { 310 $data['store_id'] = $translation_info['store_id']; 311 } else { 312 $data['store_id'] = ''; 313 } 314 315 $this->load->model('localisation/language'); 316 317 $data['languages'] = $this->model_localisation_language->getLanguages(); 318 319 if (!empty($translation_info)) { 320 $language = $this->model_localisation_language->getLanguage($translation_info['language_id']); 321 $code = $language['code']; 322 } else { 323 $code = $this->config->get('config_language'); 324 $language = $this->model_localisation_language->getLanguageByCode($code); 325 } 326 327 if (isset($this->request->post['language_id'])) { 328 $data['language_id'] = $this->request->post['language_id']; 329 } elseif (!empty($translation_info)) { 330 $data['language_id'] = $translation_info['language_id']; 331 } else { 332 $data['language_id'] = $language['language_id']; 333 } 334 335 if (empty($translation_info)) { 336 // Get a list of files ready to upload 337 $data['paths'] = array(); 338 339 $path = glob(DIR_CATALOG . 'language/'.$code.'/*'); 340 341 while (count($path) != 0) { 342 $next = array_shift($path); 343 344 foreach ((array)glob($next) as $file) { 345 if (is_dir($file)) { 346 $path[] = $file . '/*'; 347 } 348 349 if (substr($file, -4) == '.php') { 350 $data['paths'][] = substr(substr($file, strlen(DIR_CATALOG . 'language/'.$code.'/')), 0, -4); 351 } 352 } 353 } 354 } 355 356 if (isset($this->request->post['route'])) { 357 $data['route'] = $this->request->post['route']; 358 } elseif (!empty($translation_info)) { 359 $data['route'] = $translation_info['route']; 360 } else { 361 $data['route'] = ''; 362 } 363 364 if (isset($this->request->post['key'])) { 365 $data['key'] = $this->request->post['key']; 366 } elseif (!empty($translation_info)) { 367 $data['key'] = $translation_info['key']; 368 } else { 369 $data['key'] = ''; 370 } 371 372 if (!empty($translation_info)) { 373 $directory = DIR_CATALOG . 'language/'; 374 375 if (is_file($directory . $code . '/' . $translation_info['route'] . '.php') && substr(str_replace('\\', '/', realpath($directory . $code . '/' . $translation_info['route'] . '.php')), 0, strlen($directory)) == str_replace('\\', '/', $directory)) { 376 $_ = array(); 377 378 include($directory . $code . '/' . $translation_info['route'] . '.php'); 379 380 foreach ($_ as $key => $value) { 381 if ($translation_info['key'] == $key) { 382 $data['default'] = $value; 383 } 384 } 385 386 if (empty($data['default'])) { 387 $data['default'] = $translation_info['value']; 388 } 389 } 390 } 391 392 if (isset($this->request->post['value'])) { 393 $data['value'] = $this->request->post['value']; 394 } elseif (!empty($translation_info)) { 395 $data['value'] = $translation_info['value']; 396 } else { 397 $data['value'] = ''; 398 } 399 400 $data['header'] = $this->load->controller('common/header'); 401 $data['column_left'] = $this->load->controller('common/column_left'); 402 $data['footer'] = $this->load->controller('common/footer'); 403 404 $this->response->setOutput($this->load->view('design/translation_form', $data)); 405 } 406 407 protected function validateForm() { 408 if (!$this->user->hasPermission('modify', 'design/translation')) { 409 $this->error['warning'] = $this->language->get('error_permission'); 410 } 411 412 if ((utf8_strlen($this->request->post['key']) < 3) || (utf8_strlen($this->request->post['key']) > 64)) { 413 $this->error['key'] = $this->language->get('error_key'); 414 } 415 416 return !$this->error; 417 } 418 419 protected function validateDelete() { 420 if (!$this->user->hasPermission('modify', 'design/translation')) { 421 $this->error['warning'] = $this->language->get('error_permission'); 422 } 423 424 return !$this->error; 425 } 426 427 public function path() { 428 $this->load->language('design/translation'); 429 430 $json = array(); 431 432 if (isset($this->request->get['language_id'])) { 433 $language_id = $this->request->get['language_id']; 434 } else { 435 $language_id = 0; 436 } 437 438 $this->load->model('localisation/language'); 439 440 $language_info = $this->model_localisation_language->getLanguage($language_id); 441 442 if (!empty($language_info)) { 443 $path = glob(DIR_CATALOG . 'language/'.$language_info['code'].'/*'); 444 445 while (count($path) != 0) { 446 $next = array_shift($path); 447 448 foreach ((array)glob($next) as $file) { 449 if (is_dir($file)) { 450 $path[] = $file . '/*'; 451 } 452 453 if (substr($file, -4) == '.php') { 454 $json[] = substr(substr($file, strlen(DIR_CATALOG . 'language/'.$language_info['code'].'/')), 0, -4); 455 } 456 } 457 } 458 } 459 460 $this->response->addHeader('Content-Type: application/json'); 461 $this->response->setOutput(json_encode($json)); 462 } 463 464 public function translation() { 465 $this->load->language('design/translation'); 466 467 $json = array(); 468 469 if (isset($this->request->get['store_id'])) { 470 $store_id = $this->request->get['store_id']; 471 } else { 472 $store_id = 0; 473 } 474 475 if (isset($this->request->get['language_id'])) { 476 $language_id = $this->request->get['language_id']; 477 } else { 478 $language_id = 0; 479 } 480 481 if (isset($this->request->get['path'])) { 482 $route = $this->request->get['path']; 483 } else { 484 $route = ''; 485 } 486 487 $this->load->model('localisation/language'); 488 489 $language_info = $this->model_localisation_language->getLanguage($language_id); 490 491 $directory = DIR_CATALOG . 'language/'; 492 493 if ($language_info && is_file($directory . $language_info['code'] . '/' . $route . '.php') && substr(str_replace('\\', '/', realpath($directory . $language_info['code'] . '/' . $route . '.php')), 0, strlen($directory)) == str_replace('\\', '/', $directory)) { 494 $_ = array(); 495 496 include($directory . $language_info['code'] . '/' . $route . '.php'); 497 498 foreach ($_ as $key => $value) { 499 $json[] = array( 500 'key' => $key, 501 'value' => $value 502 ); 503 } 504 } 505 506 $this->response->addHeader('Content-Type: application/json'); 507 $this->response->setOutput(json_encode($json)); 508 } 509 }