Post.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. class Post extends CActiveRecord
  3. {
  4. /**
  5. * The followings are the available columns in table 'tbl_post':
  6. * @var integer $id
  7. * @var string $title
  8. * @var string $content
  9. * @var string $tags
  10. * @var integer $status
  11. * @var integer $create_time
  12. * @var integer $update_time
  13. * @var integer $author_id
  14. */
  15. const STATUS_DRAFT=1;
  16. const STATUS_PUBLISHED=2;
  17. const STATUS_ARCHIVED=3;
  18. private $_oldTags;
  19. /**
  20. * Returns the static model of the specified AR class.
  21. * @return CActiveRecord the static model class
  22. */
  23. public static function model($className=__CLASS__)
  24. {
  25. return parent::model($className);
  26. }
  27. /**
  28. * @return string the associated database table name
  29. */
  30. public function tableName()
  31. {
  32. return '{{post}}';
  33. }
  34. /**
  35. * @return array validation rules for model attributes.
  36. */
  37. public function rules()
  38. {
  39. // NOTE: you should only define rules for those attributes that
  40. // will receive user inputs.
  41. return array(
  42. array('title, content, status', 'required'),
  43. array('status', 'in', 'range'=>array(1,2,3)),
  44. array('title', 'length', 'max'=>128),
  45. array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),
  46. array('tags', 'normalizeTags'),
  47. array('title, status', 'safe', 'on'=>'search'),
  48. );
  49. }
  50. /**
  51. * @return array relational rules.
  52. */
  53. public function relations()
  54. {
  55. // NOTE: you may need to adjust the relation name and the related
  56. // class name for the relations automatically generated below.
  57. return array(
  58. 'author' => array(self::BELONGS_TO, 'User', 'author_id'),
  59. 'comments' => array(self::HAS_MANY, 'Comment', 'post_id', 'condition'=>'comments.status='.Comment::STATUS_APPROVED, 'order'=>'comments.create_time DESC'),
  60. 'commentCount' => array(self::STAT, 'Comment', 'post_id', 'condition'=>'status='.Comment::STATUS_APPROVED),
  61. );
  62. }
  63. /**
  64. * @return array customized attribute labels (name=>label)
  65. */
  66. public function attributeLabels()
  67. {
  68. return array(
  69. 'id' => 'Id',
  70. 'title' => 'Title',
  71. 'content' => 'Content',
  72. 'tags' => 'Tags',
  73. 'status' => 'Status',
  74. 'create_time' => 'Create Time',
  75. 'update_time' => 'Update Time',
  76. 'author_id' => 'Author',
  77. );
  78. }
  79. /**
  80. * @return string the URL that shows the detail of the post
  81. */
  82. public function getUrl()
  83. {
  84. return Yii::app()->createUrl('post/view', array(
  85. 'id'=>$this->id,
  86. 'title'=>$this->title,
  87. ));
  88. }
  89. /**
  90. * @return array a list of links that point to the post list filtered by every tag of this post
  91. */
  92. public function getTagLinks()
  93. {
  94. $links=array();
  95. foreach(Tag::string2array($this->tags) as $tag)
  96. $links[]=CHtml::link(CHtml::encode($tag), array('post/index', 'tag'=>$tag));
  97. return $links;
  98. }
  99. /**
  100. * Normalizes the user-entered tags.
  101. */
  102. public function normalizeTags($attribute,$params)
  103. {
  104. $this->tags=Tag::array2string(array_unique(Tag::string2array($this->tags)));
  105. }
  106. /**
  107. * Adds a new comment to this post.
  108. * This method will set status and post_id of the comment accordingly.
  109. * @param Comment the comment to be added
  110. * @return boolean whether the comment is saved successfully
  111. */
  112. public function addComment($comment)
  113. {
  114. if(Yii::app()->params['commentNeedApproval'])
  115. $comment->status=Comment::STATUS_PENDING;
  116. else
  117. $comment->status=Comment::STATUS_APPROVED;
  118. $comment->post_id=$this->id;
  119. return $comment->save();
  120. }
  121. /**
  122. * This is invoked when a record is populated with data from a find() call.
  123. */
  124. protected function afterFind()
  125. {
  126. parent::afterFind();
  127. $this->_oldTags=$this->tags;
  128. }
  129. /**
  130. * This is invoked before the record is saved.
  131. * @return boolean whether the record should be saved.
  132. */
  133. protected function beforeSave()
  134. {
  135. if(parent::beforeSave())
  136. {
  137. if($this->isNewRecord)
  138. {
  139. $this->create_time=$this->update_time=time();
  140. $this->author_id=Yii::app()->user->id;
  141. }
  142. else
  143. $this->update_time=time();
  144. return true;
  145. }
  146. else
  147. return false;
  148. }
  149. /**
  150. * This is invoked after the record is saved.
  151. */
  152. protected function afterSave()
  153. {
  154. parent::afterSave();
  155. Tag::model()->updateFrequency($this->_oldTags, $this->tags);
  156. }
  157. /**
  158. * This is invoked after the record is deleted.
  159. */
  160. protected function afterDelete()
  161. {
  162. parent::afterDelete();
  163. Comment::model()->deleteAll('post_id='.$this->id);
  164. Tag::model()->updateFrequency($this->tags, '');
  165. }
  166. /**
  167. * Retrieves the list of posts based on the current search/filter conditions.
  168. * @return CActiveDataProvider the data provider that can return the needed posts.
  169. */
  170. public function search()
  171. {
  172. $criteria=new CDbCriteria;
  173. $criteria->compare('title',$this->title,true);
  174. $criteria->compare('status',$this->status);
  175. return new CActiveDataProvider('Post', array(
  176. 'criteria'=>$criteria,
  177. 'sort'=>array(
  178. 'defaultOrder'=>'status, update_time DESC',
  179. ),
  180. ));
  181. }
  182. }