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: /**
38: * This is the model class for table "x2_form_versions".
39: *
40: * @package application.models
41: * @property integer $id
42: * @property string $model
43: * @property string $version
44: * @property string $layout
45: * @property boolean $defaultView
46: * @property boolean $defaultForm
47: * @property integer $createDate
48: * @property integer $lastUpdated
49: */
50: class FormLayout extends CActiveRecord {
51:
52: public static $scenarios = array('Default','Inline');
53:
54: /**
55: * Returns the static model of the specified AR class.
56: * @return FormVersions the static model class
57: */
58: public static function model($className=__CLASS__) {
59: return parent::model($className);
60: }
61:
62: /**
63: * @return string the associated database table name
64: */
65: public function tableName() {
66: return 'x2_form_layouts';
67: }
68:
69: /**
70: * @return array validation rules for model attributes.
71: */
72: public function rules() {
73: // NOTE: you should only define rules for those attributes that
74: // will receive user inputs.
75: return array(
76: array('version, model', 'length', 'max'=>250),
77: array('createDate, lastUpdated', 'numerical', 'integerOnly'=>true),
78: array('defaultView, defaultForm', 'boolean'),
79: array('layout', 'safe'),
80: // The following rule is used by search().
81: // Please remove those attributes that should not be searched.
82: array('id, version, model, layout, defaultView, defaultForm, createDate, lastUpdated', 'safe', 'on'=>'search'),
83: );
84: }
85:
86: /**
87: * @return array relational rules.
88: */
89: public function relations() {
90: return array();
91: }
92:
93: /**
94: * @return array customized attribute labels (name=>label)
95: */
96: public function attributeLabels() {
97: return array(
98: 'id' => 'ID',
99: 'version' => 'Version',
100: 'model' => 'Model Name',
101: 'layout' => 'Layout',
102: 'defualtView' => 'Default View',
103: 'defualtForm' => 'Default Form',
104: 'createDate' => 'Create Date',
105: 'lastUpdated' => 'Last Updated',
106: );
107: }
108:
109: /**
110: * Retrieves a list of models based on the current search/filter conditions.
111: * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
112: */
113: public function search() {
114: // Warning: Please modify the following code to remove attributes that
115: // should not be searched.
116:
117: $criteria=new CDbCriteria;
118:
119: $criteria->compare('id',$this->id);
120: $criteria->compare('model',$this->model,true);
121: $criteria->compare('version',$this->version,true);
122: $criteria->compare('defaultView',$this->defaultView,true);
123: $criteria->compare('defaultForm',$this->defaultForm,true);
124: $criteria->compare('createDate',$this->createDate,true);
125: $criteria->compare('lastUpdated',$this->lastUpdated,true);
126:
127: return new CActiveDataProvider(get_class($this), array(
128: 'criteria'=>$criteria,
129: ));
130: }
131:
132: /**
133: * Returns fieldName, fieldLabel pairs for all fields for which the user has edit rights and
134: * which are present in the layout.
135: */
136: public function getEditableFieldsInLayout ($modelName) {
137: $editableFieldsFieldInfo = X2Model::model ($modelName)->getEditableFieldNames (false);
138:
139: // Construct criteria for finding the right form layout.
140: $attributes = array('model'=>ucfirst($modelName),'defaultForm'=>1);
141:
142: $layout = self::model()->findByAttributes($attributes);
143:
144: $layoutData = json_decode((isset($layout)? $layout->layout : X2Model::getDefaultFormLayout($modelName)),true);
145:
146: $editableFieldsInLayout = array ();
147: if(isset($layoutData['sections']) && count($layoutData['sections']) > 0) {
148: foreach($layoutData['sections'] as &$section) {
149: foreach($section['rows'] as &$row) {
150: if(isset($row['cols'])) {
151: foreach($row['cols'] as &$col) {
152: if(isset($col['items'])) {
153: foreach($col['items'] as &$item) {
154:
155: if(isset($item['name'],$item['labelType'],$item['readOnly'])) {
156: $fieldName = preg_replace('/^formItem_/u','',$item['name']);
157:
158: if(in_array (
159: $fieldName, array_keys ($editableFieldsFieldInfo))) {
160:
161: $editableFieldsInLayout[$fieldName] =
162: $editableFieldsFieldInfo[$fieldName];
163: }
164: }
165: }
166: }
167: }
168: }
169: }
170: }
171: }
172: return $editableFieldsInLayout;
173: }
174:
175: /**
176: * Helper method to unset all defaultView or defaultForm flags
177: * @param string $type Form type, either 'view' or 'form', or both if argument is omitted
178: * @param string Model type to unset flags for
179: */
180: public static function clearDefaultFormLayouts($type = null, $model = null, $scenario = null) {
181: // Construct attributes to select form layouts
182: $attr = array('model' => $model);
183: if ($scenario)
184: $attr['scenario'] = $scenario;
185: if ($type === 'view')
186: $attr['defaultView'] = 1;
187: else if ($type === 'form')
188: $attr['defaultForm'] = 1;
189: $layouts = FormLayout::model()->findAllByAttributes ($attr);
190:
191: foreach ($layouts as &$layout) {
192: if ($type === 'view')
193: $layout->defaultView = false;
194: else if ($type === 'form')
195: $layout->defaultForm = false;
196: else
197: $layout->defaultView = $layout->defaultForm = false;
198: $layout->save();
199: }
200: }
201: }
202: