]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/Module/StudioModule.php
Merge pull request #100 from collinlee/master
[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-2012 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 'data/BeanFactory.php';
39 require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
40 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
41
42 class StudioModule
43 {
44     public $name ;
45     private $popups = array ( ) ;
46     public $module ;
47     public $fields ;
48     public $seed;
49
50     function __construct ($module)
51     {
52                 //Sources can be used to override the file name mapping for a specific view or the parser for a view.
53         //The
54         $this->sources = array (        'editviewdefs.php' => array ( 'name' => translate ('LBL_EDITVIEW') , 'type' => MB_EDITVIEW , 'image' => 'EditView' ) ,
55                                                                 'detailviewdefs.php' => array ( 'name' => translate('LBL_DETAILVIEW') , 'type' => MB_DETAILVIEW , 'image' => 'DetailView' ) ,
56                                                                 'listviewdefs.php' => array ( 'name' => translate('LBL_LISTVIEW') , 'type' => MB_LISTVIEW , 'image' => 'ListView' ) ) ;
57
58         $moduleNames = array_change_key_case ( $GLOBALS [ 'app_list_strings' ] [ 'moduleList' ] ) ;
59         $this->name = isset ( $moduleNames [ strtolower ( $module ) ] ) ? $moduleNames [ strtolower ( $module ) ] : strtolower ( $module ) ;
60         $this->module = $module ;
61         $this->seed = BeanFactory::getBean($this->module);
62         $this->fields = $this->seed->field_defs ;
63         //$GLOBALS['log']->debug ( get_class($this)."->__construct($module): ".print_r($this->fields,true) ) ;
64     }
65
66      /*
67      * Gets the name of this module. Some modules have naming inconsistencies such as Bug Tracker and Bugs which causes warnings in Relationships
68      * Added to resolve bug #20257
69      */
70     function getModuleName()
71     {
72         $modules_with_odd_names = array(
73         'Bug Tracker'=>'Bugs'
74         );
75         if ( isset ( $modules_with_odd_names [ $this->name ] ) )
76                 return ( $modules_with_odd_names [ $this->name ] ) ;
77
78         return $this->name;
79     }
80
81     /*
82      * Attempt to determine the type of a module, for example 'basic' or 'company'
83      * These types are defined by the SugarObject Templates in /include/SugarObjects/templates
84      * Custom modules extend one of these standard SugarObject types, so the type can be determined from their parent
85      * Standard module types can be determined simply from the module name - 'bugs' for example is of type 'issue'
86          * If all else fails, fall back on type 'basic'...
87          * @return string Module's type
88      */
89     function getType ()
90     {
91         // first, get a list of a possible parent types
92         $templates = array () ;
93         $d = dir ( 'include/SugarObjects/templates' ) ;
94                 while ( $filename = $d->read() )
95                 {
96                         if ( substr($filename,0,1) != '.' )
97                                 $templates [ strtolower ( $filename) ] = strtolower ( $filename ) ;
98                 }
99
100                 // If a custom module, then its type is determined by the parent SugarObject that it extends
101                 $type = $GLOBALS [ 'beanList' ] [ $this->module ] ;
102         require_once $GLOBALS [ 'beanFiles' ] [ $type ] ;
103
104         do
105         {
106                 $seed = new $type () ;
107                 $type = get_parent_class ($seed) ;
108         } while ( ! in_array ( strtolower ( $type ) , $templates ) && $type != 'SugarBean' ) ;
109
110         if ( $type != 'SugarBean' )
111         {
112                 return strtolower ( $type ) ;
113         }
114
115         // 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...
116                 $types = array (
117                 'Accounts' => 'company' , 
118                 'Bugs' => 'issue' , 
119                 'Cases' => 'issue' , 
120                 'Contacts' => 'person' , 
121                 'Documents' => 'file' , 
122                 'Leads' => 'person' , 
123                 'Opportunities' => 'sale'
124                 ) ;
125                 if ( isset ( $types [ $this->module ] ) )
126                         return $types [ $this->module ] ;
127
128         return "basic" ;
129     }
130
131     /*
132      * Return the fields for this module as sourced from the SugarBean
133      * @return  Array of fields
134      */
135
136     function getFields ()
137     {
138         return $this->fields ;
139     }
140
141     function getNodes ()
142     {
143         return array ( 'name' => $this->name , 'module' => $this->module , 'type' => 'StudioModule' , 'action' => "module=ModuleBuilder&action=wizard&view_module={$this->module}" , 'children' => $this->getModule() ) ;
144     }
145
146     function getModule ()
147     {
148         $sources = array (      translate('LBL_LABELS') => array ( 'action' => "module=ModuleBuilder&action=editLabels&view_module={$this->module}" , 'imageTitle' => 'Labels' , 'help' => 'labelsBtn' ) ,
149                                                 translate('LBL_FIELDS') => array ( 'action' => "module=ModuleBuilder&action=modulefields&view_package=studio&view_module={$this->module}" , 'imageTitle' => 'Fields' , 'help' => 'fieldsBtn'  ) ,
150                                                 translate('LBL_RELATIONSHIPS') => array ( 'action' => "get_tpl=true&module=ModuleBuilder&action=relationships&view_module={$this->module}" , 'imageTitle' => 'Relationships' , 'help' => 'relationshipsBtn' ) ,
151                                                 translate('LBL_LAYOUTS') => array ( 'children' => 'getLayouts' , 'action' => "module=ModuleBuilder&action=wizard&view=layouts&view_module={$this->module}" , 'imageTitle' => 'Layouts' , 'help' => 'layoutsBtn' ) ,
152                                                 translate('LBL_SUBPANELS') => array ( 'children' => 'getSubpanels' , 'action' => "module=ModuleBuilder&action=wizard&view=subpanels&view_module={$this->module}" , 'imageTitle' => 'Subpanels' , 'help' => 'subpanelsBtn' ) ) ;
153
154         $nodes = array () ;
155         foreach ( $sources as $source => $def )
156         {
157                 $nodes [ $source ] = $def ;
158                 $nodes [ $source ] [ 'name' ] = translate ( $source ) ;
159                 if ( isset ( $def [ 'children' ] ) )
160                 {
161                         $childNodes = $this->$def [ 'children' ] () ;
162                         if ( !empty ( $childNodes ) )
163                         {
164                                 $nodes [ $source ] [ 'type' ] = 'Folder' ;
165                                 $nodes [ $source ] [ 'children' ] = $childNodes ;
166                         }
167                         else
168                                 unset ( $nodes [ $source ] ) ;
169                 }
170         }
171
172         return $nodes ;
173     }
174     
175     function getViews() {
176         $views = array () ;
177         foreach ( $this->sources as $file => $def )
178         {
179             if (file_exists ( "modules/{$this->module}/metadata/$file" )
180                 || file_exists ( "custom/modules/{$this->module}/metadata/$file" ))
181             {
182                 $views [ str_replace ( '.php', '' , $file) ] = $def ;
183             }
184         }
185         return $views;
186     }
187
188     function getLayouts()
189     {
190         $views = $this->getViews();
191
192         // 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
193
194         $hideQuickCreateForModules = array ( 'kbdocuments' , 'projecttask' , 
195             'campaigns'
196             ) ;
197         // Some modules should not have a QuickCreate form at all, so do not add them to the list
198         if (! in_array ( strtolower ( $this->module ), $hideQuickCreateForModules ))
199             $views [ 'quickcreatedefs' ] = array ( 'name' => translate('LBL_QUICKCREATE') , 'type' => MB_QUICKCREATE , 'image' => 'QuickCreate' ) ;
200
201         $layouts = array ( ) ;
202         foreach ( $views as $def )
203         {
204             $view = !empty($def['view']) ? $def['view'] : $def['type'];
205             $layouts [ $def['name'] ] = array ( 'name' => $def['name'] , 'action' => "module=ModuleBuilder&action=editLayout&view={$view}&view_module={$this->module}" , 'imageTitle' => $def['image'] , 'help' => "viewBtn{$def['type']}" , 'size' => '48' ) ;
206         }
207
208         if($this->isValidDashletModule($this->module)){
209                         $dashlets = array( );
210                 $dashlets [] = array('name' => translate('LBL_DASHLETLISTVIEW') , 'type' => 'dashlet' , 'action' => 'module=ModuleBuilder&action=editLayout&view=dashlet&view_module=' . $this->module );
211                         $dashlets [] = array('name' => translate('LBL_DASHLETSEARCHVIEW') , 'type' => 'dashletsearch' , 'action' => 'module=ModuleBuilder&action=editLayout&view=dashletsearch&view_module=' . $this->module );
212                         $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);             
213         }
214                 
215         //For popup tree node
216         $popups = array( );
217         $popups [] = array('name' => translate('LBL_POPUPLISTVIEW') , 'type' => 'popuplistview' , 'action' => 'module=ModuleBuilder&action=editLayout&view=popuplist&view_module=' . $this->module );
218                 $popups [] = array('name' => translate('LBL_POPUPSEARCH') , 'type' => 'popupsearch' , 'action' => 'module=ModuleBuilder&action=editLayout&view=popupsearch&view_module=' . $this->module );
219                 $layouts [ translate('LBL_POPUP') ] = array ( 'name' => translate('LBL_POPUP') , 'type' => 'Folder', 'children' => $popups, 'imageTitle' => 'Popup', 'action' => 'module=ModuleBuilder&action=wizard&view=popup&view_module=' . $this->module);  
220                         
221         $nodes = $this->getSearch () ;
222         if ( !empty ( $nodes ) )
223         {
224                 $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' => 'BasicSearch' , 'help' => 'searchBtn' , 'size' => '48') ;
225         }
226
227         return $layouts ;
228
229     }
230
231         function isValidDashletModule($moduleName){
232                 $fileName = "My{$moduleName}Dashlet";
233                 $customFileName = "{$moduleName}Dashlet";
234                 if (file_exists ( "modules/{$moduleName}/Dashlets/{$fileName}/{$fileName}.php" )
235                         || file_exists ( "custom/modules/{$moduleName}/Dashlets/{$fileName}/{$fileName}.php" ) 
236                         || file_exists ( "modules/{$moduleName}/Dashlets/{$customFileName}/{$customFileName}.php" )
237                         || file_exists ( "custom/modules/{$moduleName}/Dashlets/{$customFileName}/{$customFileName}.php" ))
238         {
239                 return true;
240         }
241         return false;
242         }
243         
244
245     function getSearch ()
246     {
247                 require_once ('modules/ModuleBuilder/parsers/views/SearchViewMetaDataParser.php') ;
248
249                 $nodes = array () ;
250         foreach ( array ( MB_BASICSEARCH => 'LBL_BASIC_SEARCH' , MB_ADVANCEDSEARCH => 'LBL_ADVANCED_SEARCH' ) as $view => $label )
251         {
252                 try
253                 {
254                         $parser = new SearchViewMetaDataParser ( $view , $this->module ) ;
255                         $title = translate ( $label ) ;
256                         if($label == 'LBL_BASIC_SEARCH'){
257                                         $name = 'BasicSearch';
258                                 }elseif($label == 'LBL_ADVANCED_SEARCH'){
259                                         $name = 'AdvancedSearch';
260                                 }else{
261                                         $name = str_replace ( ' ', '', $title ) ;
262                                 }
263                 $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' ) ;
264                 }
265                 catch ( Exception $e )
266                 {
267                         $GLOBALS [ 'log' ]->info( 'No search layout : '. $e->getMessage() ) ;
268                 }
269         }
270
271         return $nodes ;
272     }
273
274     /*
275      * Return an object containing all the relationships participated in by this module
276      * @return AbstractRelationships Set of relationships
277      */
278     function getRelationships ()
279     {
280         return new DeployedRelationships ( $this->module ) ;
281     }
282
283
284     /**
285      * Gets a list of subpanels used by the current module
286      */
287     function getSubpanels ()
288     {
289         if(!empty($GLOBALS['current_user']) && empty($GLOBALS['modListHeader']))
290             $GLOBALS['modListHeader'] = query_module_access_list($GLOBALS['current_user']);
291
292         require_once ('include/SubPanel/SubPanel.php') ;
293
294         $nodes = array ( ) ;
295
296             $GLOBALS [ 'log' ]->debug ( "StudioModule->getSubpanels(): getting subpanels for " . $this->module ) ;
297
298             // counter to add a unique key to assoc array below
299             $ct=0;
300             foreach ( SubPanel::getModuleSubpanels ( $this->module ) as $name => $label )
301             {
302                 if ($name == 'users')
303                     continue ;
304                 $subname = sugar_ucfirst ( (! empty ( $label )) ? translate ( $label, $this->module ) : $name ) ;
305                 $action = "module=ModuleBuilder&action=editLayout&view=ListView&view_module={$this->module}&subpanel={$name}&subpanelLabel=" . urlencode($subname);
306
307                 //  bug47452 - adding a unique number to the $nodes[ key ] so if you have 2+ panels
308                 //  with the same subname they will not cancel each other out
309                 $nodes [ $subname . $ct++ ] = array (
310                         'name' => $name , 
311                         'label' => $subname , 
312                         'action' =>  $action,
313                         'imageTitle' => $subname , 
314                         'imageName' => 'icon_' . ucfirst($name) . '_32', 
315                         'altImageName' => 'Subpanels', 
316                         'size' => '48' 
317                 ) ;
318             }
319
320         return $nodes ;
321
322     }
323
324     /**
325      * gets a list of subpanels provided to other modules
326      *
327      *
328      */
329     function getProvidedSubpanels ()
330     {
331         require_once 'modules/ModuleBuilder/parsers/relationships/AbstractRelationships.php' ;
332         $this->providedSubpanels = array () ;
333         $subpanelDir = 'modules/' . $this->module . '/metadata/subpanels/' ;
334         foreach(array($subpanelDir, "custom/$subpanelDir") as $dir)
335         {
336                 if (is_dir ( $dir ))
337                 {
338                     foreach(scandir($dir) as $fileName)
339                     {
340                         // sanity check to confirm that this is a usable subpanel...
341                         if (substr ( $fileName, 0, 1 ) != '.' && substr ( strtolower($fileName), -4 ) == ".php" 
342                                 && AbstractRelationships::validSubpanel ( "$dir/$fileName" ))
343                         {
344                             $subname = str_replace ( '.php', '', $fileName ) ;
345                             $this->providedSubpanels [ $subname ] = $subname ;
346                         }
347                     }
348                 }
349         }
350
351                 return $this->providedSubpanels;
352     }
353
354     
355     function getParentModulesOfSubpanel($subpanel){
356         global $moduleList, $beanFiles, $beanList, $module;
357     
358         //use tab controller function to get module list with named keys
359         require_once("modules/MySettings/TabController.php");
360         require_once("include/SubPanel/SubPanelDefinitions.php");
361         $modules_to_check = TabController::get_key_array($moduleList);
362
363         //change case to match subpanel processing later on
364         $modules_to_check = array_change_key_case($modules_to_check);
365     
366         $spd = '';
367         $spd_arr = array();
368         //iterate through modules and build subpanel array  
369         foreach($modules_to_check as $mod_name){
370             
371             //skip if module name is not in bean list, otherwise get the bean class name
372             if(!isset($beanList[$mod_name])) continue;
373             $class = $beanList[$mod_name];
374
375             //skip if class name is not in file list, otherwise require the bean file and create new class
376             if(!isset($beanFiles[$class]) || !file_exists($beanFiles[$class])) continue;
377             
378             //retrieve subpanels for this bean
379             require_once($beanFiles[$class]);
380             $bean_class = new $class();
381
382             //create new subpanel definition instance and get list of tabs
383             $spd = new SubPanelDefinitions($bean_class);
384             if (isset($spd->layout_defs['subpanel_setup'])) 
385             {
386                 foreach ($spd->layout_defs['subpanel_setup'] as $panelname) 
387                 {
388                     if ($panelname['module'] == $subpanel) 
389                     {
390                         $spd_arr[] = array('mod_name'   => $mod_name,
391                                            'panel_name' => $panelname['get_subpanel_data']);
392                     }
393                 }
394             }
395         }
396         return  $spd_arr;
397     }
398
399     function removeFieldFromLayouts ( $fieldName )
400     {
401         require_once("modules/ModuleBuilder/parsers/ParserFactory.php");
402         $GLOBALS [ 'log' ]->info ( get_class ( $this ) . "->removeFieldFromLayouts($fieldName)" ) ;
403         $sources = $this->getViewMetadataSources();
404         $sources[] = array('type'  => MB_BASICSEARCH);
405         $sources[] = array('type'  => MB_ADVANCEDSEARCH);
406         $sources[] = array('type'  => MB_POPUPSEARCH);        
407         
408         $GLOBALS [ 'log' ]->debug ( print_r( $sources,true) ) ;
409         foreach ( $sources as $name => $defs )
410         {
411             //If this module type doesn't support a given metadata type, we will get an exception from getParser()
412             try {
413                 $parser = ParserFactory::getParser( $defs [ 'type' ] , $this->module ) ;
414                 if ($parser->removeField ( $fieldName ) )
415                     $parser->handleSave(false) ; // don't populate from $_REQUEST, just save as is...
416             } catch(Exception $e){}
417         }
418         
419         //Remove the fields in subpanel
420         $data = $this->getParentModulesOfSubpanel($this->module);
421         foreach($data as $parentModule){
422             //If this module type doesn't support a given metadata type, we will get an exception from getParser()
423             try {
424                 $parser = ParserFactory::getParser(MB_LISTVIEW, $parentModule['mod_name'], null, $parentModule['panel_name']);
425                 if ($parser->removeField($fieldName)) 
426                 {
427                     $parser->handleSave(false);
428                 }
429             } catch(Exception $e){}
430         }
431     }
432
433         
434         
435         public function getViewMetadataSources() {
436                 $sources = $this->getViews();
437         $sources[] = array('type'  => MB_BASICSEARCH);
438         $sources[] = array('type'  => MB_ADVANCEDSEARCH);
439         $sources[] = array('type'  => MB_DASHLET);
440         $sources[] = array('type'  => MB_DASHLETSEARCH);
441         $sources[] = array('type'  => MB_POPUPLIST);
442         $sources[] = array('type'  => MB_QUICKCREATE);
443                 
444                 return $sources;
445         }
446
447     public function getViewType($view)
448     {
449         foreach($this->sources as $file => $def)
450         {
451             if (!empty($def['view']) && $def['view'] == $view && !empty($def['type']))
452             {
453                 return $def['type'];
454             }
455         }
456         return $view;
457     }
458         
459         
460 }
461 ?>