Advertisement
Guest User

Untitled

a guest
Jul 16th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.88 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Class ModelModuleManual
  5.  *
  6.  * Manual model
  7.  *
  8.  * @author Yegor Chuperka (ychuperka@gmail.com)
  9.  */
  10. class ModelModuleManual extends ModelModuleItemConnectedToProduct
  11. {
  12.  
  13.     private $_dpx = DB_PREFIX;
  14.  
  15.     /**
  16.      * Add manual
  17.      *
  18.      * @param array $data
  19.      * @throws Exception
  20.      *
  21.      * @return mixed
  22.      */
  23.     public function addManual(array $data)
  24.     {
  25.         // Check if relationships already exists
  26.         $sql = "SELECT COUNT(`manual_id`) AS `cnt` FROM `{$this->_dpx}manual` WHERE `product_id` = {$data['product_id']}";
  27.         $result = $this->db->query($sql);
  28.         if ($result->row['cnt'] > 0) {
  29.             return 0;
  30.         }
  31.  
  32.         // Create new relationships
  33.         $data = $this->_prepareData($data);
  34.         $sql = "INSERT INTO `{$this->_dpx}manual` (`product_id`, `filename`, `should_update`) VALUES ({$data['product_id']}, '{$data['filename']}', 1)";
  35.         $this->db->query($sql);
  36.  
  37.         return $this->db->getLastId();
  38.     }
  39.  
  40.     /**
  41.      * Edit manual
  42.      *
  43.      * @param array $data
  44.      * @throws Exception
  45.      */
  46.     public function editManual(array $data)
  47.     {
  48.         $data = $this->_prepareData($data);
  49.  
  50.         $manualId = (int)$data['manual'];
  51.         if ($manualId == 0) {
  52.             throw new Exception('Invalid manual id');
  53.         }
  54.  
  55.         $sql = "UPDATE `{$this->_dpx}manual` SET `product_id` = {$data['product_id']}, `filename` = '{$data['filename']}'"
  56.             . " WHERE `manual_id` = $manualId";
  57.         $this->db->query($sql);
  58.     }
  59.  
  60.     /**
  61.      * Prepare data
  62.      *
  63.      * @param array $data
  64.      * @return array
  65.      *
  66.      * @throws Exception
  67.      */
  68.     private function _prepareData(array $data)
  69.     {
  70.         $preparedData = array(
  71.             'product_id' => (int)$data['product_id'],
  72.             'filename' => $this->db->escape($data['filename'])
  73.         );
  74.  
  75.         if ($preparedData['product_id'] == 0) {
  76.             throw new Exception('Invalid product id');
  77.         } else if (strlen($preparedData['filename']) == 0) {
  78.             throw new Exception('Empty filename');
  79.         }
  80.  
  81.         return $preparedData;
  82.     }
  83.  
  84.     /**
  85.      * Delete manual
  86.      *
  87.      * @param int $manualId
  88.      * @throws InvalidArgumentException
  89.      */
  90.     public function deleteManual($manualId)
  91.     {
  92.         $manualId = (int)$manualId;
  93.         if ($manualId == 0) {
  94.             throw new InvalidArgumentException('Manual id is empty');
  95.         }
  96.  
  97.         $sql = "DELETE FROM `{$this->_dpx}manual` WHERE `manual_id` = $manualId";
  98.         $this->db->query($sql);
  99.     }
  100.  
  101.     /**
  102.      * Delete all manuals
  103.      */
  104.     public function deleteAllManuals()
  105.     {
  106.         $sql = "DELETE FROM `{$this->_dpx}manual` WHERE `should_update` = 1";
  107.         $this->db->query($sql);
  108.     }
  109.  
  110.     /**
  111.      * Get manual by id
  112.      *
  113.      * @param int $manualId
  114.      * @return mixed
  115.      * @throws \Exception
  116.      */
  117.     public function get($manualId)
  118.     {
  119.         $manualId = (int)$manualId;
  120.         if ($manualId == 0) {
  121.             throw new \Exception('Invalid manual id');
  122.         }
  123.  
  124.         $sql = "SELECT * FROM `{$this->_dpx}manual` WHERE `{$this->_dpx}manual`.`manual_id` = $manualId";
  125.         $result = $this->db->query($sql);
  126.         if ($result->num_rows == 0) {
  127.             return null;
  128.         }
  129.  
  130.         return $result->row;
  131.     }
  132.  
  133.     /**
  134.      * Get list of manuals
  135.      *
  136.      * @param int $page
  137.      * @param int $limit
  138.      * @param array $filters
  139.      * @return mixed
  140.      * @throws InvalidArgumentException
  141.      * @throws Exception
  142.      */
  143.     public function getList($page = 1, $limit = 10, array $filters = null)
  144.     {
  145.         $page = (int)$page;
  146.         if ($page == 0) {
  147.             throw new InvalidArgumentException('Invalid page');
  148.         }
  149.         $page--;
  150.  
  151.         $limit = (int)$limit;
  152.         if ($limit < 1) {
  153.             throw new InvalidArgumentException('Invalid limit');
  154.         }
  155.  
  156.         $startPosition = $page * $limit;
  157.         $sql = 'SELECT '
  158.             . "`{$this->_dpx}manual`.`manual_id`, `{$this->_dpx}manual`.`product_id`, `{$this->_dpx}manual`.`filename`, "
  159.             . "`{$this->_dpx}product`.`sku` AS `product_sku`, `{$this->_dpx}product`.`model` AS `product_model` FROM "
  160.             . "`{$this->_dpx}manual` JOIN `{$this->_dpx}product` ON"
  161.             . "`{$this->_dpx}product`.`product_id` = `{$this->_dpx}manual`.`product_id`";
  162.  
  163.         if ($filters !== null && count($filters) > 0) {
  164.  
  165.             if (isset($filters['filter-sku']) && strlen($filters['filter-sku'])) {
  166.                 $sql .= " WHERE `{$this->_dpx}product`.`sku` LIKE '{$this->db->escape($filters['filter-sku'])}%'"
  167.                     . " OR `{$this->_dpx}product`.`model` LIKE '{$this->db->escape($filters['filter-sku'])}%'";
  168.             }
  169.  
  170.             if (isset($filters['filter-order-direction']) && strlen($filters['filter-order-direction']) >= 3) {
  171.  
  172.                 $orderDirection = $filters['filter-order-direction'];
  173.                 switch ($orderDirection) {
  174.                     case 'asc':
  175.                     case 'desc':
  176.                         break;
  177.                     default:
  178.                         throw new Exception('Invalid order direction');
  179.                 }
  180.  
  181.                 $sql .= " ORDER BY `{$this->_dpx}manual`.`manual_id` " . strtoupper($orderDirection);
  182.             }
  183.         }
  184.  
  185.         $sql .= " LIMIT $startPosition,$limit";
  186.  
  187.         return $this->db->query($sql);
  188.     }
  189.  
  190.     /**
  191.      * Get total count
  192.      *
  193.      * @return int
  194.      */
  195.     public function totalCount()
  196.     {
  197.         $sql = "SELECT COUNT(`manual_id`) AS `cnt` FROM `{$this->_dpx}manual`";
  198.         $result = $this->db->query($sql);
  199.  
  200.         return (int)$result->row['cnt'];
  201.     }
  202.  
  203.     protected function _prepareFileName($raw)
  204.     {
  205.         return substr(
  206.             $raw,
  207.             strpos($raw, '/module')
  208.         );
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement