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

  • ActionFormModelBase
  • CalendarEventFormModel
  • CallFormModel
  • CreatePageFormModel
  • EditMobileFormsFormModel
  • EventCommentPublisherFormModel
  • EventFormModel
  • EventPublisherFormModel
  • FileSystemObjectDataProvider
  • MassActionFormModel
  • MobilePagination
  • NoteFormModel
  • NotificationsController
  • TimeFormModel
  • UploadLogoFormModel
  • X2FormModel
  • X2HttpRequest
  • 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: /**
 38:  * User notifications & social feed controller
 39:  *
 40:  * @package application.controllers
 41:  */
 42: Yii::import('application.models.Relationships');
 43: Yii::import('application.components.RelationshipsBehavior');
 44: Yii::import('application.models.Tags');
 45: 
 46: class NotificationsController extends CController {
 47: 
 48:     public function accessRules() {
 49:         return array(
 50:             array('allow', // allow authenticated user to perform 'create' and 'update' actions
 51:                 'actions'=>array('get','delete','deleteAll','newMessage','getMessages','checkNotifications','saveGridviewSettings','saveFormSettings', 'fullScreen', 'widgetState','widgetOrder'),
 52:                 'users'=>array('@'),
 53:             ),
 54:             array('deny',
 55:                 'users'=>array('*')
 56:             )
 57:         );
 58:     }
 59: 
 60:     /**
 61:      * Obtain all current notifications/events for the current web user.
 62:      */
 63:     public function actionGet() {
 64: 
 65:         if(Yii::app()->user->isGuest) {
 66:             header('Content-type: application/json');
 67:             echo CJSON::encode(array(
 68:                 'sessionError'=>Yii::t('app','Your X2Engine session has expired. You may select "cancel" to ignore this message and recover unsaved data from the current page. Otherwise, you will be redirected to the login page.')
 69:             ));
 70:             Yii::app()->end();
 71:         }
 72: 
 73:         if(!isset($_GET['lastNotifId']))    // if the client doesn't specify the last
 74:             $_GET['lastNotifId'] = 0;        // message ID received, send everything
 75: 
 76:         $notifications = $this->getNotifications($_GET['lastNotifId']);
 77:         $notifCount = 0;
 78:         if(count($notifications))
 79:             $notifCount = X2Model::model('Notification')
 80:                 ->countByAttributes(array('user'=>Yii::app()->user->name),'createDate < '.time());
 81: 
 82:         $chatMessages = array();
 83:         $lastEventId = 0;
 84:         $lastTimestamp=0;
 85:         // if the client specifies the last message ID received,
 86:         if(isset($_GET['lastEventId']) && is_numeric($_GET['lastEventId'])){   
 87:             // only send newer messages
 88:             $lastEventId = $_GET['lastEventId'];                                
 89:         }
 90:         if(isset($_GET['lastTimestamp']) && is_numeric($_GET['lastTimestamp'])){
 91:             $lastTimestamp=$_GET['lastTimestamp'];
 92:         }
 93:         if($lastEventId==0){
 94:             // get page of newest events
 95:             $retVal = Events::getFilteredEventsDataProvider (
 96:                 null, true, null, isset ($_SESSSION['filters']));
 97:             $dataProvider = $retVal['dataProvider'];
 98:             $events = $dataProvider->getData ();
 99:         }else{
100:             // get new events
101:             $limit=null;
102:             $result=Events::getEvents($lastEventId,$lastTimestamp,$limit);
103:             $events=$result['events'];
104:         }
105: 
106:         $i=count($events)-1;
107:         for($i; $i>-1; --$i) {
108:             if(isset($events[$i])){
109:                 $userLink = '<span class="widget-event">'.$events[$i]->user.'</span>';
110:                 $chatMessages[] = array(
111:                     (int)$events[$i]->id,
112:                     (int)$events[$i]->timestamp,
113:                     $userLink,
114:                     $events[$i]->getText(array ('truncated' =>true)),
115:                     Formatter::formatFeedTimestamp($events[$i]->timestamp)
116:                 );
117:             }
118:         }
119: 
120:         if(!empty($notifications) || !empty($chatMessages)) {
121:             header('Content-type: application/json');
122:             echo CJSON::encode(array(
123:                 'notifCount'=>$notifCount,
124:                 'notifData'=>$notifications,
125:                 'chatData'=>$chatMessages,
126:             ));
127:         }
128:     }
129: 
130:     /**
131:      * Looks up notifications using the specified offset and limit
132:      */
133:     public function getNotifications($lastId=0,$getNext=false) {
134: 
135:         $notifications = array();
136: 
137:         if($getNext) {
138:             $criteria = new CDbCriteria(array(
139:                 'condition'=>'id<=:lastId AND user=:user AND createDate <= :time',                                // don't get anything more recent than lastId,
140:                 'params'=>array(':user'=>Yii::app()->user->name,':lastId'=>$lastId,':time'=>time()),        // because these are going to get appended to the end,
141:                 'order'=>'id DESC',                                                                         // not the beginning of the list
142:                 'limit'=>1,        // only get the 10th row
143:                 'offset'=>9
144:             ));
145:         } else {
146:             $criteria = new CDbCriteria(array(
147:                 'condition'=>'id>:lastId AND user=:user AND createDate <= :time',                                // normal request; get everything since lastId
148:                 'params'=>array(':user'=>Yii::app()->user->name,':lastId'=>$lastId,':time'=>time()),
149:                 'order'=>'id DESC',
150:                 'limit'=>10
151:             ));
152:         }
153: 
154: 
155:         $notifModels = X2Model::model('Notification')->findAll($criteria);
156: 
157:         foreach($notifModels as &$model) {
158:             $msg = $model->getMessage();
159: 
160:             if($msg !== null) {
161:                 $notifications[] = array(
162:                     'id'=>$model->id,
163:                     'viewed'=>$model->viewed,
164:                     'date'=>Yii::app()->dateFormatter->format(Yii::app()->locale->getDateFormat('short'),$model->createDate),
165:                     'text'=>$msg,
166:                     'timestamp'=>$model->createDate,
167:                     'modelId' => $model->modelId,
168:                     'type'=>$model->type,
169:                 );
170:                 if($model->type == 'voip_call') {
171:                     $model->viewed = 1;
172:                     $model->update('viewed');
173:                 }
174:             }
175:         }
176:         return $notifications;
177:     }
178: 
179:     /**
180:      * Mark an action as viewed.
181:      */
182:     public function actionMarkViewed() {
183:         if(isset($_GET['id'])) {
184:             if(!is_array($_GET['id']))
185:                 $_GET['id'] = array($_GET['id']);
186: 
187:             foreach($_GET['id'] as &$id) {
188:                 $notif = X2Model::model('Notification')->findByPk($id);
189:                 if(isset($notif) && $notif->user == Yii::app()->user->name) {
190:                     $notif->viewed = 1;
191:                     $notif->update();
192:                 }
193:             }
194:         }
195:     }
196: 
197:     /**
198:      * Delete an action by its ID. Encode and return the next notification if requested
199:      * @param type $id
200:      */
201:     public function actionDelete($id) {
202: 
203:         if(!isset($_GET['lastNotifId']))
204:             $_GET['lastNotifId'] = 0;
205: 
206:         $model = X2Model::model('Notification')->findByPk($id);
207:         if(isset($model) && $model->user = Yii::app()->user->name)
208:             $model->delete();
209: 
210:         if(isset($_GET['getNext']))
211:             echo CJSON::encode(array('notifData'=>$this->getNotifications($_GET['lastNotifId'],true)));
212:     }
213: 
214:     /**
215:      * Clear all notifications.
216:      */
217:     public function actionDeleteAll() {
218:         X2Model::model('Notification')->deleteAllByAttributes(array('user'=>Yii::app()->user->name));
219:         $this->redirect(array('/site/viewNotifications'));
220:     }
221: 
222:     /**
223:      * Normalize linebreaks in output.
224:      *
225:      * @todo refactor this out of controllers
226:      * @param string $text
227:      * @param boolean $allowDouble
228:      * @param boolean $allowUnlimited
229:      * @return string
230:      */
231:     public static function convertLineBreaks($text,$allowDouble = true,$allowUnlimited = false) {
232:         $text = mb_ereg_replace("\r\n","\n",$text);        //convert microsoft's stupid CRLF to just LF
233: 
234:         if(!$allowUnlimited)
235:             $text = mb_ereg_replace("[\r\n]{3,}","\n\n",$text);    // replaces 2 or more CR/LF chars with just 2
236:         if($allowDouble)
237:             $text = mb_ereg_replace("[\r\n]",'<br />',$text);    // replaces all remaining CR/LF chars with <br />
238:         else
239:             $text = mb_ereg_replace("[\r\n]+",'<br />',$text);
240: 
241:         return $text;
242:     }
243: }
244: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0