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 contact@x2engine.com.
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: /**
38: * This is the model class for table "x2_dropdowns".
39: *
40: * @package application.models
41: * @property integer $id
42: * @property string $name
43: * @property string $options
44: */
45: class Dropdowns extends CActiveRecord {
46:
47: /**
48: * Returns the static model of the specified AR class.
49: * @return Dropdowns the static model class
50: */
51: public static function model($className = __CLASS__){
52: return parent::model($className);
53: }
54:
55: public static function getSocialSubtypes () {
56: $dropdown = Dropdowns::model()->findByPk(113);
57: if (!$dropdown) return array ();
58: return json_decode (
59: $dropdown->options,true);
60: }
61:
62: /**
63: * @return string the associated database table name
64: */
65: public function tableName(){
66: return 'x2_dropdowns';
67: }
68:
69: public function scopes () {
70: return array (
71: 'children' => array (
72: 'condition' => 'parent=:id',
73: 'params' => array (':id' => $this->id)
74: )
75: );
76: }
77:
78: /**
79: * @return array validation rules for model attributes.
80: */
81: public function rules(){
82: // NOTE: you should only define rules for those attributes that
83: // will receive user inputs.
84: return array(
85: array('name', 'length', 'max' => 250),
86: array('options', 'safe'),
87: array('options,name', 'required'),
88: array('multi', 'boolean'),
89: // The following rule is used by search().
90: // Please remove those attributes that should not be searched.
91: array('id, name, options', 'safe', 'on' => 'search'),
92: );
93: }
94:
95: /**
96: * @return array relational rules.
97: */
98: public function relations(){
99: // NOTE: you may need to adjust the relation name and the related
100: // class name for the relations automatically generated below.
101: return array(
102: );
103: }
104:
105: /**
106: * @return array customized attribute labels (name=>label)
107: */
108: public function attributeLabels(){
109: return array(
110: 'id' => Yii::t('admin', 'ID'),
111: 'name' => Yii::t('admin', 'Name'),
112: 'options' => Yii::t('admin', 'Options'),
113: 'multi' => Yii::t('admin', 'Allow multiple values'),
114: );
115: }
116:
117: /**
118: * Retrieves a list of models based on the current search/filter conditions.
119: * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
120: */
121: public function search(){
122: // Warning: Please modify the following code to remove attributes that
123: // should not be searched.
124:
125: $criteria = new CDbCriteria;
126:
127: $criteria->compare('id', $this->id);
128: $criteria->compare('name', $this->name, true);
129: $criteria->compare('options', $this->options, true);
130:
131: return new CActiveDataProvider(get_class($this), array(
132: 'criteria' => $criteria,
133: ));
134: }
135:
136: /**
137: * Retrieves items for the dropdown of given id, and whether multiple selection is allowed.
138: * @param integer $id
139: * @param string $translationPack The translation module to use, if applicable
140: * @param bool $multi wheter or not to include the "multi" column for distinguishing multiple
141: * selection from single selection
142: * @return array (<options>) or array ('options' => <options>, 'multi' => <multi>)
143: */
144: public static function getItems($id, $translationPack = null, $multi = false){
145: $data = Yii::app()->db->cache (1000, null)->createCommand()
146: ->select('options,multi')
147: ->from('x2_dropdowns')
148: ->where('id=:id', array(':id' => $id))
149: ->queryRow();
150: if(!empty($data)){
151: $data['options'] = CJSON::decode($data['options']);
152: $data['options'] = is_array($data['options']) ? $data['options'] : array();
153: if(!empty($translationPack)){
154: foreach(array_keys($data['options']) as $item){
155: $data['options'][$item] = Yii::t($translationPack, $data['options'][$item]);
156: }
157: }
158: } else
159: $data = array('options' => array(), 'multi' => false);
160: return $multi ? $data : $data['options'];
161: }
162:
163: /**
164: * @return dropdown label or the value, if no corresponding label can be found
165: */
166: public function getDropdownValue($id, $index){
167: $arr = Dropdowns::getItems($id, null, true);
168: if($arr['multi']){
169: $jdIndex = CJSON::decode($index);
170: $index = empty($jdIndex) && is_string($index) ? array($index) : $jdIndex;
171: if(!is_array($index))
172: $index = array();
173: return implode(', ', array_map(function($o)use($arr){
174: return isset($arr[$o]) ? $arr[$o] : $o;
175: }, $index));
176: }
177: if(isset($arr['options'])){
178: $arr = $arr['options'];
179: }
180: if(isset($arr[$index])){
181: return $arr[$index];
182: }else{
183: return $index;
184: }
185: }
186:
187: /**
188: * Returns dropdown value(s) for given key(s)
189: * @return array|string
190: */
191: public function getDropdownIndex($id, $key){
192: $arr = Dropdowns::getItems($id);
193: if (is_array ($key)) {
194: return array_map (function ($value) use ($arr) {
195: $index = array_search($value, $arr);
196: if ($index === false) {
197: return $value;
198: } else {
199: return $index;
200: }
201: }, $key);
202: } else {
203: if(array_search($key, $arr) !== false){
204: return array_search($key, $arr);
205: }else{
206: return $key;
207: }
208: }
209: }
210:
211: }
212: