UserController.php 1.86 KB
<?php
/**
 * User action handler
 */
 

class UserController extends WRestController
{
    /**
     * Define model for the controller
     */
    protected $_modelName = "user";
    
    
    /**
     * Determine which of the standard actions will support the controller
     */
    public function actions()
    {
        return array(
            'delete' => 'WRestDeleteAction',
            'get' => 'WRestGetAction',
            'create' => 'WRestCreateAction',
            'update' => array(
                'class' => 'WRestUpdateAction',
                'scenario' => 'update'
            )
        );
    }
    
    
    public function actionList()
    {
        $criteria = new CDbCriteria();
        $model = $this->getModel();
        
        $criteria->alias = 'user';
        
        $criteria->limit = (int)(($limit = Yii::app()->request->getParam('limit')) ? $limit : -1);
        
        $page = (int)Yii::app()->request->getParam('page') - 1;
        
        $criteria->offset = ($offset = $limit * $page) ? $offset : 0;
        
        if(($order = Yii::app()->request->getParam("sort")) ? $order : null) {
            $order = json_decode($order);
            
            $sort = array();
            
            foreach($order as $item) {
                $sort[] = sprintf("user.%s %s", $item->property, $item->direction);
            }
            
            $criteria->order = implode(", ", $sort);
        }
        
        $models = $model->with('transport')->findAll($criteria);
        
        if ($models) {
            foreach ($models as $item) {
                $result[] = array_merge($item->getAllAttributes(), array("domain" => $item->transport->domain));
            }
        }
        
        $result = array_merge($result, array(
            'total' => $model->count($criteria)
        ));
        
        $this->sendResponse(200, $result);
    }
}