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: class TopicsController extends x2base {
38:
39: public $modelClass = 'Topics';
40:
41: public function behaviors(){
42: return array_merge(parent::behaviors(), array(
43: 'QuickCreateRelationshipBehavior' => array(
44: 'class' => 'QuickCreateRelationshipBehavior',
45: ),
46: 'X2MobileControllerBehavior' => array(
47: 'class' =>
48: 'application.modules.mobile.components.behaviors.'.
49: 'X2MobileTopicsControllerBehavior'
50: ),
51: ));
52: }
53:
54: 55: 56: 57:
58: public function actionGetItems($term){
59: X2LinkableBehavior::getItems ($term);
60: }
61:
62: 63: 64: 65:
66: public function actionView($id, $replyId = null, $latest = null) {
67: $page = null;
68: $model=$this->loadModel($id);
69: if(!is_null($latest) && !Yii::app()->request->isAjaxRequest) {
70: $replyId = $model->lastPost->id;
71: }
72: if(!is_null($replyId) && !Yii::app()->request->isAjaxRequest){
73: $post = TopicReplies::model()->findByPk($replyId);
74: if(!is_null($post)){
75: $page = $post->getTopicPage();
76: } else {
77: $replyId = null;
78: }
79: }
80: $this->noBackdrop = true;
81: $topicReply = new TopicReplies;
82: if(!isset($_GET['ajax'])){
83: $log=new ViewLog;
84: $log->user=Yii::app()->user->getName();
85: $log->recordType=get_class($model);
86: $log->recordId=$model->id;
87: $log->timestamp=time();
88: $log->save();
89: X2Flow::trigger('RecordViewTrigger',array('model'=>$model));
90: }
91: $dataProvider = new CArrayDataProvider($model->replies, array(
92: 'id' => 'topic-replies',
93: 'pagination' => array(
94: 'pageSize'=>Topics::PAGE_SIZE,
95: ),
96: ));
97: $dataProvider->getPagination()->setItemCount($dataProvider->getTotalItemCount());
98: if(!Yii::app()->request->isAjaxRequest && !is_null($page)){
99: $dataProvider->getPagination()->setCurrentPage($page);
100: }
101: $this->render('view', array(
102: 'model' => $model,
103: 'replyId' => $replyId,
104: 'dataProvider' => $dataProvider,
105: 'topicReply' => $topicReply,
106: 'page' => is_null($page) ? $dataProvider->getPagination()->getCurrentPage() : $page,
107: ));
108: }
109:
110: 111: 112: 113:
114: public function actionCreate() {
115: $model=new Topics;
116: $fileCreation = false;
117: if(isset($_FILES['upload']) || isset($_POST['Topics'])) {
118: $data = array();
119: $topicText = null;
120: if (isset($_FILES['upload'])) {
121: $fileCreation = true;
122: $data = array('name' => $_POST['topicName']);
123: $topicText = $_POST['topicText'];
124: } else if (isset($_POST['Topics'])) {
125: $data = $_POST['Topics'];
126: $topicText = $_POST['TopicReplies']['text'];
127: }
128: $data['text'] = $topicText;
129: $model->setX2Fields($data, false, true);
130: if(isset($_POST['x2ajax'])){
131: $ajaxErrors = $this->quickCreate ($model);
132: } else{
133: if ($fileCreation) {
134:
135: $temp = CUploadedFile::getInstanceByName('upload');
136: $model->upload = $temp;
137: }
138: if ($model->save()) {
139: if ($fileCreation && count ($model->originalPost->attachments)) {
140: echo $model->id;
141: Yii::app()->end();
142: } else {
143: $this->redirect(array('view', 'id' => $model->id));
144: }
145: } elseif ($model->hasErrors ('text')) {
146: Yii::app()->user->setFlash('error','Original post text cannot be blank.');
147: }
148: }
149: }
150:
151: if(isset($_POST['x2ajax']) || isset ($_FILES['upload'])){
152: $this->renderInlineForm ($model);
153: } else {
154: $this->render('create',array(
155: 'model'=>$model,
156: ));
157: }
158: }
159:
160: public function renderInlineForm ($model) {
161: echo CJSON::encode (
162: array (
163: 'status' => $model->hasErrors () ? 'userError' : 'success',
164: 'page' => $this->renderPartial ('_topicForm', array (
165: 'model' => $model,
166: ), true, true)
167: ));
168: }
169:
170: public function actionPinUnpinTopic($id) {
171: $model = $this->loadModel($id);
172: if(empty($model->sticky)){
173: $model->sticky = 1;
174: }else{
175: $model->sticky = 0;
176: }
177: if($model->save()){
178: echo $model->sticky ? Yii::t('topics', 'Unpin Topic') : Yii::t('topics', 'Pin Topic');
179: }
180: }
181:
182: 183: 184: 185: 186:
187: public function actionUpdate($id) {
188: $model = $this->loadModel($id);
189: $users = User::getNames();
190:
191: if(isset($_POST['Topics'])) {
192: $data = $_POST['Topics'];
193: $data['text'] = $_POST['TopicReplies']['text'];
194: $model->setX2Fields($data, false, true);
195: if ($model->save()) {
196: $this->redirect(array('view', 'id' => $model->id));
197: } elseif ($model->hasErrors ('text')) {
198: Yii::app()->user->setFlash('error','Original post text cannot be blank.');
199: }
200: }
201:
202: $this->render('update',array(
203: 'model'=>$model,
204: 'users'=>$users,
205: ));
206: }
207:
208: 209: 210: 211: 212:
213: public function actionDelete($id) {
214: if(Yii::app()->request->isPostRequest) {
215:
216: $model=$this->loadModel($id);
217: $this->cleanUpTags($model);
218: $model->delete();
219:
220: 221:
222: if(!isset($_GET['ajax']))
223: $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
224: } else {
225: throw new CHttpException(
226: 400,'Invalid request. Please do not repeat this request again.');
227: }
228: }
229:
230: 231: 232:
233: public function actionIndex($order = null) {
234: $model = new Topics('search');
235: $orderStr = $model->getOrder($order);
236: $dataProvider = new CActiveDataProvider('Topics', array(
237: 'criteria' => array(
238: 'select' => 't.*, (SELECT COUNT(id) FROM x2_topic_replies WHERE topicId = t.id) AS replyCount, min(lastPost.createDate) as minCreateDate',
239: 'with' => array('lastPost'),
240: 'order' => $orderStr,
241: 'group' => 'lastPost.topicId',
242: ),
243: 'pagination' => array(
244: 'pageSize' => Profile::getResultsPerPage(),
245: )
246: ));
247: $this->render('index', array(
248: 'model' => $model,
249: 'dataProvider' => $dataProvider,
250: 'order' => $order,
251: ));
252: }
253:
254: public function actionNewReply(){
255: if(isset($_POST['TopicReplies'])){
256: $model = $this->loadModel($_POST['TopicReplies']['topicId']);
257: if (!$this->checkPermissions($model, 'view')) {
258: $this->denied();
259: }
260: $reply = new TopicReplies;
261: $reply->text = $_POST['TopicReplies']['text'];
262: $reply->topicId = $_POST['TopicReplies']['topicId'];
263: if ($reply->save()) {
264: echo $reply->id;
265: }
266: }
267: }
268:
269: public function actionUpdateReply($id){
270: $reply = TopicReplies::model()->findByPk($id);
271: if (is_null($reply)) {
272: throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
273: }
274: if(!$this->checkPermissions($reply, 'edit')){
275: $this->denied();
276: }
277: $topicId = $reply->topicId;
278: $topic = $this->loadModel($topicId);
279: if (isset($_POST['TopicReplies'])) {
280: $reply->text = $_POST['TopicReplies']['text'];
281: if ($reply->save()) {
282: $this->redirect(array('/topics/topics/view', 'id' => $reply->topicId, 'replyId' => $reply->id));
283: }
284: }
285: $this->render('updateReply', array(
286: 'topic' => $topic,
287: 'model' => $reply,
288: ));
289: }
290:
291: public function actionDeleteReply($id){
292: if(Yii::app()->request->isPostRequest){
293: $reply = TopicReplies::model()->findByPk($id);
294: if (is_null($reply)) {
295: throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
296: }
297: if(!$this->checkPermissions($reply, 'delete') || !$reply->isDeletable()){
298: $this->denied();
299: }
300: $reply->delete();
301: }else{
302: throw new CHttpException(
303: 400,'Invalid request. Please do not repeat this request again.');
304: }
305: }
306:
307: 308: 309: 310:
311: protected function performAjaxValidation($model) {
312: if(isset($_POST['ajax']) && $_POST['ajax']==='topics-form') {
313: echo CActiveForm::validate($model);
314: Yii::app()->end();
315: }
316: }
317:
318: 319: 320: 321: 322: 323:
324: public function ($selectOptions = array(), $model = null, $menuParams = null) {
325: $Topics = Modules::displayName();
326: $Topic = Modules::displayName(false);
327: $modelId = isset($model) ? $model->id : 0;
328:
329: 330: 331: 332: 333: 334:
335:
336: $menuItems = array(
337: array(
338: 'name'=>'index',
339: 'label'=>Yii::t('topics','{topics} List', array(
340: '{topics}'=>$Topics,
341: )),
342: 'url'=>array('index')
343: ),
344: array(
345: 'name'=>'create',
346: 'label'=>Yii::t('topics','Create {topic}', array(
347: '{topic}'=>$Topic,
348: )),
349: 'url'=>array('create')
350: ),
351: array(
352: 'name'=>'view',
353: 'label'=>Yii::t('topics','View {topic}', array(
354: '{topic}'=>$Topic,
355: )),
356: 'url'=>array('view', 'id'=>$modelId)
357: ),
358: array(
359: 'name'=>'edit',
360: 'label'=>Yii::t('topics','Edit {topic}', array(
361: '{topic}'=>$Topic,
362: )),
363: 'url'=>array('update', 'id'=>$modelId)
364: ),
365: array(
366: 'name'=>'delete',
367: 'label'=>Yii::t('topics','Delete'),
368: 'url'=>'#',
369: 'linkOptions'=>array(
370: 'submit'=>array('delete','id'=>$modelId),
371: 'confirm'=>'Are you sure you want to delete this item?')
372: ),
373: );
374: $this->prepareMenu($menuItems, $selectOptions);
375: $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
376: }
377: }
378: