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 [email protected].
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: /**
39: * This is the model class for table "x2_lead_routing".
40: *
41: * @package application.models
42: * @property integer $id
43: * @property string $field
44: * @property string $value
45: * @property string $users
46: */
47: class LeadRouting extends CActiveRecord {
48:
49: /**
50: * Returns the static model of the specified AR class.
51: * @return LeadRouting the static model class
52: */
53: public static function model($className = __CLASS__) {
54: return parent::model($className);
55: }
56:
57: /**
58: * @return string the associated database table name
59: */
60: public function tableName() {
61: return 'x2_lead_routing';
62: }
63:
64: /**
65: * @return array validation rules for model attributes.
66: */
67: public function rules() {
68: // NOTE: you should only define rules for those attributes that
69: // will receive user inputs.
70: return array(
71: array('users', 'safe'),
72: // The following rule is used by search().
73: // Please remove those attributes that should not be searched.
74: array('id, users', 'safe', 'on' => 'search'),
75: );
76: }
77:
78: /**
79: * @return array relational rules.
80: */
81: public function relations() {
82: // NOTE: you may need to adjust the relation name and the related
83: // class name for the relations automatically generated below.
84: return array(
85: );
86: }
87:
88: /**
89: * @return array customized attribute labels (name=>label)
90: */
91: public function attributeLabels() {
92: return array(
93: 'id' => Yii::t('admin','ID'),
94: 'criteria' => Yii::t('admin','Criteria'),
95: 'users' => Yii::t('admin','Users'),
96: 'priority' => Yii::t('admin','Priority'),
97: );
98: }
99:
100: /**
101: * Obtains an array of criteria from a JSON-encoded list.
102: * @param string $str JSON-endoced list of lead routing criteria
103: * @return array
104: */
105: public static function parseCriteria($str) {
106: $array = json_decode($str);
107: $arr = array();
108: foreach ($array as $criteria) {
109: $pieces = explode(',', $criteria);
110: $arr[] = array('field' => $pieces[0], 'comparison' => $pieces[1], 'value' => $pieces[2]);
111: }
112: return $arr;
113: }
114:
115: /**
116: * Turns a list of lead routing criteria into a human-readable list of rules.
117: *
118: * @param string $str
119: * @return string
120: */
121: public static function humanizeText($str) {
122: $array = json_decode($str);
123: $arr = array();
124: $return = "If ";
125: $tempArr = array();
126: foreach ($array as $criteria) {
127: $tempStr = "";
128: $pieces = explode(',', $criteria);
129: $arr[] = array('field' => $pieces[0], 'comparison' => $pieces[1], 'value' => $pieces[2]);
130: $field = Fields::model()->findByAttributes(array('modelName' => 'Contacts', 'fieldName' => $pieces[0]))->attributeLabel;
131: $tempStr.=$field;
132: switch ($pieces[1]) {
133: case '<':
134: $tempStr.=" is less than or equal to $pieces[2]";
135: break;
136: case '>':
137: $tempStr.=" is greater than or equal to $pieces[2]";
138: break;
139: case '=':
140: $tempStr.=" is equal to $pieces[2]";
141: break;
142: case '!=':
143: $tempStr.=" is not equal to $pieces[2]";
144: break;
145: case 'contains':
146: $tempStr.=" contains the the text '$pieces[2]'";
147: break;
148: }
149: $tempArr[] = $tempStr;
150: }
151: $return.=implode(' and ', $tempArr);
152: return $return;
153: }
154:
155: /**
156: * Retrieves a list of models based on the current search/filter conditions.
157: * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
158: */
159: public function search() {
160: // Warning: Please modify the following code to remove attributes that
161: // should not be searched.
162:
163: $criteria = new CDbCriteria;
164:
165: $criteria->compare('id', $this->id);
166: $criteria->compare('field', $this->field, true);
167: $criteria->compare('value', $this->value, true);
168: $criteria->compare('users', $this->users, true);
169:
170: return new CActiveDataProvider(get_class($this), array(
171: 'criteria' => $criteria,
172: ));
173: }
174:
175: }