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