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 [email protected].
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: Yii::import('application.models.X2Model');
38:
39: /**
40: * This is the model class for table "x2_leads".
41: *
42: * @package application.modules.x2Leads.models
43: */
44: class X2Leads extends X2Model {
45:
46: /**
47: * Returns the static model of the specified AR class.
48: * @return X2Leads the static model class
49: */
50: public static function model($className=__CLASS__) { return parent::model($className); }
51:
52: /**
53: * @return string the associated database table name
54: */
55: public function tableName() { return 'x2_x2leads'; }
56:
57: public function behaviors() {
58: return array_merge(parent::behaviors(),array(
59: 'X2LinkableBehavior'=>array(
60: 'class'=>'X2LinkableBehavior',
61: 'module'=>'x2Leads'
62: ),
63: 'ERememberFiltersBehavior' => array(
64: 'class' => 'application.components.ERememberFiltersBehavior',
65: 'defaults'=>array(),
66: 'defaultStickOnClear'=>false
67: ),
68: 'X2ModelConversionBehavior' => array(
69: 'class' => 'application.components.recordConversion.X2ModelConversionBehavior',
70: 'deleteConvertedRecord' => false,
71: 'convertedField' => 'converted',
72: 'conversionDateField' => 'conversionDate',
73: 'convertedToTypeField' => 'convertedToType',
74: 'convertedToIdField' => 'convertedToId',
75: ),
76: 'ContactsNameBehavior' => array(
77: 'class' => 'application.components.ContactsNameBehavior',
78: ),
79: ));
80: }
81:
82: public static function getNames() {
83: $arr = X2Leads::model()->findAll();
84: $names = array(0=>'None');
85: foreach($arr as $x2Leads)
86: $names[$x2Leads->id] = $x2Leads->name;
87:
88: return $names;
89: }
90:
91: public static function getX2LeadsLinks($accountId) {
92: $allX2Leads =
93: X2Model::model('X2Leads')->findAllByAttributes(array('accountName'=>$accountId));
94:
95: $links = array();
96: foreach($allX2Leads as $model) {
97: $links[] = CHtml::link($model->name,array('/x2Leads/x2Leads/view','id'=>$model->id));
98: }
99: return implode(', ',$links);
100: }
101:
102: public function beforeSave () {
103: // backwards compatibility check for when leads didn't have first and last name fields
104: if (!$this->isNewRecord &&
105: !$this->firstName &&
106: !$this->lastName &&
107: ($this->attributeChanged ('firstName') ||
108: $this->attributeChanged ('lastName'))) {
109:
110: $this->name = '';
111: }
112: return parent::beforeSave ();
113: }
114:
115: public function search($resultsPerPage=null, $uniqueId=null) {
116: $criteria=new CDbCriteria;
117: $parameters=array('limit'=>ceil(Profile::getResultsPerPage()));
118: $criteria->scopes=array('findAll'=>array($parameters));
119:
120: // allows converted leads to be filtered out of grid by default
121: $filters = $this->asa ('ERememberFiltersBehavior')->getSetting ('filters');
122: if (!isset ($filters['converted'])) {
123: $this->converted = 'false';
124: } elseif ($filters['converted'] === 'all') {
125: unset ($this->converted);
126: }
127: return $this->searchBase($criteria, $resultsPerPage);
128: }
129:
130: public function searchAdmin() {
131: $criteria=new CDbCriteria;
132:
133: return $this->searchBase($criteria);
134: }
135:
136: public function getConvertedTo () {
137: if ($this->converted) {
138: $type = $this->convertedToType;
139: $id = $this->convertedToId;
140: return X2Model::model ($type)->findByPk ($id);
141: }
142: }
143:
144: public function renderConvertedNotice () {
145: $convertedTo = $this->getConvertedTo ();
146: if ($convertedTo) {
147: Yii::app()->user->setFlash (
148: 'notice',
149: Yii::t('x2Leads', 'This record has been converted. '.
150: 'To view the new record, click {here}.', array (
151: '{here}' => CHtml::link (
152: Yii::t('x2Leads', 'here'), $convertedTo->getUrl ()
153: ))));
154: X2Flashes::renderTopFlashes ('notice');
155: }
156: }
157:
158: }
159: