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: 39: 40: 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',
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: 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']))
74: $_GET['lastNotifId'] = 0;
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:
86: if(isset($_GET['lastEventId']) && is_numeric($_GET['lastEventId'])){
87:
88: $lastEventId = $_GET['lastEventId'];
89: }
90: if(isset($_GET['lastTimestamp']) && is_numeric($_GET['lastTimestamp'])){
91: $lastTimestamp=$_GET['lastTimestamp'];
92: }
93: if($lastEventId==0){
94:
95: $retVal = Events::getFilteredEventsDataProvider (
96: null, true, null, isset ($_SESSSION['filters']));
97: $dataProvider = $retVal['dataProvider'];
98: $events = $dataProvider->getData ();
99: }else{
100:
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: 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',
140: 'params'=>array(':user'=>Yii::app()->user->name,':lastId'=>$lastId,':time'=>time()),
141: 'order'=>'id DESC',
142: 'limit'=>1,
143: 'offset'=>9
144: ));
145: } else {
146: $criteria = new CDbCriteria(array(
147: 'condition'=>'id>:lastId AND user=:user AND createDate <= :time',
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: 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: 199: 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: 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: 224: 225: 226: 227: 228: 229: 230:
231: public static function convertLineBreaks($text,$allowDouble = true,$allowUnlimited = false) {
232: $text = mb_ereg_replace("\r\n","\n",$text);
233:
234: if(!$allowUnlimited)
235: $text = mb_ereg_replace("[\r\n]{3,}","\n\n",$text);
236: if($allowDouble)
237: $text = mb_ereg_replace("[\r\n]",'<br />',$text);
238: else
239: $text = mb_ereg_replace("[\r\n]+",'<br />',$text);
240:
241: return $text;
242: }
243: }
244: