]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/parser.modifylistview.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / parser.modifylistview.php
1 <?php
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-2012 SugarCRM Inc.
7  * 
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.
14  * 
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
18  * details.
19  * 
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
23  * 02110-1301 USA.
24  * 
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.
27  * 
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.
31  * 
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  ********************************************************************************/
38
39
40 require_once ('modules/ModuleBuilder/parsers/ModuleBuilderParser.php') ;
41 class ParserModifyListView extends ModuleBuilderParser
42 {
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 = '' )
51         {
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 ( ) ;
58
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'];
62                 //              _pp($loaded);
63                 $this->customFile = 'custom/modules/' . $this->module_name . '/metadata/listviewdefs.php' ;
64                 if (file_exists ( $this->customFile ))
65                 {
66                         $loaded = $this->_loadFromFile('ListView',$this->customFile,$this->module_name);
67                         $this->listViewDefs = $loaded['viewdefs'] [ $this->module_name ] ;
68                         $this->_variables = $loaded['variables'];
69                 } else
70                 {
71                         $this->listViewDefs = & $this->originalListViewDefs ;
72                 }
73
74                 $this->fixKeys ( $this->originalListViewDefs ) ;
75                 $this->fixKeys ( $this->listViewDefs ) ;
76                 $this->language_module = $this->module_name ;
77         }
78         
79         function getLanguage()
80         {
81             return $this->language_module;
82         }
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 )
85         {
86                 $temp = array ( ) ;
87                 foreach ( $defs as $key => $value )
88                 {
89                         if (! is_array ( $value ))
90                         {
91                                 $key = $value ;
92                                 $def = array ( ) ;
93                                 $def [ 'name' ] = (isset ( $this->module->field_defs [ $key ] )) ? $this->module->field_defs [ $key ] [ 'name' ] : $key ;
94                                 $value = $def ;
95                         }
96                         if (isset ( $value [ 'name' ] ))
97                         {
98                                 $key = $value [ 'name' ] ; // override key with name, needed when the entry lacks a key
99                         }
100                         $temp [ strtolower ( $key ) ] = $value ;
101                 }
102                 $defs = $temp ;
103         }
104         
105         /**
106          * returns the default fields for a listview
107          * Called only when displaying the listview for editing; not called when saving
108          */
109         function getDefaultFields ()
110         {
111                 $this->defaults = array ( ) ;
112                 foreach ( $this->listViewDefs as $key => $def )
113                 {
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' ] ))
117                         {
118                             if (! isset($def['studio']) || $def['studio'] === true)
119                             {
120                                 $this->defaults [ $key ] = $def ;
121                             }
122                             else
123                             // anything which doesn't go into the defaults is a reserved field - this makes sure we don't miss anything
124                             {
125                                 $this->reserved [ $key ] = $def;
126                             }
127                         }
128                 }
129                 return $this->defaults ;
130         }
131         /**
132          * returns additional fields available for users to create fields
133          */
134         function getAdditionalFields ()
135         {
136                 $this->additional = array ( ) ;
137                 foreach ( $this->listViewDefs as $key => $def )
138                 {
139                         if (empty ( $def [ 'default' ] ))
140                         {
141                                 $key = strtolower ( $key ) ;
142                                 $this->additional [ $key ] = $def ;
143                         }
144                 }
145                 return $this->additional ;
146         }
147         /**
148          * returns unused fields that are available for using in either default or additional list views
149          */
150         function getAvailableFields ()
151         {
152                 $this->availableFields = array ( ) ;
153                 $lowerFieldList = array_change_key_case ( $this->listViewDefs ) ;
154                 foreach ( $this->originalListViewDefs as $key => $def )
155                 {
156                         $key = strtolower ( $key ) ;
157                         if (! isset ( $lowerFieldList [ $key ] ))
158                         {
159                                 $this->availableFields [ $key ] = $def ;
160                         }
161                 }
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 )
165                 {
166                         $fieldName = strtolower ( $key ) ;
167             if ($fieldName == 'currency_id')
168                 continue;
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
170                         {
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 ) ;
175                                 }
176                         }
177                 }
178
179                 return $this->availableFields ;
180         }
181
182     function getFieldDefs()
183     {
184         return $this->module->field_defs;
185     }
186
187         
188         function isValidField($key, $def) {
189             //Allow fields that are studio visible  
190                 if (! empty ( $def [ 'studio' ] ) && $def [ 'studio' ] == 'visible')
191                   return true;
192                   
193                 //No ID fields
194                 if  ((!empty ( $def [ 'dbType' ] ) && $def [ 'dbType' ] == 'id') || (!empty ( $def [ 'type' ] ) && $def [ 'type' ] == 'id'))
195                   return false;
196                   
197                 //only allow DB and custom fields (if a source is specified)
198             if (!empty($def [ 'source' ]) && $def [ 'source' ] != 'db' && $def [ 'source' ] != 'custom_fields')
199                   return false;
200
201                 //Dont ever show the "deleted" fields or "_name" fields
202                 if (strcmp ( $key, 'deleted' ) == 0 || (isset ( $def [ 'name' ] ) && strpos ( $def [ 'name' ], "_name" ) !== false))
203                return false;
204
205             //If none of the "ifs" are true, the field is valid
206             return true;
207         }
208         
209         
210         function getField ( $fieldName )
211         {
212                 $fieldName = strtolower ( $fieldName ) ;
213                 foreach ( $this->listViewDefs as $key => $def )
214                 {
215                         $key = strtolower ( $key ) ;
216                         if ($key == $fieldName)
217                         {
218                                 return $def ;
219                         }
220                 }
221                 foreach ( $this->module->field_defs as $key => $def )
222                 {
223                         $key = strtolower ( $key ) ;
224                         if ($key == $fieldName)
225                         {
226                                 return $def ;
227                         }
228                 }
229                 return array ( ) ;
230         }
231         function addRelateData($fieldname, $listfielddef) {
232                 $modFieldDef = $this->module->field_defs [ strtolower ( $fieldname ) ];
233                 if (!empty($modFieldDef['module']) && !empty($modFieldDef['id_name'])) {
234                         $listfielddef['module'] = $modFieldDef['module'];
235                         $listfielddef['id'] = strtoupper($modFieldDef['id_name']);
236                         $listfielddef['link'] = true;
237                         $listfielddef['related_fields'] = array (strtolower($modFieldDef['id_name']));
238                 }
239                 return $listfielddef;
240         }
241         function _loadLayoutFromRequest ()
242         {
243             $GLOBALS['log']->debug("ParserModifyListView->_loadLayoutFromRequest()");
244                 $fields = array ( ) ;
245                 $rejectTypes = array ( 'html' , 'enum' , 'text' ) ;
246                 for ( $i = 0 ; isset ( $_POST [ 'group_' . $i ] ) && $i < 2 ; $i ++ )
247                 {
248                         //echo "\n***group-$i Size:".sizeof($_POST['group_' . $i])."\n";
249                         foreach ( $_POST [ 'group_' . $i ] as $field )
250                         {
251                                 $fieldname = strtoupper ( $field ) ;
252                                 //originalListViewDefs are all lower case
253                                 $lowerFieldName = strtolower ( $field ) ;
254                                 if (isset ( $this->originalListViewDefs [ $lowerFieldName ] ))
255                                 {
256                                         $fields [ $fieldname ] = $this->originalListViewDefs [ $lowerFieldName ] ;
257                                 } else
258                                 {
259                                         //check if we have the case wrong for custom fields
260                                         if (! isset ( $this->module->field_defs [ $fieldname ] ))
261                                         {
262                                                 foreach ( $this->module->field_defs as $key => $value )
263                                                 {
264                                                         if (strtoupper ( $key ) == $fieldname)
265                                                         {
266                                                                 $fields [ $fieldname ] = array ( 'width' => 10 , 'label' => $this->module->field_defs [ $key ] [ 'vname' ] ) ;
267                                                                 break ;
268                                                         }
269                                                 }
270                                         } else
271                                         {
272                                                 $fields [ $fieldname ] = array ( 'width' => 10 , 'label' => $this->module->field_defs [ $fieldname ] [ 'vname' ] ) ;
273                                         }
274                                         // sorting fields of certain types will cause a database engine problems
275                                         // we only check this for custom fields, as we assume that OOB fields have been independently confirmed as ok
276                                         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'])))
277                                         {
278                                                 $fields [ $fieldname ] [ 'sortable' ] = false ;
279                                         }
280                                         // Bug 23728 - Make adding a currency type field default to setting the 'currency_format' to true
281                                         if (isset ( $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type ' ] ) && $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ] == 'currency') 
282                                         {
283                                                 $fields [ $fieldname ] [ 'currency_format' ] = true;
284                                         }
285                                 }
286                                 if (isset ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ] ))
287                                 {
288                                         $width = substr ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ], 6, 3 ) ;
289                                         if (strpos ( $width, "%" ) !== false)
290                                         {
291                                                 $width = substr ( $width, 0, 2 ) ;
292                                         }
293                                         if ($width < 101 && $width > 0)
294                                         {
295                                                 $fields [ $fieldname ] [ 'width' ] = $width ;
296                                         }
297                                 } else if (isset ( $this->listViewDefs [ $fieldname ] [ 'width' ] ))
298                                 {
299                                         $fields [ $fieldname ] [ 'width' ] = $this->listViewDefs [ $fieldname ] [ 'width' ] ;
300                                 }
301                                 //Get additional Data for relate fields
302                                 if (isset($this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ]) && $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ] == 'relate') {
303                                         $fields [ $fieldname ] = $this->addRelateData($field, $fields [ $fieldname ]);
304                                 }
305                                 $fields [ $fieldname ] [ 'default' ] = ($i == 0) ;
306                         }
307                 }
308                 // Add the reserved fields back in to the end of the default fields in the layout
309                 // ASSUMPTION: reserved fields go back at the end
310                 // First, load the reserved fields - we cannot assume that getDefaultFields has been called earlier when saving
311                 $this->getDefaultFields();
312                 foreach ( $this->reserved as $key => $def)
313                 {
314                     $fields[ $key ] = $def;
315                 }
316                 
317                 return $fields ;
318         }
319         function handleSave ()
320         {
321                 $fields = $this->_loadLayoutFromRequest();
322                 $this->_writeToFile($this->customFile,'ListView',$this->module_name,$fields,$this->_variables);
323
324                 $GLOBALS [ "listViewDefs" ] [ $this->module_name ] = $fields ;
325                 // now clear the cache so that the results are immediately visible
326                 include_once ('include/TemplateHandler/TemplateHandler.php') ;
327                 TemplateHandler::clearCache ( $this->module_name, "ListView.tpl" ) ; // not currently cached, but here for the future
328         }
329 }
330 ?>