~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/demos/blog/protected/models/Post.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

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