]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/controller.php
Release 6.3.0
[Github/sugarcrm.git] / modules / ModuleBuilder / controller.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 require_once ('modules/ModuleBuilder/MB/ModuleBuilder.php') ;
38 require_once ('modules/ModuleBuilder/parsers/ParserFactory.php') ;
39 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
40
41 class ModuleBuilderController extends SugarController
42 {
43     var $action_remap = array ( ) ;
44
45     /**
46      * Used by the _getModuleTitleParams() method calls in ModuleBuilder views to get the correct string
47      * for the section you are in
48      *
49      * @return string
50      */
51     public static function getModuleTitle()
52     {
53         global $mod_strings;
54
55         if(!empty($_REQUEST['type'])){
56             if ( $_REQUEST['type'] == 'studio' ) {
57                 return $mod_strings['LBL_STUDIO'];
58             }
59             elseif ( $_REQUEST['type'] == 'sugarportal' ) {
60                 return $mod_strings['LBL_SUGARPORTAL'];
61             }
62             elseif ( $_REQUEST['type'] == 'mb' ) {
63                 return $mod_strings['LBL_MODULEBUILDER'];
64             }
65             elseif ( $_REQUEST['type'] == 'dropdowns') {
66                 return $mod_strings['LBL_DROPDOWNEDITOR'];
67             }
68             elseif ( $_REQUEST['type'] == 'home' ) {
69                 return $mod_strings['LBL_HOME'];
70             }
71             else {
72                 return $mod_strings['LBL_DEVELOPER_TOOLS'];
73             }
74         }else{
75             return $mod_strings['LBL_DEVELOPER_TOOLS'];
76         }
77     }
78
79     function fromModuleBuilder ()
80     {
81         return (isset ( $_REQUEST [ 'MB' ] ) && ($_REQUEST [ 'MB' ] == '1')) ;
82     }
83
84     function process(){
85         $GLOBALS [ 'log' ]->info ( get_class($this).":" ) ;
86         global $current_user;
87         $access = $current_user->getDeveloperModules();
88             if($current_user->isAdmin() || ($current_user->isDeveloperForAnyModule() && !isset($_REQUEST['view_module']) && (isset($_REQUEST['action']) && $_REQUEST['action'] != 'package'))||
89           (isset($_REQUEST['view_module']) && (in_array($_REQUEST['view_module'], $access)|| empty($_REQUEST['view_module']))) ||
90                (isset($_REQUEST['type']) && (($_REQUEST['type']=='dropdowns' && $current_user->isDeveloperForAnyModule())||
91           ($_REQUEST['type']=='studio' && displayStudioForCurrentUser() == true))))
92         {
93             $this->hasAccess = true;
94         }
95         else
96         {
97             $this->hasAccess = false;
98         }
99         parent::process();
100     }
101
102
103     function action_editLayout ()
104     {
105         switch ( strtolower ( $_REQUEST [ 'view' ] ))
106         {
107             case MB_EDITVIEW :
108             case MB_DETAILVIEW :
109             case MB_QUICKCREATE :
110                 $this->view = 'layoutView' ;
111                 break ;
112             case MB_LISTVIEW :
113                 $this->view = 'listView' ;
114                 break ;
115             case MB_BASICSEARCH :
116             case MB_ADVANCEDSEARCH :
117                 $this->view = 'searchView' ;
118                 break ;
119             case MB_DASHLET :
120             case MB_DASHLETSEARCH :
121                 $this->view = 'dashlet' ;
122                 break ;
123             case MB_POPUPLIST :
124             case MB_POPUPSEARCH :
125                 $this->view = 'popupview' ;
126                 break ;
127             default :
128                 $GLOBALS [ 'log' ]->fatal ( 'Action = editLayout with unknown view=' . $_REQUEST [ 'view' ] ) ;
129         }
130     }
131
132
133     function action_ViewTree ()
134     {
135         require_once ('modules/ModuleBuilder/MB/AjaxCompose.php') ;
136         switch ( $_REQUEST [ 'tree' ])
137         {
138             case 'ModuleBuilder' :
139                 require_once ('modules/ModuleBuilder/MB/MBPackageTree.php') ;
140                 $mbt = new MBPackageTree ( ) ;
141                 break ;
142             case 'Studio' :
143                 require_once ('modules/ModuleBuilder/Module/StudioTree.php') ;
144                 $mbt = new StudioTree ( ) ;
145         }
146         $ajax = new AjaxCompose ( ) ;
147         $ajax->addSection ( 'west', $mbt->getName (), $mbt->fetchNodes () ) ;
148         echo $ajax->getJavascript () ;
149
150         sugar_cleanup ( true ) ;
151
152     }
153
154     function action_SavePackage ()
155     {
156         $mb = new ModuleBuilder ( ) ;
157         $load = (! empty ( $_REQUEST [ 'original_name' ] )) ? $_REQUEST [ 'original_name' ] : $_REQUEST [ 'name' ] ;
158         if (! empty ( $load ))
159         {
160             $mb->getPackage ( $load ) ;
161
162             if (! empty ( $_REQUEST [ 'duplicate' ] ))
163             {
164                 $result = $mb->packages [ $load ]->copy ( $_REQUEST [ 'name' ] ) ;
165                 $load = $mb->packages [ $load ]->name ;
166                 $mb->getPackage ( $load ) ;
167             }
168             $mb->packages [ $load ]->populateFromPost () ;
169             $mb->packages [ $load ]->loadModules () ;
170             $mb->save () ;
171             if (! empty ( $_REQUEST [ 'original_name' ] ) && $_REQUEST [ 'original_name' ] != $_REQUEST [ 'name' ])
172             {
173                 if (! $mb->packages [ $load ]->rename ( $_REQUEST [ 'name' ] ))
174                 {
175                     $mb->packages [ $load ]->name = $_REQUEST [ 'original_name' ] ;
176                     $_REQUEST [ 'name' ] = $_REQUEST [ 'original_name' ] ;
177                 }
178             }
179             $_REQUEST [ 'package' ] = $_REQUEST [ 'name' ] ;
180             $this->view = 'package' ;
181         }
182     }
183
184     function action_BuildPackage ()
185     {
186         $mb = new ModuleBuilder ( ) ;
187         $load = $_REQUEST [ 'name' ] ;
188         if (! empty ( $load ))
189         {
190             $mb->getPackage ( $load ) ;
191             $mb->packages [ $load ]->build () ;
192         }
193     }
194
195     function action_DeployPackage ()
196     {
197         if(defined('TEMPLATE_URL')){
198                 sugar_cache_reset();
199                 SugarTemplateUtilities::disableCache();
200         }
201
202         $mb = new ModuleBuilder ( ) ;
203         $load = $_REQUEST [ 'package' ] ;
204         $message = $GLOBALS [ 'mod_strings' ] [ 'LBL_MODULE_DEPLOYED' ] ;
205         if (! empty ( $load ))
206         {
207             $zip = $mb->getPackage ( $load ) ;
208             require_once ('ModuleInstall/PackageManager/PackageManager.php') ;
209             $pm = new PackageManager ( ) ;
210             $info = $mb->packages [ $load ]->build ( false ) ;
211             mkdir_recursive ( $GLOBALS [ 'sugar_config' ] [ 'cache_dir' ] . '/upload/upgrades/module/') ;
212             rename ( $info [ 'zip' ], $GLOBALS [ 'sugar_config' ] [ 'cache_dir' ] . '/' . 'upload/upgrades/module/' . $info [ 'name' ] . '.zip' ) ;
213             copy ( $info [ 'manifest' ], $GLOBALS [ 'sugar_config' ] [ 'cache_dir' ] . '/' . 'upload/upgrades/module/' . $info [ 'name' ] . '-manifest.php' ) ;
214             $_REQUEST [ 'install_file' ] = $GLOBALS [ 'sugar_config' ] [ 'cache_dir' ] . '/' . 'upload/upgrades/module/' . $info [ 'name' ] . '.zip' ;
215             $GLOBALS [ 'mi_remove_tables' ] = false ;
216             $pm->performUninstall ( $load ) ;
217             //#23177 , js cache clear
218             clearAllJsAndJsLangFilesWithoutOutput();
219             //#30747, clear the cache in memory
220             $cache_key = 'app_list_strings.'.$GLOBALS['current_language'];
221             sugar_cache_clear($cache_key );
222             sugar_cache_reset();
223             //clear end
224             $pm->performInstall ( $_REQUEST [ 'install_file' ] , true);
225
226             //clear the unified_search_module.php file
227             require_once('modules/Home/UnifiedSearchAdvanced.php');
228             UnifiedSearchAdvanced::unlinkUnifiedSearchModulesFile();
229
230             //bug 44269 - start
231             global $current_user;
232             //clear workflow admin modules cache
233             if (isset($_SESSION['get_workflow_admin_modules_for_user'])) unset($_SESSION['get_workflow_admin_modules_for_user']);
234
235             //clear "is_admin_for_module" cache
236             $sessionVar = 'MLA_'.$current_user->user_name;
237             foreach ($mb->packages as $package) {
238                 foreach ($package->modules as $module) {
239                     $_SESSION[$sessionVar][$package->name . '_' . $module->name] = true;
240                 }
241             }
242
243             //recreate acl cache
244             $actions = ACLAction::getUserActions($current_user->id, true);
245             //bug 44269 - end
246         }
247
248         echo 'complete' ;
249     }
250
251     function action_ExportPackage ()
252     {
253         $mb = new ModuleBuilder ( ) ;
254         $load = $_REQUEST [ 'name' ] ;
255         $author = $_REQUEST [ 'author' ] ;
256         $description = $_REQUEST [ 'description' ] ;
257         $readme = $_REQUEST [ 'readme' ] ;
258         if (! empty ( $load ))
259         {
260             $mb->getPackage ( $load ) ;
261             $mb->packages [ $load ]->author = $author ;
262             $mb->packages [ $load ]->description = $description ;
263             $mb->packages [ $load ]->exportProject () ;
264             $mb->packages [ $load ]->readme = $readme ;
265         }
266     }
267
268     function action_DeletePackage ()
269     {
270         $mb = new ModuleBuilder ( ) ;
271         $mb->getPackage ( $_REQUEST [ 'package' ] ) ;
272         $mb->packages [ $_REQUEST [ 'package' ] ]->delete () ;
273         $this->view = 'deletepackage' ;
274     }
275
276     function action_SaveModule ()
277     {
278         $mb = new ModuleBuilder ( ) ;
279         $load = (! empty ( $_REQUEST [ 'original_name' ] )) ? $_REQUEST [ 'original_name' ] : $_REQUEST [ 'name' ] ;
280         if (! empty ( $load ))
281         {
282             $mb->getPackage ( $_REQUEST [ 'package' ] ) ;
283             $mb->packages [ $_REQUEST [ 'package' ] ]->getModule ( $load ) ;
284             $module = & $mb->packages [ $_REQUEST [ 'package' ] ]->modules [ $load ] ;
285             $module->populateFromPost () ;
286             $mb->save () ;
287             if (! empty ( $_REQUEST [ 'duplicate' ] ))
288             {
289                 $module->copy ( $_REQUEST [ 'name' ] ) ;
290             } else if (! empty ( $_REQUEST [ 'original_name' ] ) && $_REQUEST [ 'original_name' ] != $_REQUEST [ 'name' ])
291             {
292                 if (! $module->rename ( $_REQUEST [ 'name' ] ))
293                 {
294                     $module->name = $_REQUEST [ 'original_name' ] ;
295                     $_REQUEST [ 'name' ] = $_REQUEST [ 'original_name' ] ;
296                 }
297             }
298
299             $_REQUEST [ 'view_package' ] = $_REQUEST [ 'package' ] ;
300             $_REQUEST [ 'view_module' ] = $module->name ;
301             $this->view = 'module' ;
302         }
303     }
304
305     function action_DeleteModule ()
306     {
307         $mb = new ModuleBuilder ( ) ;
308         $module = & $mb->getPackageModule ( $_REQUEST [ 'package' ], $_REQUEST [ 'view_module' ] ) ;
309         $module->delete () ;
310         $this->view = 'package' ;
311     }
312
313     function action_saveLabels ()
314     {
315         require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
316         $parser = new ParserLabel ( $_REQUEST['view_module'] , isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
317         $parser->handleSave ( $_REQUEST, $_REQUEST [ 'selected_lang' ] ) ;
318         if (isset ( $_REQUEST [ 'view_package' ] )) //MODULE BUILDER
319         {
320             $this->view = 'modulelabels' ;
321         } else //STUDIO
322         {
323             $this->view = isset ( $_REQUEST [ 'view' ] ) ? 'edit' : 'labels' ; // detect if we are being called by the LayoutEditor rather than the LabelEditor (set in view.layoutlabel.php)
324         }
325     }
326
327     function action_SaveLabel ()
328     {
329         if (! empty ( $_REQUEST [ 'view_module' ] ) && !empty($_REQUEST [ 'labelValue' ]))
330         {
331             $_REQUEST [ "label_" . $_REQUEST [ 'label' ] ] = $_REQUEST [ 'labelValue' ] ;
332             require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
333             $parser = new ParserLabel ( $_REQUEST['view_module'] , isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
334             $parser->handleSave ( $_REQUEST, $GLOBALS [ 'current_language' ] ) ;
335
336         }
337         $this->view = 'modulefields' ;
338     }
339
340     function action_ExportCustom ()
341     {
342         $modules = $_REQUEST [ 'modules' ] ;
343         $name = $_REQUEST [ 'name' ] ;
344         $author = $_REQUEST [ 'author' ] ;
345         $description = $_REQUEST [ 'description' ] ;
346         ob_clean () ;
347         if (! empty ( $modules ) && ! empty ( $name ))
348         {
349             require_once ('modules/ModuleBuilder/MB/ModuleBuilder.php') ;
350             $mb = new MBPackage ( $name ) ;
351             $mb->author = $author ;
352             $mb->description = $description ;
353             $mb->exportCustom ( $modules, true, true ) ;
354         }
355     }
356
357     function action_SaveField ()
358     {
359         require_once ('modules/DynamicFields/FieldCases.php') ;
360         $field = get_widget ( $_REQUEST [ 'type' ] ) ;
361         $_REQUEST [ 'name' ] = trim ( $_REQUEST [ 'name' ] ) ;
362
363         $field->populateFromPost () ;
364
365         if (!isset ( $_REQUEST [ 'view_package' ] ))
366         {
367             require_once ('modules/DynamicFields/DynamicField.php') ;
368             if (! empty ( $_REQUEST [ 'view_module' ] ))
369             {
370                 $module = $_REQUEST [ 'view_module' ] ;
371
372                 $bean = BeanFactory::getBean($module);
373                 if(!empty($bean))
374                 {
375                         $field_defs = $bean->field_defs;
376                         if(isset($field_defs[$field->name. '_c']))
377                         {
378                                                 $GLOBALS['log']->error($GLOBALS['mod_strings']['ERROR_ALREADY_EXISTS'] . '[' . $field->name . ']');
379                                                 sugar_die($GLOBALS['mod_strings']['ERROR_ALREADY_EXISTS']);
380                         }
381                 }
382
383                 $df = new DynamicField ( $module ) ;
384                 $class_name = $GLOBALS [ 'beanList' ] [ $module ] ;
385                 require_once ($GLOBALS [ 'beanFiles' ] [ $class_name ]) ;
386                 $mod = new $class_name ( ) ;
387                 $df->setup ( $mod ) ;
388
389                 $field->save ( $df ) ;
390                 $this->action_SaveLabel () ;
391                 include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
392                         global $mod_strings;
393                 $mod_strings['LBL_ALL_MODULES'] = 'all_modules';
394                 require_once('ModuleInstall/ModuleInstaller.php');
395                 $mi = new ModuleInstaller();
396                 $mi->silent = true;
397                 $mi->rebuild_extensions();
398                 $repair = new RepairAndClear();
399                         $repair->repairAndClearAll(array('clearVardefs', 'clearTpls'), array($class_name), true, false);
400                 //#28707 ,clear all the js files in cache
401                         $repair->module_list = array();
402                         $repair->clearJsFiles();
403             }
404         } else
405         {
406             $mb = new ModuleBuilder ( ) ;
407             $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
408             $field->save ( $module ) ;
409             $module->mbvardefs->save () ;
410             // get the module again to refresh the labels we might have saved with the $field->save (e.g., for address fields)
411             $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
412             if (isset ( $_REQUEST [ 'label' ] ) && isset ( $_REQUEST [ 'labelValue' ] ))
413                 $module->setLabel ( $GLOBALS [ 'current_language' ], $_REQUEST [ 'label' ], $_REQUEST [ 'labelValue' ] ) ;
414             $module->save();
415         }
416         $this->view = 'modulefields' ;
417     }
418
419     function action_saveSugarField ()
420     {
421         global $mod_strings;
422         require_once ('modules/DynamicFields/FieldCases.php') ;
423         $field = get_widget ( $_REQUEST [ 'type' ] ) ;
424         $_REQUEST [ 'name' ] = trim ( $_POST [ 'name' ] ) ;
425
426         $field->populateFromPost () ;
427         require_once ('modules/ModuleBuilder/parsers/StandardField.php') ;
428         $module = $_REQUEST [ 'view_module' ] ;
429         $df = new StandardField ( $module ) ;
430         $mod = BeanFactory::getBean($module);
431         $class_name = $GLOBALS [ 'beanList' ] [ $module ] ;
432         $df->setup ( $mod ) ;
433
434         $field->module = $mod;
435         $field->save ( $df ) ;
436         $this->action_SaveLabel () ;
437
438         $MBmodStrings = $mod_strings;
439         $GLOBALS [ 'mod_strings' ] = return_module_language ( '', 'Administration' ) ;
440
441         include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
442         $GLOBALS [ 'mod_strings' ]['LBL_ALL_MODULES'] = 'all_modules';
443         $_REQUEST['execute_sql'] = true;
444
445         require_once('ModuleInstall/ModuleInstaller.php');
446                 $mi = new ModuleInstaller();
447         $mi->silent = true;
448                 $mi->rebuild_extensions();
449
450         $repair = new RepairAndClear();
451         $repair->repairAndClearAll(array('clearVardefs', 'clearTpls'), array($class_name), true, false);
452         //#28707 ,clear all the js files in cache
453         $repair->module_list = array();
454         $repair->clearJsFiles();
455
456         // now clear the cache so that the results are immediately visible
457         include_once ('include/TemplateHandler/TemplateHandler.php') ;
458         TemplateHandler::clearCache ( $module ) ;
459
460         $GLOBALS [ 'mod_strings' ] = $MBmodStrings;
461     }
462
463     function action_RefreshField ()
464     {
465         require_once ('modules/DynamicFields/FieldCases.php') ;
466         $field = get_widget ( $_POST [ 'type' ] ) ;
467         $field->populateFromPost () ;
468         $this->view = 'modulefield' ;
469     }
470
471     function action_saveVisibility ()
472     {
473                 $packageName = (isset ( $_REQUEST [ 'view_package' ] ) && (strtolower($_REQUEST['view_package']) != 'studio')) ? $_REQUEST [ 'view_package' ] : null ;
474         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php' ;
475         $parser = ParserFactory::getParser ( MB_VISIBILITY, $_REQUEST [ 'view_module' ], $packageName ) ;
476
477         $json = getJSONobj();
478         $visibility_grid = $json->decode(html_entity_decode(rawurldecode($_REQUEST [ 'visibility_grid' ]), ENT_QUOTES) );
479                 $parser->saveVisibility ( $_REQUEST [ 'fieldname' ] , $_REQUEST [ 'trigger' ] , $visibility_grid ) ;
480
481         echo $json->encode(array( "visibility_editor_{$_REQUEST['fieldname']}" => array("action" => "deactivate")));
482     }
483
484         function action_SaveRelationshipLabel (){
485             $selected_lang = (!empty($_REQUEST['relationship_lang'])?$_REQUEST['relationship_lang']:$_SESSION['authenticated_user_language']);
486                  if (empty($_REQUEST [ 'view_package' ])){
487             require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
488             $relationships = new DeployedRelationships ( $_REQUEST [ 'view_module' ] ) ;
489             if (! empty ( $_REQUEST [ 'relationship_name' ] ))
490                 {
491                     if ($relationship = $relationships->get ( $_REQUEST [ 'relationship_name' ] )){
492                         $metadata = $relationship->buildLabels(true);
493                          require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
494                                 $parser = new ParserLabel ( $_REQUEST['view_module'] ) ;
495                                 $parser->handleSaveRelationshipLabels ( $metadata, $selected_lang ) ;
496                     }
497             }
498         }
499         else {
500             //TODO FOR MB
501         }
502         $this->view = 'relationships' ;
503         }
504
505     function action_SaveRelationship ()
506     {
507         if(!empty($GLOBALS['current_user']) && empty($GLOBALS['modListHeader']))
508         {
509             $GLOBALS['modListHeader'] = query_module_access_list($GLOBALS['current_user']);
510         }
511
512         if (empty($_REQUEST [ 'view_package' ]))
513         {
514             require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
515             $relationships = new DeployedRelationships ( $_REQUEST [ 'view_module' ] ) ;
516         } else
517         {
518             $mb = new ModuleBuilder ( ) ;
519             $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
520             require_once 'modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php' ;
521             $relationships = new UndeployedRelationships ( $module->getModuleDir () ) ;
522         }
523
524         $relationships->addFromPost () ;
525         $relationships->save () ;
526         $GLOBALS['log']->debug("\n\nSTART BUILD");
527         if (empty($_REQUEST [ 'view_package' ])) {
528             $relationships->build () ;
529             LanguageManager::clearLanguageCache($_REQUEST [ 'view_module' ]);
530         }
531         $GLOBALS['log']->debug("\n\nEND BUILD");
532         $this->view = 'relationships' ;
533     }
534
535     function action_DeleteRelationship ()
536     {
537         if (isset ( $_REQUEST [ 'relationship_name' ] ))
538         {
539             if (empty($_REQUEST [ 'view_package' ] ))
540             {
541                 require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
542                 if (!empty($_REQUEST['remove_tables']))
543                                     $GLOBALS['mi_remove_tables'] = $_REQUEST['remove_tables'];
544                 $relationships = new DeployedRelationships ( $_REQUEST [ 'view_module' ] ) ;
545             } else
546             {
547                 $mb = new ModuleBuilder ( ) ;
548                 $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
549                 require_once 'modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php' ;
550                 $relationships = new UndeployedRelationships ( $module->getModuleDir () ) ;
551             }
552             $relationships->delete ( $_REQUEST [ 'relationship_name' ] ) ;
553
554             $relationships->save () ;
555             require_once("data/Relationships/RelationshipFactory.php");
556             SugarRelationshipFactory::deleteCache();
557         }
558         $this->view = 'relationships' ;
559     }
560
561     function action_SaveDropDown ()
562     {
563         require_once 'modules/ModuleBuilder/parsers/parser.dropdown.php' ;
564         $parser = new ParserDropDown ( ) ;
565         $parser->saveDropDown ( $_REQUEST ) ;
566         $this->view = 'dropdowns' ;
567     }
568
569     function action_DeleteField ()
570     {
571         require_once ('modules/DynamicFields/FieldCases.php') ;
572         $field = get_widget ( $_REQUEST [ 'type' ] ) ;
573         $field->name = $_REQUEST [ 'name' ] ;
574         if (!isset ( $_REQUEST [ 'view_package' ] ))
575         {
576             if (! empty ( $_REQUEST [ 'name' ] ) && ! empty ( $_REQUEST [ 'view_module' ] ))
577             {
578                 require_once ('modules/DynamicFields/DynamicField.php') ;
579                 $moduleName = $_REQUEST [ 'view_module' ] ;
580                 $class_name = $GLOBALS [ 'beanList' ] [ $moduleName ] ;
581                 require_once ($GLOBALS [ 'beanFiles' ] [ $class_name ]) ;
582                 $seed = new $class_name ( ) ;
583                 $df = new DynamicField ( $moduleName ) ;
584                 $df->setup ( $seed ) ;
585                 //Need to load the entire field_meta_data for some field types
586                 $field = $df->getFieldWidget($moduleName, $field->name);
587                 $field->delete ( $df ) ;
588
589                 $GLOBALS [ 'mod_strings' ]['LBL_ALL_MODULES'] = 'all_modules';
590                 $_REQUEST['execute_sql'] = true;
591                 include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
592                 $repair = new RepairAndClear();
593                 $repair->repairAndClearAll(array('rebuildExtensions', 'clearVardefs', 'clearTpls'), array($class_name), true, false);
594                 require_once 'modules/ModuleBuilder/Module/StudioModuleFactory.php' ;
595                 $module = StudioModuleFactory::getStudioModule( $moduleName ) ;
596             }
597         }
598         else
599         {
600             $mb = new ModuleBuilder ( ) ;
601             $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
602             $field->delete ( $module ) ;
603             $mb->save () ;
604         }
605         $module->removeFieldFromLayouts( $field->name );
606         $this->view = 'modulefields' ;
607
608         if (isset($GLOBALS['current_language']) && isset($_REQUEST['label']) &&
609                 isset($_REQUEST['labelValue']) && isset($_REQUEST['view_module'])) {
610             $this->DeleteLabel($GLOBALS['current_language'], $_REQUEST['label'], $_REQUEST['labelValue'], $_REQUEST['view_module']);
611         }
612     }
613
614     function DeleteLabel($language, $label, $labelvalue, $modulename, $basepath = null, $forRelationshipLabel = false)
615     {
616         // remove the label
617         require_once 'modules/ModuleBuilder/parsers/parser.label.php';
618         ParserLabel::removeLabel($language, $label, $labelvalue, $modulename, $basepath, $forRelationshipLabel);
619     }
620
621     function action_CloneField ()
622     {
623         $this->view_object_map [ 'field_name' ] = $_REQUEST [ 'name' ] ;
624         $this->view_object_map [ 'is_clone' ] = true ;
625         $this->view = 'modulefield' ;
626     }
627
628     function action_SaveAssistantPref ()
629     {
630         global $current_user ;
631         if (isset ( $_REQUEST [ 'pref_value' ] ))
632         {
633             if ($_REQUEST [ 'pref_value' ] == 'ignore')
634             {
635                 $current_user->setPreference ( 'mb_assist', 'DISABLED', 0, 'Assistant' ) ;
636             } else
637             {
638                 $current_user->setPreference ( 'mb_assist', 'ENABLED', 0, 'Assistant' ) ;
639             }
640             $current_pref = $current_user->getPreference ( 'mb_assist', 'Assistant' ) ;
641             echo "Assistant.processUserPref('$current_pref')" ;
642             sugar_cleanup ( true ) ; //push preferences to DB.
643         }
644     }
645
646     // Studio2 Actions
647
648
649     function action_EditProperty ()
650     {
651         $this->view = 'property' ;
652     }
653
654     function action_saveProperty ()
655     {
656         require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
657         $modules = $_REQUEST['view_module'];
658         if(!empty($_REQUEST['subpanel'])){
659                 $modules = $_REQUEST['subpanel'];
660         }
661         $parser = new ParserLabel ( $modules , isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
662         // if no language provided, then use the user's current language which is most likely what they intended
663         $language = (isset($_REQUEST [ 'selected_lang' ])) ? $_REQUEST [ 'selected_lang' ] : $GLOBALS['current_language'] ;
664         $parser->handleSave ( $_REQUEST, $language ) ;
665         $json = getJSONobj();
666         echo $json->encode(array("east" => array("action" => "deactivate")));
667     }
668
669     function action_editModule ()
670     {
671         $this->view = 'module' ;
672     }
673
674     function action_wizard ()
675     {
676         $this->view = 'wizard' ;
677     }
678
679     /**
680      * Receive a layout through $_REQUEST and save it out to the working files directory
681      * Expects a series of $_REQUEST parameters all in the format $_REQUEST['slot-panel#-slot#-property']=value
682      */
683
684     function action_saveLayout ()
685     {
686             $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
687             $this->view = 'layoutview' ;
688         $parser->writeWorkingFile () ;
689
690         if(!empty($_REQUEST [ 'sync_detail_and_edit' ]) && $_REQUEST['sync_detail_and_edit'] != false && $_REQUEST['sync_detail_and_edit'] != "false"){
691                 if(strtolower ($parser->_view) == MB_EDITVIEW){
692                         $parser2 = ParserFactory::getParser ( MB_DETAILVIEW, $_REQUEST [ 'view_module' ], isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
693                         $parser2->setUseTabs($parser->getUseTabs());
694                 $parser2->writeWorkingFile () ;
695                 }
696         }
697     }
698
699     function action_saveAndPublishLayout ()
700     {
701             $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
702             $this->view = 'layoutview' ;
703         $parser->handleSave () ;
704
705         if(!empty($_REQUEST [ 'sync_detail_and_edit' ]) && $_REQUEST['sync_detail_and_edit'] != false && $_REQUEST['sync_detail_and_edit'] != "false"){
706                 if(strtolower ($parser->_view) == MB_EDITVIEW){
707                         $parser2 = ParserFactory::getParser ( MB_DETAILVIEW, $_REQUEST [ 'view_module' ], isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
708                         $parser2->setUseTabs($parser->getUseTabs());
709                 $parser2->handleSave () ;
710                 }
711         }
712     }
713
714     function action_manageBackups ()
715     {
716
717     }
718
719     function action_listViewSave ()
720     {
721         $GLOBALS [ 'log' ]->info ( "action_listViewSave" ) ;
722
723         $packageName = (isset ( $_REQUEST [ 'view_package' ] ) && (strtolower($_REQUEST['view_package']) != 'studio')) ? $_REQUEST [ 'view_package' ] : null ;
724         $subpanelName = (! empty ( $_REQUEST [ 'subpanel' ] )) ? $_REQUEST [ 'subpanel' ] : null ;
725         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php' ;
726         $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], $packageName, $subpanelName ) ;
727         $this->view = 'listView' ;
728         $parser->handleSave () ;
729
730     }
731
732     function action_dashletSave () {
733         $this->view = 'dashlet' ;
734         $packageName = (isset ( $_REQUEST [ 'view_package' ] ) && (strtolower($_REQUEST['view_package']) != 'studio')) ? $_REQUEST [ 'view_package' ] : null ;
735         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php' ;
736         $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], $packageName ) ;
737         $parser->handleSave () ;
738     }
739
740         function action_popupSave(){
741                 $this->view = 'popupview' ;
742         $packageName = (isset ( $_REQUEST [ 'view_package' ] ) && (strtolower($_REQUEST['view_package']) != 'studio')) ? $_REQUEST [ 'view_package' ] : null ;
743         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php' ;
744         $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], $packageName ) ;
745         $parser->handleSave () ;
746         if(empty($packageName)){
747                 include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
748                         global $mod_strings;
749                 $mod_strings['LBL_ALL_MODULES'] = 'all_modules';
750                 $repair = new RepairAndClear();
751                         $repair->show_output = false;
752                         $class_name = $GLOBALS [ 'beanList' ] [ $_REQUEST [ 'view_module' ] ] ;
753                         $repair->module_list = array($class_name);
754                         $repair->clearTpls();
755         }
756
757         }
758
759     function action_searchViewSave ()
760     {
761         $packageName = (isset ( $_REQUEST [ 'view_package' ] )) ? $_REQUEST [ 'view_package' ] : null ;
762         require_once 'modules/ModuleBuilder/parsers/views/SearchViewMetaDataParser.php' ;
763         $parser = new SearchViewMetaDataParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], $packageName ) ;
764         $parser->handleSave () ;
765
766
767         //Repair or create a custom SearchFields.php file as needed
768         $module_name = $_REQUEST [ 'view_module' ] ;
769         global $beanList;
770         if (isset($beanList[$module_name]) && $beanList[$module_name]!="") {
771             $objectName = BeanFactory::getObjectName($module_name);
772
773             //Load the vardefs for the module to pass to TemplateRange
774             VardefManager::loadVardef($module_name, $objectName, true);
775             global $dictionary;
776             $vardefs = $dictionary[$objectName]['fields'];
777             require_once('modules/DynamicFields/templates/Fields/TemplateRange.php');
778             TemplateRange::repairCustomSearchFields($vardefs, $module_name, $packageName);
779         }
780         $this->view = 'searchView' ;
781     }
782
783     function action_editLabels ()
784     {
785         if (isset ( $_REQUEST [ 'view_package' ] )) //MODULE BUILDER
786         {
787             $this->view = 'modulelabels';
788         }else{ //STUDIO
789             $this->view = 'labels';
790         }
791     }
792
793     function action_get_app_list_string ()
794     {
795         require_once ('include/JSON.php') ;
796         $json = new JSON ( ) ;
797         if (isset ( $_REQUEST [ 'key' ] ) && ! empty ( $_REQUEST [ 'key' ] ))
798         {
799             $key = $_REQUEST [ 'key' ] ;
800             $value = array ( ) ;
801             if (! empty ( $GLOBALS [ 'app_list_strings' ] [ $key ] ))
802             {
803                 $value = $GLOBALS [ 'app_list_strings' ] [ $key ] ;
804             } else
805             {
806                 $package_strings = array ( ) ;
807                 if (! empty ( $_REQUEST [ 'view_package' ] ) && $_REQUEST [ 'view_package' ] != 'studio' && ! empty ( $_REQUEST [ 'view_module' ] ))
808                 {
809                     require_once ('modules/ModuleBuilder/MB/ModuleBuilder.php') ;
810                     $mb = new ModuleBuilder ( ) ;
811                     $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
812                     $lang = $GLOBALS [ 'current_language' ] ;
813                     $module->mblanguage->generateAppStrings ( false ) ;
814                     $package_strings = $module->mblanguage->appListStrings [ $lang . '.lang.php' ] ;
815                     if (isset ( $package_strings [ $key ] ) && is_array ( $package_strings [ $key ] ))
816                     {
817                         $value = $package_strings [ $key ] ;
818                     }
819                 }
820             }
821             echo $json->encode ( $value ) ;
822         }
823     }
824
825     function action_history ()
826     {
827         $this->view = 'history' ;
828     }
829
830     function resetmodule()
831     {
832         $this->view = 'resetmodule';
833     }
834
835
836
837 }
838 ?>