PostController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. class PostController 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 all users to access 'index' and 'view' actions.
  27. 'actions'=>array('index','view'),
  28. 'users'=>array('*'),
  29. ),
  30. array('allow', // allow authenticated users to access all actions
  31. 'users'=>array('@'),
  32. ),
  33. array('deny', // deny all users
  34. 'users'=>array('*'),
  35. ),
  36. );
  37. }
  38. /**
  39. * Displays a particular model.
  40. */
  41. public function actionView()
  42. {
  43. $post=$this->loadModel();
  44. $comment=$this->newComment($post);
  45. $this->render('view',array(
  46. 'model'=>$post,
  47. 'comment'=>$comment,
  48. ));
  49. }
  50. /**
  51. * Creates a new model.
  52. * If creation is successful, the browser will be redirected to the 'view' page.
  53. */
  54. public function actionCreate()
  55. {
  56. $model=new Post;
  57. if(isset($_POST['Post']))
  58. {
  59. $model->attributes=$_POST['Post'];
  60. if($model->save())
  61. $this->redirect(array('view','id'=>$model->id));
  62. }
  63. $this->render('create',array(
  64. 'model'=>$model,
  65. ));
  66. }
  67. /**
  68. * Updates a particular model.
  69. * If update is successful, the browser will be redirected to the 'view' page.
  70. */
  71. public function actionUpdate()
  72. {
  73. $model=$this->loadModel();
  74. if(isset($_POST['Post']))
  75. {
  76. $model->attributes=$_POST['Post'];
  77. if($model->save())
  78. $this->redirect(array('view','id'=>$model->id));
  79. }
  80. $this->render('update',array(
  81. 'model'=>$model,
  82. ));
  83. }
  84. /**
  85. * Deletes a particular model.
  86. * If deletion is successful, the browser will be redirected to the 'index' page.
  87. */
  88. public function actionDelete()
  89. {
  90. if(Yii::app()->request->isPostRequest)
  91. {
  92. // we only allow deletion via POST request
  93. $this->loadModel()->delete();
  94. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  95. if(!isset($_GET['ajax']))
  96. $this->redirect(array('index'));
  97. }
  98. else
  99. throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  100. }
  101. /**
  102. * Lists all models.
  103. */
  104. public function actionIndex()
  105. {
  106. $criteria=new CDbCriteria(array(
  107. 'condition'=>'status='.Post::STATUS_PUBLISHED,
  108. 'order'=>'update_time DESC',
  109. 'with'=>'commentCount',
  110. ));
  111. if(isset($_GET['tag']))
  112. $criteria->addSearchCondition('tags',$_GET['tag']);
  113. $dataProvider=new CActiveDataProvider('Post', array(
  114. 'pagination'=>array(
  115. 'pageSize'=>Yii::app()->params['postsPerPage'],
  116. ),
  117. 'criteria'=>$criteria,
  118. ));
  119. $this->render('index',array(
  120. 'dataProvider'=>$dataProvider,
  121. ));
  122. }
  123. /**
  124. * Manages all models.
  125. */
  126. public function actionAdmin()
  127. {
  128. $model=new Post('search');
  129. if(isset($_GET['Post']))
  130. $model->attributes=$_GET['Post'];
  131. $this->render('admin',array(
  132. 'model'=>$model,
  133. ));
  134. }
  135. /**
  136. * Suggests tags based on the current user input.
  137. * This is called via AJAX when the user is entering the tags input.
  138. */
  139. public function actionSuggestTags()
  140. {
  141. if(isset($_GET['q']) && ($keyword=trim($_GET['q']))!=='')
  142. {
  143. $tags=Tag::model()->suggestTags($keyword);
  144. if($tags!==array())
  145. echo implode("\n",$tags);
  146. }
  147. }
  148. /**
  149. * Returns the data model based on the primary key given in the GET variable.
  150. * If the data model is not found, an HTTP exception will be raised.
  151. */
  152. public function loadModel()
  153. {
  154. if($this->_model===null)
  155. {
  156. if(isset($_GET['id']))
  157. {
  158. if(Yii::app()->user->isGuest)
  159. $condition='status='.Post::STATUS_PUBLISHED.' OR status='.Post::STATUS_ARCHIVED;
  160. else
  161. $condition='';
  162. $this->_model=Post::model()->findByPk($_GET['id'], $condition);
  163. }
  164. if($this->_model===null)
  165. throw new CHttpException(404,'The requested page does not exist.');
  166. }
  167. return $this->_model;
  168. }
  169. /**
  170. * Creates a new comment.
  171. * This method attempts to create a new comment based on the user input.
  172. * If the comment is successfully created, the browser will be redirected
  173. * to show the created comment.
  174. * @param Post the post that the new comment belongs to
  175. * @return Comment the comment instance
  176. */
  177. protected function newComment($post)
  178. {
  179. $comment=new Comment;
  180. if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
  181. {
  182. echo CActiveForm::validate($comment);
  183. Yii::app()->end();
  184. }
  185. if(isset($_POST['Comment']))
  186. {
  187. $comment->attributes=$_POST['Comment'];
  188. if($post->addComment($comment))
  189. {
  190. if($comment->status==Comment::STATUS_PENDING)
  191. Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
  192. $this->refresh();
  193. }
  194. }
  195. return $comment;
  196. }
  197. }