Created
July 27, 2016 12:58
-
-
Save pistol88/864d0906a36e9c70af432069aa182350 to your computer and use it in GitHub Desktop.
REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace frontend\controllers; | |
use yii; | |
use common\models\Product; | |
use pistol88\order\models\Order; | |
use vova07\users\models\backend\User; | |
use yii\filters\VerbFilter; | |
class ApiController extends \yii\web\Controller | |
{ | |
public function behaviors() | |
{ | |
return [ | |
'verbs' => [ | |
'class' => VerbFilter::className(), | |
'actions' => [ | |
'delete' => ['push-good', 'push-new-users'], | |
], | |
], | |
'contentNegotiator' => [ | |
'class' => \yii\filters\ContentNegotiator::className(), | |
'only' => ['get-orders', 'get-users', 'push-good'], | |
'formatParam' => '_format', | |
'formats' => [ | |
'application/xml' => \yii\web\Response::FORMAT_XML, | |
'application/json' => \yii\web\Response::FORMAT_JSON, | |
], | |
], | |
]; | |
} | |
public function beforeAction($action) | |
{ | |
$this->enableCsrfValidation = false; | |
return parent::beforeAction($action); | |
} | |
public function actionGetOrders($limit = 100) | |
{ | |
$orders = Order::find()->limit($limit)->orderBy('id DESC')->all(); | |
$ordersArray = []; | |
foreach($orders as $order) { | |
$userId = ''; | |
if($order->user_id) { | |
$user = User::findOne($order->user_id); | |
$userId = $user->id; | |
} | |
$productsIds = []; | |
$productsCounts = []; | |
foreach($order->elements as $element) { | |
if($product = $element->getProduct()) { | |
$productsIds[] = $product->id_1c; | |
$productsCounts[] = $element->count; | |
} | |
} | |
$productsIds = implode(',', $productsIds); | |
$productsCounts = implode(',', $productsCounts); | |
$ordersArray[] = [ | |
'id' => $order->id, | |
'date' => $order->date, | |
'userId' => $userId, | |
'name' => $order->client_name, | |
'phone' => $order->phone, | |
'email' => $order->email, | |
'discount' => $order->getField(3), | |
'deliveryAddress' => $order->getField(2), | |
'comment' => $order->comment, | |
'payment' => $order->payment, | |
'productIds' => $productsIds, | |
'productCounts' => $productsCounts, | |
]; | |
} | |
return $ordersArray; | |
} | |
public function actionGetUsers($limit = 100) | |
{ | |
$users = User::find()->limit($limit)->orderBy('id DESC')->all(); | |
$usersArray = []; | |
foreach($users as $user) { | |
$user->attachBehavior('field', ['class' => 'pistol88\field\behaviors\AttachFields']); | |
if($user->profile) { | |
$userName = implode(' ', [$user->profile->name, $user->profile->surname]); | |
} else { | |
$userName = $user->username; | |
} | |
$usersArray[] = [ | |
'name' => $userName, | |
'phone' => $user->getField('phone'), | |
'email' => $user->getField('email'), | |
'address' => $user->getField('address'), | |
'area' => $user->getField('area'), | |
'work' => $user->getField('work'), | |
'likedbeer' => $user->getField('likedbeer'), | |
'sex' => $user->getField('sex'), | |
'updateTime' => $user->getField('updateTime'), | |
'regTime' => $user->getField('regTime'), | |
]; | |
} | |
return $usersArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment