]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/views/SubpanelMetaDataParser.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / views / SubpanelMetaDataParser.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40 require_once ('modules/ModuleBuilder/parsers/views/ListLayoutMetaDataParser.php') ;
41 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
42
43 class SubpanelMetaDataParser extends ListLayoutMetaDataParser
44 {
45
46     // Columns is used by the view to construct the listview - each column is built by calling the named function
47     public $columns = array ( 'LBL_DEFAULT' => 'getDefaultFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
48     protected $labelIdentifier = 'vname' ; // labels in the subpanel defs are tagged 'vname' =>
49
50     /*
51      * Constructor
52      * Must set:
53      * $this->columns   Array of 'Column LBL'=>function_to_retrieve_fields_for_this_column() - expected by the view
54      *
55      * @param string subpanelName   The name of this subpanel
56      * @param string moduleName     The name of the module to which this subpanel belongs
57      * @param string packageName    If not empty, the name of the package to which this subpanel belongs
58      */
59     function __construct ($subpanelName , $moduleName , $packageName = '')
60     {
61         $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . ": __construct()" ) ;
62
63         // TODO: check the implementations
64         if (empty ( $packageName ))
65         {
66             require_once 'modules/ModuleBuilder/parsers/views/DeployedSubpanelImplementation.php' ;
67             $this->implementation = new DeployedSubpanelImplementation ( $subpanelName, $moduleName ) ;
68             //$this->originalViewDef = $this->implementation->getOriginalDefs ();
69         } else
70         {
71             require_once 'modules/ModuleBuilder/parsers/views/UndeployedSubpanelImplementation.php' ;
72             $this->implementation = new UndeployedSubpanelImplementation ( $subpanelName, $moduleName, $packageName ) ;
73         }
74
75         $this->_viewdefs = array_change_key_case ( $this->implementation->getViewdefs () ) ; // force to lower case so don't have problems with case mismatches later
76         $this->_fielddefs =  $this->implementation->getFielddefs ();
77         $this->_standardizeFieldLabels( $this->_fielddefs );
78         $GLOBALS['log']->debug ( get_class($this)."->__construct(): viewdefs = ".print_r($this->_viewdefs,true));
79         $GLOBALS['log']->debug ( get_class($this)."->__construct(): viewdefs = ".print_r($this->_viewdefs,true));
80         $this->_invisibleFields = $this->findInvisibleFields( $this->_viewdefs ) ;
81
82         $GLOBALS['log']->debug ( get_class($this)."->__construct(): invisibleFields = ".print_r($this->_invisibleFields,true));
83     }
84
85     /*
86      * Save the layout
87      */
88     function handleSave ($populate = true)
89     {
90         if ($populate)
91         {
92             $this->_populateFromRequest() ;
93             if (isset ($_REQUEST['subpanel_title']) && isset($_REQUEST['subpanel_title_key'])) {
94                     $selected_lang = (!empty($_REQUEST['selected_lang'])? $_REQUEST['selected_lang']:$_SESSION['authenticated_user_language']);
95                         if(empty($selected_lang)){
96                             $selected_lang = $GLOBALS['sugar_config']['default_language'];
97                         }
98                         require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
99                 $labelParser = new ParserLabel ( $_REQUEST['view_module'] , isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
100                 $labelParser->addLabels($selected_lang, array($_REQUEST['subpanel_title_key'] =>  remove_xss(from_html($_REQUEST['subpanel_title']))), $_REQUEST['view_module']);
101             }            
102         } 
103         // Bug 46291 - Missing widget_class for edit_button and remove_button
104         foreach ( $this->_viewdefs as $key => $def )
105         {        
106             if (isset ( $this->_fielddefs [ $key ] [ 'widget_class' ]))
107             {
108                 $this->_viewdefs [ $key ] [ 'widget_class' ] = $this->_fielddefs [ $key ] [ 'widget_class' ];
109             } 
110         }
111         $defs = $this->restoreInvisibleFields($this->_invisibleFields,$this->_viewdefs); // unlike our parent, do not force the field names back to upper case
112         $defs = $this->makeRelateFieldsAsLink($defs);
113         $this->implementation->deploy ($defs);
114     }
115
116     /**
117      * Return a list of the default fields for a subpanel
118      * TODO: have this return just a list of fields, without definitions
119      * @return array    List of default fields as an array, where key = value = <field name>
120      */
121     function getDefaultFields ()
122     {
123         $defaultFields = array ( ) ;
124         foreach ( $this->_viewdefs as $key => $def )
125         {
126             if (empty ( $def [ 'usage' ] ) || strcmp ( $def [ 'usage' ], 'query_only' ) == 1)
127             {
128                 $defaultFields [ strtolower ( $key ) ] = $this->_viewdefs [ $key ] ;
129             }
130         }
131
132         return $defaultFields ;
133     }
134
135     /*
136      * Find the query_only fields in the viewdefs
137      * Query_only fields are used by the MVC to generate the subpanel but are not editable - they must be maintained in the layout
138      * @param viewdefs The viewdefs to be searched for invisible fields
139      * @return Array of invisible fields, ready to be provided to $this->restoreInvisibleFields
140      */
141     function findInvisibleFields( $viewdefs )
142     {
143         $invisibleFields = array () ;
144         foreach ( $viewdefs as $name => $def )
145             if ( isset($def [ 'usage' ] ) && ($def [ 'usage'] == 'query_only') )
146                 $invisibleFields [ $name ] = $def ;
147         return $invisibleFields ;
148     }
149
150     function restoreInvisibleFields ( $invisibleFields , $viewdefs )
151     {
152         foreach ( $invisibleFields as $name => $def )
153         {
154             $viewdefs [ $name ] = $def ;
155         }
156         return $viewdefs ;
157     }
158
159     static function _trimFieldDefs ( $def )
160     {
161         $listDef = parent::_trimFieldDefs($def);
162         if (isset($listDef ['label']))
163         {
164             $listDef ['vname'] = $listDef ['label'];
165             unset($listDef ['label']);
166         }
167         return $listDef;
168     }
169
170         /**
171      * makeRelateFieldsAsLink
172      * This method will go through the subpanel definition entries being saved and then apply formatting to any that are
173      * relate field so that a link to the related record may be shown in the subpanel code.  This is done by adding the
174      * widget_class, target_module and target_record_key deltas to the related subpanel definition entry.
175      *
176      * @param Array of subpanel definitions to possibly alter
177      * @return $defs Array of formatted subpanel definition entries to include any relate field attributes for Subpanels
178      */
179     protected function makeRelateFieldsAsLink($defs)
180     {
181         foreach($defs as $index => $fieldData)
182         {
183             if ((isset($fieldData['type']) && $fieldData['type'] == 'relate') 
184                 || (isset($fieldData['link']) && self::isTrue($fieldData['link'])))
185             {
186                 $defs[$index]['widget_class'] = 'SubPanelDetailViewLink';
187                 $defs[$index]['target_module'] = isset($this->_fielddefs[$index]['module']) ? $this->_fielddefs[$index]['module'] : $this->_moduleName;
188                 $defs[$index]['target_record_key'] = $this->_fielddefs[$index]['id_name'];
189             }
190         }
191
192         return $defs;
193     }
194
195 }
196 ?>