Comment.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. class Comment extends CActiveRecord
  3. {
  4. /**
  5. * The followings are the available columns in table 'tbl_comment':
  6. * @var integer $id
  7. * @var string $content
  8. * @var integer $status
  9. * @var integer $create_time
  10. * @var string $author
  11. * @var string $email
  12. * @var string $url
  13. * @var integer $post_id
  14. */
  15. const STATUS_PENDING=1;
  16. const STATUS_APPROVED=2;
  17. /**
  18. * Returns the static model of the specified AR class.
  19. * @return CActiveRecord the static model class
  20. */
  21. public static function model($className=__CLASS__)
  22. {
  23. return parent::model($className);
  24. }
  25. /**
  26. * @return string the associated database table name
  27. */
  28. public function tableName()
  29. {
  30. return '{{comment}}';
  31. }
  32. /**
  33. * @return array validation rules for model attributes.
  34. */
  35. public function rules()
  36. {
  37. // NOTE: you should only define rules for those attributes that
  38. // will receive user inputs.
  39. return array(
  40. array('content, author, email', 'required'),
  41. array('author, email, url', 'length', 'max'=>128),
  42. array('email','email'),
  43. array('url','url'),
  44. );
  45. }
  46. /**
  47. * @return array relational rules.
  48. */
  49. public function relations()
  50. {
  51. // NOTE: you may need to adjust the relation name and the related
  52. // class name for the relations automatically generated below.
  53. return array(
  54. 'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
  55. );
  56. }
  57. /**
  58. * @return array customized attribute labels (name=>label)
  59. */
  60. public function attributeLabels()
  61. {
  62. return array(
  63. 'id' => 'Id',
  64. 'content' => 'Comment',
  65. 'status' => 'Status',
  66. 'create_time' => 'Create Time',
  67. 'author' => 'Name',
  68. 'email' => 'Email',
  69. 'url' => 'Website',
  70. 'post_id' => 'Post',
  71. );
  72. }
  73. /**
  74. * Approves a comment.
  75. */
  76. public function approve()
  77. {
  78. $this->status=Comment::STATUS_APPROVED;
  79. $this->update(array('status'));
  80. }
  81. /**
  82. * @param Post the post that this comment belongs to. If null, the method
  83. * will query for the post.
  84. * @return string the permalink URL for this comment
  85. */
  86. public function getUrl($post=null)
  87. {
  88. if($post===null)
  89. $post=$this->post;
  90. return $post->url.'#c'.$this->id;
  91. }
  92. /**
  93. * @return string the hyperlink display for the current comment's author
  94. */
  95. public function getAuthorLink()
  96. {
  97. if(!empty($this->url))
  98. return CHtml::link(CHtml::encode($this->author),$this->url);
  99. else
  100. return CHtml::encode($this->author);
  101. }
  102. /**
  103. * @return integer the number of comments that are pending approval
  104. */
  105. public function getPendingCommentCount()
  106. {
  107. return $this->count('status='.self::STATUS_PENDING);
  108. }
  109. /**
  110. * @param integer the maximum number of comments that should be returned
  111. * @return array the most recently added comments
  112. */
  113. public function findRecentComments($limit=10)
  114. {
  115. return $this->with('post')->findAll(array(
  116. 'condition'=>'t.status='.self::STATUS_APPROVED,
  117. 'order'=>'t.create_time DESC',
  118. 'limit'=>$limit,
  119. ));
  120. }
  121. /**
  122. * This is invoked before the record is saved.
  123. * @return boolean whether the record should be saved.
  124. */
  125. protected function beforeSave()
  126. {
  127. if(parent::beforeSave())
  128. {
  129. if($this->isNewRecord)
  130. $this->create_time=time();
  131. return true;
  132. }
  133. else
  134. return false;
  135. }
  136. }