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: Yii::import ('application.modules.topics.components.formatters.TopicsFieldFormatter');
39:
40: 41: 42: 43:
44: class Topics extends X2Model {
45:
46: const PAGE_SIZE = 20;
47:
48: public $upload;
49:
50: public $supportsFieldLevelPermissions = false;
51:
52: public $supportsWorkflow = false;
53:
54: protected $fieldFormatterClass = 'TopicsFieldFormatter';
55:
56: private $_originalPost;
57: private $_postTextChanged = false;
58:
59: 60: 61: 62:
63: public static function model($className = __CLASS__) {
64: return parent::model($className);
65: }
66:
67: public static function () {
68: $titleHeader = X2Html::tag(
69: 'div', array('class' => 'topic-title'), Yii::t('topics', 'Topic Title'));
70: $replyCountHeader = X2Html::tag(
71: 'div', array('class' => 'topic-reply-count'), Yii::t('topics', 'Replies'));
72: $lastUpdatedHeader = X2Html::tag(
73: 'div', array('class' => 'topic-last-updated'), Yii::t('topics', 'Latest Post'));
74: $attributesHeader = X2Html::tag(
75: 'div', array('class' => 'topic-attributes'), $replyCountHeader . $lastUpdatedHeader);
76: $header = X2Html::tag(
77: 'div', array('class' => 'clear-fix'), $titleHeader . $attributesHeader);
78:
79: return X2Html::tag('div', array('class' => 'topics-header'), $header);
80: }
81:
82: public static function getSortLinks($order = null) {
83: $ret = X2Html::link(
84: Yii::t('topics','Sorting'), '#',
85: array(
86: 'id' => 'topics-sort-toggle',
87: 'class' => 'x2-button',
88: 'style' => 'vertical-align:top;'));
89: $links = array(
90: X2Html::link(
91: Yii::t('topics','Most Recent'),
92: Yii::app()->controller->createUrl(
93: '/topics/topics/index',
94: array('order' => 'mostRecent')
95: ),
96: array(
97: 'class' => 'x2-button' . (($order == 'mostRecent' || is_null($order)) ?
98: ' disabled disabled-link' : ''), 'style' => 'vertical-align:top;')
99: ),
100: X2Html::link(
101: Yii::t('topics','Alphabetical'),
102: Yii::app()->controller->createUrl(
103: '/topics/topics/index',
104: array('order' => 'alphabetical')
105: ),
106: array(
107: 'class' => 'x2-button' . ($order == 'alphabetical' ? ' disabled disabled-link' : ''),
108: 'style' => 'vertical-align:top;')),
109: X2Html::link(
110: Yii::t('topics','Create Date'),
111: Yii::app()->controller->createUrl(
112: '/topics/topics/index',
113: array('order' => 'firstCreated')
114: ),
115: array('class' => 'x2-button' . ($order == 'firstCreated' ? ' disabled disabled-link' : ''),
116: 'style' => 'vertical-align:top;')),
117: X2Html::link(
118: Yii::t('topics','Most Popular'),
119: Yii::app()->controller->createUrl(
120: '/topics/topics/index',
121: array('order' => 'mostPopular')),
122: array(
123: 'class' => 'x2-button' . ($order == 'mostPopular' ? ' disabled disabled-link' : ''),
124: 'style' => 'vertical-align:top;')),
125: );
126: $ret .= X2Html::tag(
127: 'div', array('id' => 'topics-sort-buttons', 'style' => 'display:none;'),
128: implode(' ', $links));
129: return $ret;
130: }
131:
132: 133: 134:
135: public function tableName() {
136: return 'x2_topics';
137: }
138:
139: public function behaviors() {
140: $that = $this;
141: return array_merge(parent::behaviors(), array(
142: 'AssociatedMediaBehavior' => array(
143: 'class' => 'application.components.behaviors.AssociatedMediaBehavior',
144: 'fileAttribute' => 'upload',
145: 'associationType' => 'topicReply',
146: 'getAssociationId' => function () use ($that) {
147: return $that->originalPost->id;
148: },
149: ),
150: 'X2LinkableBehavior' => array(
151: 'class' => 'X2LinkableBehavior',
152: 'module' => 'topics'
153: ),
154: 'ERememberFiltersBehavior' => array(
155: 'class' => 'application.components.ERememberFiltersBehavior',
156: 'defaults' => array(),
157: 'defaultStickOnClear' => false
158: ),
159: 'InlineEmailModelBehavior' => array(
160: 'class' => 'application.components.InlineEmailModelBehavior',
161: )
162: ));
163: }
164:
165: public function relations() {
166: return array(
167: 'originalPostRelation' => array(self::HAS_ONE, 'TopicReplies',
168: 'topicId', 'order' => 'originalPostRelation.createDate ASC'),
169: 'replies' => array(self::HAS_MANY, 'TopicReplies', 'topicId',
170: 'order'=>'replies.createDate ASC'),
171: 'lastPost' => array(self::HAS_ONE, 'TopicReplies', 'topicId',
172: 'order' => 'lastPost.createDate DESC'),
173: );
174: }
175:
176: public function renderAttribute (
177: $fieldName, $makeLinks = true, $textOnly = true, $encode = true) {
178:
179: switch ($fieldName) {
180: case 'replyCount':
181: $val = Yii::t('topics', '1#{n} reply|n!=1#{n} replies', array (
182: $this->replyCount,
183: ));
184: return $encode ? CHtml::encode($val) : $val;
185: default:
186: return call_user_func_array ('parent::'.__FUNCTION__, func_get_args ());
187: }
188: }
189:
190: private $_text;
191: public function getText () {
192: if (!isset ($this->_text) && $this->originalPost) {
193: $this->_text = $this->originalPost->text;
194: }
195: return $this->_text;
196: }
197:
198: public function setText ($text) {
199: $this->_text = $text;
200: $this->setOriginalPostText ($text);
201: }
202:
203: public function afterSave(){
204: if ($this->_originalPost->isNewRecord || $this->_postTextChanged) {
205: if ($this->_originalPost->isNewRecord) {
206: $this->_originalPost->topicId = $this->id;
207: $this->_originalPost->assignedTo = Yii::app()->user->getName();
208: }
209: $this->_originalPost->updatedBy = Yii::app()->user->getName();
210: $this->_originalPost->save();
211: }
212: return parent::afterSave();
213: }
214:
215: 216: 217: 218: 219:
220: public function search() {
221: $criteria = new CDbCriteria;
222: return $this->searchBase($criteria);
223: }
224:
225: public function getOrder($order){
226: $ret = 'sticky DESC, ';
227: switch($order){
228: case 'alphabetical':
229: $ret .= 't.name ASC';
230: break;
231: case 'firstCreated':
232: $ret .= 't.createDate ASC';
233: break;
234: case 'mostPopular':
235: $ret .= 'replyCount DESC';
236: break;
237: case 'mostRecent':
238: default:
239: $ret.='minCreateDate DESC';
240: }
241: return $ret;
242: }
243:
244: public function getOriginalPost(){
245: if(is_null($this->_originalPost)){
246: $this->_originalPost = $this->originalPostRelation;
247: }
248: return $this->_originalPost;
249: }
250:
251: public function setOriginalPostText($text){
252: if(is_null($this->originalPost)){
253: $this->_originalPost = new TopicReplies;
254: }
255: if($this->_originalPost->text !== $text){
256: $this->_originalPost->text = $text;
257: $this->_postTextChanged = true;
258: }
259: }
260:
261: public function getReplyCount(){
262: return Yii::app()->db->createCommand()
263: ->select('COUNT(id)')
264: ->from('x2_topic_replies')
265: ->where('topicId = :id',array(':id'=>$this->id))
266: ->queryScalar() - 1;
267: }
268:
269: private $_attachments;
270: public function getAttachments () {
271: if (!isset ($this->_attachments)) {
272: $this->_attachments = $this->originalPost ? $this->originalPost->attachments : array ();
273: }
274: return $this->_attachments;
275: }
276:
277: }
278: