/**
  * @return mixed
  */
 public function replace()
 {
     if (!$this->user->canModify('common/resource_library')) {
         $error = new AError('');
         return $error->toJSONResponse('NO_PERMISSIONS_402', array('error_text' => sprintf($this->language->get('error_permission_modify'), 'common/resource_library'), 'reset_value' => true));
     }
     //init controller data
     $this->extensions->hk_InitData($this, __FUNCTION__);
     $resource_id = (int) $this->request->get['resource_id'];
     if (!$resource_id) {
         $error = new AError('');
         return $error->toJSONResponse('VALIDATION_ERROR_406', array('error_text' => $this->language->get('error_not_replaced'), 'reset_value' => false));
     }
     $rm = new AResourceManager();
     $info = $rm->getResource($resource_id, $this->language->getContentLanguageID());
     if (!$info) {
         $error = new AError('');
         return $error->toJSONResponse('VALIDATION_ERROR_406', array('error_text' => $this->language->get('error_not_exists'), 'reset_value' => false));
     }
     $rm->setType($info['type_name']);
     $upload_handler = new ResourceUploadHandler(array('script_url' => $this->html->getSecureURL('common/resource_library/delete', '&type=' . $this->request->get['type']), 'max_file_size' => (int) $this->config->get('config_upload_max_size') * 1024, 'upload_dir' => $rm->getTypeDir(), 'upload_url' => '', 'accept_file_types' => $rm->getTypeFileTypes()));
     $this->response->addHeader('Pragma: no-cache');
     $this->response->addHeader('Cache-Control: private, no-cache');
     $this->response->addHeader('Content-Disposition: inline; filename="files.json"');
     $this->response->addHeader('X-Content-Type-Options: nosniff');
     $result = null;
     switch ($this->request->server['REQUEST_METHOD']) {
         case 'HEAD':
         case 'GET':
             $result = $upload_handler->get();
             break;
         case 'POST':
             $result = $upload_handler->post();
             break;
         case 'DELETE':
         case 'OPTIONS':
         default:
             $this->response->addHeader('HTTP/1.0 405 Method Not Allowed');
     }
     foreach ($result as $k => $r) {
         if (!empty($r->error)) {
             $result[$k]->error_text = $this->language->get('error_' . $r->error);
             continue;
         }
         $result[$k]->resource_id = $resource_id;
         $result[$k]->type = $info['type_name'];
         //resource_path
         $resource_path = $rm->buildResourcePath($resource_id, $r->name);
         if (!rename(DIR_RESOURCE . $info['type_name'] . '/' . $r->name, DIR_RESOURCE . $info['type_name'] . '/' . $resource_path)) {
             $message = sprintf($this->language->get('error_cannot_move'), $r->name);
             $error = new AError($message);
             $error->toLog()->toDebug();
             $result[$k]->error_text = $message;
             continue;
         }
         $rm->updateResource($resource_id, array('resource_path' => $resource_path));
         //remove old file of resource
         if ($info['resource_path'] && is_file(DIR_RESOURCE . $info['type_name'] . '/' . $info['resource_path']) && $info['resource_path'] != $resource_path) {
             unlink(DIR_RESOURCE . $info['type_name'] . '/' . $info['resource_path']);
         }
     }
     //update controller data
     $this->extensions->hk_UpdateData($this, __FUNCTION__);
     $this->load->library('json');
     $this->response->addJSONHeader();
     $this->response->setOutput(AJson::encode($result));
 }
 /**
  * @return mixed
  */
 public function upload()
 {
     if (!$this->user->canModify('common/resource_library')) {
         $error = new AError('');
         return $error->toJSONResponse('NO_PERMISSIONS_402', array('error_text' => sprintf($this->language->get('error_permission_modify'), 'common/resource_library'), 'reset_value' => true));
     }
     $rm = new AResourceManager();
     $rm->setType($this->request->get['type']);
     $upload_handler = new ResourceUploadHandler(array('script_url' => $this->html->getSecureURL('common/resource_library/delete', '&type=' . $this->request->get['type']), 'max_file_size' => (int) $this->config->get('config_upload_max_size') * 1024, 'upload_dir' => $rm->getTypeDir(), 'upload_url' => '', 'accept_file_types' => $rm->getTypeFileTypes()));
     $this->response->addHeader('Pragma: no-cache');
     $this->response->addHeader('Cache-Control: private, no-cache');
     $this->response->addHeader('Content-Disposition: inline; filename="files.json"');
     $this->response->addHeader('X-Content-Type-Options: nosniff');
     $result = null;
     switch ($this->request->server['REQUEST_METHOD']) {
         case 'HEAD':
         case 'GET':
             $result = $upload_handler->get();
             break;
         case 'POST':
             $result = $upload_handler->post();
             break;
         case 'DELETE':
         case 'OPTIONS':
         default:
             $this->response->addHeader('HTTP/1.0 405 Method Not Allowed');
     }
     foreach ($result as $k => $r) {
         if (!empty($r->error)) {
             continue;
         }
         $data = array('resource_path' => $r->name, 'resource_code' => '', 'language_id' => $this->config->get('storefront_language_id'));
         $data['name'][$data['language_id']] = $r->name;
         $data['title'][$data['language_id']] = '';
         $data['description'][$data['language_id']] = '';
         $resource_id = $rm->addResource($data);
         if ($resource_id) {
             $info = $rm->getResource($resource_id, $data['language_id']);
             $result[$k]->resource_id = $resource_id;
             $result[$k]->language_id = $data['language_id'];
             $result[$k]->resource_detail_url = $this->html->getSecureURL('common/resource_library/update_resource_details', '&resource_id=' . $resource_id);
             $result[$k]->resource_path = $info['resource_path'];
             $result[$k]->thumbnail_url = $rm->getResourceThumb($resource_id, $this->config->get('config_image_grid_width'), $this->config->get('config_image_grid_height'));
             if (!empty($this->request->get['object_name']) && !empty($this->request->get['object_id'])) {
                 $rm->mapResource($this->request->get['object_name'], $this->request->get['object_id'], $resource_id);
             }
         } else {
             $result[$k]->error = $this->language->get('error_not_added');
         }
     }
     $this->load->library('json');
     $this->response->addJSONHeader();
     $this->response->setOutput(AJson::encode($result));
 }