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: * Generic condition list form which enables user specification of conditions on model properties.
39: * User specified conditions can be retrieved through the front-end X2ConditionList API (see
40: * X2ConditionList.js).
41: */
42:
43: class X2ConditionList extends X2Widget {
44:
45: /**
46: * @var string $id id of container element
47: */
48: public $id;
49:
50: /**
51: * @var string $name condition list input name
52: */
53: public $name;
54:
55: /**
56: * @var array $value conditions already added
57: */
58: public $value;
59:
60: /**
61: * @var X2Model $model model whose attributes should be used to populate attribute list
62: */
63: public $model;
64:
65: /**
66: * @var bool $useLinkedModels if true, add field options for related models
67: */
68: public $useLinkedModels = false;
69:
70: /**
71: * @var array (optional) Used to instantiate JS X2ConditionList class. If not specified, this
72: * value will default to return value of {@link X2Model::getFieldsForDropdown}
73: */
74: public $attributes;
75:
76: /**
77: * @var array $_packages
78: */
79: protected $_packages;
80:
81: public static function listOption ($attributes, $name) {
82: if ($attributes instanceof Fields) {
83: $attributes = $attributes->getAttributes ();
84: }
85: $data = array(
86: 'name' => $name,
87: 'label' => $attributes['attributeLabel'],
88: );
89:
90: if(isset ($attributes['type']) && $attributes['type'])
91: $data['type'] = $attributes['type'];
92: if(isset ($attributes['required']) && $attributes['required'])
93: $data['required'] = 1;
94: if(isset ($attributes['readOnly']) && $attributes['readOnly'])
95: $data['readOnly'] = 1;
96: if(isset ($attributes['type'])) {
97: if (($attributes['type'] === 'assignment' ||
98: $attributes['type'] === 'optionalAssignment')) {
99: $data['options'] = AuxLib::dropdownForJson(
100: X2Model::getAssignmentOptions(true, true));
101: } elseif ($attributes['type'] === 'dropdown' && isset ($attributes['linkType'])) {
102: $data['linkType'] = $attributes['linkType'];
103: $data['options'] = AuxLib::dropdownForJson(
104: Dropdowns::getItems($attributes['linkType']));
105: } elseif ($attributes['type'] === 'link' && isset ($attributes['linkType'])) {
106: $staticLinkModel = X2Model::model($attributes['linkType']);
107: if(array_key_exists('X2LinkableBehavior', $staticLinkModel->behaviors())) {
108: $data['linkType'] = $attributes['linkType'];
109: $data['linkSource'] = Yii::app()->controller->createUrl(
110: $staticLinkModel->autoCompleteSource);
111: }
112: }
113: }
114:
115: return $data;
116: }
117:
118: public function getPackages () {
119: if (!isset ($this->_packages)) {
120: $this->_packages = array (
121: 'X2Fields' => array(
122: 'baseUrl' => Yii::app()->request->baseUrl,
123: 'js' => array(
124: 'js/X2Fields.js',
125: 'js/X2FieldsGeneric.js',
126: 'js/jquery-ui-timepicker-addon.js',
127: ),
128: 'depends' => array ('jquery.ui')
129: ),
130: 'X2ConditionListJS' => array(
131: 'baseUrl' => Yii::app()->request->baseUrl,
132: 'js' => array(
133: 'js/X2ConditionList.js',
134: ),
135: 'depends' => array ('auxlib', 'X2Fields')
136: ),
137: );
138: }
139: return $this->_packages;
140: }
141:
142: public function init () {
143: if (!$this->attributes) {
144: $this->attributes = $this->model->getFieldsForDropdown ($this->useLinkedModels);
145: }
146: }
147:
148: public function run () {
149: $this->registerPackages ();
150: $this->render ('x2ConditionList');
151: }
152:
153: }
154: