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: class MobileLayouts extends CActiveRecord {
38:
39: public static function model($className=__CLASS__)
40: {
41: return parent::model($className);
42: }
43:
44: // private $_layoutModel;
45: // public function getLayoutModel () {
46: // if (!isset ($this->_layoutModel)) {
47: // $modelName = $this->modelName;
48: // if (!is_subclass_of ($modelName, 'X2Model')) return null;
49: // $this->_layoutModel = $modelName::model ();
50: // }
51: // return $this->_layoutModel;
52: // }
53:
54: /**
55: * Generates a default layout from the desktop layout if available, or from the model's
56: * fields otherwise.
57: * @param string $type 'form' or 'view'
58: * @param string $modelName
59: * @return array
60: */
61: public static function generateDefaultLayout ($type, $modelName) {
62: if ($type === 'form') {
63: $layout = FormLayout::model()->findByAttributes(
64: array(
65: 'model' => ucfirst($modelName),
66: 'defaultForm' => 1,
67: 'scenario' => 'Default'
68: ));
69: } else {
70: $layout = FormLayout::model()->findByAttributes(
71: array(
72: 'model' => ucfirst($modelName),
73: 'defaultView' => 1,
74: 'scenario' => 'Default'
75: ));
76: }
77: $layoutData = array ();
78: if ($layout) {
79: $layout = CJSON::decode ($layout->layout);
80: if (isset ($layout['sections'])) {
81: foreach ($layout['sections'] as $section) {
82: foreach ($section['rows'] as $row) {
83: foreach ($row['cols'] as $col) {
84: foreach ($col['items'] as $item) {
85: if (isset ($item['name'])) {
86: $fieldName = preg_replace(
87: '/^formItem_/u', '', $item['name']);
88: $layoutData[] = $fieldName;
89: }
90: }
91: }
92: }
93: }
94: }
95: } elseif (is_subclass_of ($modelName, 'X2Model')) {
96: $layoutData = Yii::app()->db->createCommand ()
97: ->select ('fieldName')
98: ->from ('x2_fields')
99: ->where ('modelName=:modelName', array (':modelName' => $modelName))
100: ->andWhere ($type==='view' ? 'readOnly' : 'true')
101: ->queryColumn ();
102: }
103: return $layoutData;
104: }
105:
106:
107: public static function getFieldOptions (array $layout, $modelName) {
108: // format layout options
109: $model = new $modelName;
110: $labelled = array ();
111: foreach ($layout as $fieldName) {
112: $field = $model->getField ($fieldName);
113: if ($field) {
114: $labelled[$fieldName] = $field->attributeLabel;
115: }
116: }
117: $layout = $labelled;
118:
119: // get all field options
120: $fields = Fields::model()->findAllByAttributes(
121: array('modelName' => $modelName),
122: new CDbCriteria(
123: array(
124: 'order' => 'attributeLabel ASC',
125: 'condition' => 'keyType IS NULL OR (keyType!="PRI" AND keyType!="FIX")',
126: )));
127:
128: // format field options
129: $labelled = array ();
130: foreach ($fields as $field) {
131: $labelled[$field->fieldName] = $field->attributeLabel;
132: }
133: $fields = $labelled;
134:
135: $unselected = array_diff_key ($fields, $layout);
136: return array ($layout, $unselected);
137: }
138:
139: public static function getMobileLayout ($modelName, $type='view') {
140: $mobileLayout = self::model ()->findByAttributes (array (
141: 'modelName' => $modelName ,
142: 'defaultView' => $type !== 'form',
143: 'defaultForm' => $type === 'form',
144: ));
145: return $mobileLayout;
146: }
147:
148: public function attributeLabels () {
149: return array (
150: 'modelName' => Yii::t('mobile', 'Record Type'),
151: 'defaultView' => Yii::t('mobile', 'Default View Layout'),
152: 'defaultForm' => Yii::t('mobile', 'Default Form Layout'),
153: 'layout' => Yii::t('mobile', 'Fields'),
154: );
155: }
156:
157: public function tableName () {
158: return 'x2_mobile_layouts';
159: }
160:
161: public function behaviors () {
162: return array_merge (parent::behaviors (), array (
163: 'JSONFieldsBehavior' => array (
164: 'class' => 'application.components.JSONFieldsBehavior',
165: 'transformAttributes' => array (
166: 'layout',
167: ),
168: ),
169: ));
170: }
171:
172: }
173:
174: ?>
175: