]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/Module/StudioModule.php
Release 6.2.0
[Github/sugarcrm.git] / modules / ModuleBuilder / Module / StudioModule.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
39 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
40
41 class StudioModule
42 {
43     public $name ;
44     private $popups = array ( ) ;
45     public $module ;
46     public $fields ;
47     public $seed;
48
49     function __construct ($module)
50     {
51                 $this->sources = array (        'editviewdefs.php' => array ( 'name' => translate ('LBL_EDITVIEW') , 'type' => MB_EDITVIEW , 'image' => 'EditView' ) ,
52                                                                 'detailviewdefs.php' => array ( 'name' => translate('LBL_DETAILVIEW') , 'type' => MB_DETAILVIEW , 'image' => 'DetailView' ) ,
53                                                                 'listviewdefs.php' => array ( 'name' => translate('LBL_LISTVIEW') , 'type' => MB_LISTVIEW , 'image' => 'ListView' ) ) ;
54
55         $moduleNames = array_change_key_case ( $GLOBALS [ 'app_list_strings' ] [ 'moduleList' ] ) ;
56         $this->name = isset ( $moduleNames [ strtolower ( $module ) ] ) ? $moduleNames [ strtolower ( $module ) ] : strtolower ( $module ) ;
57         $this->module = $module ;
58         $class = $GLOBALS [ 'beanList' ] [ $this->module ] ;
59         require_once $GLOBALS [ 'beanFiles' ] [ $class ] ;
60         $this->seed = new $class ( ) ;
61         $this->fields = $this->seed->field_defs ;
62         //$GLOBALS['log']->debug ( get_class($this)."->__construct($module): ".print_r($this->fields,true) ) ;
63     }
64
65      /*
66      * Gets the name of this module. Some modules have naming inconsistencies such as Bug Tracker and Bugs which causes warnings in Relationships
67      * Added to resolve bug #20257
68      */
69     function getModuleName()
70     {
71         $modules_with_odd_names = array(
72         'Bug Tracker'=>'Bugs'
73         );
74         if ( isset ( $modules_with_odd_names [ $this->name ] ) )
75                 return ( $modules_with_odd_names [ $this->name ] ) ;
76
77         return $this->name;
78     }
79
80     /*
81      * Attempt to determine the type of a module, for example 'basic' or 'company'
82      * These types are defined by the SugarObject Templates in /include/SugarObjects/templates
83      * Custom modules extend one of these standard SugarObject types, so the type can be determined from their parent
84      * Standard module types can be determined simply from the module name - 'bugs' for example is of type 'issue'
85          * If all else fails, fall back on type 'basic'...
86          * @return string Module's type
87      */
88     function getType ()
89     {
90         // first, get a list of a possible parent types
91         $templates = array () ;
92         $d = dir ( 'include/SugarObjects/templates' ) ;
93                 while ( $filename = $d->read() )
94                 {
95                         if ( substr($filename,0,1) != '.' )
96                                 $templates [ strtolower ( $filename) ] = strtolower ( $filename ) ;
97                 }
98
99                 // If a custom module, then its type is determined by the parent SugarObject that it extends
100                 $type = $GLOBALS [ 'beanList' ] [ $this->module ] ;
101         require_once $GLOBALS [ 'beanFiles' ] [ $type ] ;
102
103         do
104         {
105                 $seed = new $type () ;
106                 $type = get_parent_class ($seed) ;
107         } while ( ! in_array ( strtolower ( $type ) , $templates ) && $type != 'SugarBean' ) ;
108
109         if ( $type != 'SugarBean' )
110         {
111                 return strtolower ( $type ) ;
112         }
113
114         // If a standard module then just look up its type - type is implicit for standard modules. Perhaps one day we will make it explicit, just as we have done for custom modules...
115                 $types = array (
116                 'Accounts' => 'company' , 
117                 'Bugs' => 'issue' , 
118                 'Cases' => 'issue' , 
119                 'Contacts' => 'person' , 
120                 'Documents' => 'file' , 
121                 'Leads' => 'person' , 
122                 'Opportunities' => 'sale'
123                 ) ;
124                 if ( isset ( $types [ $this->module ] ) )
125                         return $types [ $this->module ] ;
126
127         return "basic" ;
128     }
129
130     /*
131      * Return the fields for this module as sourced from the SugarBean
132      * @return  Array of fields
133      */
134
135     function getFields ()
136     {
137         return $this->fields ;
138     }
139
140     function getNodes ()
141     {
142         return array ( 'name' => $this->name , 'module' => $this->module , 'type' => 'StudioModule' , 'action' => "module=ModuleBuilder&action=wizard&view_module={$this->module}" , 'children' => $this->getModule() ) ;
143     }
144
145     function getModule ()
146     {
147         $sources = array (      translate('LBL_LABELS') => array ( 'action' => "module=ModuleBuilder&action=editLabels&view_module={$this->module}" , 'imageTitle' => 'Labels' , 'help' => 'labelsBtn' ) ,
148                                                 translate('LBL_FIELDS') => array ( 'action' => "module=ModuleBuilder&action=modulefields&view_package=studio&view_module={$this->module}" , 'imageTitle' => 'Fields' , 'help' => 'fieldsBtn'  ) ,
149                                                 translate('LBL_RELATIONSHIPS') => array ( 'action' => "get_tpl=true&module=ModuleBuilder&action=relationships&view_module={$this->module}" , 'imageTitle' => 'Relationships' , 'help' => 'relationshipsBtn' ) ,
150                                                 translate('LBL_LAYOUTS') => array ( 'children' => 'getLayouts' , 'action' => "module=ModuleBuilder&action=wizard&view=layouts&view_module={$this->module}" , 'imageTitle' => 'Layouts' , 'help' => 'layoutsBtn' ) ,
151                                                 translate('LBL_SUBPANELS') => array ( 'children' => 'getSubpanels' , 'action' => "module=ModuleBuilder&action=wizard&view=subpanels&view_module={$this->module}" , 'imageTitle' => 'Subpanels' , 'help' => 'subpanelsBtn' ) ) ;
152
153         $nodes = array () ;
154         foreach ( $sources as $source => $def )
155         {
156                 $nodes [ $source ] = $def ;
157                 $nodes [ $source ] [ 'name' ] = translate ( $source ) ;
158                 if ( isset ( $def [ 'children' ] ) )
159                 {
160                         $childNodes = $this->$def [ 'children' ] () ;
161                         if ( !empty ( $childNodes ) )
162                         {
163                                 $nodes [ $source ] [ 'type' ] = 'Folder' ;
164                                 $nodes [ $source ] [ 'children' ] = $childNodes ;
165                         }
166                         else
167                                 unset ( $nodes [ $source ] ) ;
168                 }
169         }
170
171         return $nodes ;
172     }
173     
174     function getViews() {
175         $views = array () ;
176         foreach ( $this->sources as $file => $def )
177         {
178             if (file_exists ( "modules/{$this->module}/metadata/$file" ))
179             {
180                 $views [ str_replace ( '.php', '' , $file) ] = $def ;
181             }
182         }
183         return $views;
184     }
185
186     function getLayouts()
187     {
188         $views = $this->getViews();
189
190         // Now add in the QuickCreates - quickcreatedefs can be created by Studio from editviewdefs if they are absent, so just add them in regardless of whether the quickcreatedefs file exists
191
192         $hideQuickCreateForModules = array ( 'kbdocuments' , 'projecttask' , 
193             'campaigns'
194             ) ;
195         // Some modules should not have a QuickCreate form at all, so do not add them to the list
196         if (! in_array ( strtolower ( $this->module ), $hideQuickCreateForModules ))
197             $views [ 'quickcreatedefs' ] = array ( 'name' => translate('LBL_QUICKCREATE') , 'type' => MB_QUICKCREATE , 'image' => 'QuickCreate' ) ;
198
199         $layouts = array ( ) ;
200         foreach ( $views as $def )
201         {
202             $layouts [ $def['name'] ] = array ( 'name' => $def['name'] , 'action' => "module=ModuleBuilder&action=editLayout&view={$def['type']}&view_module={$this->module}" , 'imageTitle' => $def['image'] , 'help' => "viewBtn{$def['type']}" , 'size' => '48' ) ;
203         }
204
205         if($this->isValidDashletModule($this->module)){
206                         $dashlets = array( );
207                 $dashlets [] = array('name' => translate('LBL_DASHLETLISTVIEW') , 'type' => 'dashlet' , 'action' => 'module=ModuleBuilder&action=editLayout&view=dashlet&view_module=' . $this->module );
208                         $dashlets [] = array('name' => translate('LBL_DASHLETSEARCHVIEW') , 'type' => 'dashletsearch' , 'action' => 'module=ModuleBuilder&action=editLayout&view=dashletsearch&view_module=' . $this->module );
209                         $layouts [ translate('LBL_DASHLET') ] = array ( 'name' => translate('LBL_DASHLET') , 'type' => 'Folder', 'children' => $dashlets,  'imageTitle' => 'Dashlet',  'action' => 'module=ModuleBuilder&action=wizard&view=dashlet&view_module=' . $this->module);             
210         }
211                 
212         //For popup tree node
213         $popups = array( );
214         $popups [] = array('name' => translate('LBL_POPUPLISTVIEW') , 'type' => 'popuplistview' , 'action' => 'module=ModuleBuilder&action=editLayout&view=popuplist&view_module=' . $this->module );
215                 $popups [] = array('name' => translate('LBL_POPUPSEARCH') , 'type' => 'popupsearch' , 'action' => 'module=ModuleBuilder&action=editLayout&view=popupsearch&view_module=' . $this->module );
216                 $layouts [ translate('LBL_POPUP') ] = array ( 'name' => translate('LBL_POPUP') , 'type' => 'Folder', 'children' => $popups, 'imageTitle' => 'Popup',  'imageName' => 'icon_Popup.gif', 'action' => 'module=ModuleBuilder&action=wizard&view=popup&view_module=' . $this->module);  
217                         
218         $nodes = $this->getSearch () ;
219         if ( !empty ( $nodes ) )
220         {
221                 $layouts [ translate('LBL_SEARCH') ] = array ( 'name' => translate('LBL_SEARCH') , 'type' => 'Folder' , 'children' => $nodes , 'action' => "module=ModuleBuilder&action=wizard&view=search&view_module={$this->module}" , 'imageTitle' => 'SearchForm' , 'help' => 'searchBtn' , 'size' => '48') ;
222         }
223
224         return $layouts ;
225
226     }
227
228         function isValidDashletModule($moduleName){
229                 $fileName = "My{$moduleName}Dashlet";
230                 $customFileName = "{$moduleName}Dashlet";
231                 if (file_exists ( "modules/{$moduleName}/Dashlets/{$fileName}/{$fileName}.php" )
232                         || file_exists ( "custom/modules/{$moduleName}/Dashlets/{$fileName}/{$fileName}.php" ) 
233                         || file_exists ( "modules/{$moduleName}/Dashlets/{$customFileName}/{$customFileName}.php" )
234                         || file_exists ( "custom/modules/{$moduleName}/Dashlets/{$customFileName}/{$customFileName}.php" ))
235         {
236                 return true;
237         }
238         return false;
239         }
240         
241
242     function getSearch ()
243     {
244                 require_once ('modules/ModuleBuilder/parsers/views/SearchViewMetaDataParser.php') ;
245
246                 $nodes = array () ;
247         foreach ( array ( MB_BASICSEARCH => 'LBL_BASIC_SEARCH' , MB_ADVANCEDSEARCH => 'LBL_ADVANCED_SEARCH' ) as $view => $label )
248         {
249                 try
250                 {
251                         $parser = new SearchViewMetaDataParser ( $view , $this->module ) ;
252                         $title = translate ( $label ) ;
253                         if($label == 'LBL_BASIC_SEARCH'){
254                                         $name = 'BasicSearch';
255                                 }elseif($label == 'LBL_ADVANCED_SEARCH'){
256                                         $name = 'AdvancedSearch';
257                                 }else{
258                                         $name = str_replace ( ' ', '', $title ) ;
259                                 }
260                 $nodes [ $title ] = array ( 'name' => $title , 'action' => "module=ModuleBuilder&action=editLayout&view={$view}&view_module={$this->module}" , 'imageTitle' => $title , 'imageName' => $name , 'help' => "{$name}Btn" , 'size' => '48' ) ;
261                 }
262                 catch ( Exception $e )
263                 {
264                         $GLOBALS [ 'log' ]->info( 'No search layout : '. $e->getMessage() ) ;
265                 }
266         }
267
268         return $nodes ;
269     }
270
271     /*
272      * Return an object containing all the relationships participated in by this module
273      * @return AbstractRelationships Set of relationships
274      */
275     function getRelationships ()
276     {
277         return new DeployedRelationships ( $this->module ) ;
278     }
279
280
281     /**
282      * Gets a list of subpanels used by the current module
283      */
284     function getSubpanels ()
285     {
286         if(!empty($GLOBALS['current_user']) && empty($GLOBALS['modListHeader']))
287             $GLOBALS['modListHeader'] = query_module_access_list($GLOBALS['current_user']);
288
289         require_once ('include/SubPanel/SubPanel.php') ;
290
291         $nodes = array ( ) ;
292
293             $GLOBALS [ 'log' ]->debug ( "StudioModule->getSubpanels(): getting subpanels for " . $this->module ) ;
294
295             foreach ( SubPanel::getModuleSubpanels ( $this->module ) as $name => $label )
296             {
297                 if ($name == 'users')
298                     continue ;
299                 $subname = sugar_ucfirst ( (! empty ( $label )) ? translate ( $label, $this->module ) : $name ) ;
300                 $nodes [ $subname ] = array ( 
301                         'name' => $name , 
302                         'label' => $subname , 
303                         'action' => "module=ModuleBuilder&action=editLayout&view=ListView&view_module={$this->module}&subpanel={$name}&subpanelLabel={$subname}" , 
304                         'imageTitle' => $subname , 
305                         'imageName' => 'icon_' . ucfirst($name) . '_32', 
306                         'altImageName' => 'Subpanels', 
307                         'size' => '48' 
308                 ) ;
309             }
310
311         return $nodes ;
312
313     }
314
315     /**
316      * gets a list of subpanels provided to other modules
317      *
318      *
319      */
320     function getProvidedSubpanels ()
321     {
322         require_once 'modules/ModuleBuilder/parsers/relationships/AbstractRelationships.php' ;
323         $this->providedSubpanels = array () ;
324         $subpanelDir = 'modules/' . $this->module . '/metadata/subpanels/' ;
325         foreach(array($subpanelDir, "custom/$subpanelDir") as $dir)
326         {
327                 if (is_dir ( $dir ))
328                 {
329                     foreach(scandir($dir) as $fileName)
330                     {
331                         // sanity check to confirm that this is a usable subpanel...
332                         if (substr ( $fileName, 0, 1 ) != '.' && substr ( strtolower($fileName), -4 ) == ".php" 
333                                 && AbstractRelationships::validSubpanel ( "$dir/$fileName" ))
334                         {
335                             $subname = str_replace ( '.php', '', $fileName ) ;
336                             $this->providedSubpanels [ $subname ] = $subname ;
337                         }
338                     }
339                 }
340         }
341
342                 return $this->providedSubpanels;
343     }
344
345     
346     function getParentModulesOfSubpanel($subpanel){
347         global $moduleList, $beanFiles, $beanList, $module;
348     
349         //use tab controller function to get module list with named keys
350         require_once("modules/MySettings/TabController.php");
351         require_once("include/SubPanel/SubPanelDefinitions.php");
352         $modules_to_check = TabController::get_key_array($moduleList);
353
354         //change case to match subpanel processing later on
355         $modules_to_check = array_change_key_case($modules_to_check);
356     
357         $spd = '';
358         $spd_arr = array();
359         //iterate through modules and build subpanel array  
360         foreach($modules_to_check as $mod_name){
361             
362             //skip if module name is not in bean list, otherwise get the bean class name
363             if(!isset($beanList[$mod_name])) continue;
364             $class = $beanList[$mod_name];
365
366             //skip if class name is not in file list, otherwise require the bean file and create new class
367             if(!isset($beanFiles[$class]) || !file_exists($beanFiles[$class])) continue;
368             
369             //retrieve subpanels for this bean
370             require_once($beanFiles[$class]);
371             $bean_class = new $class();
372
373             //create new subpanel definition instance and get list of tabs
374             $spd = new SubPanelDefinitions($bean_class) ;
375             if ( isset($spd->layout_defs['subpanel_setup'][strtolower($subpanel)]['module']) ){
376                 $spd_arr[] = $mod_name;
377             }
378         }
379         return  $spd_arr;
380     }
381
382     function removeFieldFromLayouts ( $fieldName )
383     {
384         require_once("modules/ModuleBuilder/parsers/ParserFactory.php");
385         $GLOBALS [ 'log' ]->info ( get_class ( $this ) . "->removeFieldFromLayouts($fieldName)" ) ;
386         $sources = $this->getViewMetadataSources();
387         $sources[] = array('type'  => MB_BASICSEARCH);
388         $sources[] = array('type'  => MB_ADVANCEDSEARCH);
389         
390         $GLOBALS [ 'log' ]->debug ( print_r( $sources,true) ) ;
391         foreach ( $sources as $name => $defs )
392         {
393             //If this module type doesn't support a given metadata type, we will get an exception from getParser()
394             try {
395                 $parser = ParserFactory::getParser( $defs [ 'type' ] , $this->module ) ;
396                 if ($parser->removeField ( $fieldName ) )
397                     $parser->handleSave(false) ; // don't populate from $_REQUEST, just save as is...
398             } catch(Exception $e){}
399         }
400         
401         //Remove the fields in subpanel
402         $data = $this->getParentModulesOfSubpanel($this->module);
403         foreach($data as $parentModule){
404             //If this module type doesn't support a given metadata type, we will get an exception from getParser()
405             try {
406                 $parser = ParserFactory::getParser( MB_LISTVIEW , $parentModule, null ,  $this->module) ;
407                 if ($parser->removeField ( $fieldName ) )
408                     $parser->handleSave(false) ;
409             } catch(Exception $e){}
410         }
411     }
412
413         
414         
415         public function getViewMetadataSources() {
416                 $sources = $this->getViews();
417         $sources[] = array('type'  => MB_BASICSEARCH);
418         $sources[] = array('type'  => MB_ADVANCEDSEARCH);
419         $sources[] = array('type'  => MB_DASHLET);
420         $sources[] = array('type'  => MB_DASHLETSEARCH);
421         $sources[] = array('type'  => MB_POPUPLIST);
422         $sources[] = array('type'  => MB_QUICKCREATE);
423                 
424                 return $sources;
425         }
426         
427         
428         
429 }
430 ?>