/protected/controllers/TaskController.php

https://github.com/nterms/taskpage · PHP · 188 lines · 119 code · 19 blank · 50 comment · 14 complexity · 485a52da36e5ee731e8e2181999aaad3 MD5 · raw file

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