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

Source for file OrderForm.class.php

Documentation is available at OrderForm.class.php

  1. <?php
  2. /**
  3.  * Содержит класс OrderForm
  4.  *
  5.  * @package energine
  6.  * @subpackage shop
  7.  * @author dr.Pavka
  8.  * @copyright ColoCall 2006
  9.  * @version $Id$
  10.  */
  11.  
  12. //require_once('core/modules/share/components/DBDataSet.class.php');
  13. //require_once('core/modules/shop/components/Order.class.php');
  14. //require_once('core/modules/shop/components/CurrencyConverter.class.php');
  15. //require_once('core/framework/Mail.class.php');
  16.  
  17. /**
  18.  * Предназначен для формирования заказа пользователем
  19.  *
  20.  * @package energine
  21.  * @subpackage shop
  22.  * @author dr.Pavka
  23.  */
  24. class OrderForm extends DBDataSet {
  25.     /**
  26.      * Корзина
  27.      *
  28.      * @var Order 
  29.      * @access protected
  30.      */
  31.     protected $order;
  32.     /**
  33.      * Конструктор класса
  34.      *
  35.      * @param string $name 
  36.      * @param string $module 
  37.      * @param Document $document 
  38.      * @param array $params 
  39.      * @access public
  40.      */
  41.     public function __construct($name$moduleDocument $document,  array $params null{
  42.         parent::__construct($name$module$document,  $params);
  43.         $this->setType(self::COMPONENT_TYPE_FORM_ADD);
  44.         $this->order = new Order();
  45.         $this->setTableName(Order::ORDER_TABLE_NAME);
  46.         $this->setDataSetAction('save-order');
  47.         $this->setTitle($this->translate('TXT_ORDER_FORM'));
  48.     }
  49.     /**
  50.      * Переопределен параметр active
  51.      *
  52.      * @return int 
  53.      * @access protected
  54.      */
  55.  
  56.     protected function defineParams({
  57.         $result array_merge(parent::defineParams(),
  58.         array(
  59.         'active'=>true,
  60.         ));
  61.         return $result;
  62.     }
  63.  
  64.     /**
  65.      * Если корзина - пуста то что заказывать?
  66.      *
  67.      * @return void 
  68.      * @access protected
  69.      */
  70.  
  71.      protected function main({
  72.         if (!Basket::getInstance()->getContents()) {
  73.             throw new SystemException('ERR_BASKET_IS_EMPTY'SystemException::ERR_CRITICAL);
  74.         }
  75.         parent::main();
  76.      }
  77.  
  78.     /**
  79.      * Подтягиваем перечень полей из таблицы пользователей
  80.      *
  81.      * @return mixed 
  82.      * @access protected
  83.      */
  84.  
  85.     protected function loadDataDescription({
  86.         $result parent::loadDataDescription();
  87.         foreach (array_keys($resultas $fieldName{
  88.             if (!in_array($fieldNamearray('order_id''order_delivery_comment'))) {
  89.                 unset($result[$fieldName]);
  90.             }
  91.         }
  92.         $result array_merge($result$this->dbh->getColumnsInfo('user_users'));
  93.         unset($result['u_is_active']$result['u_password']$result['u_avatar_prfile']);
  94.         $commentField $result['order_delivery_comment'];
  95.         unset($result['order_delivery_comment']);
  96.         $result['order_delivery_comment'$commentField;
  97.         return $result;
  98.     }
  99.  
  100.     /**
  101.      * Если пользователь аутентифицирован загружаем его данные из объекта AuthUser
  102.      *
  103.      * @return Data 
  104.      * @access protected
  105.      */
  106.  
  107.     protected function createData({
  108.         $result false;
  109.         $user $this->document->getUser();
  110.         $result new Data();
  111.         if ($user->isAuthenticated()) {
  112.             foreach (array_keys($user->getFields()) as $fieldName{
  113.                 $data[$fieldName$user->getValue($fieldName);
  114.             }
  115.             $result->load(array($data));
  116.  
  117.         }
  118.         return $result;
  119.     }
  120.  
  121.     /**
  122.      * Сохраняет данные о заказе
  123.      *
  124.      * @return void 
  125.      * @access protected
  126.      */
  127.  
  128.     protected function save({
  129.         $this->dbh->beginTransaction();
  130.         try {
  131.             if (!isset($_POST[$this->getTableName()])) {
  132.                 throw new SystemException('ERR_DEV_NO_DATA'SystemException::ERR_WARNING);
  133.             }
  134.             $request Request::getInstance();
  135.             $data $_POST[$this->getTableName()];
  136.             $userData $_POST['user_users'];
  137.             if (!$this->document->getUser()->isAuthenticated()) {
  138.                 $newUser new User();
  139.                 $newUser->create(array_merge($userDataarray('u_password'=>User::generatePassword())));
  140.                 $this->order->setUser($newUser);
  141.             }
  142.             else {
  143.                 $this->order->setUser($this->document->getUser());
  144.             }
  145.  
  146.  
  147.             $data['order_id'$this->order->create(array_merge($userDataarray('order_delivery_comment'=>$data['order_delivery_comment'])));
  148.             $this->order->sendNotification(array_merge($userData$data));
  149.             $_SESSION['order_saved'true;
  150.             $this->order->getBasket()->purify();
  151.  
  152.             $this->dbh->commit();
  153.             $this->response->redirectToCurrentSection('success/');
  154.         }
  155.         catch (Exception $error{
  156.             $this->dbh->rollback();
  157.             $this->failure($error->getMessage());
  158.         }
  159.  
  160.     }
  161.  
  162.     /**
  163.      * Метод отрабатывающий если что то не так пошло
  164.      *
  165.      * @return void 
  166.      * @access protected
  167.      */
  168.  
  169.     protected function failure($errors{
  170.         $this->setBuilder($this->createBuilder());
  171.  
  172.         $dataDescription new DataDescription();
  173.         $ddi new FieldDescription('message');
  174.         $ddi->setType(FieldDescription::FIELD_TYPE_TEXT);
  175.         $ddi->setMode(FieldDescription::FIELD_MODE_READ);
  176.         $ddi->removeProperty('title');
  177.         $dataDescription->addFieldDescription($ddi);
  178.  
  179.         $data new Data();
  180.         $di new Field('message');
  181.         $di->setData($this->translate('MSG_ORDER_FAILED').$errors);
  182.         $data->addField($di);
  183.  
  184.         $this->setDataDescription($dataDescription);
  185.         $this->setData($data);
  186.  
  187.         if ($component $this->document->componentManager->getComponentByName('textBlock_order')) {
  188.             $component->disable();
  189.         }
  190.  
  191.         if ($component $this->document->componentManager->getComponentByName('basket')) {
  192.             $component->disable();
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Метод выводящий сообщение об успешном сохранении данных
  198.      *
  199.      * @return void 
  200.      * @access protected
  201.      */
  202.  
  203.     protected function success({
  204.         //если в сессии нет переменной saved значит этот метод пытаются дернуть напрямую. Не выйдет!
  205.         if (!isset($_SESSION['order_saved'])) {
  206.             throw new SystemException('ERR_404'SystemException::ERR_404);
  207.         }
  208.         //Мавр сделал свое дело...
  209.         unset($_SESSION['order_saved']);
  210.         $this->setBuilder($this->createBuilder());
  211.  
  212.         $dataDescription new DataDescription();
  213.         $ddi new FieldDescription('success_message');
  214.         $ddi->setType(FieldDescription::FIELD_TYPE_TEXT);
  215.         $ddi->setMode(FieldDescription::FIELD_MODE_READ);
  216.         $ddi->removeProperty('title');
  217.         $dataDescription->addFieldDescription($ddi);
  218.  
  219.         $data new Data();
  220.         $di new Field('success_message');
  221.         $di->setData($this->translate('TXT_ORDER_SEND'));
  222.         $data->addField($di);
  223.  
  224.         $this->setDataDescription($dataDescription);
  225.         $this->setData($data);
  226.  
  227.         if ($component $this->document->componentManager->getComponentByName('textBlock_order')) {
  228.             $component->disable();
  229.         }
  230.         if ($component $this->document->componentManager->getComponentByName('basket')) {
  231.             $component->disable();
  232.         }
  233.     }
  234. }
В создании документации нам помог: phpDocumentor