Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Class ControllerModuleManuals
- *
- * @author Yegor Chuperka <ychuperka@live.com>
- */
- class ControllerModuleManuals extends ControllerModuleProductsWithItemsRelationshipsCreator {
- const MANUALS_PER_PAGE = 30;
- private $_manualsDirectoryPath;
- protected function _getModelsList()
- {
- return array(
- 'module/manual',
- 'catalog/product'
- );
- }
- protected function _getTextStringsList()
- {
- return array(
- 'heading_title',
- 'text_table_manuals_column_id',
- 'text_table_manuals_column_product_sku',
- 'text_table_manuals_column_filename',
- 'text_table_manuals_column_manage',
- 'text_table_manuals_link_delete',
- 'text_table_manuals_link_download',
- 'text_table_manuals_header',
- 'text_table_manuals_delete_confirmation',
- 'text_manuals_update',
- 'text_update',
- );
- }
- protected function _getLanguageFileName()
- {
- return 'module/manuals';
- }
- protected function _getUrlPath()
- {
- return 'module/manuals';
- }
- /**
- * Get manuals directory path
- *
- * Returns null if directory not found
- *
- * @return string|null
- */
- private function _getManualsDirectoryPath()
- {
- if ($this->_manualsDirectoryPath === null) {
- $ds = DIRECTORY_SEPARATOR;
- $this->_manualsDirectoryPath = DIR_APPLICATION . '..' . $ds . 'manuals';
- }
- return $this->_manualsDirectoryPath;
- }
- /**
- * Module index page
- */
- public function index()
- {
- $r = $this->request;
- if (isset($r->get['page'])) {
- $page = (int)$r->get['page'];
- } else {
- $page = 1;
- }
- // Put manuals into template data
- $data = $this->_getData();
- $data['manuals'] = $this->model_module_manual->getList($page, self::MANUALS_PER_PAGE);
- // Process pagination
- $pagination = new Pagination();
- $pagination->total = $this->model_module_manual->totalCount();
- $pagination->page = $page;
- $pagination->limit = self::MANUALS_PER_PAGE;
- $pagination->url = $this->url->link(
- 'module/manuals', 'page={page}&token=' . $this->session->data['token'], 'SSL'
- );
- $data['pagination_content'] = $pagination->render();
- // Links
- $data['link_update'] = $this->url->link(
- 'module/manuals/update', 'token=' . $this->session->data['token'], 'SSL'
- );
- // Send output
- $this->response->setOutput($this->load->view('module/manuals.tpl', $data));
- }
- /**
- * Update manuals
- */
- public function update()
- {
- $urlForRedirect = $this->url->link(
- 'module/manuals', 'token=' . $this->session->data['token'], 'SSL'
- );
- // Get manuals file names
- $path = $this->_getManualsDirectoryPath();
- try {
- $fileNames = $this->model_module_manual->getFileNamesFromFileSystem($path);
- if (count($fileNames) > 0) {
- // Delete all relationships between products and manuals
- $this->model_module_manual->deleteAllManuals();
- } else {
- $this->session->data['errors'][] = $this->_getTextString('manuals_directory_is_empty');
- }
- } catch (\Exception $ex) {
- $this->session->data['errors'][] = $ex->getMessage();
- $this->response->redirect($urlForRedirect);
- return;
- }
- // Process each file name
- foreach ($fileNames as $item) {
- // Get products
- $products = $this->_getProductsByFileName($item);
- if (!$products) {
- continue;
- }
- // Prepare path
- $item = substr(
- $item, strpos($item, '/manuals')
- );
- // Create relationships between manual and found products
- foreach ($products as $product) {
- $data = array(
- 'product_id' => $product['product_id'],
- 'filename' => $item
- );
- $this->model_module_manual->addManual($data);
- }
- }
- if (count($this->session->data['errors']) == 0) {
- $this->session->data['success'] = $this->_getTextString('success_update');
- }
- $this->response->redirect($urlForRedirect);
- }
- protected function _getItemSignature()
- {
- return '/manuals';
- }
- /**
- * Module install hook
- *
- * Create some tables here
- */
- public function install()
- {
- $dp = DB_PREFIX;
- $sql = "CREATE TABLE IF NOT EXISTS `{$dp}manual` (
- `manual_id` INT(11) NOT NULL AUTO_INCREMENT,
- `product_id` INT(11) NOT NULL,
- `filename` VARCHAR(256) NOT NULL,
- PRIMARY KEY (`manual_id`)
- ) ENGINE=InnoDB CHARACTER SET = `utf8`;";
- $this->db->query($sql);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement