]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/parser.modifylistview.php
Release 6.1.4
[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 is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2011 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 isValidField($key, $def) {
183             //Allow fields that are studio visible  
184                 if (! empty ( $def [ 'studio' ] ) && $def [ 'studio' ] == 'visible')
185                   return true;
186                   
187                 //No ID fields
188                 if  ((!empty ( $def [ 'dbType' ] ) && $def [ 'dbType' ] == 'id') || (!empty ( $def [ 'type' ] ) && $def [ 'type' ] == 'id'))
189                   return false;
190                   
191                 //only allow DB and custom fields (if a source is specified)
192             if (!empty($def [ 'source' ]) && $def [ 'source' ] != 'db' && $def [ 'source' ] != 'custom_fields')
193                   return false;
194
195                 //Dont ever show the "deleted" fields or "_name" fields
196                 if (strcmp ( $key, 'deleted' ) == 0 || (isset ( $def [ 'name' ] ) && strpos ( $def [ 'name' ], "_name" ) !== false))
197                return false;
198
199             _pp($def); 
200             //If none of the "ifs" are true, the field is valid
201             return true;
202         }
203         
204         
205         function getField ( $fieldName )
206         {
207                 $fieldName = strtolower ( $fieldName ) ;
208                 foreach ( $this->listViewDefs as $key => $def )
209                 {
210                         $key = strtolower ( $key ) ;
211                         if ($key == $fieldName)
212                         {
213                                 return $def ;
214                         }
215                 }
216                 foreach ( $this->module->field_defs as $key => $def )
217                 {
218                         $key = strtolower ( $key ) ;
219                         if ($key == $fieldName)
220                         {
221                                 return $def ;
222                         }
223                 }
224                 return array ( ) ;
225         }
226         function addRelateData($fieldname, $listfielddef) {
227                 $modFieldDef = $this->module->field_defs [ strtolower ( $fieldname ) ];
228                 if (!empty($modFieldDef['module']) && !empty($modFieldDef['id_name'])) {
229                         $listfielddef['module'] = $modFieldDef['module'];
230                         $listfielddef['id'] = strtoupper($modFieldDef['id_name']);
231                         $listfielddef['link'] = true;
232                         $listfielddef['related_fields'] = array (strtolower($modFieldDef['id_name']));
233                 }
234                 return $listfielddef;
235         }
236         function _loadLayoutFromRequest ()
237         {
238             $GLOBALS['log']->debug("ParserModifyListView->_loadLayoutFromRequest()");
239                 $fields = array ( ) ;
240                 $rejectTypes = array ( 'html' , 'enum' , 'text' ) ;
241                 for ( $i = 0 ; isset ( $_POST [ 'group_' . $i ] ) && $i < 2 ; $i ++ )
242                 {
243                         //echo "\n***group-$i Size:".sizeof($_POST['group_' . $i])."\n";
244                         foreach ( $_POST [ 'group_' . $i ] as $field )
245                         {
246                                 $fieldname = strtoupper ( $field ) ;
247                                 //originalListViewDefs are all lower case
248                                 $lowerFieldName = strtolower ( $field ) ;
249                                 if (isset ( $this->originalListViewDefs [ $lowerFieldName ] ))
250                                 {
251                                         $fields [ $fieldname ] = $this->originalListViewDefs [ $lowerFieldName ] ;
252                                 } else
253                                 {
254                                         //check if we have the case wrong for custom fields
255                                         if (! isset ( $this->module->field_defs [ $fieldname ] ))
256                                         {
257                                                 foreach ( $this->module->field_defs as $key => $value )
258                                                 {
259                                                         if (strtoupper ( $key ) == $fieldname)
260                                                         {
261                                                                 $fields [ $fieldname ] = array ( 'width' => 10 , 'label' => $this->module->field_defs [ $key ] [ 'vname' ] ) ;
262                                                                 break ;
263                                                         }
264                                                 }
265                                         } else
266                                         {
267                                                 $fields [ $fieldname ] = array ( 'width' => 10 , 'label' => $this->module->field_defs [ $fieldname ] [ 'vname' ] ) ;
268                                         }
269                                         // sorting fields of certain types will cause a database engine problems
270                                         // we only check this for custom fields, as we assume that OOB fields have been independently confirmed as ok
271                                         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'])))
272                                         {
273                                                 $fields [ $fieldname ] [ 'sortable' ] = false ;
274                                         }
275                                         // Bug 23728 - Make adding a currency type field default to setting the 'currency_format' to true
276                                         if (isset ( $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type ' ] ) && $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ] == 'currency') 
277                                         {
278                                                 $fields [ $fieldname ] [ 'currency_format' ] = true;
279                                         }
280                                 }
281                                 if (isset ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ] ))
282                                 {
283                                         $width = substr ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ], 6, 3 ) ;
284                                         if (strpos ( $width, "%" ) != false)
285                                         {
286                                                 $width = substr ( $width, 0, 2 ) ;
287                                         }
288                                         if ($width < 101 && $width > 0)
289                                         {
290                                                 $fields [ $fieldname ] [ 'width' ] = $width ;
291                                         }
292                                 } else if (isset ( $this->listViewDefs [ $fieldname ] [ 'width' ] ))
293                                 {
294                                         $fields [ $fieldname ] [ 'width' ] = $this->listViewDefs [ $fieldname ] [ 'width' ] ;
295                                 }
296                                 //Get additional Data for relate fields
297                                 if (isset($this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ]) && $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ] == 'relate') {
298                                         $fields [ $fieldname ] = $this->addRelateData($field, $fields [ $fieldname ]);
299                                 }
300                                 $fields [ $fieldname ] [ 'default' ] = ($i == 0) ;
301                         }
302                 }
303                 // Add the reserved fields back in to the end of the default fields in the layout
304                 // ASSUMPTION: reserved fields go back at the end
305                 // First, load the reserved fields - we cannot assume that getDefaultFields has been called earlier when saving
306                 $this->getDefaultFields();
307                 foreach ( $this->reserved as $key => $def)
308                 {
309                     $fields[ $key ] = $def;
310                 }
311                 
312                 return $fields ;
313         }
314         function handleSave ()
315         {
316                 $fields = $this->_loadLayoutFromRequest();
317                 $this->_writeToFile($this->customFile,'ListView',$this->module_name,$fields,$this->_variables);
318
319                 $GLOBALS [ "listViewDefs" ] [ $this->module_name ] = $fields ;
320                 // now clear the cache so that the results are immediately visible
321                 include_once ('include/TemplateHandler/TemplateHandler.php') ;
322                 TemplateHandler::clearCache ( $this->module_name, "ListView.tpl" ) ; // not currently cached, but here for the future
323         }
324 }
325 ?>