Skip to content

Commit

Permalink
save on ctrl+s
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed May 16, 2015
1 parent ff96aab commit 49b09d1
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 75 deletions.
8 changes: 8 additions & 0 deletions media/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ $(function(){
});
});

$(document).bind('keydown', function (e) {
if(e.ctrlKey && e.which === 83){ // Check for the Ctrl key being pressed, and if the key = [S] (83)
$('.model-form').submit();
e.preventDefault();
return false;
}
});

window.notify = new Notify();
});

Expand Down
2 changes: 1 addition & 1 deletion modules/shopcart/api/GoodObject.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace yii\easyii\modules\shopcart\api;

use app\modules\shop\api\ItemObject;
use yii\easyii\modules\catalog\api\ItemObject;

class GoodObject extends \yii\easyii\components\ApiObject
{
Expand Down
130 changes: 89 additions & 41 deletions modules/shopcart/api/Shopcart.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
<?php
namespace yii\easyii\modules\shopcart\api;

use app\modules\shop\models\Item;
use Yii;
use yii\easyii\models\Setting;
use yii\easyii\modules\catalog\models\Item;
use yii\easyii\modules\shopcart\models\Good;
use yii\easyii\modules\shopcart\models\Order;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;

class Shopcart extends \yii\easyii\components\API
{
const SENT_VAR = 'shopcart_sent';

private $_order;
private $_items;

private $_defaultFormOptions = [
'errorUrl' => '',
'successUrl' => ''
];

public function api_items()
{
if(!$this->_items){
$this->_items = [];
if(!$this->order->isNewRecord){
foreach(Good::find()->where(['order_id' => $this->order->order_id])->with('item')->all() as $good){
$this->_items[] = new GoodObject($good);
}
}
}
return $this->_items;
return $this->items;
}

public function api_order()
{
return new OrderObject($this->order);
}

public function api_add($item_id, $options = '', $count = 1, $increaseOnDublicate = true)
public function api_form($options = [])
{
$model = new Order;
$model->scenario = 'confirm';
$settings = Yii::$app->getModule('admin')->activeModules['shopcart']->settings;
$options = array_merge($this->_defaultFormOptions, $options);

ob_start();
$form = ActiveForm::begin([
'action' => Url::to(['/admin/shopcart/send'])
]);

echo Html::hiddenInput('errorUrl', $options['errorUrl'] ? $options['errorUrl'] : Url::current([self::SENT_VAR => 0]));
echo Html::hiddenInput('successUrl', $options['successUrl'] ? $options['successUrl'] : Url::current([self::SENT_VAR => 1]));

echo $form->field($model, 'name');
echo $form->field($model, 'address');

if($settings['enableEmail']) echo $form->field($model, 'email');
if($settings['enablePhone']) echo $form->field($model, 'phone');

echo $form->field($model, 'comment')->textarea();

echo Html::submitButton(Yii::t('easyii', 'Send'), ['class' => 'btn btn-primary']);
ActiveForm::end();

return ob_get_clean();
}

public function api_add($item_id, $count = 1, $options = '', $increaseOnDublicate = true)
{
$shopItem = Item::findOne($item_id);
if(!$shopItem){
Expand Down Expand Up @@ -69,14 +98,14 @@ public function api_add($item_id, $options = '', $count = 1, $increaseOnDublicat
}

if($good->save()){
$response = [
'result' => 'success',
'order_id' => $good->order_id,
'good_id' => $good->primaryKey,
'item_id' => $shopItem->primaryKey,
'options' => $good->options,
'discount' => $good->discount,
];
$response = [
'result' => 'success',
'order_id' => $good->order_id,
'good_id' => $good->primaryKey,
'item_id' => $shopItem->primaryKey,
'options' => $good->options,
'discount' => $good->discount,
];
if($response['discount']){
$response['price'] = round($good->price * (1 - $good->discount / 100));
$response['old_price'] = $good->price;
Expand All @@ -95,19 +124,34 @@ public function api_remove($good_id)
if(!$good){
return ['result' => 'error', 'code' => 1, 'error' => 'Good not found'];
}
$order = $good->order;
if($order->access_token != $this->token){
if($good->order_id != $this->order->order_id){
return ['result' => 'error', 'code' => 2, 'error' => 'Access denied'];
}

$good->delete();

return ['result' => 'success', 'good_id' => $good_id, 'order_id' => $order->primaryKey];
return ['result' => 'success', 'good_id' => $good_id, 'order_id' => $good->order_id];
}

public function api_update($goods)
{
if(is_array($goods) && count($this->items)) {
foreach($this->items as $good){
if(!empty($goods[$good->id]))
{
$count = (int)$goods[$good->id];
if($count > 0){
$good->model->count = $count;
$good->model->update();
}
}
}
}
}

public function api_confirm($data)
public function api_send($data)
{
if($this->order->isNewRecord || $this->order->status != Order::STATUS_NEW){
if($this->order->isNewRecord || $this->order->status != Order::STATUS_BLANK){
return ['result' => 'error', 'code' => 1, 'error' => 'Order not found'];
}
if(!count($this->order->goods)){
Expand All @@ -116,9 +160,6 @@ public function api_confirm($data)
$this->order->setAttributes($data);
$this->order->status = Order::STATUS_PENDING;
if($this->order->save()){
if(Yii::$app->getModule('admin')->activeModules['shopcart']->settings['mailAdminOnNewOrder']) {
$this->api_mailAdmin($this->order->primaryKey);
}
return [
'result' => 'success',
'order_id' => $this->order->primaryKey,
Expand All @@ -129,29 +170,36 @@ public function api_confirm($data)
}
}

public function api_mailAdmin($order_id)
public function api_cost()
{
$settings = Yii::$app->getModule('admin')->activeModules['shopcart']->settings;
$template = $settings['templateOnNewOrder'];
$subject = $settings['subjectOnNewOrder'];

if($template && $subject)
{
Yii::$app->mailer->compose($template, ['order_id' => $order_id, 'link' => Url::to(['/admin/shopcart/a/view', 'id' => $order_id])])
->setFrom(Setting::get('robot_email'))
->setTo(Setting::get('admin_email'))
->setSubject($subject)
->send();
$cost = 0;
if(count($this->items)){
foreach($this->items as $good){
$cost += $good->price * $good->count;
}
}
return $cost;
}

public function getItems()
{
if(!$this->_items){
$this->_items = [];
if(!$this->order->isNewRecord){
foreach(Good::find()->where(['order_id' => $this->order->order_id])->with('item')->all() as $good){
$this->_items[] = new GoodObject($good);
}
}
}
return $this->_items;
}

public function getOrder()
{
if(!$this->_order){
$access_token = $this->token;

if(!$access_token || !($this->_order = Order::find()->where(['access_token' => $access_token])->status(Order::STATUS_NEW)->one())){
if(!$access_token || !($this->_order = Order::find()->where(['access_token' => $access_token])->status(Order::STATUS_BLANK)->one())){
$this->_order = new Order();
}
}
Expand Down
79 changes: 52 additions & 27 deletions modules/shopcart/controllers/AController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
class AController extends Controller
{
public $pending = 0;
public $confirmed = 0;
public $processed = 0;
public $sent = 0;

public function init()
{
parent::init();

$this->pending = Order::find()->status(Order::STATUS_PENDING)->count();
$this->confirmed = Order::find()->status(Order::STATUS_CONFIRMED)->count();
$this->processed = Order::find()->status(Order::STATUS_PROCESSED)->count();
$this->sent = Order::find()->status(Order::STATUS_SENT)->count();
}

Expand All @@ -33,18 +33,20 @@ public function actionIndex()
]);
}

public function actionConfirmed()
public function actionProcessed()
{
$this->setReturnUrl();
return $this->render('index', [
'data' => new ActiveDataProvider([
'query' => Order::find()->with('goods')->status(Order::STATUS_CONFIRMED)->asc(),
'totalCount' => $this->confirmed
'query' => Order::find()->with('goods')->status(Order::STATUS_PROCESSED)->asc(),
'totalCount' => $this->processed
])
]);
}

public function actionSent()
{
$this->setReturnUrl();
return $this->render('index', [
'data' => new ActiveDataProvider([
'query' => Order::find()->with('goods')->status(Order::STATUS_SENT)->asc(),
Expand All @@ -55,6 +57,7 @@ public function actionSent()

public function actionCompleted()
{
$this->setReturnUrl();
return $this->render('index', [
'data' => new ActiveDataProvider([
'query' => Order::find()->with('goods')->status(Order::STATUS_COMPLETED)->desc()
Expand All @@ -64,53 +67,75 @@ public function actionCompleted()

public function actionFails()
{
$this->setReturnUrl();
return $this->render('index', [
'data' => new ActiveDataProvider([
'query' => Order::find()->with('goods')->where(['in', 'status', [Order::STATUS_DECLINED, Order::STATUS_ERROR, Order::STATUS_RETURNED, Order::STATUS_NEW]])->desc()
'query' => Order::find()->with('goods')->where(['in', 'status', [Order::STATUS_DECLINED, Order::STATUS_ERROR, Order::STATUS_RETURNED]])->desc()
])
]);
}

public function actionBlank()
{
$this->setReturnUrl();
return $this->render('index', [
'data' => new ActiveDataProvider([
'query' => Order::find()->with('goods')->status(Order::STATUS_BLANK)->desc()
])
]);
}

public function actionView($id)
{
$request = Yii::$app->request;
$order = Order::findOne($id);

if($order === null){
$this->flash('error', Yii::t('easyii', 'Not found'));
return $this->redirect(['/admin/'.$this->module->id]);
}

if($order->new > 0){
$order->new = 0;
$order->update();
}

$goods = Good::find()->where(['order_id' => $order->primaryKey])->with('item')->asc()->all();
if($request->post('status')){
$newStatus = $request->post('status');
$oldStatus = $order->status;

return $this->render('view', [
'order' => $order,
'goods' => $goods
]);
$order->status = $newStatus;
$order->remark = filter_var($request->post('remark'), FILTER_SANITIZE_STRING);

if($order->save()){
if($newStatus != $oldStatus && $request->post('notify')){
$order->notifyUser();
}
$this->flash('success', Yii::t('easyii/shopcart', 'Order updated'));
}
else {
$this->flash('error', Yii::t('easyii', 'Update error. {0}', $order->formatErrors()));
}
return $this->refresh();
}
else {
if ($order->new > 0) {
$order->new = 0;
$order->update();
}

$goods = Good::find()->where(['order_id' => $order->primaryKey])->with('item')->asc()->all();

return $this->render('view', [
'order' => $order,
'goods' => $goods
]);
}
}

public function actionDelete($id)
{
if(($model = Order::findOne($id))){
$model->delete();
} else{
} else {
$this->error = Yii::t('easyii', 'Not found');
}
return $this->formatResponse(Yii::t('easyii/shopcart', 'Order deleted'));
}

public function actionStatus($id, $status)
{
if(($model = Order::findOne($id)) && array_key_exists($status, Order::states())){
$model->status = $status;
$model->save();
} else{
$this->error = Yii::t('easyii', 'Not found');
}
return $this->formatResponse(Yii::t('easyii/shopcart', 'Order status changed'));
}
}
2 changes: 1 addition & 1 deletion modules/shopcart/controllers/GoodsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function actionDelete($id)
{
if(($model = Good::findOne($id))){
$model->delete();
} else{
} else {
$this->error = Yii::t('easyii', 'Not found');
}
return $this->formatResponse(Yii::t('easyii/shopcart', 'Order deleted'));
Expand Down
Loading

0 comments on commit 49b09d1

Please sign in to comment.