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

  • Contacts
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /*****************************************************************************************
  4:  * X2Engine Open Source Edition is a customer relationship management program developed by
  5:  * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  6:  * 
  7:  * This program is free software; you can redistribute it and/or modify it under
  8:  * the terms of the GNU Affero General Public License version 3 as published by the
  9:  * Free Software Foundation with the addition of the following permission added
 10:  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 11:  * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
 12:  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 13:  * 
 14:  * This program is distributed in the hope that it will be useful, but WITHOUT
 15:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 16:  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 17:  * details.
 18:  * 
 19:  * You should have received a copy of the GNU Affero General Public License along with
 20:  * this program; if not, see http://www.gnu.org/licenses or write to the Free
 21:  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 22:  * 02110-1301 USA.
 23:  * 
 24:  * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
 25:  * California 95067, USA. or at email address contact@x2engine.com.
 26:  * 
 27:  * The interactive user interfaces in modified source and object code versions
 28:  * of this program must display Appropriate Legal Notices, as required under
 29:  * Section 5 of the GNU Affero General Public License version 3.
 30:  * 
 31:  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 32:  * these Appropriate Legal Notices must retain the display of the "Powered by
 33:  * X2Engine" logo. If the display of the logo is not reasonably feasible for
 34:  * technical reasons, the Appropriate Legal Notices must display the words
 35:  * "Powered by X2Engine".
 36:  *****************************************************************************************/
 37: 
 38: Yii::import('application.models.X2Model');
 39: 
 40: /**
 41:  * This is the model class for table "x2_contacts".
 42:  *
 43:  * @package application.modules.contacts.models
 44:  */
 45: class Contacts extends X2Model {
 46: 
 47:     public $name;
 48: 
 49:     /**
 50:      * Returns the static model of the specified AR class.
 51:      * @return Contacts the static model class
 52:      */
 53:     public static function model($className = __CLASS__) {
 54:         return parent::model($className);
 55:     }
 56: 
 57:     /**
 58:      * @return array relational rules.
 59:      */
 60:     public function relations() {
 61:         // NOTE: you may need to adjust the relation name and the related
 62:         // class name for the relations automatically generated below.
 63:         return array_merge(parent::relations(), array(
 64:             
 65:         ));
 66:     }
 67: 
 68:     /**
 69:      * @return string the associated database table name
 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:      * @return boolean whether or not to save
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:      * Responds when {@link X2Model::afterUpdate()} is called (record saved, but
131:      * not a new record). Sends a notification to anyone subscribed to this contact.
132:      *
133:      * Before executing this, the model must check whether the contact has the
134:      * "changelog" behavior. That is because the behavior is disabled
135:      * when checking for duplicates in {@link ContactsController}
136:      */
137:     public function afterUpdate() {
138:         if (!Yii::app()->params->noSession && $this->asa('changelog') &&
139:                 $this->asa('changelog')->enabled) {//$this->scenario != 'noChangelog') {
140:             // send subscribe emails if anyone has subscribed to this contact
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:         // $condition = 'visibility="1" OR assignedTo="Anyone"  OR assignedTo="'.Yii::app()->user->getName().'"';
187:         // /* x2temp */
188:         // $groupLinks = Yii::app()->db->createCommand()->select('groupId')->from('x2_group_to_user')->where('userId='.Yii::app()->user->getId())->queryColumn();
189:         // if(!empty($groupLinks))
190:         // $condition .= ' OR assignedTo IN ('.implode(',',$groupLinks).')';
191:         // $condition .= 'OR (visibility=2 AND assignedTo IN
192:         // (SELECT username FROM x2_group_to_user WHERE groupId IN
193:         // (SELECT groupId FROM x2_group_to_user WHERE userId='.Yii::app()->user->getId().')))';
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:      *    Returns all public contacts.
207:      *    @return $names An array of strings containing the names of contacts.
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:                 //$links.=$link.', ';
232:             }
233:         }
234:         //$links=substr($links,0,strlen($links)-2);
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:      * An alias for search ()
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:         // $condition = 'assignedTo="'.Yii::app()->user->getName().'"';
270:         // $parameters=array('limit'=>ceil(Profile::getResultsPerPage()));
271:         // $parameters['condition']=$condition;
272:         // $criteria->scopes=array('findAll'=>array($parameters));
273: 
274:         return $this->searchBase($criteria);
275:     }
276: 
277:     public function searchNewContacts() {
278:         $criteria = new CDbCriteria;
279:         // $condition = 'assignedTo="'.Yii::app()->user->getName().'" AND createDate > '.mktime(0,0,0);
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:      * Adds tag filtering to search base 
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:      * Returns a DataProvider for all the contacts in the specified list,
321:      * using this Contact model's attributes as a search filter
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'    // true = ASC
335:                 ),
336:                 'pagination' => array(
337:                     'pageSize' => isset($pageSize) ? $pageSize : Profile::getResultsPerPage(),
338:                 ),
339:             ));
340:         } else {    //if list is not working, return all contacts
341:             return $this->searchBase();
342:         }
343:     }
344: 
345:     /**
346:      * Generates a random tracking key and guarantees uniqueness
347:      * @return String $key a unique random tracking key
348:      */
349:     public static function getNewTrackingKey() {
350: 
351:         $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
352: 
353:         // try up to 100 times to guess a unique key
354:         for ($i = 0; $i < 100; $i++) {
355:             $key = '';
356:             for ($j = 0; $j < 32; $j++)    // generate a random 32 char alphanumeric string
357:                 $key .= substr($chars, rand(0, strlen($chars) - 1), 1);
358: 
359:             // check if this key is already used
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: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0