2 if (! defined ( 'sugarEntry' ) || ! sugarEntry)
3 die ( 'Not A Valid Entry Point' ) ;
4 /*********************************************************************************
5 * SugarCRM Community Edition is a customer relationship management program developed by
6 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Affero General Public License version 3 as published by the
10 * Free Software Foundation with the addition of the following permission added
11 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
20 * You should have received a copy of the GNU Affero General Public License along with
21 * this program; if not, see http://www.gnu.org/licenses or write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
28 * The interactive user interfaces in modified source and object code versions
29 * of this program must display Appropriate Legal Notices, as required under
30 * Section 5 of the GNU Affero General Public License version 3.
32 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33 * these Appropriate Legal Notices must retain the display of the "Powered by
34 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35 * technical reasons, the Appropriate Legal Notices must display the words
36 * "Powered by SugarCRM".
37 ********************************************************************************/
40 require_once ('modules/ModuleBuilder/parsers/ModuleBuilderParser.php') ;
41 class ParserModifyListView extends ModuleBuilderParser
43 var $listViewDefs = false ;
44 var $defaults = array ( ) ;
45 var $additional = array ( ) ;
46 var $available = array ( ) ;
47 var $reserved = array(); // fields marked by 'studio'=>false in the listviewdefs; need to be preserved
48 // var $language_module = '';
49 var $columns = array ( 'LBL_DEFAULT' => 'getDefaultFields' , 'LBL_AVAILABLE' => 'getAdditionalFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
50 function init ( $module_name , $submodule = '' )
52 global $app_list_strings ;
53 $this->module_name = $module_name ;
54 $mod_strings = return_module_language ( $GLOBALS [ 'current_language' ], $this->module_name ) ; // needed solely so that listviewdefs that reference this can be included without error
55 $class = $GLOBALS [ 'beanList' ] [ $this->module_name ] ;
56 require_once ($GLOBALS [ 'beanFiles' ] [ $class ]) ;
57 $this->module = new $class ( ) ;
59 $loaded = $this->_loadFromFile('ListView','modules/' . $this->module_name . '/metadata/listviewdefs.php',$this->module_name);
60 $this->originalListViewDefs = $loaded['viewdefs'] [ $this->module_name ] ;
61 $this->_variables = $loaded['variables'];
63 $this->customFile = 'custom/modules/' . $this->module_name . '/metadata/listviewdefs.php' ;
64 if (file_exists ( $this->customFile ))
66 $loaded = $this->_loadFromFile('ListView',$this->customFile,$this->module_name);
67 $this->listViewDefs = $loaded['viewdefs'] [ $this->module_name ] ;
68 $this->_variables = $loaded['variables'];
71 $this->listViewDefs = & $this->originalListViewDefs ;
74 $this->fixKeys ( $this->originalListViewDefs ) ;
75 $this->fixKeys ( $this->listViewDefs ) ;
76 $this->language_module = $this->module_name ;
79 function getLanguage()
81 return $this->language_module;
83 // re-key array so that every entry has a key=name and all keys are lowercase - makes it easier in handleSave() later...
84 function fixKeys ( &$defs )
87 foreach ( $defs as $key => $value )
89 if (! is_array ( $value ))
93 $def [ 'name' ] = (isset ( $this->module->field_defs [ $key ] )) ? $this->module->field_defs [ $key ] [ 'name' ] : $key ;
96 if (isset ( $value [ 'name' ] ))
98 $key = $value [ 'name' ] ; // override key with name, needed when the entry lacks a key
100 $temp [ strtolower ( $key ) ] = $value ;
106 * returns the default fields for a listview
107 * Called only when displaying the listview for editing; not called when saving
109 function getDefaultFields ()
111 $this->defaults = array ( ) ;
112 foreach ( $this->listViewDefs as $key => $def )
114 // add in the default fields from the listviewdefs, stripping out any field with 'studio' set to a value other than true
115 // Important: the 'studio' fields must be added back into the layout on save, as they're not editable rather than hidden
116 if (! empty ( $def [ 'default' ] ))
118 if (! isset($def['studio']) || $def['studio'] === true)
120 $this->defaults [ $key ] = $def ;
123 // anything which doesn't go into the defaults is a reserved field - this makes sure we don't miss anything
125 $this->reserved [ $key ] = $def;
129 return $this->defaults ;
132 * returns additional fields available for users to create fields
134 function getAdditionalFields ()
136 $this->additional = array ( ) ;
137 foreach ( $this->listViewDefs as $key => $def )
139 if (empty ( $def [ 'default' ] ))
141 $key = strtolower ( $key ) ;
142 $this->additional [ $key ] = $def ;
145 return $this->additional ;
148 * returns unused fields that are available for using in either default or additional list views
150 function getAvailableFields ()
152 $this->availableFields = array ( ) ;
153 $lowerFieldList = array_change_key_case ( $this->listViewDefs ) ;
154 foreach ( $this->originalListViewDefs as $key => $def )
156 $key = strtolower ( $key ) ;
157 if (! isset ( $lowerFieldList [ $key ] ))
159 $this->availableFields [ $key ] = $def ;
162 $GLOBALS['log']->debug('parser.modifylistview.php->getAvailableFields(): field_defs='.print_r($this->availableFields,true));
163 $modFields = !empty($this->module->field_name_map) ? $this->module->field_name_map : $this->module->field_defs;
164 foreach ( $modFields as $key => $def )
166 $fieldName = strtolower ( $key ) ;
167 if ($fieldName == 'currency_id')
169 if (!isset ( $lowerFieldList [ $fieldName ] )) // bug 16728 - check this first, so that other conditions (e.g., studio == visible) can't override and add duplicate entries
171 // bug 19656: this test changed after 5.0.0b - we now remove all ID type fields - whether set as type, or dbtype, from the fielddefs
172 if ($this->isValidField($key, $def)){
173 $label = (isset ( $def [ 'vname' ] )) ? $def [ 'vname' ] : (isset($def [ 'label' ]) ? $def['label'] : $def['name']) ;
174 $this->availableFields [ $fieldName ] = array ( 'width' => '10' , 'label' => $label ) ;
179 return $this->availableFields ;
182 function getFieldDefs()
184 return $this->module->field_defs;
188 function isValidField($key, $def) {
189 //Allow fields that are studio visible
190 if (! empty ( $def [ 'studio' ] ) && $def [ 'studio' ] == 'visible')
194 if ((!empty ( $def [ 'dbType' ] ) && $def [ 'dbType' ] == 'id') || (!empty ( $def [ 'type' ] ) && $def [ 'type' ] == 'id'))
197 //only allow DB and custom fields (if a source is specified)
198 if (!empty($def [ 'source' ]) && $def [ 'source' ] != 'db' && $def [ 'source' ] != 'custom_fields')
201 //Dont ever show the "deleted" fields or "_name" fields
202 if (strcmp ( $key, 'deleted' ) == 0 || (isset ( $def [ 'name' ] ) && strpos ( $def [ 'name' ], "_name" ) !== false))
206 //If none of the "ifs" are true, the field is valid
211 function getField ( $fieldName )
213 $fieldName = strtolower ( $fieldName ) ;
214 foreach ( $this->listViewDefs as $key => $def )
216 $key = strtolower ( $key ) ;
217 if ($key == $fieldName)
222 foreach ( $this->module->field_defs as $key => $def )
224 $key = strtolower ( $key ) ;
225 if ($key == $fieldName)
232 function addRelateData($fieldname, $listfielddef) {
233 $modFieldDef = $this->module->field_defs [ strtolower ( $fieldname ) ];
234 if (!empty($modFieldDef['module']) && !empty($modFieldDef['id_name'])) {
235 $listfielddef['module'] = $modFieldDef['module'];
236 $listfielddef['id'] = strtoupper($modFieldDef['id_name']);
237 $listfielddef['link'] = true;
238 $listfielddef['related_fields'] = array (strtolower($modFieldDef['id_name']));
240 return $listfielddef;
242 function _loadLayoutFromRequest ()
244 $GLOBALS['log']->debug("ParserModifyListView->_loadLayoutFromRequest()");
245 $fields = array ( ) ;
246 $rejectTypes = array ( 'html' , 'enum' , 'text' ) ;
247 for ( $i = 0 ; isset ( $_POST [ 'group_' . $i ] ) && $i < 2 ; $i ++ )
249 //echo "\n***group-$i Size:".sizeof($_POST['group_' . $i])."\n";
250 foreach ( $_POST [ 'group_' . $i ] as $field )
252 $fieldname = strtoupper ( $field ) ;
253 //originalListViewDefs are all lower case
254 $lowerFieldName = strtolower ( $field ) ;
255 if (isset ( $this->originalListViewDefs [ $lowerFieldName ] ))
257 $fields [ $fieldname ] = $this->originalListViewDefs [ $lowerFieldName ] ;
260 //check if we have the case wrong for custom fields
261 if (! isset ( $this->module->field_defs [ $fieldname ] ))
263 foreach ( $this->module->field_defs as $key => $value )
265 if (strtoupper ( $key ) == $fieldname)
267 $fields [ $fieldname ] = array ( 'width' => 10 , 'label' => $this->module->field_defs [ $key ] [ 'vname' ] ) ;
273 $fields [ $fieldname ] = array ( 'width' => 10 , 'label' => $this->module->field_defs [ $fieldname ] [ 'vname' ] ) ;
275 // sorting fields of certain types will cause a database engine problems
276 // we only check this for custom fields, as we assume that OOB fields have been independently confirmed as ok
277 if (isset ( $this->module->field_defs [ strtolower ( $fieldname ) ] ) && (in_array ( $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ], $rejectTypes ) || isset($this->module->field_defs [ strtolower ( $fieldname ) ]['custom_module'])))
279 $fields [ $fieldname ] [ 'sortable' ] = false ;
281 // Bug 23728 - Make adding a currency type field default to setting the 'currency_format' to true
282 if (isset ( $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type ' ] ) && $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ] == 'currency')
284 $fields [ $fieldname ] [ 'currency_format' ] = true;
287 if (isset ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ] ))
289 $width = substr ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ], 6, 3 ) ;
290 if (strpos ( $width, "%" ) != false)
292 $width = substr ( $width, 0, 2 ) ;
294 if ($width < 101 && $width > 0)
296 $fields [ $fieldname ] [ 'width' ] = $width ;
298 } else if (isset ( $this->listViewDefs [ $fieldname ] [ 'width' ] ))
300 $fields [ $fieldname ] [ 'width' ] = $this->listViewDefs [ $fieldname ] [ 'width' ] ;
302 //Get additional Data for relate fields
303 if (isset($this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ]) && $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ] == 'relate') {
304 $fields [ $fieldname ] = $this->addRelateData($field, $fields [ $fieldname ]);
306 $fields [ $fieldname ] [ 'default' ] = ($i == 0) ;
309 // Add the reserved fields back in to the end of the default fields in the layout
310 // ASSUMPTION: reserved fields go back at the end
311 // First, load the reserved fields - we cannot assume that getDefaultFields has been called earlier when saving
312 $this->getDefaultFields();
313 foreach ( $this->reserved as $key => $def)
315 $fields[ $key ] = $def;
320 function handleSave ()
322 $fields = $this->_loadLayoutFromRequest();
323 $this->_writeToFile($this->customFile,'ListView',$this->module_name,$fields,$this->_variables);
325 $GLOBALS [ "listViewDefs" ] [ $this->module_name ] = $fields ;
326 // now clear the cache so that the results are immediately visible
327 include_once ('include/TemplateHandler/TemplateHandler.php') ;
328 TemplateHandler::clearCache ( $this->module_name, "ListView.tpl" ) ; // not currently cached, but here for the future