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: class Notification extends CActiveRecord {
43: 44: 45: 46: 47:
48: public static function model($className=__CLASS__) {
49: return parent::model($className);
50: }
51:
52: 53: 54:
55: public function tableName() {
56: return 'x2_notifications';
57: }
58:
59: 60: 61:
62: public function rules() {
63:
64:
65: return array(
66: array('type', 'required'),
67: array('modelId, createDate', 'numerical', 'integerOnly'=>true),
68: array('viewed', 'boolean'),
69:
70: array('user, createdBy, comparison, type', 'length', 'max'=>20),
71: array('type, value', 'length', 'max'=>250),
72:
73: array('id, user, viewed, text, createDate, type, comparison, modelType, modelId, fieldName', 'safe', 'on'=>'search'),
74: );
75: }
76:
77: 78: 79:
80: public function relations() {
81:
82:
83: return array(
84: 'event'=>array(self::HAS_ONE,'Events','associationId','condition'=>'associationType="Notification"'),
85: );
86: }
87:
88: 89: 90:
91: public function attributeLabels() {
92: return array(
93: 'id' => 'ID',
94:
95:
96: 'user' => 'User',
97: 'createdBy' => 'Created By',
98: 'viewed' => 'Viewed',
99: 'createDate' => 'Create Date',
100: 'type' => 'Type',
101: 'comparison' => 'Comparison',
102: 'value' => 'Value',
103: 'text' => 'Text',
104: 'modelType' => 'Model Type',
105: 'modelId' => 'Model ID',
106: 'fieldName' => 'Field Name',
107: );
108: }
109:
110: 111: 112: 113:
114: public function search() {
115:
116:
117:
118: $criteria=new CDbCriteria;
119:
120: $criteria->compare('id',$this->id);
121:
122:
123: $criteria->compare('user',$this->user,true);
124: $criteria->compare('createdBy',$this->createdBy,true);
125: $criteria->compare('viewed',$this->viewed);
126: $criteria->compare('createDate',$this->createDate,true);
127: $criteria->compare('type',$this->type,true);
128: $criteria->compare('comparison',$this->comparison,true);
129:
130: $criteria->compare('modelType',$this->modelType,true);
131: $criteria->compare('modelId',$this->modelId,true);
132: $criteria->compare('fieldName',$this->fieldName,true);
133:
134: return new CActiveDataProvider($this, array(
135: 'criteria'=>$criteria,
136: ));
137: }
138:
139:
140: public function getMessage() {
141:
142: if(empty($this->modelId) || empty($this->modelType))
143: $record = null;
144: else {
145: if(class_exists($this->modelType)) {
146: $record = X2Model::model($this->modelType)->findByPk($this->modelId);
147: } else {
148: return 'Error: unknown record <b>'.$this->modelType.'</b>';
149: }
150: if($record === null) {
151: $this->delete();
152: return null;
153: }
154: }
155:
156: if(!isset($record) && $this->type !== 'lead_failure' && $this->type !== 'custom') {
157:
158: return null;
159: }
160: $passive = $this->createdBy === 'API' || empty($this->createdBy);
161:
162: switch($this->type) {
163:
164: case 'action_complete':
165: if($passive)
166: return Yii::t('actions','Action completed: {action}',array('{action}'=>$record->getLink()));
167: else
168: return Yii::t('actions','{user} completed an action: {action}',array('{user}'=>User::getUserLinks($record->completedBy),'{action}'=>$record->getLink(20)));
169:
170: case 'action_reminder':
171: return Yii::t('actions','<b>Reminder!</b> The following action is due: {action}',array('{action}'=>$record->getLink()));
172:
173:
174:
175:
176:
177:
178: case 'create':
179: return Yii::t('app','New record assigned to you: {link}.',array('{link}'=>$record->getLink()));
180:
181: case 'change':
182: if($this->comparison == 'change') {
183:
184: $msg = $passive? '{record}\'s {field} was changed to {value}' : '{user} changed {record}\'s {field} to {value}';
185:
186: return Yii::t('app',$msg,array(
187: '{field}'=>$record->getAttributeLabel($this->fieldName),
188: '{value}'=>$record->renderAttribute($this->fieldName,true,true),
189: '{record}'=>$record->getLink(),
190: '{user}'=>(Yii::app()->user->getName()==$this->createdBy)?CHtml::link('You',array('/profile/view','id'=>Yii::app()->user->getId())):User::getUserLinks($this->createdBy)
191: ));
192:
193: } else {
194:
195: $msg = $passive? '{record}\'s {field} was changed to {value}' : '{user} changed {record}\'s {field} to {value}';
196:
197: return Yii::t('app',$msg,array(
198: '{field}'=>$record->getAttributeLabel($this->fieldName),
199: '{value}'=>$record->renderAttribute($this->fieldName,true,true),
200: '{record}'=>$record->getLink(),
201: '{user}'=>(Yii::app()->user->getName()==$this->createdBy)?CHtml::link('You',array('/profile/view','id'=>Yii::app()->user->getId())):User::getUserLinks($this->createdBy)
202: ));
203:
204:
205: }
206:
207: case 'lead_failure':
208: return Yii::t('app','A lead failed to come through Lead Capture. Check {link} to recover it.',array(
209: '{link}'=>CHtml::link(Yii::t('app','here'),Yii::app()->controller->createUrl('/contacts/contacts/cleanFailedLeads'))
210: ));
211: case 'assignment':
212: if($passive)
213: return Yii::t('app','You have been assigned a record: {record}',array('{record}'=>$record->getLink()));
214: else
215: return Yii::t('app','{user} assigned a record to you: {record}',array('{user}'=>User::getUserLinks($this->createdBy),'{record}'=>$record->getLink()));
216:
217: case 'delete':
218: if($passive)
219: return Yii::t('app','Record deleted: {record}',array('{record}'=>$this->modelType.' '.$this->modelId));
220: else
221: return Yii::t('app','{user} deleted a record: {record}',array('{user}'=>User::getUserLinks($this->createdBy),'{record}'=>$this->modelType.' '.$this->modelId));
222: case 'event_broadcast':
223: return Yii::t('app','{user} broadcast an event: {event}',array('{user}'=>User::getUserLinks($record->user),'{event}'=>$record->getText()));
224: case 'update':
225: if($passive)
226: return Yii::t('app','Record updated: {record}',array('{record}'=>$record->getLink()));
227: else
228: return Yii::t('app','{user} updated a record: {record}',array('{user}'=>User::getUserLinks($this->createdBy),'{record}'=>$record->getLink()));
229:
230: case 'dup_discard':
231: if($passive)
232: return Yii::t('app','A record has been marked as a duplicate and hidden to everyone but the admin: {record}',
233: array('{record}'=>$record->getLink()));
234: else
235: return Yii::t('app','{user} marked a record as a duplicate. This record is hidden to everyone but the admin: {record}',
236: array('{user}'=>User::getUserLinks($this->createdBy),'{record}'=>$record->getLink()));
237:
238: case 'email_clicked':
239: return Yii::t('app','{record} clicked an email link: {campaign}',array('{record}'=>$record->getLink(),'{campaign}'=>$this->value));
240:
241: case 'email_opened':
242: return Yii::t('app','{record} opened an email: {campaign}',array('{record}'=>$record->getLink(),'{campaign}'=>$this->value));
243:
244: case 'email_unsubscribed':
245: return Yii::t('app','{record} unsubscribed from a campaign: {campaign}',array('{record}'=>$record->getLink(),'{campaign}'=>$this->value));
246:
247: case 'social_post':
248: return Yii::t('app','{user} posted on {link}',array('{user}'=>User::getUserLinks($this->createdBy),'{link}'=>$record->getLink()));
249:
250: case 'social_comment':
251: return Yii::t('app','{user} replied on {link}',array('{user}'=>User::getUserLinks($this->createdBy),'{link}'=>$record->getLink()));
252:
253: case 'voip_call':
254: return Yii::t(
255: 'app',
256: 'Incoming call from <b>{phone}</b> ({record}) {time}',
257: array(
258: '{record}'=>$record->getLink(),
259: '{phone}'=>$this->value,
260: '{time}'=>Formatter::formatDateDynamic ($this->createDate)
261: )
262: );
263: case 'weblead':
264: return Yii::t('app','New web lead: {link}.',array('{link}'=>$record->getLink()));
265:
266: case 'webactivity':
267: if($record instanceof Actions) {
268: if($link = $record->getAssociationLink())
269: return Yii::t('app','{name} is currently on {url}',array('{name}'=>$link,'{url}'=>$record->actionDescription));
270: } elseif($record instanceof Contacts) {
271: return Yii::t('app','{name} is currently on your website.',array('{name}'=>$record->getLink()));
272: }
273: return null;
274: case 'escalateCase':
275: return Yii::t('app', '{user} escalated a Service Case to you: {record}', array(
276: '{user}'=>User::getUserLinks($this->createdBy),
277: '{record}'=>$record->createLink(),
278: ));
279: case 'custom':
280: return $this->text;
281: default:
282: return 'Error: unknown type <b>'.$this->type.'</b>';
283:
284: }
285: }
286: }
287: