Advertisement
Guest User

Untitled

a guest
Jul 16th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Class ControllerModuleManuals
  5.  *
  6.  * @author Yegor Chuperka <ychuperka@live.com>
  7.  */
  8. class ControllerModuleManuals extends ControllerModuleProductsWithItemsRelationshipsCreator {
  9.  
  10.     const MANUALS_PER_PAGE = 30;
  11.     private $_manualsDirectoryPath;
  12.  
  13.     protected function _getModelsList()
  14.     {
  15.         return array(
  16.             'module/manual',
  17.             'catalog/product'
  18.         );
  19.     }
  20.  
  21.     protected function _getTextStringsList()
  22.     {
  23.         return array(
  24.             'heading_title',
  25.             'text_table_manuals_column_id',
  26.             'text_table_manuals_column_product_sku',
  27.             'text_table_manuals_column_filename',
  28.             'text_table_manuals_column_manage',
  29.             'text_table_manuals_link_delete',
  30.             'text_table_manuals_link_download',
  31.             'text_table_manuals_header',
  32.             'text_table_manuals_delete_confirmation',
  33.             'text_manuals_update',
  34.             'text_update',
  35.         );
  36.     }
  37.  
  38.     protected function _getLanguageFileName()
  39.     {
  40.         return 'module/manuals';
  41.     }
  42.  
  43.     protected function _getUrlPath()
  44.     {
  45.         return 'module/manuals';
  46.     }
  47.  
  48.     /**
  49.      * Get manuals directory path
  50.      *
  51.      * Returns null if directory not found
  52.      *
  53.      * @return string|null
  54.      */
  55.     private function _getManualsDirectoryPath()
  56.     {
  57.         if ($this->_manualsDirectoryPath === null) {
  58.  
  59.             $ds = DIRECTORY_SEPARATOR;
  60.             $this->_manualsDirectoryPath = DIR_APPLICATION . '..' . $ds . 'manuals';
  61.         }
  62.  
  63.         return $this->_manualsDirectoryPath;
  64.     }
  65.  
  66.     /**
  67.      * Module index page
  68.      */
  69.     public function index()
  70.     {
  71.         $r = $this->request;
  72.         if (isset($r->get['page'])) {
  73.             $page = (int)$r->get['page'];
  74.         } else {
  75.             $page = 1;
  76.         }
  77.  
  78.         // Put manuals into template data
  79.         $data = $this->_getData();
  80.         $data['manuals'] = $this->model_module_manual->getList($page, self::MANUALS_PER_PAGE);
  81.  
  82.         // Process pagination
  83.         $pagination = new Pagination();
  84.         $pagination->total = $this->model_module_manual->totalCount();
  85.         $pagination->page = $page;
  86.         $pagination->limit = self::MANUALS_PER_PAGE;
  87.         $pagination->url = $this->url->link(
  88.             'module/manuals', 'page={page}&token=' . $this->session->data['token'], 'SSL'
  89.         );
  90.         $data['pagination_content'] = $pagination->render();
  91.  
  92.         // Links
  93.         $data['link_update'] = $this->url->link(
  94.             'module/manuals/update', 'token=' . $this->session->data['token'], 'SSL'
  95.         );
  96.  
  97.         // Send output
  98.         $this->response->setOutput($this->load->view('module/manuals.tpl', $data));
  99.     }
  100.  
  101.     /**
  102.      * Update manuals
  103.      */
  104.     public function update()
  105.     {
  106.         $urlForRedirect = $this->url->link(
  107.             'module/manuals', 'token=' . $this->session->data['token'], 'SSL'
  108.         );
  109.  
  110.         // Get manuals file names
  111.         $path = $this->_getManualsDirectoryPath();
  112.         try {
  113.             $fileNames = $this->model_module_manual->getFileNamesFromFileSystem($path);
  114.             if (count($fileNames) > 0) {
  115.                 // Delete all relationships between products and manuals
  116.                 $this->model_module_manual->deleteAllManuals();
  117.             } else {
  118.                 $this->session->data['errors'][] = $this->_getTextString('manuals_directory_is_empty');
  119.             }
  120.         } catch (\Exception $ex) {
  121.             $this->session->data['errors'][] = $ex->getMessage();
  122.             $this->response->redirect($urlForRedirect);
  123.             return;
  124.         }
  125.  
  126.         // Process each file name
  127.         foreach ($fileNames as $item) {
  128.  
  129.             // Get products
  130.             $products = $this->_getProductsByFileName($item);
  131.             if (!$products) {
  132.                 continue;
  133.             }
  134.  
  135.             // Prepare path
  136.             $item = substr(
  137.                 $item, strpos($item, '/manuals')
  138.             );
  139.  
  140.             // Create relationships between manual and found products
  141.             foreach ($products as $product) {
  142.                 $data = array(
  143.                     'product_id' => $product['product_id'],
  144.                     'filename' => $item
  145.                 );
  146.                 $this->model_module_manual->addManual($data);
  147.             }
  148.         }
  149.  
  150.         if (count($this->session->data['errors']) == 0) {
  151.             $this->session->data['success'] = $this->_getTextString('success_update');
  152.         }
  153.         $this->response->redirect($urlForRedirect);
  154.     }
  155.  
  156.     protected function _getItemSignature()
  157.     {
  158.         return '/manuals';
  159.     }
  160.  
  161.     /**
  162.      * Module install hook
  163.      *
  164.      * Create some tables here
  165.      */
  166.     public function install()
  167.     {
  168.         $dp = DB_PREFIX;
  169.         $sql = "CREATE TABLE IF NOT EXISTS `{$dp}manual` (
  170.                  `manual_id` INT(11) NOT NULL AUTO_INCREMENT,
  171.                  `product_id` INT(11) NOT NULL,
  172.                  `filename` VARCHAR(256) NOT NULL,
  173.                  PRIMARY KEY (`manual_id`)
  174.                ) ENGINE=InnoDB CHARACTER SET = `utf8`;";
  175.         $this->db->query($sql);
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement