Advertisement
Guest User

PostController.php

a guest
Nov 25th, 2013
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.68 KB | None | 0 0
  1. <?php
  2.  
  3. class PostController 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','admin'),
  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.     }
  65.  
  66.     /**
  67.      * Creates a new model.
  68.      * If creation is successful, the browser will be redirected to the 'view' page.
  69.      */
  70.     public function actions()
  71.     {
  72.         return array(
  73.             'REST.'=>'ext.starship.RestfullYii.actions.ERestActionProvider',
  74.         );
  75.     }
  76.     public function actionCreate()
  77.     {
  78.         $model=new Post;
  79.  
  80.         // Uncomment the following line if AJAX validation is needed
  81.         // $this->performAjaxValidation($model);
  82.  
  83.         if(isset($_POST['Post']))
  84.         {
  85.             $model->attributes=$_POST['Post'];
  86.             if($model->save())
  87.                 $this->redirect(array('view','id'=>$model->id));
  88.         }
  89.  
  90.         $this->render('create',array(
  91.             'model'=>$model,
  92.         ));
  93.     }
  94.  
  95.     /**
  96.      * Updates a particular model.
  97.      * If update is successful, the browser will be redirected to the 'view' page.
  98.      * @param integer $id the ID of the model to be updated
  99.      */
  100.     public function actionUpdate($id)
  101.     {
  102.         $model=$this->loadModel($id);
  103.  
  104.         // Uncomment the following line if AJAX validation is needed
  105.         // $this->performAjaxValidation($model);
  106.  
  107.         if(isset($_POST['Post']))
  108.         {
  109.             $model->attributes=$_POST['Post'];
  110.             if($model->save())
  111.                 $this->redirect(array('view','id'=>$model->id));
  112.         }
  113.  
  114.         $this->render('update',array(
  115.             'model'=>$model,
  116.         ));
  117.     }
  118.  
  119.     /**
  120.      * Deletes a particular model.
  121.      * If deletion is successful, the browser will be redirected to the 'admin' page.
  122.      * @param integer $id the ID of the model to be deleted
  123.      */
  124.     public function actionDelete($id)
  125.     {
  126.         $this->loadModel($id)->delete();
  127.  
  128.         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  129.         if(!isset($_GET['ajax']))
  130.             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  131.     }
  132.  
  133.     /**
  134.      * Lists all models.
  135.      */
  136.     public function actionIndex()
  137.     {
  138.         $dataProvider=new CActiveDataProvider('Post');
  139.         $this->render('index',array(
  140.             'dataProvider'=>$dataProvider,
  141.         ));
  142.     }
  143.  
  144.     /**
  145.      * Manages all models.
  146.      */
  147.     public function actionAdmin()
  148.     {
  149.         $model=new Post('search');
  150.         $model->unsetAttributes();  // clear any default values
  151.         if(isset($_GET['Post']))
  152.             $model->attributes=$_GET['Post'];
  153.  
  154.         $this->render('admin',array(
  155.             'model'=>$model,
  156.         ));
  157.     }
  158.  
  159.     /**
  160.      * Returns the data model based on the primary key given in the GET variable.
  161.      * If the data model is not found, an HTTP exception will be raised.
  162.      * @param integer $id the ID of the model to be loaded
  163.      * @return Post the loaded model
  164.      * @throws CHttpException
  165.      */
  166.     public function loadModel($id)
  167.     {
  168.         $model=Post::model()->findByPk($id);
  169.         if($model===null)
  170.             throw new CHttpException(404,'The requested page does not exist.');
  171.         return $model;
  172.     }
  173.  
  174.     /**
  175.      * Performs the AJAX validation.
  176.      * @param Post $model the model to be validated
  177.      */
  178.     protected function performAjaxValidation($model)
  179.     {
  180.         if(isset($_POST['ajax']) && $_POST['ajax']==='post-form')
  181.         {
  182.             echo CActiveForm::validate($model);
  183.             Yii::app()->end();
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement