1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36:
37:
38: class TopicReplies extends X2ActiveRecord {
39:
40: public $module = 'topics';
41: public $upload;
42:
43: private $_attachments;
44:
45: 46: 47: 48:
49: public static function model($className = __CLASS__) {
50: return parent::model($className);
51: }
52:
53: 54: 55:
56: public function tableName() {
57: return 'x2_topic_replies';
58: }
59:
60: public function rules() {
61: return array(
62: array(
63: 'text','filter','filter'=>array($obj=new CHtmlPurifier(),'purify')
64: ),
65: array(
66: 'topicId', 'required'
67: ),
68: array(
69: 'text', 'application.components.validators.RequiredIfNotSetValidator',
70: 'otherAttr' => 'upload'
71: ),
72: );
73: }
74:
75: public function behaviors(){
76: $that = $this;
77: return array_merge(parent::behaviors(),array(
78: 'AssociatedMediaBehavior' => array(
79: 'class' => 'application.components.behaviors.AssociatedMediaBehavior',
80: 'fileAttribute' => 'upload',
81: 'associationType' => 'topicReply',
82: 'getAssociationId' => function () use ($that) {
83: return $that->id;
84: },
85: ),
86: 'X2StaticFieldsBehavior' => array(
87: 'class' => 'application.components.behaviors.X2StaticFieldsBehavior',
88: 'translationCategory' => 'topics',
89: 'fields' => array (
90: array (
91: 'fieldName' => 'text',
92: 'attributeLabel' => 'Text',
93: 'type' => 'text',
94: ),
95: ),
96: ),
97: 'X2TimestampBehavior' => array('class' => 'X2TimestampBehavior'),
98: 'permissions' => array('class' => Yii::app()->params->modelPermissions),
99: ));
100: }
101:
102: public function relations(){
103: return array(
104: 'topic' => array(self::BELONGS_TO, 'Topics', 'topicId'),
105: 'profile' => array(self::HAS_ONE, 'Profile',
106: array ('username' => 'assignedTo')),
107: );
108: }
109:
110: public function beforeSave(){
111: if(empty($this->assignedTo)){
112: $this->assignedTo = Yii::app()->user->getName();
113: }
114: $this->updatedBy = Yii::app()->user->getName();
115: return parent::beforeSave();
116: }
117:
118: public function afterSave () {
119: parent::afterSave ();
120: $event = new Events;
121: $event->visibility = 1;
122: $event->associationType = 'TopicRepies';
123: $event->associationId = $this->id;
124: $event->user = Yii::app()->user->getName();
125: $event->type = 'topic_reply';
126: $event->save();
127: }
128:
129: public function getTopicPage(){
130: $tableSchema = $this->getTableSchema();
131: $criteria = new CDbCriteria();
132: $criteria->select = 't.id';
133: $criteria->compare('t.topicId',$this->topicId);
134: $criteria->order = 't.createDate ASC';
135: $searchConditions = Yii::app()->db->getCommandBuilder()
136: ->createFindCommand($tableSchema, $criteria)->getText();
137: $varPrefix = '@';
138: $varName = $varPrefix.'rownum';
139: $varText = 'SET '.$varName.' = 0';
140: Yii::app()->db->createCommand()
141: ->setText($varText)
142: ->execute();
143: $subQuery = Yii::app()->db->createCommand()
144: ->select('*, ('.$varName.':='.$varName.'+1) r')
145: ->from('('.$searchConditions.') t1')
146: ->getText();
147: $rowNumberQuery = Yii::app()->db->createCommand()
148: ->select('(r-1)')
149: ->from('('.$subQuery.') t2')
150: ->where('t2.id=:t2_id');
151: $rowNumberQuery->params = array_merge(array(':t2_id'=>$this->id),$criteria->params);
152: $rowNumber = $rowNumberQuery->queryScalar();
153: return (int)($rowNumber / Topics::PAGE_SIZE);
154: }
155:
156: public function getAuthorId(){
157: return Yii::app()->db->createCommand()
158: ->select('id')
159: ->from('x2_profile')
160: ->where('username=:user', array(':user' => $this->assignedTo))
161: ->queryScalar();
162: }
163:
164: public function isOriginalPost(){
165: return $this->id === $this->topic->originalPost->id;
166: }
167:
168: public function isEditable(){
169: return Yii::app()->controller->checkPermissions($this, 'edit');
170: }
171:
172: public function isDeletable(){
173: return !$this->isOriginalPost() && Yii::app()->controller->checkPermissions($this, 'delete');
174: }
175:
176: public function isEdited(){
177: return $this->createDate !== $this->lastUpdated;
178: }
179:
180: public function getAttachments() {
181: if (is_null($this->_attachments)) {
182: $this->_attachments = Media::model()->findAllByAttributes(array('associationType' => 'topicReply',
183: 'associationId' => $this->id));
184: }
185: return $this->_attachments;
186: }
187:
188: }
189: