]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/views/GridLayoutMetaDataParser.php
Release 6.1.5
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / views / GridLayoutMetaDataParser.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/views/AbstractMetaDataParser.php' ;
41 require_once 'modules/ModuleBuilder/parsers/views/MetaDataParserInterface.php' ;
42 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
43
44 class GridLayoutMetaDataParser extends AbstractMetaDataParser implements MetaDataParserInterface
45 {
46
47     static $variableMap = array (
48         MB_EDITVIEW => 'EditView' ,
49         MB_DETAILVIEW => 'DetailView' ,
50         MB_QUICKCREATE => 'QuickCreate',
51         ) ;
52
53         protected $FILLER ;
54
55     /*
56      * Constructor
57      * @param string view           The view type, that is, editview, searchview etc
58      * @param string moduleName     The name of the module to which this view belongs
59      * @param string packageName    If not empty, the name of the package to which this view belongs
60      */
61     function __construct ($view , $moduleName , $packageName = '')
62     {
63         $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->__construct( {$view} , {$moduleName} , {$packageName} )" ) ;
64
65         $view = strtolower ( $view ) ;
66
67         // BEGIN ASSERTIONS
68         if (! isset ( self::$variableMap [ $view ] ) )
69             sugar_die ( get_class ( $this ) . ": View $view is not supported" ) ;
70         // END ASSERTIONS
71
72                 $this->FILLER = array ( 'name' => MBConstants::$FILLER['name'] , 'label' => translate ( MBConstants::$FILLER['label'] ) ) ;
73
74         $this->_moduleName = $moduleName ;
75         $this->_view = $view ;
76
77         if (empty ( $packageName ))
78         {
79             require_once 'modules/ModuleBuilder/parsers/views/DeployedMetaDataImplementation.php' ;
80             $this->implementation = new DeployedMetaDataImplementation ( $view, $moduleName, self::$variableMap ) ;
81         } else
82         {
83             require_once 'modules/ModuleBuilder/parsers/views/UndeployedMetaDataImplementation.php' ;
84             $this->implementation = new UndeployedMetaDataImplementation ( $view, $moduleName, $packageName ) ;
85         }
86
87         $viewdefs = $this->implementation->getViewdefs () ;
88
89         if (! isset ( $viewdefs [ self::$variableMap [ $view ] ] ))
90             sugar_die ( get_class ( $this ) . ": missing variable " . self::$variableMap [ $view ] . " in layout definition" ) ;
91
92         $viewdefs = $viewdefs [ self::$variableMap [ $view ] ] ;
93         if (! isset ( $viewdefs [ 'templateMeta' ] ))
94             sugar_die ( get_class ( $this ) . ": missing templateMeta section in layout definition (case sensitive)" ) ;
95
96         if (! isset ( $viewdefs [ 'panels' ] ))
97             sugar_die ( get_class ( $this ) . ": missing panels section in layout definition (case sensitive)" ) ;
98
99         $this->_viewdefs = $viewdefs ;
100         if ($this->getMaxColumns () < 1)
101             sugar_die ( get_class ( $this ) . ": maxColumns=" . $this->getMaxColumns () . " - must be greater than 0!" ) ;
102
103         $this->_fielddefs =  $this->implementation->getFielddefs() ;
104         $this->_standardizeFieldLabels( $this->_fielddefs );
105         $this->_viewdefs [ 'panels' ] = $this->_convertFromCanonicalForm ( $this->_viewdefs [ 'panels' ] , $this->_fielddefs ) ; // put into our internal format
106         $this->_originalViewDef = $this->getFieldsFromLayout($this->implementation->getOriginalViewdefs ());
107     }
108
109     /*
110      * Save a draft layout
111      */
112     function writeWorkingFile ()
113     {
114         $this->_populateFromRequest ( $this->_fielddefs ) ;
115         $viewdefs = $this->_viewdefs ;
116         $viewdefs [ 'panels' ] = $this->_convertToCanonicalForm ( $this->_viewdefs [ 'panels' ] , $this->_fielddefs ) ;
117         $this->implementation->save ( array ( self::$variableMap [ $this->_view ] => $viewdefs ) ) ;
118     }
119
120     /*
121      * Deploy the layout
122      * @param boolean $populate If true (default), then update the layout first with new layout information from the $_REQUEST array
123      */
124     function handleSave ($populate = true)
125     {
126         $GLOBALS [ 'log' ]->info ( get_class ( $this ) . "->handleSave()" ) ;
127
128         if ($populate)
129             $this->_populateFromRequest ( $this->_fielddefs ) ;
130
131         $viewdefs = $this->_viewdefs ;
132         $viewdefs [ 'panels' ] = $this->_convertToCanonicalForm ( $this->_viewdefs [ 'panels' ] , $this->_fielddefs ) ;
133         $this->implementation->deploy ( array ( self::$variableMap [ $this->_view ] => $viewdefs ) ) ;
134     }
135
136     /*
137      * Return the layout, padded out with (empty) and (filler) fields ready for display
138      */
139     function getLayout ()
140     {
141         $viewdefs = array () ;
142         $fielddefs = $this->_fielddefs;
143         $fielddefs [ $this->FILLER [ 'name' ] ] = $this->FILLER ;
144         $fielddefs [ MBConstants::$EMPTY [ 'name' ] ] = MBConstants::$EMPTY ;
145         
146                 foreach ( $this->_viewdefs [ 'panels' ] as $panelID => $panel )
147         {
148             foreach ( $panel as $rowID => $row )
149             {
150                 foreach ( $row as $colID => $fieldname )
151                 {
152                         if (isset ($this->_fielddefs [ $fieldname ]))
153                                         {
154                                                 $viewdefs [ $panelID ] [ $rowID ] [ $colID ] = self::_trimFieldDefs( $this->_fielddefs [ $fieldname ] ) ;
155                                         } 
156                                         else if (isset($this->_originalViewDef [ $fieldname ]) && is_array($this->_originalViewDef [ $fieldname ]))
157                                         {
158                                                 $viewdefs [ $panelID ] [ $rowID ] [ $colID ] = self::_trimFieldDefs( $this->_originalViewDef [ $fieldname ] ) ;
159                                         } 
160                                         else 
161                                         {
162                                                 $viewdefs [ $panelID ] [ $rowID ] [ $colID ] = array("name" => $fieldname, "label" => $fieldname);
163                                         }
164                 }
165             }
166         }
167         return $viewdefs ;
168     }
169
170     function getMaxColumns ()
171     {
172         if (!empty( $this->_viewdefs) && isset($this->_viewdefs [ 'templateMeta' ] [ 'maxColumns' ]))
173                 {
174                         return $this->_viewdefs [ 'templateMeta' ] [ 'maxColumns' ] ;
175                 }else
176                 {
177                         return 2;
178                 }
179     }
180
181     function getAvailableFields ()
182     {
183
184         // Obtain the full list of valid fields in this module
185         $availableFields = array () ;
186         foreach ( $this->_fielddefs as $key => $def )
187         {
188             if ( GridLayoutMetaDataParser::validField ( $def,  $this->_view ) || isset($this->_originalViewDef[$key]) )
189             {
190                 //If the field original label existing, we should use the original label instead the label in its fielddefs.
191                 if(isset($this->_originalViewDef[$key]) && is_array($this->_originalViewDef[$key]) && isset($this->_originalViewDef[$key]['label'])){
192                     $availableFields [ $key ] = array ( 'name' => $key , 'label' => $this->_originalViewDef[$key]['label']) ; 
193                 }else{
194                     $availableFields [ $key ] = array ( 'name' => $key , 'label' => isset($def [ 'label' ]) ? $def [ 'label' ] : $def['vname'] ) ; // layouts use 'label' not 'vname' for the label entry
195             }
196             }
197                         
198         }
199
200                 // Available fields are those that are in the Model and the original layout definition, but not already shown in the View
201         // So, because the formats of the two are different we brute force loop through View and unset the fields we find in a copy of Model
202         if (! empty ( $this->_viewdefs ))
203         {
204             foreach ( $this->_viewdefs [ 'panels' ] as $panel )
205             {
206                 foreach ( $panel as $row )
207                 {
208                     foreach ( $row as $field )
209                     {
210                         unset ( $availableFields [ $field ] ) ;
211                     }
212                 }
213             }
214         }
215
216         return $availableFields ;
217     }
218
219     function getPanelDependency ( $panelID )
220     {
221         if ( ! isset ( $this->_viewdefs [ 'templateMeta' ][ 'dependency' ] ) && ! isset ( $this->_viewdefs [ 'templateMeta' ][ 'dependency' ] [ $panelID ] ) )
222                 return false;
223
224         return $this->_viewdefs  [ 'templateMeta' ][ 'dependency' ] [ $panelID ] ;
225     }
226
227     /*
228      * Add a new field to the layout
229      * If $panelID is passed in, attempt to add to that panel, otherwise add to the first panel
230      * The field is added in place of the first empty (not filler) slot after the last field in the panel; if that row is full, then a new row will be added to the end of the panel
231      * and the field added to the start of it.
232      * @param array $def Set of properties for the field, in same format as in the viewdefs
233      * @param string $panelID Identifier of the panel to add the field to; empty or false if we should use the first panel
234      */
235     function addField ( $def , $panelID = FALSE)
236     {
237
238         if (count ( $this->_viewdefs [ 'panels' ] ) == 0)
239         {
240             $GLOBALS [ 'log' ]->error ( get_class ( $this ) . "->addField(): _viewdefs empty for module {$this->_moduleName} and view {$this->_view}" ) ;
241         }
242
243         // if a panelID was not provided, use the first available panel in the list
244         if (! $panelID)
245         {
246             $panels = array_keys ( $this->_viewdefs [ 'panels' ] ) ;
247             list ( $dummy, $panelID ) = each ( $panels ) ;
248         }
249
250         if (isset ( $this->_viewdefs [ 'panels' ] [ $panelID ] ))
251         {
252
253             $panel = $this->_viewdefs [ 'panels' ] [ $panelID ] ;
254             $lastrow = count ( $panel ) - 1 ; // index starts at 0
255             $maxColumns = $this->getMaxColumns () ;
256             $lastRowDef = $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastrow ];
257             for ( $column = 0 ; $column < $maxColumns ; $column ++ )
258             {
259                 if (! isset ( $lastRowDef [ $column ] )
260                         || (is_array( $lastRowDef [ $column ]) && $lastRowDef [ $column ][ 'name' ] == '(empty)')
261                         || (is_string( $lastRowDef [ $column ]) && $lastRowDef [ $column ] == '(empty)')
262                 ){
263                     break ;
264                 }
265             }
266
267             // if we're on the last column of the last row, start a new row
268             if ($column >= $maxColumns)
269             {
270                 $lastrow ++ ;
271                 $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastrow ] = array ( ) ;
272                 $column = 0 ;
273             }
274
275             $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastrow ] [ $column ] = $def [ 'name' ] ;
276             // now update the fielddefs
277             if (isset($this->_fielddefs [ $def [ 'name' ] ]))
278             {
279                 $this->_fielddefs [ $def [ 'name' ] ] = array_merge ( $this->_fielddefs [ $def [ 'name' ] ] , $def ) ;
280             } else
281             {
282                 $this->_fielddefs [ $def [ 'name' ] ] = $def;
283             }
284         }
285         return true ;
286     }
287
288     /*
289      * Remove all instances of a field from the layout, and replace by (filler)
290      * Filler because we attempt to preserve the customized layout as much as possible - replacing by (empty) would mean that the positions or sizes of adjacent fields may change
291      * If the last row of a panel only consists of (filler) after removing the fields, then remove the row also. This undoes the standard addField() scenario;
292      * If the fields had been moved around in the layout however then this will not completely undo any addField()
293      * @param string $fieldName Name of the field to remove
294      * @return boolean True if the field was removed; false otherwise
295      */
296     function removeField ($fieldName)
297     {
298         $GLOBALS [ 'log' ]->info ( get_class ( $this ) . "->removeField($fieldName)" ) ;
299
300         $result = false ;
301         reset ( $this->_viewdefs ) ;
302         $firstPanel = each ( $this->_viewdefs [ 'panels' ] ) ;
303         $firstPanelID = $firstPanel [ 'key' ] ;
304
305         foreach ( $this->_viewdefs [ 'panels' ] as $panelID => $panel )
306         {
307             $lastRowTouched = false ;
308             $lastRowID = count ( $this->_viewdefs [ 'panels' ] [ $panelID ] ) - 1 ; // zero offset
309
310             foreach ( $panel as $rowID => $row )
311             {
312
313                 foreach ( $row as $colID => $field )
314                     if ($field == $fieldName)
315                     {
316                         $lastRowTouched = $rowID ;
317                         $this->_viewdefs [ 'panels' ] [ $panelID ] [ $rowID ] [ $colID ] = $this->FILLER [ 'name' ];
318                     }
319
320             }
321
322             // if we removed a field from the last row of this panel, tidy up if the last row now consists only of (empty) or (filler)
323
324             if ( $lastRowTouched ==  $lastRowID )
325             {
326                 $lastRow = $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastRowID ] ; // can't use 'end' for this as we need the key as well as the value...
327
328                 $empty = true ;
329
330                 foreach ( $lastRow as $colID => $field )
331                     $empty &=  $field == MBConstants::$EMPTY ['name' ] || $field == $this->FILLER [ 'name' ]  ;
332
333                 if ($empty)
334                 {
335                     unset ( $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastRowID ] ) ;
336                     // if the row was the only one in the panel, and the panel is not the first (default) panel, then remove the panel also
337                                         if ( count ( $this->_viewdefs [ 'panels' ] [ $panelID ] ) == 0 && $panelID != $firstPanelID )
338                                                 unset ( $this->_viewdefs [ 'panels' ] [ $panelID ] ) ;
339                 }
340
341             }
342
343             $result |= ($lastRowTouched !== false ); // explicitly compare to false as row 0 will otherwise evaluate as false
344         }
345
346         return $result ;
347
348     }
349
350     function setPanelDependency ( $panelID , $dependency )
351     {
352         // only accept dependencies for pre-existing panels
353         if ( ! isset ( $this->_viewdefs [ 'panels' ] [ $panelID ] ) )
354                 return false;
355
356         $this->_viewdefs  [ 'templateMeta' ] [ 'dependency' ] [ $panelID ] = $dependency ;
357         return true ;
358     }
359
360     /*
361      * Return an integer value for the next unused panel identifier, such that it and any larger numbers are guaranteed to be unused already in the layout
362      * Necessary when adding new panels to a layout
363      * @return integer First unique panel ID suffix
364      */
365     function getFirstNewPanelId ()
366     {
367         $firstNewPanelId = 0 ;
368         foreach ( $this->_viewdefs [ 'panels' ] as $panelID => $panel )
369         {
370             // strip out all but the numerics from the panelID - can't just use a cast as numbers may not be first in the string
371             for ( $i = 0, $result = '' ; $i < strlen ( $panelID ) ; $i ++ )
372             {
373                 if (is_numeric ( $panelID [ $i ] ))
374                 {
375                     $result .= $panelID [ $i ] ;
376                 }
377             }
378
379             $firstNewPanelId = max ( ( int ) $result, $firstNewPanelId ) ;
380         }
381         return $firstNewPanelId + 1 ;
382     }
383
384     /*
385      * Load the panel layout from the submitted form and update the _viewdefs
386      */
387     protected function _populateFromRequest ( &$fielddefs )
388     {
389         $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->populateFromRequest()" ) ;
390         $i = 1 ;
391
392         // set up the map of panel# (as provided in the _REQUEST) to panel ID (as used in $this->_viewdefs['panels'])
393         $i = 1 ;
394         foreach ( $this->_viewdefs [ 'panels' ] as $panelID => $panel )
395         {
396             $panelMap [ $i ++ ] = $panelID ;
397         }
398
399         foreach ( $_REQUEST as $key => $displayLabel )
400         {
401             $components = explode ( '-', $key ) ;
402             if ($components [ 0 ] == 'panel' && $components [ 2 ] == 'label')
403             {
404                 $panelMap [ $components [ '1' ] ] = $displayLabel ;
405             }
406         }
407
408         $this->_viewdefs [ 'panels' ] = array () ; // because the new field properties should replace the old fields, not be merged
409
410         // run through the $_REQUEST twice - first to obtain the fieldnames, the second to update the field properties
411         for ( $pass=1 ; $pass<=2 ; $pass++ )
412         {
413                 foreach ( $_REQUEST as $slot => $value )
414                 {
415                 $slotComponents = explode ( '-', $slot ) ; // [0] = 'slot', [1] = panel #, [2] = slot #, [3] = property name
416
417                 if ($slotComponents [ 0 ] == 'slot')
418                 {
419                         $slotNumber = $slotComponents [ '2' ] ;
420                         $panelID = $panelMap [ $slotComponents [ '1' ] ] ;
421                         $rowID = floor ( $slotNumber / $this->getMaxColumns () ) ;
422                         $colID = $slotNumber - ($rowID * $this->getMaxColumns ()) ;
423                         $property = $slotComponents [ '3' ] ;
424
425                         //If this field has a custom definition, copy that over
426                         if ( $pass == 1 )
427                         {
428                                 if ( $property == 'name' )
429                                 $this->_viewdefs [ 'panels' ] [ $panelID ] [ $rowID ] [ $colID ] = $value ;
430                         } else
431                         {
432                                 // update fielddefs for this property in the provided position
433                                 if ( isset ( $this->_viewdefs [ 'panels' ] [ $panelID ] [ $rowID ] [ $colID ] ) )
434                                 {
435                                         $fieldname = $this->_viewdefs [ 'panels' ] [ $panelID ] [ $rowID ] [ $colID ] ;
436                                                         $fielddefs [ $fieldname ] [ $property ] = $value ;
437                                 }
438                         }
439                 }
440
441                 }
442         }
443         
444         //Set the tabs setting
445         if (isset($_REQUEST['panels_as_tabs']))
446         {
447                 if ($_REQUEST['panels_as_tabs'] == false || $_REQUEST['panels_as_tabs'] == "false")
448                    $this->setUseTabs( false );
449                 else
450                    $this->setUseTabs( true );
451         }
452
453         $GLOBALS [ 'log' ]->debug ( print_r ( $this->_viewdefs [ 'panels' ], true ) ) ;
454
455     }
456
457     /*  Convert our internal format back to the standard Canonical MetaData layout
458      *  First non-(empty) field goes in at column 0; all other (empty)'s removed
459      *  Studio required fields are also added to the layout.
460      *  Do this AFTER reading in all the $_REQUEST parameters as can't guarantee the order of those, and we need to operate on complete rows
461      */
462     protected function _convertToCanonicalForm ( $panels , $fielddefs )
463     {
464         $previousViewDef = $this->getFieldsFromLayout($this->implementation->getViewdefs ());
465         $oldDefs = $this->implementation->getViewdefs ();
466         $currentFields = $this->getFieldsFromLayout($this->_viewdefs);
467         foreach($fielddefs as $field => $def)
468         {
469                 if (self::fieldIsRequired($def) && !isset($currentFields[$field]))
470                 {
471                 //Use the previous viewdef if this field was on it.
472                 if (isset($previousViewDef[$field]))
473                 {
474                     $def = $previousViewDef[$field];
475                 }
476                 //next see if the field was on the original layout.
477                 else if (isset ($this->_originalViewDef [ $field ]))
478                 {
479                     $def = $this->_originalViewDef [ $field ] ;   
480                 }
481                 //Otherwise make up a viewdef for it from field_defs
482                 else
483                 {
484                     $def =  self::_trimFieldDefs( $def ) ;
485                 }
486                 $this->addField($def);
487                 }
488         }
489         
490         foreach ( $panels as $panelID => $panel )
491         {
492             // remove all (empty)s
493             foreach ( $panel as $rowID => $row )
494             {
495                 $startOfRow = true ;
496                 $offset = 0 ;
497                 foreach ( $row as $colID => $fieldname )
498                 {
499                     if ($fieldname == MBConstants::$EMPTY[ 'name' ])
500                     {
501                         // if a leading (empty) then remove (by noting that remaining fields need to be shuffled along)
502                         if ($startOfRow)
503                         {
504                             $offset ++ ;
505                         }
506                         unset ( $row [ $colID ] ) ;
507                     } else
508                     {
509                         $startOfRow = false ;
510                     }
511                 }
512
513                 // reindex to remove leading (empty)s and replace fieldnames by full definition from fielddefs
514                 $newRow = array ( ) ;
515                 foreach ( $row as $colID => $fieldname )
516                 {
517                         if ($fieldname == null)
518                            continue;
519                     
520                     //Backwards compatibility and a safeguard against multiple calls to _convertToCanonicalForm
521                            if(is_array($fieldname))
522                     {
523                         $newRow [ $colID - $offset ] = $fieldname;
524                         continue;
525                     }
526                         
527                         //Replace (filler) with the empty string
528                         if ($fieldname == $this->FILLER[ 'name' ]) {
529                         $newRow [ $colID - $offset ] = '' ;
530                     }
531                     //Use the previous viewdef if this field was on it.
532                                         else if (isset($previousViewDef[$fieldname]))
533                         {
534                                  $newRow [ $colID - $offset ] = $previousViewDef[$fieldname];
535                         //We should copy over the tabindex if it is set.
536                         if (isset ($fielddefs [ $fieldname ]) && !empty($fielddefs [ $fieldname ]['tabindex']))
537                             $newRow [ $colID - $offset ]['tabindex'] = $fielddefs [ $fieldname ]['tabindex'];
538                         }
539                     //next see if the field was on the original layout.
540                     else if (isset ($this->_originalViewDef [ $fieldname ]))
541                     {
542                         $newRow [ $colID - $offset ] = $this->_originalViewDef [ $fieldname ] ;  
543                         //We should copy over the tabindex if it is set.
544                         if (isset ($fielddefs [ $fieldname ]) && !empty($fielddefs [ $fieldname ]['tabindex']))
545                             $newRow [ $colID - $offset ]['tabindex'] = $fielddefs [ $fieldname ]['tabindex']; 
546                     }
547                         //Otherwise make up a viewdef for it from field_defs
548                         else if (isset ($fielddefs [ $fieldname ]))
549                         {
550                                 $newRow [ $colID - $offset ] =  self::_trimFieldDefs( $fielddefs [ $fieldname ] ) ;
551                                 
552                         }
553                         //No additional info on this field can be found, jsut use the name;
554                         else 
555                         {
556                         $newRow [ $colID - $offset ] = $fieldname;
557                         }
558                 }
559                 $panels [ $panelID ] [ $rowID ] = $newRow ;
560             }
561         }
562         
563         return $panels ;
564     }
565
566     /*
567      * Convert from the standard MetaData format to our internal format
568      * Replace NULL with (filler) and missing entries with (empty)
569      */
570     protected function _convertFromCanonicalForm ( $panels , $fielddefs )
571     {
572         if (empty ( $panels ))
573             return ;
574
575         // Fix for a flexibility in the format of the panel sections - if only one panel, then we don't have a panel level defined,
576                 // it goes straight into rows
577         // See EditView2 for similar treatment
578         if (! empty ( $panels ) && count ( $panels ) > 0)
579         {
580             $keys = array_keys ( $panels ) ;
581             if (is_numeric ( $keys [ 0 ] ))
582             {
583                 $defaultPanel = $panels ;
584                 unset ( $panels ) ; //blow away current value
585                 $panels [ 'default' ] = $defaultPanel ;
586             }
587         }
588
589         $newPanels = array ( ) ;
590
591         // replace '' with (filler)
592         foreach ( $panels as $panelID => $panel )
593         {
594             foreach ( $panel as $rowID => $row )
595             {
596                 $cols = 0;
597                 foreach ( $row as $colID => $col )
598                 {
599                     if ( ! empty ( $col ) )
600                     {
601                         if ( is_string ( $col ))
602                         {
603                             $fieldname = $col ;
604                         } else if (! empty ( $col [ 'name' ] ))
605                         {
606                             $fieldname = $col [ 'name' ] ;
607                         }
608                     } else
609                     {
610                         $fieldname = $this->FILLER['name'] ;
611                     }
612
613                     $newPanels [ $panelID ] [ $rowID ] [ $cols ] = $fieldname ;
614                     $cols++;
615                 }
616             }
617         }
618
619         // replace missing fields with (empty)
620         foreach ( $newPanels as $panelID => $panel )
621         {
622             $column = 0 ;
623             foreach ( $panel as $rowID => $row )
624             {
625                 // pad between fields on a row
626                 foreach ( $row as $colID => $col )
627                 {
628                     for ( $i = $column + 1 ; $i < $colID ; $i ++ )
629                     {
630                         $row [ $i ] = MBConstants::$EMPTY ['name'];
631                     }
632                     $column = $colID ;
633                 }
634                 // now pad out to the end of the row
635                 if (($column + 1) < $this->getMaxColumns ())
636                 { // last column is maxColumns-1
637                     for ( $i = $column + 1 ; $i < $this->getMaxColumns () ; $i ++ )
638                     {
639                         $row [ $i ] = MBConstants::$EMPTY ['name'] ;
640                     }
641                 }
642                 ksort ( $row ) ;
643                 $newPanels [ $panelID ] [ $rowID ] = $row ;
644             }
645         }
646
647         return $newPanels ;
648     }
649     
650     protected function getFieldsFromLayout($viewdef) {
651         if (isset($viewdef['panels']))
652         {
653                 $panels = $viewdef['panels']; 
654         } else {
655         $panels = $viewdef[self::$variableMap [ $this->_view ] ]['panels']; 
656         }
657         
658         $ret = array();
659         if (is_array($panels)) 
660         {       
661                 foreach ( $panels as $rows) {
662                     foreach ($rows as $fields) {
663                         //wireless layouts have one less level of depth
664                         if (is_array($fields) && isset($fields['name'])) {
665                                 $ret[$fields['name']] = $fields;  
666                                 continue;
667                         }
668                         if (!is_array($fields)) {
669                                 $ret[$fields] = $fields;
670                                 continue;
671                         }
672                         foreach ($fields as $field) {
673                             if (is_array($field) && !empty($field['name']))
674                             {
675                                 $ret[$field['name']] = $field;  
676                             }
677                             else if(!is_array($field)){
678                             $ret[$field] = $field;
679                         }                           
680                         }
681                     }
682                 }
683         }
684         return $ret;
685     }
686     
687     protected function fieldIsRequired($def)
688     {
689         if (isset($def['studio']))
690         { 
691                 if (is_array($def['studio']))
692                 {
693                         if (!empty($def['studio'][$this->_view]) && $def['studio'][$this->_view] == "required")
694                         {
695                                 return true;
696     }
697                         else if (!empty($def['studio']['required']) && $def['studio']['required'] == true)
698                         {
699                                 return true;
700                         }
701                 }
702                 else if ($def['studio'] == "required" ){
703                   return true;
704                 }
705     }
706         return false;
707     }
708
709     static function _trimFieldDefs ( $def )
710         {
711                 $ret = array_intersect_key ( $def , 
712             array ( 'studio' => true , 'name' => true , 'label' => true , 'displayParams' => true , 'comment' => true , 
713                     'customCode' => true , 'customLabel' => true , 'tabindex' => true , 'hideLabel' => true) ) ;
714         if (!empty($def['vname']) && empty($def['label']))
715             $ret['label'] = $def['vname'];
716                 return $ret;
717         }
718         
719         public function getUseTabs(){
720         if (isset($this->_viewdefs  [ 'templateMeta' ]['useTabs']))
721            return $this->_viewdefs  [ 'templateMeta' ]['useTabs'];
722            
723         return false;
724     }
725     
726     public function setUseTabs($useTabs){
727         $this->_viewdefs  [ 'templateMeta' ]['useTabs'] = $useTabs;
728     }
729     
730     /**
731      * @return Array list of fields in this module that have the calculated property
732      */
733     public function getCalculatedFields() {
734         $ret = array();
735         foreach ($this->_fielddefs as $field => $def)
736         {
737             if(!empty($def['calculated']) && !empty($def['formula']))
738             {
739                 $ret[] = $field;
740             }
741         }
742         
743         return $ret;
744     }
745 }
746
747 ?>