CommentController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. class CommentController extends Controller
  3. {
  4. public $layout='column2';
  5. /**
  6. * @var CActiveRecord the currently loaded data model instance.
  7. */
  8. private $_model;
  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 authenticated users to access all actions
  27. 'users'=>array('@'),
  28. ),
  29. array('deny', // deny all users
  30. 'users'=>array('*'),
  31. ),
  32. );
  33. }
  34. /**
  35. * Updates a particular model.
  36. * If update is successful, the browser will be redirected to the 'view' page.
  37. */
  38. public function actionUpdate()
  39. {
  40. $model=$this->loadModel();
  41. if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
  42. {
  43. echo CActiveForm::validate($model);
  44. Yii::app()->end();
  45. }
  46. if(isset($_POST['Comment']))
  47. {
  48. $model->attributes=$_POST['Comment'];
  49. if($model->save())
  50. $this->redirect(array('index'));
  51. }
  52. $this->render('update',array(
  53. 'model'=>$model,
  54. ));
  55. }
  56. /**
  57. * Deletes a particular model.
  58. * If deletion is successful, the browser will be redirected to the 'index' page.
  59. */
  60. public function actionDelete()
  61. {
  62. if(Yii::app()->request->isPostRequest)
  63. {
  64. // we only allow deletion via POST request
  65. $this->loadModel()->delete();
  66. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  67. if(!isset($_POST['ajax']))
  68. $this->redirect(array('index'));
  69. }
  70. else
  71. throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  72. }
  73. /**
  74. * Lists all models.
  75. */
  76. public function actionIndex()
  77. {
  78. $dataProvider=new CActiveDataProvider('Comment', array(
  79. 'criteria'=>array(
  80. 'with'=>'post',
  81. 'order'=>'t.status, t.create_time DESC',
  82. ),
  83. ));
  84. $this->render('index',array(
  85. 'dataProvider'=>$dataProvider,
  86. ));
  87. }
  88. /**
  89. * Approves a particular comment.
  90. * If approval is successful, the browser will be redirected to the comment index page.
  91. */
  92. public function actionApprove()
  93. {
  94. if(Yii::app()->request->isPostRequest)
  95. {
  96. $comment=$this->loadModel();
  97. $comment->approve();
  98. $this->redirect(array('index'));
  99. }
  100. else
  101. throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  102. }
  103. /**
  104. * Returns the data model based on the primary key given in the GET variable.
  105. * If the data model is not found, an HTTP exception will be raised.
  106. */
  107. public function loadModel()
  108. {
  109. if($this->_model===null)
  110. {
  111. if(isset($_GET['id']))
  112. $this->_model=Comment::model()->findbyPk($_GET['id']);
  113. if($this->_model===null)
  114. throw new CHttpException(404,'The requested page does not exist.');
  115. }
  116. return $this->_model;
  117. }
  118. }