energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file Basket.class.php

Documentation is available at Basket.class.php

  1. <?php
  2. /**
  3.  * Содержит класс Basket
  4.  *
  5.  * @package energine
  6.  * @subpackage shop
  7.  * @author dr.Pavka
  8.  * @copyright ColoCall 2006
  9.  * @version $Id$
  10.  */
  11.  
  12. //require_once('core/framework/DBWorker.class.php');
  13. //require_once('core/modules/shop/components/Discounts.class.php');
  14. //require_once('core/modules/shop/components/CurrencyConverter.class.php');
  15.  
  16. /**
  17.  * Корзина с выбранными продуктами
  18.  *
  19.  * @package energine
  20.  * @subpackage shop
  21.  * @author dr.Pavka
  22.  */
  23. class Basket extends DBWorker {
  24.  
  25.     /**
  26.      * @access private
  27.      * @static
  28.      * @var Basket единый для всей системы экземпляр класса Basket
  29.      */
  30.     private static $instance;
  31.  
  32.     /**
  33.      * Имя таблицы содержащей данные корзины
  34.      *
  35.      * @var string 
  36.      * @access private
  37.      */
  38.     private $tableName;
  39.  
  40.     /**
  41.      * Идентификатор пользовательского сеанса
  42.      *
  43.      * @var int 
  44.      * @access private
  45.      */
  46.     private $id;
  47.  
  48.     /**
  49.      * Содержание корзины
  50.      *
  51.      * @var array 
  52.      * @access private
  53.      */
  54.     private $contents = null;
  55.  
  56.     /**
  57.      * @access private
  58.      * @var Discounts скидки
  59.      */
  60.     //private $discounts;
  61.  
  62.     /**
  63.      * Конструктор класса
  64.      *
  65.      * @access public
  66.      */
  67.     public function __construct({
  68.         parent::__construct();
  69.         $this->tableName = 'shop_basket';
  70.         $this->id = UserSession::getInstance()->getID();
  71.         //$this->discounts = Discounts::getInstance();
  72.  
  73.         $this->refresh();
  74.     }
  75.  
  76.     private function refresh(){
  77.         if(isset($_POST['basket'])){
  78.             $basketData $_POST['basket'];
  79.  
  80.             if(isset($basketData['add']&& is_array($basketData['add'])){
  81.                 foreach($basketData['add'as $productID => $productCount){
  82.                     $this->put((int)$productID(int)$productCount);
  83.                 }
  84.             }
  85.             elseif(isset($basketData['delete']&& is_array($basketData['deleteItems'])) {
  86.                 foreach ($basketData['deleteItems'as $productID{
  87.                     $this->takeOut((int)$productID);
  88.                 }
  89.             }
  90.             elseif(isset($basketData['update']&& is_array($basketData['update'])){
  91.                 foreach($basketData['update'as $productID => $productCount){
  92.                     $this->update((int)$productID(int)$productCount);
  93.                 }
  94.             }
  95.             
  96.         }
  97.          
  98.  
  99.     }
  100.  
  101.     /**
  102.      * Возвращает единый для всей системы экземпляр класса UserSession.
  103.      *
  104.      * @access public
  105.      * @static
  106.      * @return Basket 
  107.      */
  108.     public static function getInstance({
  109.         if (!isset(self::$instance)) {
  110.             self::$instance new Basket;
  111.         }
  112.         return self::$instance;
  113.     }
  114.     /**
  115.      * Возвращает имя таблицы
  116.      *
  117.      * @return string 
  118.      * @access protected
  119.      */
  120.  
  121.     protected function getTableName({
  122.         return $this->tableName;
  123.     }
  124.  
  125.     /**
  126.      * Добавление товара в корзину
  127.      *
  128.      * @param int идентификатор продукта
  129.      * @param int количество позиций
  130.      * @return void 
  131.      * @access public
  132.      */
  133.  
  134.     public function put($productID$productCount 1{
  135.         $this->dbh->modifyRequest(
  136.         'INSERT INTO '.$this->getTableName().' (product_id, session_id, basket_count) VALUES (%s, %s, %s) '.
  137.         'ON DUPLICATE KEY UPDATE basket_count=basket_count+1',
  138.         $productID,
  139.         $this->id,
  140.         $productCount
  141.         );
  142.     }
  143.  
  144.     /**
  145.      * Изменяет количество позиций продукта
  146.      *
  147.      * @param int идентификатор продукта
  148.      * @param int количество позиций
  149.      * @return void 
  150.      * @access public
  151.      */
  152.  
  153.     public function update($productID$productCount{
  154.         if($productCount{
  155.             $this->dbh->modify(QAL::UPDATE $this->getTableName()array('basket_count' => $productCount)array('session_id'=>$this->id'product_id'=>$productID));
  156.         }
  157.         else {
  158.             $this->dbh->modify(QAL::DELETE$this->getTableName()nullarray('product_id' => $productID));
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Удаляет товар из корзины
  164.      *
  165.      * @param int идентификатор продукта
  166.      * @return void 
  167.      * @access public
  168.      */
  169.  
  170.     public function takeOut($productID{
  171.         $this->dbh->modify(QAL::DELETE$this->getTableName()nullarray('session_id'=>$this->id'basket_id'=>$productID));
  172.     }
  173.  
  174.     /**
  175.      * Очищает корзин
  176.      *
  177.      * @return void 
  178.      * @access public
  179.      */
  180.  
  181.     public function purify({
  182.         $this->dbh->modify(QAL::DELETE$this->getTableName()nullarray('session_id'=>$this->id));
  183.     }
  184.  
  185.  
  186.     /**
  187.      * Возвращает суммарную стоимость товаров в корзине с учетом скидки.
  188.      *
  189.      * @return float 
  190.      * @access public
  191.      */
  192.  
  193.     public function getTotal($withDiscount false{
  194.         $withDiscount false;
  195.         $contents $this->getContents();
  196.         $summ 0;
  197.         if(is_array($contents)) {
  198.             foreach ($contents as $row{
  199.                 if ($withDiscount{
  200.                     $summ += $this->discounts->calculateCost($row['product_summ']);
  201.                 }
  202.                 else {
  203.                     //inspect($row['product_summ']);
  204.                     $summ += $row['product_summ'];
  205.                 }
  206.             }
  207.         }
  208.  
  209.         $converter CurrencyConverter::getInstance();
  210.         $HRNID $converter->getCurrent();
  211.  
  212.         return $converter->format($converter->convert($summ$HRNID$contents[0]['curr_id'])$HRNID);
  213.     }
  214.     
  215.     
  216.  
  217.     /**
  218.      * Возвращает содержимое корзины
  219.      *
  220.      * @param bool 
  221.      * @return array 
  222.      * @access public
  223.      * @see QAL::select()
  224.      */
  225.  
  226.     public function getContents({
  227.         if(is_null($this->contents)){
  228.               $result $this->dbh->selectRequest(
  229.                 //'SELECT main.*, pt.product_name, ext.product_price, ext.product_price*main.basket_count as product_summ, ext.product_price * (1 - dscnt.dscnt_percent / 100) AS product_summ_with_discount, ext.curr_id, product.product_segment, product.product_code '.
  230.                 'SELECT main.*, pt.product_name, ext.product_price, ext.product_price*main.basket_count as product_summ, ext.curr_id, product.product_segment, product.product_code '.
  231.                 'FROM '.$this->getTableName().' main '.
  232.                 'LEFT JOIN shop_products product ON product.product_id = main.product_id '.
  233.                 'LEFT JOIN shop_products_translation pt ON pt.product_id = main.product_id '.
  234.                 'LEFT JOIN shop_product_external_properties ext ON ext.product_code = product.product_code '.
  235.                 //'LEFT JOIN shop_discounts dscnt ON dscnt.group_id = '.$this->discounts->getDefaultGroup().' '.
  236.                 'WHERE pt.lang_id = %s AND session_id = %s',
  237.                 Language::getInstance()->getCurrent(),
  238.                 $this->id
  239.                 );
  240.         
  241.                 if (is_array($result)) {
  242.                     $this->contents = $result;
  243.                 }
  244.                 else {
  245.                     $this->contents = false;
  246.                 }
  247.         }
  248.         return $this->contents;
  249.     }
  250.     
  251.     public function getFormattedContents(){
  252.         $contents $this->getContents()
  253.         
  254.         return ($contents)?array_map(array($this'prepare')$contents):false;         
  255.     }
  256.     
  257.  
  258.     /**
  259.      * Обработка корзины
  260.      *
  261.      * @return array 
  262.      * @access private
  263.      */
  264.  
  265.     private function prepare($row{
  266.         $converter CurrencyConverter::getInstance();
  267.         $HRNID $converter->getCurrent();
  268.         $row['product_price'$converter->format($converter->convert($row['product_price']$HRNID$row['curr_id']),$HRNID);
  269.         $row['product_summ'$converter->format($converter->convert($row['product_summ']$HRNID$row['curr_id']),$HRNID);
  270.         return $row;
  271.     }
  272. }
В создании документации нам помог: phpDocumentor