Skip to content

Commit

Permalink
Small refactoring of order module
Browse files Browse the repository at this point in the history
  • Loading branch information
yupe committed Nov 1, 2016
1 parent 3698bd8 commit 11dc088
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 34 deletions.
8 changes: 0 additions & 8 deletions protected/modules/cart/CartModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,4 @@ public function init()
]
);
}

/**
*
*/
public function clearCart()
{
Yii::app()->cart->clear();
}
}
1 change: 1 addition & 0 deletions protected/modules/order/OrderModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public function init()
[
'order.models.*',
'order.forms.*',
'order.listeners.*',
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OrderBackendController extends yupe\components\controllers\BackController
public function init()
{
parent::init();

$this->productRepository = Yii::app()->getComponent('productRepository');
}

Expand Down
10 changes: 0 additions & 10 deletions protected/modules/order/controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ public function actionCreate()
Yii::t('OrderModule.order', 'The order created')
);

if (Yii::app()->hasModule('cart')) {
Yii::app()->getModule('cart')->clearCart();
}

//отправить уведомления
Yii::app()->orderNotifyService->sendOrderCreatedAdminNotify($model);

Yii::app()->orderNotifyService->sendOrderCreatedUserNotify($model);


if (Yii::app()->getModule('order')->showOrder) {
$this->redirect(['/order/order/view', 'url' => $model->url]);
}
Expand Down
39 changes: 39 additions & 0 deletions protected/modules/order/events/OrderEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use yupe\components\Event;

/**
* Class OrderEvent
*/
class OrderEvent extends Event
{
/**
* @var
*/
protected $order;

/**
* @return mixed
*/
public function getOrder()
{
return $this->order;
}

/**
* @param mixed $order
*/
public function setOrder($order)
{
$this->order = $order;
}

/**
* OrderEvent constructor.
* @param Order $order
*/
public function __construct(Order $order)
{
$this->order = $order;
}
}
3 changes: 3 additions & 0 deletions protected/modules/order/install/order.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
'order.pay.success' => [
['PayOrderListener', 'onSuccessPay']
],
'order.create.success' => [
['OrderListener', 'onCreate']
]
]
],
'orderNotifyService' => [
Expand Down
25 changes: 25 additions & 0 deletions protected/modules/order/listeners/OrderListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use yupe\components\Event;

/**
* Class OrderListener
*/
class OrderListener
{
/**
* @param Event $event
*/
public static function onCreate(Event $event)
{
if (Yii::app()->hasModule('cart')) {
Yii::app()->cart->clear();
}

$order = $event->getOrder();

Yii::app()->orderNotifyService->sendOrderCreatedAdminNotify($order);

Yii::app()->orderNotifyService->sendOrderCreatedUserNotify($order);
}
}
5 changes: 4 additions & 1 deletion protected/modules/order/models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
Yii::import('application.modules.order.OrderModule');
Yii::import('application.modules.order.events.OrderEvents');
Yii::import('application.modules.order.events.PayOrderEvent');
Yii::import('application.modules.order.events.OrderEvent');
Yii::import('application.modules.order.events.OrderChangeStatusEvent');

/**
Expand Down Expand Up @@ -479,6 +480,8 @@ public function store(array $attributes, array $products, $client = null, $statu
return false;
}

Yii::app()->eventManager->fire(OrderEvents::SUCCESS_CREATED, new OrderEvent($this));

$transaction->commit();

return true;
Expand Down Expand Up @@ -751,7 +754,7 @@ public function pay(Payment $payment, $paid = self::PAID_STATUS_PAID)
$this->setAttributes([
'paid' => (int)$paid,
'payment_method_id' => $payment->id,
'payment_time' => new CDbExpression('now()'),
'payment_time' => new CDbExpression('NOW()'),
]);

$result = $this->save();
Expand Down
36 changes: 22 additions & 14 deletions protected/modules/yupe/components/urlManager/LangUrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ public function init()

$languages = implode('|', $languages);
$rules = [];
$langPattern = '/<' . $this->langParam . ':(' . $languages . ')>/';
$langPattern = '/<'.$this->langParam.':('.$languages.')>/';

foreach ($this->rules as $pattern => $route) {
if (is_array($route)) {
if (isset($route['pattern'])) {
$route['pattern'] = $langPattern . ltrim($route['pattern'], '/');
$route['pattern'] = $langPattern.ltrim($route['pattern'], '/');
$rules[] = $route;
}
} else {
$pattern = $langPattern . ltrim($pattern, '/');
$pattern = $langPattern.ltrim($pattern, '/');
$rules[$pattern] = $route;
}
}
Expand All @@ -86,6 +86,9 @@ public function init()
parent::init();
}

/**
* @return array
*/
public function getAvailableLanguages()
{
if (null === $this->_languages) {
Expand Down Expand Up @@ -170,9 +173,12 @@ public function getLangFromCookie()
return $this->_langFromCookie;
}

/**
* @return string
*/
public function getCookieKey()
{
return $this->langParam . '_' . ($this->isBackend() ? 'backend' : 'frontend');
return $this->langParam.'_'.($this->isBackend() ? 'backend' : 'frontend');
}


Expand All @@ -194,7 +200,7 @@ public function createUrl($route, $params = [], $ampersand = '&')
if ($this->getDefaultLang() === $params[$this->langParam]) {
unset($params[$this->langParam]);
} elseif (trim($route, '/') === '') {
return Yii::app()->getHomeUrl() . $params[$this->langParam];
return Yii::app()->getHomeUrl().$params[$this->langParam];
}
}

Expand All @@ -212,26 +218,26 @@ public function replaceLangInUrl($url, $lang = null)
$result = '';

if (isset($parsed['scheme'])) {
$result .= $parsed['scheme'] . '://';
$result .= $parsed['scheme'].'://';
}

if (isset($parsed['user'])) {
$result .= $parsed['user'];
if (isset($parsed['pass'])) {
$result .= ':' . $parsed['pass'];
$result .= ':'.$parsed['pass'];
}
$result .= '@';
}

if (isset($parsed['host'])) {
$result .= $parsed['host'] . '/';
$result .= $parsed['host'].'/';
}

if ('path' === $this->urlFormat && isset($parsed['path'])) {
$path = trim($parsed['path'], '/');

$replaced = preg_replace(
'#^(' . implode('|', $this->_languages) . '){1}#',
'#^('.implode('|', $this->_languages).'){1}#',
$lang,
$path
);
Expand All @@ -240,15 +246,15 @@ public function replaceLangInUrl($url, $lang = null)
if ($path === $replaced && null !== $lang) {
$replaced = $lang;
if ($path !== '') {
$replaced .= '/' . $path;
$replaced .= '/'.$path;
}
}

$result .= $replaced;

if ($result !== '') {
if (strpos($url, '/') === 0) {
$result = '/' . $result;
$result = '/'.$result;
}

if (substr($url, -1) === '/') {
Expand All @@ -273,7 +279,7 @@ public function replaceLangInUrl($url, $lang = null)
$query = urldecode(http_build_query($queryParams));

if ($query !== '') {
$result .= '?' . $query;
$result .= '?'.$query;
}
}

Expand All @@ -286,14 +292,16 @@ public function replaceLangInUrl($url, $lang = null)

/**
* @param $url
* @param bool $lang
* @return mixed|string
* @return string
*/
public function removeLangFromUrl($url)
{
return $this->replaceLangInUrl($url);
}

/**
* @return bool
*/
public function isBackend()
{
$url = trim($this->removeLangFromUrl(Yii::app()->getRequest()->getUrl()), '/');
Expand Down

0 comments on commit 11dc088

Please sign in to comment.