/protected/controllers/CartController.php

https://bitbucket.org/markmoskalenko/svitor · PHP · 256 lines · 153 code · 60 blank · 43 comment · 31 complexity · 2c6cc8c9f453ed19a97d062a0f209985 MD5 · raw file

  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: it-Yes
  5. * Date: 25.07.13
  6. * Time: 15:44
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. class CartController extends AppController
  10. {
  11. public function filters()
  12. {
  13. return array('accessControl');
  14. }
  15. public function accessRules()
  16. {
  17. return array(
  18. array(
  19. 'deny',
  20. 'actions' => array('AddCart', 'DeleteCart', 'UpdateCart', 'Orders', 'ViewOrder', 'ViewOrder',
  21. 'EditOrderContact', 'EditOrderProduct', 'SaveProduct', 'DeleteProduct', 'ConfirmOrder', 'MyOrders'),
  22. 'users' => array('?')
  23. ),
  24. array(
  25. 'allow',
  26. 'actions' => array('AddCart', 'DeleteCart', 'UpdateCart', 'Orders', 'ViewOrder', 'ViewOrder',
  27. 'EditOrderContact', 'EditOrderProduct', 'SaveProduct', 'DeleteProduct', 'ConfirmOrder', 'MyOrders'),
  28. 'users' => array('@')
  29. )
  30. );
  31. }
  32. /**
  33. * Добавить в корзину товар | AJAX |
  34. *
  35. * @param $id
  36. */
  37. public function actionAddCart($id)
  38. {
  39. $id = intval($id);
  40. $product = Product::model()->find('id = :id', array(':id' => $id));
  41. Yii::app()->shoppingCart->put($product);
  42. echo Yii::app()->shoppingCart->getCount();
  43. if(!Yii::app()->request->isAjaxRequest) $this->redirect('/cart/orders'); else Yii::app()->end();
  44. }
  45. /**
  46. * Удалить из корзины товар | AJAX |
  47. *
  48. * @param $id
  49. */
  50. public function actionDeleteCart($id)
  51. {
  52. Yii::app()->shoppingCart->remove( Product::model()->findByPk(intval( $id ))->getId() );
  53. echo Yii::app()->shoppingCart->getCount();
  54. if(!Yii::app()->request->isAjaxRequest) $this->redirect('/cart/orders'); else Yii::app()->end();
  55. }
  56. public function actionUpdateCart($id, $quantity)
  57. {
  58. Yii::app()->shoppingCart->update( Product::model()->findByPk(intval( $id )), $quantity );
  59. if(!Yii::app()->request->isAjaxRequest) $this->redirect('/cart/orders'); else Yii::app()->end();
  60. }
  61. /**
  62. * Корзина заказов
  63. */
  64. public function actionOrders()
  65. {
  66. $dataProvider = $this->normalizeOrders(Yii::app()->shoppingCart->getPositions());
  67. $this->render(
  68. 'order',
  69. array(
  70. 'orders' => $dataProvider
  71. )
  72. );
  73. }
  74. public function actionViewOrder( $id ){
  75. $model = Orders::model()->findByPk($id);
  76. if(count($model) && count($model->bondOrders)){
  77. $this->render('viewOrder', array(
  78. 'model' => $model
  79. )
  80. );
  81. }
  82. }
  83. /**
  84. * Оформление заказа
  85. */
  86. public function actionCreateOrder( $vendor_id = null )
  87. {
  88. $model = new Orders();
  89. if ( $attributes = Yii::app()->request->getPost( 'Orders' ) ) {
  90. $model->attributes = $attributes;
  91. if ($model->validate() && $model->save()) {
  92. BondOrders::saveOrderPosition( $model->id, $vendor_id );
  93. $this->redirect($this->createUrl('/cart/confirmOrder', array('order_id'=>$model->id)));
  94. }
  95. }
  96. if( $vendor_id === null || !is_numeric($vendor_id) ) $this->redirect('/cart/orders');
  97. $model->vendor_id = intval( $vendor_id );
  98. $this->render( 'createOrder', array(
  99. 'model' => $model
  100. ));
  101. }
  102. public function actionEditOrderContact( $order_id ){
  103. $model = Orders::model()->my()->notConfirmed()->findByPk( $order_id );
  104. if( !count($model) ) $this->redirect('/cart/myOrders');
  105. if ( $attributes = Yii::app()->request->getPost( 'Orders' ) ) {
  106. $model->attributes = $attributes;
  107. if ($model->validate() && $model->save()) {
  108. $this->redirect($this->createUrl('/cart/confirmOrder',array('order_id'=>$order_id)));
  109. }
  110. }
  111. $this->render( 'createOrder', array(
  112. 'model' => $model
  113. ));
  114. }
  115. public function actionEditOrderProduct( $order_id ){
  116. $model = Orders::model()->my()->notConfirmed()->findByPk( $order_id );
  117. if( !count($model) ) $this->redirect('/cart/myOrders');
  118. $this->render( 'editOrderProduct', array(
  119. 'model' => $model
  120. ));
  121. }
  122. /**
  123. * Метод вызывается по средствам AJAX запроса.
  124. * Реализует безопасное изменения атрибутов товара который заказан, но заказ не подтвержден
  125. */
  126. public function actionSaveProduct(){
  127. if (Yii::app()->request->getPost('order_id') && Yii::app()->request->getPost('product_id') && Yii::app()->request->isAjaxRequest) {
  128. $order_id = intval(Yii::app()->request->getPost('order_id'));
  129. $product_id = intval(Yii::app()->request->getPost('product_id'));
  130. unset($_POST['order_id']);
  131. unset($_POST['product_id']);
  132. unset($_POST['id']);
  133. if ( !Orders::isEdit( $order_id ) ) { echo 0; Yii::app()->end(); }
  134. $model = BondOrders::model()->findByPk($product_id);
  135. $model->attributes = $_POST;
  136. $model->save();
  137. }
  138. echo CJSON::encode($model->attributes);
  139. Yii::app()->end();
  140. }
  141. public function actionDeleteProduct( $id ){
  142. $productModel = BondOrders::model()->findByPk( $id );
  143. if( $productModel->order->user_id == User::getId() && $productModel->order->status == Orders::STATUS_NOT_CONFIRMED ){
  144. $productModel->delete();
  145. }
  146. return false;
  147. }
  148. /**
  149. * Подтверждение заказа
  150. * @param $order_id
  151. */
  152. public function actionConfirmOrder( $order_id, $confirm = false ){
  153. $model = Orders::model()->my()->notConfirmed()->findByPk( $order_id );
  154. if( !count($model) ) $this->redirect('/cart/myOrders');
  155. if( $confirm ){
  156. $model->status = Orders::STATUS_QUEUED;
  157. $model->save();
  158. $this->redirect('/cart/myOrders');
  159. }
  160. $this->render( 'confirmOrder', array(
  161. 'model' => $model,
  162. )
  163. );
  164. }
  165. /**
  166. * Мои заказы
  167. */
  168. public function actionMyOrders(){
  169. if( isset($_GET['status']) && $_GET['status'] == Orders::STATUS_CLOSED )
  170. $model = Orders::model()->my()->closed()->findAll();
  171. else
  172. $model = Orders::model()->my()->notClosed()->findAll();
  173. $this->render('myOrder', array(
  174. 'model' => $model,
  175. ));
  176. }
  177. /**
  178. * Нормализация формата данных для вьюшки
  179. *
  180. * Разделяем товары по продовцам
  181. *
  182. * @param array $orders
  183. *
  184. * @return array
  185. */
  186. private function normalizeOrders($orders = array())
  187. {
  188. $result = array();
  189. foreach ($orders as $order) {
  190. $result[$order->Vendor->id]['vendor'] = $order->Vendor;
  191. $result[$order->Vendor->id]['price'] = isset($result[$order->Vendor->id]['price']) ? $result[$order->Vendor->id]['price'] + $order->getSumPrice() : $order->getSumPrice();
  192. $result[$order->Vendor->id]['dataProvider'][] = $order;
  193. }
  194. return $result;
  195. }
  196. }