]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/views/GridLayoutMetaDataParser.php
Release 6.5.0
[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 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/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                 $this->FILLER = array ( 'name' => MBConstants::$FILLER['name'] , 'label' => translate ( MBConstants::$FILLER['label'] ) ) ;
68
69         $this->_moduleName = $moduleName ;
70         $this->_view = $view ;
71
72         if (empty ( $packageName ))
73         {
74             require_once 'modules/ModuleBuilder/parsers/views/DeployedMetaDataImplementation.php' ;
75             $this->implementation = new DeployedMetaDataImplementation ( $view, $moduleName, self::$variableMap ) ;
76         } else
77         {
78             require_once 'modules/ModuleBuilder/parsers/views/UndeployedMetaDataImplementation.php' ;
79             $this->implementation = new UndeployedMetaDataImplementation ( $view, $moduleName, $packageName ) ;
80         }
81
82         $viewdefs = $this->implementation->getViewdefs () ;
83         if (!isset(self::$variableMap [ $view ]))
84             self::$variableMap [ $view ] = $view;
85
86         if (!isset($viewdefs [ self::$variableMap [ $view ]])){
87             sugar_die ( get_class ( $this ) . ": incorrect view variable for $view" ) ;
88         }
89
90         $viewdefs = $viewdefs [ self::$variableMap [ $view ] ] ;
91         if (! isset ( $viewdefs [ 'templateMeta' ] ))
92             sugar_die ( get_class ( $this ) . ": missing templateMeta section in layout definition (case sensitive)" ) ;
93
94         if (! isset ( $viewdefs [ 'panels' ] ))
95             sugar_die ( get_class ( $this ) . ": missing panels section in layout definition (case sensitive)" ) ;
96
97         $this->_viewdefs = $viewdefs ;
98         if ($this->getMaxColumns () < 1)
99             sugar_die ( get_class ( $this ) . ": maxColumns=" . $this->getMaxColumns () . " - must be greater than 0!" ) ;
100
101         $this->_fielddefs =  $this->implementation->getFielddefs() ;
102         $this->_standardizeFieldLabels( $this->_fielddefs );
103         $this->_viewdefs [ 'panels' ] = $this->_convertFromCanonicalForm ( $this->_viewdefs [ 'panels' ] , $this->_fielddefs ) ; // put into our internal format
104         $this->_originalViewDef = $this->getFieldsFromLayout($this->implementation->getOriginalViewdefs ());
105     }
106
107     /*
108      * Save a draft layout
109      */
110     function writeWorkingFile ($populate = true)
111     {
112         if ($populate)
113             $this->_populateFromRequest ( $this->_fielddefs ) ;
114         
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                 $availableFields[$key]['translatedLabel'] = translate( isset($def [ 'label' ]) ? $def [ 'label' ] : $def['vname'], $this->_moduleName);
198             }
199                         
200         }
201
202                 // Available fields are those that are in the Model and the original layout definition, but not already shown in the View
203         // 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
204         if (! empty ( $this->_viewdefs ))
205         {
206             foreach ( $this->_viewdefs [ 'panels' ] as $panel )
207             {
208                 foreach ( $panel as $row )
209                 {
210                     foreach ( $row as $field )
211                     {
212                         unset ( $availableFields [ $field ] ) ;
213                     }
214                 }
215             }
216         }
217         
218         //eggsurplus: Bug 10329 - sort on intuitive display labels
219         //sort by translatedLabel
220         function cmpLabel($a, $b) 
221         {
222             return strcmp($a["translatedLabel"], $b["translatedLabel"]);
223         }
224         usort($availableFields , 'cmpLabel');
225
226         return $availableFields ;
227     }
228
229     function getPanelDependency ( $panelID )
230     {
231         if ( ! isset ( $this->_viewdefs [ 'templateMeta' ][ 'dependency' ] ) && ! isset ( $this->_viewdefs [ 'templateMeta' ][ 'dependency' ] [ $panelID ] ) )
232                 return false;
233
234         return $this->_viewdefs  [ 'templateMeta' ][ 'dependency' ] [ $panelID ] ;
235     }
236
237     /*
238      * Add a new field to the layout
239      * If $panelID is passed in, attempt to add to that panel, otherwise add to the first panel
240      * 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
241      * and the field added to the start of it.
242      * @param array $def Set of properties for the field, in same format as in the viewdefs
243      * @param string $panelID Identifier of the panel to add the field to; empty or false if we should use the first panel
244      */
245     function addField ( $def , $panelID = FALSE)
246     {
247
248         if (count ( $this->_viewdefs [ 'panels' ] ) == 0)
249         {
250             $GLOBALS [ 'log' ]->error ( get_class ( $this ) . "->addField(): _viewdefs empty for module {$this->_moduleName} and view {$this->_view}" ) ;
251         }
252
253         // if a panelID was not provided, use the first available panel in the list
254         if (! $panelID)
255         {
256             $panels = array_keys ( $this->_viewdefs [ 'panels' ] ) ;
257             list ( $dummy, $panelID ) = each ( $panels ) ;
258         }
259
260         if (isset ( $this->_viewdefs [ 'panels' ] [ $panelID ] ))
261         {
262
263             $panel = $this->_viewdefs [ 'panels' ] [ $panelID ] ;
264             $lastrow = count ( $panel ) - 1 ; // index starts at 0
265             $maxColumns = $this->getMaxColumns () ;
266             $lastRowDef = $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastrow ];
267             for ( $column = 0 ; $column < $maxColumns ; $column ++ )
268             {
269                 if (! isset ( $lastRowDef [ $column ] )
270                         || (is_array( $lastRowDef [ $column ]) && $lastRowDef [ $column ][ 'name' ] == '(empty)')
271                         || (is_string( $lastRowDef [ $column ]) && $lastRowDef [ $column ] == '(empty)')
272                 ){
273                     break ;
274                 }
275             }
276
277             // if we're on the last column of the last row, start a new row
278             if ($column >= $maxColumns)
279             {
280                 $lastrow ++ ;
281                 $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastrow ] = array ( ) ;
282                 $column = 0 ;
283             }
284
285             $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastrow ] [ $column ] = $def [ 'name' ] ;
286             // now update the fielddefs
287             if (isset($this->_fielddefs [ $def [ 'name' ] ]))
288             {
289                 $this->_fielddefs [ $def [ 'name' ] ] = array_merge ( $this->_fielddefs [ $def [ 'name' ] ] , $def ) ;
290             } else
291             {
292                 $this->_fielddefs [ $def [ 'name' ] ] = $def;
293             }
294         }
295         return true ;
296     }
297
298     /*
299      * Remove all instances of a field from the layout, and replace by (filler)
300      * 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
301      * 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;
302      * If the fields had been moved around in the layout however then this will not completely undo any addField()
303      * @param string $fieldName Name of the field to remove
304      * @return boolean True if the field was removed; false otherwise
305      */
306     function removeField ($fieldName)
307     {
308         $GLOBALS [ 'log' ]->info ( get_class ( $this ) . "->removeField($fieldName)" ) ;
309
310         $result = false ;
311         reset ( $this->_viewdefs ) ;
312         $firstPanel = each ( $this->_viewdefs [ 'panels' ] ) ;
313         $firstPanelID = $firstPanel [ 'key' ] ;
314
315         foreach ( $this->_viewdefs [ 'panels' ] as $panelID => $panel )
316         {
317             $lastRowTouched = false ;
318             $lastRowID = count ( $this->_viewdefs [ 'panels' ] [ $panelID ] ) - 1 ; // zero offset
319
320             foreach ( $panel as $rowID => $row )
321             {
322
323                 foreach ( $row as $colID => $field )
324                     if ($field == $fieldName)
325                     {
326                         $lastRowTouched = $rowID ;
327                         $this->_viewdefs [ 'panels' ] [ $panelID ] [ $rowID ] [ $colID ] = $this->FILLER [ 'name' ];
328                     }
329
330             }
331
332             // 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)
333
334             if ( $lastRowTouched ==  $lastRowID )
335             {
336                 $lastRow = $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastRowID ] ; // can't use 'end' for this as we need the key as well as the value...
337
338                 $empty = true ;
339
340                 foreach ( $lastRow as $colID => $field )
341                     $empty &=  $field == MBConstants::$EMPTY ['name' ] || $field == $this->FILLER [ 'name' ]  ;
342
343                 if ($empty)
344                 {
345                     unset ( $this->_viewdefs [ 'panels' ] [ $panelID ] [ $lastRowID ] ) ;
346                     // if the row was the only one in the panel, and the panel is not the first (default) panel, then remove the panel also
347                                         if ( count ( $this->_viewdefs [ 'panels' ] [ $panelID ] ) == 0 && $panelID != $firstPanelID )
348                                                 unset ( $this->_viewdefs [ 'panels' ] [ $panelID ] ) ;
349                 }
350
351             }
352
353             $result |= ($lastRowTouched !== false ); // explicitly compare to false as row 0 will otherwise evaluate as false
354         }
355
356         return $result ;
357
358     }
359
360     function setPanelDependency ( $panelID , $dependency )
361     {
362         // only accept dependencies for pre-existing panels
363         if ( ! isset ( $this->_viewdefs [ 'panels' ] [ $panelID ] ) )
364                 return false;
365
366         $this->_viewdefs  [ 'templateMeta' ] [ 'dependency' ] [ $panelID ] = $dependency ;
367         return true ;
368     }
369
370     /*
371      * 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
372      * Necessary when adding new panels to a layout
373      * @return integer First unique panel ID suffix
374      */
375     function getFirstNewPanelId ()
376     {
377         $firstNewPanelId = 0 ;
378         foreach ( $this->_viewdefs [ 'panels' ] as $panelID => $panel )
379         {
380             // strip out all but the numerics from the panelID - can't just use a cast as numbers may not be first in the string
381             for ( $i = 0, $result = '' ; $i < strlen ( $panelID ) ; $i ++ )
382             {
383                 if (is_numeric ( $panelID [ $i ] ))
384                 {
385                     $result .= $panelID [ $i ] ;
386                 }
387             }
388
389             $firstNewPanelId = max ( ( int ) $result, $firstNewPanelId ) ;
390         }
391         return $firstNewPanelId + 1 ;
392     }
393
394     /*
395      * Load the panel layout from the submitted form and update the _viewdefs
396      */
397     protected function _populateFromRequest ( &$fielddefs )
398     {
399         $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->populateFromRequest()" ) ;
400         $i = 1 ;
401
402         // set up the map of panel# (as provided in the _REQUEST) to panel ID (as used in $this->_viewdefs['panels'])
403         $i = 1 ;
404         foreach ( $this->_viewdefs [ 'panels' ] as $panelID => $panel )
405         {
406             $panelMap [ $i ++ ] = $panelID ;
407         }
408
409         foreach ( $_REQUEST as $key => $displayLabel )
410         {
411             $components = explode ( '-', $key ) ;
412             if ($components [ 0 ] == 'panel' && $components [ 2 ] == 'label')
413             {
414                 $panelMap [ $components [ '1' ] ] = $displayLabel ;
415             }
416         }
417
418         $this->_viewdefs [ 'panels' ] = array () ; // because the new field properties should replace the old fields, not be merged
419
420         // run through the $_REQUEST twice - first to obtain the fieldnames, the second to update the field properties
421         for ( $pass=1 ; $pass<=2 ; $pass++ )
422         {
423                 foreach ( $_REQUEST as $slot => $value )
424                 {
425                 $slotComponents = explode ( '-', $slot ) ; // [0] = 'slot', [1] = panel #, [2] = slot #, [3] = property name
426
427                 if ($slotComponents [ 0 ] == 'slot')
428                 {
429                         $slotNumber = $slotComponents [ '2' ] ;
430                         $panelID = $panelMap [ $slotComponents [ '1' ] ] ;
431                         $rowID = floor ( $slotNumber / $this->getMaxColumns () ) ;
432                         $colID = $slotNumber - ($rowID * $this->getMaxColumns ()) ;
433                         $property = $slotComponents [ '3' ] ;
434
435                         //If this field has a custom definition, copy that over
436                         if ( $pass == 1 )
437                         {
438                                 if ( $property == 'name' )
439                                 $this->_viewdefs [ 'panels' ] [ $panelID ] [ $rowID ] [ $colID ] = $value ;
440                         } else
441                         {
442                                 // update fielddefs for this property in the provided position
443                                 if ( isset ( $this->_viewdefs [ 'panels' ] [ $panelID ] [ $rowID ] [ $colID ] ) )
444                                 {
445                                         $fieldname = $this->_viewdefs [ 'panels' ] [ $panelID ] [ $rowID ] [ $colID ] ;
446                                                         $fielddefs [ $fieldname ] [ $property ] = $value ;
447                                 }
448                         }
449                 }
450
451                 }
452         }
453         
454         //Set the tabs setting
455         if (isset($_REQUEST['panels_as_tabs']))
456         {
457                 if ($_REQUEST['panels_as_tabs'] == false || $_REQUEST['panels_as_tabs'] == "false")
458                    $this->setUseTabs( false );
459                 else
460                    $this->setUseTabs( true );
461         }
462         
463         //bug: 38232 - Set the sync detail and editview settings
464         if (isset($_REQUEST['sync_detail_and_edit']))
465         {
466                 if ($_REQUEST['sync_detail_and_edit'] === false || $_REQUEST['sync_detail_and_edit'] === "false")
467             {
468                    $this->setSyncDetailEditViews( false );
469             }
470             elseif(!empty($_REQUEST['sync_detail_and_edit']))
471             {
472                    $this->setSyncDetailEditViews( true );
473             }
474         }
475
476         $GLOBALS [ 'log' ]->debug ( print_r ( $this->_viewdefs [ 'panels' ], true ) ) ;
477
478     }
479
480     /*  Convert our internal format back to the standard Canonical MetaData layout
481      *  First non-(empty) field goes in at column 0; all other (empty)'s removed
482      *  Studio required fields are also added to the layout.
483      *  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
484      */
485     protected function _convertToCanonicalForm ( $panels , $fielddefs )
486     {
487         $previousViewDef = $this->getFieldsFromLayout($this->implementation->getViewdefs ());
488         $oldDefs = $this->implementation->getViewdefs ();
489         $currentFields = $this->getFieldsFromLayout($this->_viewdefs);
490         foreach($fielddefs as $field => $def)
491         {
492                 if (self::fieldIsRequired($def) && !isset($currentFields[$field]))
493                 {
494                 //Use the previous viewdef if this field was on it.
495                 if (isset($previousViewDef[$field]))
496                 {
497                     $def = $previousViewDef[$field];
498                 }
499                 //next see if the field was on the original layout.
500                 else if (isset ($this->_originalViewDef [ $field ]))
501                 {
502                     $def = $this->_originalViewDef [ $field ] ;   
503                 }
504                 //Otherwise make up a viewdef for it from field_defs
505                 else
506                 {
507                     $def =  self::_trimFieldDefs( $def ) ;
508                 }
509                 $this->addField($def);
510                 }
511         }
512         
513         foreach ( $panels as $panelID => $panel )
514         {
515             // remove all (empty)s
516             foreach ( $panel as $rowID => $row )
517             {
518                 $startOfRow = true ;
519                 $offset = 0 ;
520                 foreach ( $row as $colID => $fieldname )
521                 {
522                     if ($fieldname == MBConstants::$EMPTY[ 'name' ])
523                     {
524                         // if a leading (empty) then remove (by noting that remaining fields need to be shuffled along)
525                         if ($startOfRow)
526                         {
527                             $offset ++ ;
528                         }
529                         unset ( $row [ $colID ] ) ;
530                     } else
531                     {
532                         $startOfRow = false ;
533                     }
534                 }
535
536                 // reindex to remove leading (empty)s and replace fieldnames by full definition from fielddefs
537                 $newRow = array ( ) ;
538                 foreach ( $row as $colID => $fieldname )
539                 {
540                         if ($fieldname == null || !isset($fielddefs[$fieldname]))
541                            continue;
542                     
543                     //Backwards compatibility and a safeguard against multiple calls to _convertToCanonicalForm
544                            if(is_array($fieldname))
545                     {
546                         $newRow [ $colID - $offset ] = $fieldname;
547                         continue;
548                     }
549                         
550                         //Replace (filler) with the empty string
551                         if ($fieldname == $this->FILLER[ 'name' ]) {
552                         $newRow [ $colID - $offset ] = '' ;
553                     }
554                     //Use the previous viewdef if this field was on it.
555                                         else if (isset($previousViewDef[$fieldname]))
556                         {
557                         $newRow[$colID - $offset] = $this->getNewRowItem($previousViewDef[$fieldname], $fielddefs[$fieldname]);
558                         }
559                     //next see if the field was on the original layout.
560                     else if (isset ($this->_originalViewDef [ $fieldname ]))
561                     {
562                         $newRow[$colID - $offset] = $this->getNewRowItem($this->_originalViewDef[$fieldname], $fielddefs[$fieldname]);  
563                     }
564                         //Otherwise make up a viewdef for it from field_defs
565                         else if (isset ($fielddefs [ $fieldname ]))
566                         {
567                                 $newRow [ $colID - $offset ] =  self::_trimFieldDefs( $fielddefs [ $fieldname ] ) ;
568                                 
569                         }
570                         //No additional info on this field can be found, jsut use the name;
571                         else 
572                         {
573                         $newRow [ $colID - $offset ] = $fieldname;
574                         }
575                 }
576                 $panels [ $panelID ] [ $rowID ] = $newRow ;
577             }
578         }
579         
580         return $panels ;
581     }
582
583     /*
584      * fixing bug #44428: Studio | Tab Order causes layout errors
585      * @param string|array $source it can be a string which contain just a name of field 
586      *                                  or an array with field attributes including name
587      * @param array $fielddef stores field defs from request
588      * @return string|array definition of new row item
589      */
590     function getNewRowItem($source, $fielddef)
591     {
592         //We should copy over the tabindex if it is set.
593         $newRow = array();
594         if (isset ($fielddef) && !empty($fielddef['tabindex']))
595         {
596             if (is_array($source))
597             {
598                 $newRow = $source;
599             }
600             else
601             {
602                 $newRow['name'] = $source;
603             }
604             $newRow['tabindex'] = $fielddef['tabindex'];
605         }
606         else
607         {
608             $newRow = $source;
609         }
610         return $newRow;
611     }
612
613     /*
614      * Convert from the standard MetaData format to our internal format
615      * Replace NULL with (filler) and missing entries with (empty)
616      */
617     protected function _convertFromCanonicalForm ( $panels , $fielddefs )
618     {
619         if (empty ( $panels ))
620             return ;
621
622         // Fix for a flexibility in the format of the panel sections - if only one panel, then we don't have a panel level defined,
623                 // it goes straight into rows
624         // See EditView2 for similar treatment
625         if (! empty ( $panels ) && count ( $panels ) > 0)
626         {
627             $keys = array_keys ( $panels ) ;
628             if (is_numeric ( $keys [ 0 ] ))
629             {
630                 $defaultPanel = $panels ;
631                 unset ( $panels ) ; //blow away current value
632                 $panels [ 'default' ] = $defaultPanel ;
633             }
634         }
635
636         $newPanels = array ( ) ;
637
638         // replace '' with (filler)
639         foreach ( $panels as $panelID => $panel )
640         {
641             foreach ( $panel as $rowID => $row )
642             {
643                 $cols = 0;
644                 foreach ( $row as $colID => $col )
645                 {
646                     if ( ! empty ( $col ) )
647                     {
648                         if ( is_string ( $col ))
649                         {
650                             $fieldname = $col ;
651                         } else if (! empty ( $col [ 'name' ] ))
652                         {
653                             $fieldname = $col [ 'name' ] ;
654                         }
655                     } else
656                     {
657                         $fieldname = $this->FILLER['name'] ;
658                     }
659
660                     $newPanels [ $panelID ] [ $rowID ] [ $cols ] = $fieldname ;
661                     $cols++;
662                 }
663             }
664         }
665
666         // replace missing fields with (empty)
667         foreach ( $newPanels as $panelID => $panel )
668         {
669             $column = 0 ;
670             foreach ( $panel as $rowID => $row )
671             {
672                 // pad between fields on a row
673                 foreach ( $row as $colID => $col )
674                 {
675                     for ( $i = $column + 1 ; $i < $colID ; $i ++ )
676                     {
677                         $row [ $i ] = MBConstants::$EMPTY ['name'];
678                     }
679                     $column = $colID ;
680                 }
681                 // now pad out to the end of the row
682                 if (($column + 1) < $this->getMaxColumns ())
683                 { // last column is maxColumns-1
684                     for ( $i = $column + 1 ; $i < $this->getMaxColumns () ; $i ++ )
685                     {
686                         $row [ $i ] = MBConstants::$EMPTY ['name'] ;
687                     }
688                 }
689                 ksort ( $row ) ;
690                 $newPanels [ $panelID ] [ $rowID ] = $row ;
691             }
692         }
693
694         return $newPanels ;
695     }
696     
697     protected function getFieldsFromLayout($viewdef) {
698         if (isset($viewdef['panels']))
699         {
700                 $panels = $viewdef['panels']; 
701         } else {
702             $panels = $viewdef[self::$variableMap [ $this->_view ] ]['panels'];
703         }
704         
705         $ret = array();
706         if (is_array($panels)) 
707         {       
708                 foreach ( $panels as $rows) {
709                     foreach ($rows as $fields) {
710                         //wireless layouts have one less level of depth
711                         if (is_array($fields) && isset($fields['name'])) {
712                                 $ret[$fields['name']] = $fields;  
713                                 continue;
714                         }
715                         if (!is_array($fields)) {
716                                 $ret[$fields] = $fields;
717                                 continue;
718                         }
719                         foreach ($fields as $field) {
720                             if (is_array($field) && !empty($field['name']))
721                             {
722                                 $ret[$field['name']] = $field;  
723                             }
724                             else if(!is_array($field)){
725                             $ret[$field] = $field;
726                         }                           
727                         }
728                     }
729                 }
730         }
731         return $ret;
732     }
733     
734     protected function fieldIsRequired($def)
735     {
736         if (isset($def['studio']))
737         { 
738                 if (is_array($def['studio']))
739                 {
740                         if (!empty($def['studio'][$this->_view]) && $def['studio'][$this->_view] == "required")
741                         {
742                                 return true;
743     }
744                         else if (!empty($def['studio']['required']) && $def['studio']['required'] == true)
745                         {
746                                 return true;
747                         }
748                 }
749                 else if ($def['studio'] == "required" ){
750                   return true;
751                 }
752     }
753         return false;
754     }
755
756     static function _trimFieldDefs ( $def )
757         {
758                 $ret = array_intersect_key ( $def , 
759             array ( 'studio' => true , 'name' => true , 'label' => true , 'displayParams' => true , 'comment' => true , 
760                     'customCode' => true , 'customLabel' => true , 'tabindex' => true , 'hideLabel' => true) ) ;
761         if (!empty($def['vname']) && empty($def['label']))
762             $ret['label'] = $def['vname'];
763                 return $ret;
764         }
765         
766         public function getUseTabs(){
767         if (isset($this->_viewdefs  [ 'templateMeta' ]['useTabs']))
768            return $this->_viewdefs  [ 'templateMeta' ]['useTabs'];
769            
770         return false;
771     }
772     
773     public function setUseTabs($useTabs){
774         $this->_viewdefs  [ 'templateMeta' ]['useTabs'] = $useTabs;
775     }
776     
777     /**
778      * Return whether the Detail & EditView should be in sync.
779      */
780         public function getSyncDetailEditViews(){
781         if (isset($this->_viewdefs  [ 'templateMeta' ]['syncDetailEditViews']))
782            return $this->_viewdefs  [ 'templateMeta' ]['syncDetailEditViews'];
783            
784         return false;
785     }
786     
787     /**
788      * Sync DetailView & EditView. This should only be set on the EditView
789      * @param bool $syncViews
790      */
791     public function setSyncDetailEditViews($syncDetailEditViews){
792         $this->_viewdefs  [ 'templateMeta' ]['syncDetailEditViews'] = $syncDetailEditViews;
793     }
794
795     /**
796      * Getter function to get the implementation method which is a private variable
797      * @return DeployedMetaDataImplementation
798      */
799     public function getImplementation(){
800         return $this->implementation;
801     }
802
803     /**
804      * Public access to _convertFromCanonicalForm
805      * @param  $panels
806      * @param  $fielddefs
807      * @return array
808      */
809     public function convertFromCanonicalForm ( $panels , $fielddefs )
810     {
811         return $this->_convertFromCanonicalForm ( $panels , $fielddefs );
812     }
813
814      /**
815      * Public access to _convertToCanonicalForm
816      * @param  $panels
817      * @param  $fielddefs
818      * @return array
819      */
820     public function convertToCanonicalForm ( $panels , $fielddefs )
821     {
822         return $this->_convertToCanonicalForm ( $panels , $fielddefs );
823     }
824
825     
826     /**
827      * @return Array list of fields in this module that have the calculated property
828      */
829     public function getCalculatedFields() {
830         $ret = array();
831         foreach ($this->_fielddefs as $field => $def)
832         {
833             if(!empty($def['calculated']) && !empty($def['formula']))
834             {
835                 $ret[] = $field;
836             }
837         }
838         
839         return $ret;
840     }
841
842     /**
843      * @return Array fields in the given panel
844      */
845     public function getFieldsInPanel($targetPanel) {
846         return iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($this->_viewdefs['panels'][$targetPanel])));
847     }
848 }
849
850 ?>