Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • None
  • system
    • base
    • caching
    • console
    • db
      • ar
      • schema
    • validators
    • web
      • actions
      • auth
      • helpers
      • widgets
        • captcha
        • pagers
  • zii
    • widgets
      • grid

Classes

  • AdminController
  • Api2Controller
  • ApiController
  • BugReportsController
  • CommonSiteControllerBehavior
  • ProfileController
  • RelationshipsController
  • SearchController
  • SiteController
  • StudioController
  • TemplatesController
  • TopicsController
  • x2base
  • X2Controller
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /*****************************************************************************************
  3:  * X2Engine Open Source Edition is a customer relationship management program developed by
  4:  * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  5:  * 
  6:  * This program is free software; you can redistribute it and/or modify it under
  7:  * the terms of the GNU Affero General Public License version 3 as published by the
  8:  * Free Software Foundation with the addition of the following permission added
  9:  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 10:  * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
 11:  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 12:  * 
 13:  * This program is distributed in the hope that it will be useful, but WITHOUT
 14:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 15:  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 16:  * details.
 17:  * 
 18:  * You should have received a copy of the GNU Affero General Public License along with
 19:  * this program; if not, see http://www.gnu.org/licenses or write to the Free
 20:  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 21:  * 02110-1301 USA.
 22:  * 
 23:  * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
 24:  * California 95067, USA. or at email address contact@x2engine.com.
 25:  * 
 26:  * The interactive user interfaces in modified source and object code versions
 27:  * of this program must display Appropriate Legal Notices, as required under
 28:  * Section 5 of the GNU Affero General Public License version 3.
 29:  * 
 30:  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 31:  * these Appropriate Legal Notices must retain the display of the "Powered by
 32:  * X2Engine" logo. If the display of the logo is not reasonably feasible for
 33:  * technical reasons, the Appropriate Legal Notices must display the words
 34:  * "Powered by X2Engine".
 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:      * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
 56:      * using two-column layout. See 'protected/views/layouts/column2.php'.
 57:      */
 58:     public function actionGetItems($term){
 59:         X2LinkableBehavior::getItems ($term);
 60:     }
 61: 
 62:     /**
 63:      * Displays a particular model.
 64:      * @param integer $id the ID of the model to be displayed
 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:      * Creates a new model.
112:      * If creation is successful, the browser will be redirected to the 'view' page.
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:                     // file uploaded through form
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:      * Updates a particular model.
184:      * If update is successful, the browser will be redirected to the 'view' page.
185:      * @param integer $id the ID of the model to be updated
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:      * Deletes a particular model.
210:      * If deletion is successful, the browser will be redirected to the 'admin' page.
211:      * @param integer $id the ID of the model to be deleted
212:      */
213:     public function actionDelete($id) {
214:         if(Yii::app()->request->isPostRequest) {
215:             // we only allow deletion via POST request
216:             $model=$this->loadModel($id);
217:             $this->cleanUpTags($model);
218:             $model->delete();
219: 
220:             /* if AJAX request (triggered by deletion via admin grid view), we should not redirect 
221:                the browser */
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:      * Lists all models.
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:      * Performs the AJAX validation.
309:      * @param CModel the model to be validated
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:      * Create a menu for Topics
320:      * @param array Menu options to remove
321:      * @param X2Model Model object passed to the view
322:      * @param array Additional menu parameters
323:      */
324:     public function insertMenu($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:          * To show all options:
331:          * $menuOptions = array(
332:          *     'index', 'create', 'view', 'edit', 'delete',
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: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0