2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
39 require_once 'modules/ModuleBuilder/parsers/views/AbstractMetaDataParser.php' ;
40 require_once 'modules/ModuleBuilder/parsers/views/MetaDataParserInterface.php' ;
42 class ListLayoutMetaDataParser extends AbstractMetaDataParser implements MetaDataParserInterface
45 // Columns is used by the view to construct the listview - each column is built by calling the named function
46 public $columns = array ( 'LBL_DEFAULT' => 'getDefaultFields' , 'LBL_AVAILABLE' => 'getAdditionalFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
47 protected $labelIdentifier = 'label' ; // labels in the listviewdefs.php are tagged 'label' =>
48 protected $allowParent = false;
51 * Simple function for array_udiff_assoc function call in getAvailableFields()
53 static function getArrayDiff ($one , $two)
56 foreach($one as $key => $value)
58 if (!isset($two[$key]))
60 $retArray[$key] = $value;
68 * @param string view The view type, that is, editview, searchview etc
69 * @param string moduleName The name of the module to which this listview belongs
70 * @param string packageName If not empty, the name of the package to which this listview belongs
72 function __construct ($view , $moduleName , $packageName = '')
74 $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . ": __construct()" ) ;
77 $views = array ( MB_LISTVIEW, MB_DASHLET, MB_DASHLETSEARCH, MB_POPUPLIST, MB_POPUPSEARCH ) ;
78 if (! in_array ( $view , $views ) )
80 sugar_die ( "ListLayoutMetaDataParser: View $view is not supported" ) ;
84 if (empty ( $packageName ))
86 require_once 'modules/ModuleBuilder/parsers/views/DeployedMetaDataImplementation.php' ;
87 $this->implementation = new DeployedMetaDataImplementation ( $view, $moduleName ) ;
90 require_once 'modules/ModuleBuilder/parsers/views/UndeployedMetaDataImplementation.php' ;
91 $this->implementation = new UndeployedMetaDataImplementation ( $view, $moduleName, $packageName ) ;
95 $this->_fielddefs = $this->implementation->getFielddefs () ;
96 $this->_standardizeFieldLabels( $this->_fielddefs );
97 $this->_viewdefs = array_change_key_case ( $this->implementation->getViewdefs () ) ; // force to lower case so don't have problems with case mismatches later
103 * @param boolean $populate If true (default), then update the layout first with new layout information from the $_REQUEST array
105 function handleSave ($populate = true)
108 $this->_populateFromRequest () ;
109 $this->implementation->deploy ( array_change_key_case ( $this->_viewdefs, CASE_UPPER ) ) ; // force the field names back to upper case so the list view will work correctly
112 function getLayout ()
114 return $this->_viewdefs ;
118 * Return a list of the default fields for a listview
119 * @return array List of default fields as an array, where key = value = <field name>
121 function getDefaultFields ()
123 $defaultFields = array ( ) ;
124 foreach ( $this->_viewdefs as $key => $def )
126 // add in the default fields from the listviewdefs but hide fields disabled in the listviewdefs.
127 if (! empty ( $def [ 'default' ] ) && (!isset($def['enabled']) || $def['enabled'] != false)
128 && (!isset($def [ 'studio' ]) || ($def [ 'studio' ] !== false && $def [ 'studio' ] != "false")))
130 if (isset($this->_fielddefs [ $key ] )) {
131 $defaultFields [ $key ] = self::_trimFieldDefs ( $this->_fielddefs [ $key ] ) ;
132 if (!empty($def['label']))
133 $defaultFields [ $key ]['label'] = $def['label'];
136 $defaultFields [ $key ] = $def;
141 return $defaultFields ;
145 * Returns additional fields available for users to create fields
146 @return array List of additional fields as an array, where key = value = <field name>
148 function getAdditionalFields ()
150 $additionalFields = array ( ) ;
151 foreach ( $this->_viewdefs as $key => $def )
154 if(strtolower ( $key ) == 'email_opt_out'){
158 if (empty ( $def [ 'default' ] ))
160 if (isset($this->_fielddefs [ $key ] ))
161 $additionalFields [ $key ] = self::_trimFieldDefs ( $this->_fielddefs [ $key ] ) ;
163 $additionalFields [ $key ] = $def;
166 return $additionalFields ;
170 * Returns unused fields that are available for use in either default or additional list views
171 * @return array List of available fields as an array, where key = value = <field name>
173 function getAvailableFields ()
175 $availableFields = array ( ) ;
176 // Select available fields from the field definitions - don't need to worry about checking if ok to include as the Implementation has done that already in its constructor
177 foreach ( $this->_fielddefs as $key => $def )
179 if ($this->isValidField($key, $def) && !isset($this->_viewdefs[$key]))
180 $availableFields [ $key ] = self::_trimFieldDefs( $this->_fielddefs [ $key ] ) ;
182 $origDefs = $this->getOriginalViewDefs();
183 foreach($origDefs as $key => $def)
185 if (!isset($this->_viewdefs[$key]) ||
186 (isset($this->_viewdefs[$key]['enabled']) && $this->_viewdefs[$key]['enabled'] == false))
187 $availableFields [ $key ] = $def;
190 return $availableFields;
193 public function isValidField($key, $def)
195 //Studio invisible fields should always be hidden
196 if (! empty ($def[ 'studio' ] ) )
198 if (is_array($def [ 'studio' ]))
200 if (isset($def [ 'studio' ]['listview'])) {
201 $lv = $def [ 'studio' ]['listview'];
202 return $lv !== false && (!is_string($lv) || $lv != 'false');
204 if (isset($def [ 'studio' ]['visible']))
205 return $def [ 'studio' ]['visible'];
208 return ($def [ 'studio' ] != 'false' && $def [ 'studio' ] !== false && $def [ 'studio' ] != 'hidden') ;
212 //Bug 32520. We need to dissalow currency_id fields on list views.
213 //This should be removed once array based studio definitions are in.
214 if (isset($def['type']) && $def['type'] == "id" && $def['name'] == 'currency_id')
218 if (isset($def['dbType']) && $def['dbType'] == "id")
223 if (isset($def['type']))
225 if ($def['type'] == 'html' || ($def['type'] == 'parent' && !$this->allowParent)
226 || $def['type'] == "id" || $def['type'] == "link" || $def['type'] == 'image')
230 //hide currency_id, deleted, and _name fields by key-name
231 if(strcmp ( $key, 'deleted' ) == 0 ) {
235 //if all the tests failed, the field is probably ok
239 protected function _populateFromRequest ()
241 $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->populateFromRequest() - fielddefs = ".print_r($this->_fielddefs, true));
242 // Transfer across any reserved fields, that is, any where studio !== true, which are not editable but must be preserved
243 $newViewdefs = array ( ) ;
244 $rejectTypes = array ( 'html'=>'html' , 'enum'=>'enum' , 'text'=>'text', 'encrypt'=>'encrypt' ) ;
246 $originalViewDefs = $this->getOriginalViewDefs();
248 foreach ( $this->_viewdefs as $key => $def )
250 //If the field is on the layout, but studio disabled, put it back on the layout at the front
251 if (isset ($def['studio']) && (
252 (is_array($def['studio']) && isset($def['studio']['listview']) &&
253 ($def['studio']['listview'] === false || strtolower($def['studio']['listview']) == 'false'
254 || strtolower($def['studio']['listview']) == 'required')
256 || (!is_array($def['studio']) &&
257 ($def [ 'studio' ] === false || strtolower($def['studio']) == 'false' || strtolower($def['studio']) == 'required'))
260 $newViewdefs [ $key ] = $def ;
263 // only take items from group_0 for searchviews (basic_search or advanced_search) and subpanels (which both are missing the Available column) - take group_0, _1 and _2 for all other list views
264 $lastGroup = (isset ( $this->columns [ 'LBL_AVAILABLE' ] )) ? 2 : 1 ;
266 for ( $i = 0 ; isset ( $_POST [ 'group_' . $i ] ) && $i < $lastGroup ; $i ++ )
268 foreach ( $_POST [ 'group_' . $i ] as $fieldname )
270 $fieldname = strtolower ( $fieldname ) ;
271 //Check if the field was previously on the layout
272 if (isset ($this->_viewdefs[$fieldname])) {
273 $newViewdefs [ $fieldname ] = $this->_viewdefs[$fieldname];
274 // print_r($this->_viewdefs[ $fieldname ]);
276 //Next check if the original view def contained it
277 else if (isset($originalViewDefs[ $fieldname ]))
279 $newViewdefs [ $fieldname ] = $originalViewDefs[ $fieldname ];
281 //create a definition from the fielddefs
284 // if we don't have a valid fieldname then just ignore it and move on...
285 if ( ! isset ( $this->_fielddefs [ $fieldname ] ) )
288 $newViewdefs [ $fieldname ] = $this->_trimFieldDefs($this->_fielddefs [ $fieldname ]) ;
289 // sorting fields of certain types will cause a database engine problems
290 if ( isset($this->_fielddefs[$fieldname]['type']) &&
291 isset ( $rejectTypes [ $this->_fielddefs [ $fieldname ] [ 'type' ] ] ))
293 $newViewdefs [ $fieldname ] [ 'sortable' ] = false ;
296 // Bug 23728 - Make adding a currency type field default to setting the 'currency_format' to true
297 if (isset ( $this->_fielddefs [ $fieldname ] [ 'type' ]) && $this->_fielddefs [ $fieldname ] [ 'type' ] == 'currency')
299 $newViewdefs [ $fieldname ] [ 'currency_format' ] = true;
302 if (isset($newViewdefs [ $fieldname ]['enabled']))
303 $newViewdefs [ $fieldname ]['enabled'] = true;
305 if (isset ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ] ))
307 $width = substr ( $_REQUEST [ $fieldname . 'width' ], 6, 3 ) ;
308 if (strpos ( $width, "%" ) != false)
310 $width = substr ( $width, 0, 2 ) ;
312 if (!($width < 101 && $width > 0))
316 $newViewdefs [ $fieldname ] [ 'width' ] = $width."%" ;
317 } else if (isset ( $this->_viewdefs [ $fieldname ] [ 'width' ] ))
319 $newViewdefs [ $fieldname ] [ 'width' ] = $this->_viewdefs [ $fieldname ] [ 'width' ] ;
322 $newViewdefs [ $fieldname ] [ 'width' ] = "10%";
325 $newViewdefs [ $fieldname ] [ 'default' ] = ($i == 0) ;
328 $this->_viewdefs = $newViewdefs ;
333 * Remove all instances of a field from the layout
334 * @param string $fieldName Name of the field to remove
335 * @return boolean True if the field was removed; false otherwise
337 function removeField ($fieldName)
339 if (isset ( $this->_viewdefs [ $fieldName ] ))
341 unset( $this->_viewdefs [ $fieldName ] ) ;
347 function getOriginalViewDefs() {
348 $defs = $this->implementation->getOriginalViewdefs ();
350 foreach ($defs as $field => $def)
352 $out[strtolower($field)] = $def;
358 static function _trimFieldDefs ( $def )
360 if ( isset ( $def [ 'vname' ] ) )
361 $def [ 'label' ] = $def [ 'vname' ] ;
362 return array_intersect_key ( $def , array ( 'type' => true, 'studio' => true , 'label' => true , 'width' => true , 'sortable' => true , 'related_fields' => true , 'default' => true , 'link' => true , 'align' => true , 'orderBy' => true ,'hideLabel' => true, 'customLable' => true , 'currency_format' => true ) ) ;