Guest User

UserController.php

a guest
Nov 25th, 2013
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class UserController extends Controller
  4. {
  5.     /**
  6.      * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  7.      * using two-column layout. See 'protected/views/layouts/column2.php'.
  8.      */
  9.     public $layout='//layouts/column2';
  10.  
  11.     /**
  12.      * @return array action filters
  13.     */
  14.     public function filters()
  15.     {
  16.         return array(
  17.             'accessControl', // perform access control for CRUD operations
  18.             'postOnly + delete', // we only allow deletion via POST request
  19.             array(
  20.                 'ext.starship.RestfullYii.filters.ERestFilter +
  21.                REST.GET, REST.PUT, REST.POST, REST.DELETE'
  22.             ),
  23.         );
  24.     }
  25.  
  26.     /**
  27.      * Specifies the access control rules.
  28.      * This method is used by the 'accessControl' filter.
  29.      * @return array access control rules
  30.      */
  31.     public function accessRules()
  32.     {
  33.         return array(
  34.             array('allow', 'actions'=>array('REST.GET', 'REST.PUT', 'REST.POST', 'REST.DELETE'),
  35.             'users'=>array('*'),
  36.             ),
  37.             array('allow',  // allow all users to perform 'index' and 'view' actions
  38.                 'actions'=>array('index','view'),
  39.                 'users'=>array('*'),
  40.             ),
  41.             array('allow', // allow authenticated user to perform 'create' and 'update' actions
  42.                 'actions'=>array('create','update'),
  43.                 'users'=>array('@'),
  44.             ),
  45.             array('allow', // allow admin user to perform 'admin' and 'delete' actions
  46.                 'actions'=>array('admin','delete'),
  47.                 'users'=>array('admin'),
  48.             ),
  49.             array('deny',  // deny all users
  50.                 'users'=>array('*'),
  51.             ),
  52.         );
  53.     }
  54.  
  55.     /**
  56.      * Displays a particular model.
  57.      * @param integer $id the ID of the model to be displayed
  58.      */
  59.     public function actionView($id)
  60.     {
  61.         $this->render('view',array(
  62.             'model'=>$this->loadModel($id),
  63.         ));
  64.         echo json_encode($model->attributes); Yii::app()->end();
  65.     }
  66.  
  67.     /**
  68.      * Creates a new model.
  69.      * If creation is successful, the browser will be redirected to the 'view' page.
  70.      */
  71.     public function actions()
  72.     {
  73.         return array(
  74.             'REST.'=>'ext.starship.RestfullYii.actions.ERestActionProvider',
  75.         );
  76.     }
  77.     public function actionCreate()
  78.     {
  79.         $model=new User;
  80.  
  81.         // Uncomment the following line if AJAX validation is needed
  82.         // $this->performAjaxValidation($model);
  83.  
  84.         if(isset($_POST['User']))
  85.         {
  86.             $model->attributes=$_POST['User'];
  87.             if($model->save())
  88.                 $this->redirect(array('view','id'=>$model->id));
  89.         }
  90.  
  91.         $this->render('create',array(
  92.             'model'=>$model,
  93.         ));
  94.     }
  95.  
  96.     /**
  97.      * Updates a particular model.
  98.      * If update is successful, the browser will be redirected to the 'view' page.
  99.      * @param integer $id the ID of the model to be updated
  100.      */
  101.     public function actionUpdate($id)
  102.     {
  103.         $model=$this->loadModel($id);
  104.  
  105.         // Uncomment the following line if AJAX validation is needed
  106.         // $this->performAjaxValidation($model);
  107.  
  108.         if(isset($_POST['User']))
  109.         {
  110.             $model->attributes=$_POST['User'];
  111.             if($model->save())
  112.                 $this->redirect(array('view','id'=>$model->id));
  113.         }
  114.  
  115.         $this->render('update',array(
  116.             'model'=>$model,
  117.         ));
  118.     }
  119.  
  120.     /**
  121.      * Deletes a particular model.
  122.      * If deletion is successful, the browser will be redirected to the 'admin' page.
  123.      * @param integer $id the ID of the model to be deleted
  124.      */
  125.     public function actionDelete($id)
  126.     {
  127.         $this->loadModel($id)->delete();
  128.  
  129.         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  130.         if(!isset($_GET['ajax']))
  131.             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  132.     }
  133.  
  134.     /**
  135.      * Lists all models.
  136.      */
  137.     public function actionIndex()
  138.     {
  139.         $dataProvider=new CActiveDataProvider('User');
  140.         $this->render('index',array(
  141.             'dataProvider'=>$dataProvider,
  142.         ));
  143.     }
  144.  
  145.     /**
  146.      * Manages all models.
  147.      */
  148.     public function actionAdmin()
  149.     {
  150.         $model=new User('search');
  151.         $model->unsetAttributes();  // clear any default values
  152.         if(isset($_GET['User']))
  153.             $model->attributes=$_GET['User'];
  154.  
  155.         $this->render('admin',array(
  156.             'model'=>$model,
  157.         ));
  158.     }
  159.    
  160.     /**
  161.      * Returns the data model based on the primary key given in the GET variable.
  162.      * If the data model is not found, an HTTP exception will be raised.
  163.      * @param integer $id the ID of the model to be loaded
  164.      * @return User the loaded model
  165.      * @throws CHttpException
  166.      */
  167.     public function loadModel($id)
  168.     {
  169.         $model=User::model()->findByPk($id);
  170.         if($model===null)
  171.             throw new CHttpException(404,'The requested page does not exist.');
  172.         return $model;
  173.     }
  174.  
  175.     /**
  176.      * Performs the AJAX validation.
  177.      * @param User $model the model to be validated
  178.      */
  179.     protected function performAjaxValidation($model)
  180.     {
  181.         if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
  182.         {
  183.             echo CActiveForm::validate($model);
  184.             Yii::app()->end();
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment