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: abstract class WidgetLayout extends JSONEmbeddedModel {
38:
39: /**
40: * @var string $alias
41: */
42: protected $alias;
43:
44: protected $_fields;
45:
46: public $whitelist;
47:
48: protected function widgetOrder () {
49: return array ();
50: }
51:
52: /**
53: * Ensures that each subarray in $currentFields corresponds to a JSON properties structure
54: * definition defined in some SortableWidget subclass.
55: *
56: * @param array $expectedFields The array with key => default value pairs
57: * @param array $currentFields The array to copy values from
58: * @return array
59: */
60: private function normalizeToWidgetJSONPropertiesStructures ($expectedFields, $currentFields) {
61: $fields = array ();
62: foreach ($currentFields as $key => $val) {
63: // widget class name can optionally be followed by a sequence of digits. This is
64: // used for widget cloning
65: $widgetClassName = preg_replace ("/_\w+$/", '', $key);
66: if (is_array ($val) && isset ($currentFields[$key]) &&
67: isset ($expectedFields[$widgetClassName]) &&
68: is_array ($expectedFields[$widgetClassName])) {
69:
70: // JSON property structure definitions can be nested
71: $fields[$key] = ArrayUtil::normalizeToArrayR (
72: $expectedFields[$widgetClassName], $currentFields[$key]);
73: }
74: }
75:
76: foreach ($expectedFields as $widgetClassName => $val) {
77: if (!$widgetClassName::$createByDefault) {
78: continue;
79: }
80:
81: if (!isset ($fields[$widgetClassName])) {
82: $fields[$widgetClassName] = $expectedFields[$widgetClassName];
83: }
84: }
85:
86: return $fields;
87: }
88:
89:
90: /**
91: * Returns an array defining the expected structure of the JSON-bearing
92: * attribute
93: * @return array
94: */
95: public function fields() {
96: if(!isset($this->_fields)) {
97: $this->_fields = array();
98:
99: // get expected fields from contents of widget directory
100: $that = $this;
101: $widgetClasses = array_map (function ($file) {
102: return preg_replace ('/\.php$/', '', $file);
103: }, array_filter (
104: scandir(Yii::getPathOfAlias($this->alias)), function ($file) use ($that){
105:
106: return preg_match ('/\.php$/', $file) &&
107: (!$that->whitelist ||
108: in_array (preg_replace ('/.php$/', '', $file), $that->whitelist));
109: }));
110:
111: // get JSON structure from widget class property
112: $unordered = array ();
113: foreach($widgetClasses as $widgetName) {
114: if (method_exists ($widgetName, 'getJSONPropertiesStructure')) {
115: $unordered[$widgetName] =
116: $widgetName::getJSONPropertiesStructure ();
117: }
118: }
119: $orderedFields = array ();
120: if ($this->widgetOrder ()) {
121: $widgetOrder = $this->widgetOrder ();
122: foreach ($widgetOrder as $widgetName) {
123: $orderedFields[$widgetName] = $unordered[$widgetName];
124: }
125: }
126: foreach (array_diff ($widgetClasses, array_keys ($orderedFields)) as $widgetName) {
127: $orderedFields[$widgetName] = $unordered[$widgetName];
128: }
129: $this->_fields = $orderedFields;
130: }
131: return $this->_fields;
132: }
133:
134: /**
135: * Removes fields which have JSON properties structures (for the purposes of array
136: * normalization) but which should not be saved
137: */
138: private function removeExcludedFields (&$attribute) {
139: // Templates Summary can be in saved json object but should not be added by default.
140: // This is because templates summaries can be created but don't exist by default
141: $excludeList = array (
142: 'TemplatesGridViewProfileWidget',
143: 'TransactionalViewWidget',
144: 'RecordViewWidget',
145: );
146: $attribute = array_diff_key ($attribute, array_flip ($excludeList));
147: }
148:
149: /**
150: * Normalize attribute to properties array structures defined in widget classes
151: * @return string
152: */
153: private $_attributes = null;
154: public function setAttributes ($values, $safeOnly=true){
155: $fields = $this->fields();
156: $attribute = is_array ($values) ?
157: $this->normalizeToWidgetJSONPropertiesStructures ($fields, $values) : $fields;
158: $this->removeExcludedFields ($attribute);
159: $this->_attributes = $attribute;
160: }
161:
162: /**
163: * Normalize attribute to properties array structures defined in widget classes
164: * @return $attribute
165: */
166: public function getAttributes ($names=null) {
167: $fields = $this->fields();
168: $exoAttr = $this->exoAttr;
169: $attribute = $this->_attributes;
170: $attribute = is_array ($attribute) ?
171: $this->normalizeToWidgetJSONPropertiesStructures ($fields, $attribute) : $fields;
172: $this->removeExcludedFields ($attribute);
173: return $attribute;
174: }
175:
176: }
177:
178: ?>
179: