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: Yii::import('application.models.X2Model');
39:
40: 41: 42: 43: 44:
45: class Contacts extends X2Model {
46:
47: public $name;
48:
49: 50: 51: 52:
53: public static function model($className = __CLASS__) {
54: return parent::model($className);
55: }
56:
57: 58: 59:
60: public function relations() {
61:
62:
63: return array_merge(parent::relations(), array(
64:
65: ));
66: }
67:
68: 69: 70:
71: public function tableName() {
72: return 'x2_contacts';
73: }
74:
75: public function behaviors() {
76: return array_merge(parent::behaviors(), array(
77: 'X2LinkableBehavior' => array(
78: 'class' => 'X2LinkableBehavior',
79: 'module' => 'contacts',
80: ),
81:
82: 'ERememberFiltersBehavior' => array(
83: 'class' => 'application.components.ERememberFiltersBehavior',
84: 'defaults' => array(),
85: 'defaultStickOnClear' => false
86: ),
87: 'X2AddressBehavior' => array(
88: 'class' => 'application.components.X2AddressBehavior',
89: ),
90: 'X2DuplicateBehavior' => array(
91: 'class' => 'application.components.X2DuplicateBehavior',
92: ),
93: 'ContactsNameBehavior' => array(
94: 'class' => 'application.components.ContactsNameBehavior',
95: ),
96: ));
97: }
98:
99: public function rules() {
100: $parentRules = parent::rules();
101: return $parentRules;
102: }
103:
104: public function duplicateFields() {
105: return array_merge(parent::duplicateFields(), array(
106: 'email',
107: ));
108: }
109:
110: public function afterFind() {
111: parent::afterFind();
112: if ($this->trackingKey === null && self::$autoPopulateFields) {
113: $this->trackingKey = self::getNewTrackingKey();
114: $this->update(array('trackingKey'));
115: }
116: }
117:
118: 119: 120:
121: public function beforeSave() {
122: if ($this->trackingKey === null) {
123: $this->trackingKey = self::getNewTrackingKey();
124: }
125:
126: return parent::beforeSave();
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136:
137: public function afterUpdate() {
138: if (!Yii::app()->params->noSession && $this->asa('changelog') &&
139: $this->asa('changelog')->enabled) {
140:
141: $result = Yii::app()->db->createCommand()
142: ->select('user_id')
143: ->from('x2_subscribe_contacts')
144: ->where('contact_id=:id', array(':id' => $this->id))
145: ->queryColumn();
146:
147: $datetime = Formatter::formatLongDateTime(time());
148: $modelLink = CHtml::link($this->name, Yii::app()->controller->createAbsoluteUrl('/contacts/' . $this->id));
149: $subject = 'X2Engine: ' . $this->name . ' updated';
150: $message = "Hello,<br>\n<br>\n";
151: $message .= 'You are receiving this email because you are subscribed to changes made to the contact ' . $modelLink . ' in X2Engine. ';
152: $message .= 'The following changes were made on ' . $datetime . ":<br>\n<br>\n";
153:
154: foreach ($this->getChanges() as $attribute => $change) {
155: if ($attribute != 'lastActivity') {
156: $old = $change[0] == '' ? '-----' : $change[0];
157: $new = $change[1] == '' ? '-----' : $change[1];
158: $label = $this->getAttributeLabel($attribute);
159: $message .= "$label: $old => $new<br>\n";
160: }
161: }
162:
163: $message .="<br>\nYou can unsubscribe to these messages by going to $modelLink and clicking Unsubscribe.<br>\n<br>\n";
164:
165: $adminProfile = Yii::app()->params->adminProfile;
166: foreach ($result as $subscription) {
167: $subscription = array();
168: if (isset($subscription['user_id'])) {
169: $profile = X2Model::model('Profile')->findByPk($subscription['user_id']);
170: if ($profile && $profile->emailAddress && $adminProfile && $adminProfile->emailAddress) {
171: $to = array('to' => array(array($profile->fullName, $profile->emailAddress)));
172: Yii::app()->controller->sendUserEmail($to, $subject, $message, null, Credentials::$sysUseId['systemNotificationEmail']);
173: }
174: }
175: }
176: }
177:
178:
179: parent::afterUpdate();
180: }
181:
182: public static function getNames() {
183:
184: $criteria = $this->getAccessCriteria();
185:
186:
187:
188:
189:
190:
191:
192:
193:
194: $contactArray = X2Model::model('Contacts')->findAll($condition);
195: $names = array(0 => 'None');
196: foreach ($contactArray as $user) {
197: $first = $user->firstName;
198: $last = $user->lastName;
199: $name = $first . ' ' . $last;
200: $names[$user->id] = $name;
201: }
202: return $names;
203: }
204:
205: 206: 207: 208:
209: public static function getAllNames() {
210: $contactArray = X2Model::model('Contacts')->findAll($condition = 'visibility=1');
211: $names = array(0 => 'None');
212: foreach ($contactArray as $user) {
213: $first = $user->firstName;
214: $last = $user->lastName;
215: $name = $first . ' ' . $last;
216: $names[$user->id] = $name;
217: }
218: return $names;
219: }
220:
221: public static function getContactLinks($contacts) {
222: if (!is_array($contacts))
223: $contacts = explode(' ', $contacts);
224:
225: $links = array();
226: foreach ($contacts as &$id) {
227: if ($id != 0) {
228: $model = X2Model::model('Contacts')->findByPk($id);
229: if (isset($model))
230: $links[] = CHtml::link($model->name, array('/contacts/contacts/view', 'id' => $id));
231:
232: }
233: }
234:
235: return implode(', ', $links);
236: }
237:
238: public static function getMailingList($criteria) {
239:
240: $mailingList = array();
241:
242: $arr = X2Model::model('Contacts')->findAll();
243: foreach ($arr as $contact) {
244: $i = preg_match("/$criteria/i", $contact->backgroundInfo);
245: if ($i >= 1) {
246: $mailingList[] = $contact->email;
247: }
248: }
249: return $mailingList;
250: }
251:
252: 253: 254:
255: public function searchAll($pageSize=null, CDbCriteria $criteria = null) {
256: return $this->search ($pageSize, $criteria);
257: }
258:
259: public function searchMyContacts() {
260: $criteria = new CDbCriteria;
261:
262: $accessLevel = Yii::app()->user->checkAccess('ContactsView') ? 1 : 0;
263: $conditions = $this->getAccessConditions($accessLevel);
264: foreach ($conditions as $arr) {
265: $criteria->addCondition($arr['condition'], $arr['operator']);
266: $criteria->params = array_merge($criteria->params, $arr['params']);
267: }
268:
269:
270:
271:
272:
273:
274: return $this->searchBase($criteria);
275: }
276:
277: public function searchNewContacts() {
278: $criteria = new CDbCriteria;
279:
280: $condition = 't.createDate > ' . mktime(0, 0, 0);
281: $accessLevel = Yii::app()->user->checkAccess('ContactsView') ? 1 : 0;
282: $conditions = $this->getAccessConditions($accessLevel);
283: foreach ($conditions as $arr) {
284: $criteria->addCondition($arr['condition'], $arr['operator']);
285: $criteria->params = array_merge($criteria->params, $arr['params']);
286: }
287:
288: $parameters = array('limit' => ceil(Profile::getResultsPerPage()));
289:
290: $parameters['condition'] = $condition;
291: $criteria->scopes = array('findAll' => array($parameters));
292:
293: return $this->searchBase($criteria);
294: }
295:
296: 297: 298:
299: public function search($pageSize=null, CDbCriteria $criteria = null) {
300: if ($criteria === null){
301: $criteria = new CDbCriteria;
302: }
303:
304: return $this->searchBase($criteria, $pageSize);
305: }
306:
307: public function searchAdmin() {
308: $criteria = new CDbCriteria;
309: return $this->searchBase($criteria);
310: }
311:
312: public function searchAccount($id) {
313: $criteria = new CDbCriteria;
314: $criteria->compare('company', $id);
315:
316: return $this->searchBase($criteria);
317: }
318:
319: 320: 321: 322:
323: public function searchList($id, $pageSize = null) {
324: $list = X2List::model()->findByPk($id);
325:
326: if (isset($list)) {
327: $search = $list->queryCriteria();
328:
329: $this->compareAttributes($search);
330:
331: return new SmartActiveDataProvider('Contacts', array(
332: 'criteria' => $search,
333: 'sort' => array(
334: 'defaultOrder' => 't.lastUpdated DESC'
335: ),
336: 'pagination' => array(
337: 'pageSize' => isset($pageSize) ? $pageSize : Profile::getResultsPerPage(),
338: ),
339: ));
340: } else {
341: return $this->searchBase();
342: }
343: }
344:
345: 346: 347: 348:
349: public static function getNewTrackingKey() {
350:
351: $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
352:
353:
354: for ($i = 0; $i < 100; $i++) {
355: $key = '';
356: for ($j = 0; $j < 32; $j++)
357: $key .= substr($chars, rand(0, strlen($chars) - 1), 1);
358:
359:
360: if (X2Model::model('Contacts')->exists('trackingKey="' . $key . '"'))
361: continue;
362: else
363: return $key;
364: }
365: return null;
366: }
367:
368:
369:
370: }
371: