]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/controller.php
Release 6.5.16
[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-2013 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
359             $req = $_REQUEST;
360             foreach (ModuleBuilder::getModuleAliases($_REQUEST['view_module']) as $key)
361             {
362                 $req['view_module'] = $key;
363                 $parser = new ParserLabel($req['view_module'], isset($req['view_package']) ? $req['view_package'] : null);
364                 $parser->handleSave($req, $GLOBALS['current_language']);
365             }
366         }
367         $this->view = 'modulefields' ;
368     }
369
370     function action_ExportCustom ()
371     {
372         $modules = $_REQUEST [ 'modules' ] ;
373         $name = $_REQUEST [ 'name' ] ;
374         $author = $_REQUEST [ 'author' ] ;
375         $description = $_REQUEST [ 'description' ] ;
376         ob_clean () ;
377         if (! empty ( $modules ) && ! empty ( $name ))
378         {
379             require_once ('modules/ModuleBuilder/MB/ModuleBuilder.php') ;
380             $mb = new MBPackage ( $name ) ;
381             $mb->author = $author ;
382             $mb->description = $description ;
383             $mb->exportCustom ( $modules, true, true ) ;
384         }
385     }
386
387     function action_SaveField ()
388     {
389         require_once ('modules/DynamicFields/FieldCases.php') ;
390         $field = get_widget ( $_REQUEST [ 'type' ] ) ;
391         $_REQUEST [ 'name' ] = trim ( $_REQUEST [ 'name' ] ) ;
392
393         $field->populateFromPost () ;
394
395         if (!isset ( $_REQUEST [ 'view_package' ] ))
396         {
397             require_once ('modules/DynamicFields/DynamicField.php') ;
398             if (! empty ( $_REQUEST [ 'view_module' ] ))
399             {
400                 $module = $_REQUEST [ 'view_module' ] ;
401                 if ( $module == 'Employees' ) {
402                     $module = 'Users';
403                 }
404
405                 $bean = BeanFactory::getBean($module);
406                 if(!empty($bean))
407                 {
408                         $field_defs = $bean->field_defs;
409                         if(isset($field_defs[$field->name. '_c']))
410                         {
411                                                 $GLOBALS['log']->error($GLOBALS['mod_strings']['ERROR_ALREADY_EXISTS'] . '[' . $field->name . ']');
412                                                 sugar_die($GLOBALS['mod_strings']['ERROR_ALREADY_EXISTS']);
413                         }
414                 }
415
416                 $df = new DynamicField ( $module ) ;
417                 $class_name = $GLOBALS [ 'beanList' ] [ $module ] ;
418                 require_once ($GLOBALS [ 'beanFiles' ] [ $class_name ]) ;
419                 $mod = new $class_name ( ) ;
420                 $df->setup ( $mod ) ;
421
422                 $field->save ( $df ) ;
423                 $this->action_SaveLabel () ;
424                 include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
425                         global $mod_strings;
426                 $mod_strings['LBL_ALL_MODULES'] = 'all_modules';
427                 require_once('ModuleInstall/ModuleInstaller.php');
428                 $mi = new ModuleInstaller();
429                 $mi->silent = true;
430                 $mi->rebuild_extensions();
431                 $repair = new RepairAndClear();
432
433                         $repair->repairAndClearAll(array('rebuildExtensions', 'clearVardefs', 'clearTpls'), array($class_name), true, false);
434                 if ( $module == 'Users' ) {
435                     $repair->repairAndClearAll(array('rebuildExtensions', 'clearVardefs', 'clearTpls'), array('Employee'), true, false);
436                     
437                 }
438
439                 //#28707 ,clear all the js files in cache
440                         $repair->module_list = array();
441                         $repair->clearJsFiles();
442             }
443         } else
444         {
445             $mb = new ModuleBuilder ( ) ;
446             $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
447             $field->save ( $module ) ;
448             $module->mbvardefs->save () ;
449             // get the module again to refresh the labels we might have saved with the $field->save (e.g., for address fields)
450             $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
451             if (isset ( $_REQUEST [ 'label' ] ) && isset ( $_REQUEST [ 'labelValue' ] ))
452                 $module->setLabel ( $GLOBALS [ 'current_language' ], $_REQUEST [ 'label' ], $_REQUEST [ 'labelValue' ] ) ;
453             $module->save();
454         }
455         $this->view = 'modulefields' ;
456     }
457
458     function action_saveSugarField ()
459     {
460         global $mod_strings;
461         require_once ('modules/DynamicFields/FieldCases.php') ;
462                 
463         $field = get_widget ( $_REQUEST [ 'type' ] ) ;
464         $_REQUEST [ 'name' ] = trim ( $_POST [ 'name' ] ) ;
465
466         $field->populateFromPost () ;
467         require_once ('modules/ModuleBuilder/parsers/StandardField.php') ;
468         $module = $_REQUEST [ 'view_module' ] ;
469         
470         // Need to map Employees -> Users
471         if ( $module=='Employees') {
472             $module = 'Users';
473         }
474         
475         $df = new StandardField ( $module ) ;
476         $mod = BeanFactory::getBean($module);
477         $class_name = $GLOBALS [ 'beanList' ] [ $module ] ;
478         $df->setup ( $mod ) ;
479
480         $field->module = $mod;
481         $field->save ( $df ) ;
482         $this->action_SaveLabel () ;
483
484         $MBmodStrings = $mod_strings;
485         $GLOBALS [ 'mod_strings' ] = return_module_language ( '', 'Administration' ) ;
486
487         include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
488         $GLOBALS [ 'mod_strings' ]['LBL_ALL_MODULES'] = 'all_modules';
489         $_REQUEST['execute_sql'] = true;
490
491         require_once('ModuleInstall/ModuleInstaller.php');
492                 $mi = new ModuleInstaller();
493         $mi->silent = true;
494                 $mi->rebuild_extensions();
495
496         $repair = new RepairAndClear();
497         $repair->repairAndClearAll(array('clearVardefs', 'clearTpls'), array($class_name), true, false);
498         //#28707 ,clear all the js files in cache
499         $repair->module_list = array();
500         $repair->clearJsFiles();
501
502         // now clear the cache so that the results are immediately visible
503         include_once ('include/TemplateHandler/TemplateHandler.php') ;
504         TemplateHandler::clearCache ( $module ) ;
505         if ( $module == 'Users' ) {
506             TemplateHandler::clearCache('Employees');
507         }
508
509         $GLOBALS [ 'mod_strings' ] = $MBmodStrings;
510     }
511
512     function action_RefreshField ()
513     {
514         require_once ('modules/DynamicFields/FieldCases.php') ;
515         $field = get_widget ( $_POST [ 'type' ] ) ;
516         $field->populateFromPost () ;
517         $this->view = 'modulefield' ;
518     }
519
520     function action_saveVisibility ()
521     {
522                 $packageName = (isset ( $_REQUEST [ 'view_package' ] ) && (strtolower($_REQUEST['view_package']) != 'studio')) ? $_REQUEST [ 'view_package' ] : null ;
523         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php' ;
524         $parser = ParserFactory::getParser ( MB_VISIBILITY, $_REQUEST [ 'view_module' ], $packageName ) ;
525
526         $json = getJSONobj();
527         $visibility_grid = $json->decode(html_entity_decode(rawurldecode($_REQUEST [ 'visibility_grid' ]), ENT_QUOTES) );
528                 $parser->saveVisibility ( $_REQUEST [ 'fieldname' ] , $_REQUEST [ 'trigger' ] , $visibility_grid ) ;
529
530         echo $json->encode(array( "visibility_editor_{$_REQUEST['fieldname']}" => array("action" => "deactivate")));
531     }
532
533         function action_SaveRelationshipLabel (){
534             $selected_lang = (!empty($_REQUEST['relationship_lang'])?$_REQUEST['relationship_lang']:$_SESSION['authenticated_user_language']);
535                  if (empty($_REQUEST [ 'view_package' ])){
536             require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
537             $relationships = new DeployedRelationships ( $_REQUEST [ 'view_module' ] ) ;
538             if (! empty ( $_REQUEST [ 'relationship_name' ] ))
539                 {
540                     if ($relationship = $relationships->get ( $_REQUEST [ 'relationship_name' ] )){
541                         $metadata = $relationship->buildLabels(true);
542                          require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
543                                 $parser = new ParserLabel ( $_REQUEST['view_module'] ) ;
544                                 $parser->handleSaveRelationshipLabels ( $metadata, $selected_lang ) ;
545                     }
546             }
547         }
548         else {
549             //TODO FOR MB
550         }
551         $this->view = 'relationships' ;
552         }
553
554     function action_SaveRelationship ()
555     {
556         if(!empty($GLOBALS['current_user']) && empty($GLOBALS['modListHeader']))
557         {
558             $GLOBALS['modListHeader'] = query_module_access_list($GLOBALS['current_user']);
559         }
560
561         if (empty($_REQUEST [ 'view_package' ]))
562         {
563             require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
564             $relationships = new DeployedRelationships ( $_REQUEST [ 'view_module' ] ) ;
565         } else
566         {
567             $mb = new ModuleBuilder ( ) ;
568             $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
569             require_once 'modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php' ;
570             $relationships = new UndeployedRelationships ( $module->getModuleDir () ) ;
571         }
572
573         $relationships->addFromPost () ;
574         $relationships->save () ;
575         $GLOBALS['log']->debug("\n\nSTART BUILD");
576         if (empty($_REQUEST [ 'view_package' ])) {
577             $relationships->build () ;
578             LanguageManager::clearLanguageCache($_REQUEST [ 'view_module' ]);
579         }
580         $GLOBALS['log']->debug("\n\nEND BUILD");
581         $this->view = 'relationships' ;
582     }
583
584     function action_DeleteRelationship ()
585     {
586         if (isset ( $_REQUEST [ 'relationship_name' ] ))
587         {
588             if (empty($_REQUEST [ 'view_package' ] ))
589             {
590                 require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
591                 if (!empty($_REQUEST['remove_tables']))
592                                     $GLOBALS['mi_remove_tables'] = $_REQUEST['remove_tables'];
593                 $relationships = new DeployedRelationships ( $_REQUEST [ 'view_module' ] ) ;
594             } else
595             {
596                 $mb = new ModuleBuilder ( ) ;
597                 $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
598                 require_once 'modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php' ;
599                 $relationships = new UndeployedRelationships ( $module->getModuleDir () ) ;
600             }
601             $relationships->delete ( $_REQUEST [ 'relationship_name' ] ) ;
602
603             $relationships->save () ;
604             require_once("data/Relationships/RelationshipFactory.php");
605             SugarRelationshipFactory::deleteCache();
606         }
607         $this->view = 'relationships' ;
608     }
609
610     function action_SaveDropDown ()
611     {
612         require_once 'modules/ModuleBuilder/parsers/parser.dropdown.php' ;
613         $parser = new ParserDropDown ( ) ;
614         $parser->saveDropDown ( $_REQUEST ) ;
615         $this->view = 'dropdowns' ;
616     }
617
618     function action_DeleteField ()
619     {
620         require_once ('modules/DynamicFields/FieldCases.php') ;
621         $field = get_widget ( $_REQUEST [ 'type' ] ) ;
622         $field->name = $_REQUEST [ 'name' ] ;
623         if (!isset ( $_REQUEST [ 'view_package' ] ))
624         {
625             if (! empty ( $_REQUEST [ 'name' ] ) && ! empty ( $_REQUEST [ 'view_module' ] ))
626             {
627                 require_once ('modules/DynamicFields/DynamicField.php') ;
628                 $moduleName = $_REQUEST [ 'view_module' ] ;
629
630                 // bug 51325 make sure we make this switch or delete will not work
631                 if( $moduleName == 'Employees' )
632                     $moduleName = 'Users';
633                 
634                 $class_name = $GLOBALS [ 'beanList' ] [ $moduleName ] ;
635                 require_once ($GLOBALS [ 'beanFiles' ] [ $class_name ]) ;
636                 $seed = new $class_name ( ) ;
637                 $df = new DynamicField ( $moduleName ) ;
638                 $df->setup ( $seed ) ;
639                 //Need to load the entire field_meta_data for some field types
640                 $field = $df->getFieldWidget($moduleName, $field->name);
641                 $field->delete ( $df ) ;
642
643                 $GLOBALS [ 'mod_strings' ]['LBL_ALL_MODULES'] = 'all_modules';
644                 $_REQUEST['execute_sql'] = true;
645                 include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
646                 $repair = new RepairAndClear();
647                 $repair->repairAndClearAll(array('rebuildExtensions', 'clearVardefs', 'clearTpls'), array($class_name), true, false);
648                 require_once 'modules/ModuleBuilder/Module/StudioModuleFactory.php' ;
649                 $module = StudioModuleFactory::getStudioModule( $moduleName ) ;
650             }
651         }
652         else
653         {
654             $mb = new ModuleBuilder ( ) ;
655             $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
656             $field = $module->getField($field->name);
657             $field->delete ( $module ) ;
658             $mb->save () ;
659         }
660         $module->removeFieldFromLayouts( $field->name );
661         $this->view = 'modulefields' ;
662
663         if (isset($GLOBALS['current_language']) && isset($_REQUEST['label']) &&
664                 isset($_REQUEST['labelValue']) && isset($_REQUEST['view_module'])) {
665             $this->DeleteLabel($GLOBALS['current_language'], $_REQUEST['label'], $_REQUEST['labelValue'], $_REQUEST['view_module']);
666         }
667     }
668
669     function DeleteLabel($language, $label, $labelvalue, $modulename, $basepath = null, $forRelationshipLabel = false)
670     {
671         // remove the label
672         require_once 'modules/ModuleBuilder/parsers/parser.label.php';
673         ParserLabel::removeLabel($language, $label, $labelvalue, $modulename, $basepath, $forRelationshipLabel);
674     }
675
676     function action_CloneField ()
677     {
678         $this->view_object_map [ 'field_name' ] = $_REQUEST [ 'name' ] ;
679         $this->view_object_map [ 'is_clone' ] = true ;
680         $this->view = 'modulefield' ;
681     }
682
683     function action_SaveAssistantPref ()
684     {
685         global $current_user ;
686         if (isset ( $_REQUEST [ 'pref_value' ] ))
687         {
688             if ($_REQUEST [ 'pref_value' ] == 'ignore')
689             {
690                 $current_user->setPreference ( 'mb_assist', 'DISABLED', 0, 'Assistant' ) ;
691             } else
692             {
693                 $current_user->setPreference ( 'mb_assist', 'ENABLED', 0, 'Assistant' ) ;
694             }
695             $current_pref = $current_user->getPreference ( 'mb_assist', 'Assistant' ) ;
696             echo "Assistant.processUserPref('$current_pref')" ;
697             sugar_cleanup ( true ) ; //push preferences to DB.
698         }
699     }
700
701     // Studio2 Actions
702
703
704     function action_EditProperty ()
705     {
706         $this->view = 'property' ;
707     }
708
709     function action_saveProperty ()
710     {
711         require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
712         $modules = $_REQUEST['view_module'];
713         if(!empty($_REQUEST['subpanel'])){
714                 $modules = $_REQUEST['subpanel'];
715         }
716         $parser = new ParserLabel ( $modules , isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
717         // if no language provided, then use the user's current language which is most likely what they intended
718         $language = (isset($_REQUEST [ 'selected_lang' ])) ? $_REQUEST [ 'selected_lang' ] : $GLOBALS['current_language'] ;
719         $parser->handleSave ( $_REQUEST, $language ) ;
720         $json = getJSONobj();
721         echo $json->encode(array("east" => array("action" => "deactivate")));
722     }
723
724     function action_editModule ()
725     {
726         $this->view = 'module' ;
727     }
728
729     function action_wizard ()
730     {
731         $this->view = 'wizard' ;
732     }
733
734     /**
735      * Receive a layout through $_REQUEST and save it out to the working files directory
736      * Expects a series of $_REQUEST parameters all in the format $_REQUEST['slot-panel#-slot#-property']=value
737      */
738
739     function action_saveLayout ()
740     {
741             $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
742             $this->view = 'layoutview' ;
743         $parser->writeWorkingFile () ;
744
745         if(!empty($_REQUEST [ 'sync_detail_and_edit' ]) && $_REQUEST['sync_detail_and_edit'] != false && $_REQUEST['sync_detail_and_edit'] != "false"){
746                 if(strtolower ($parser->_view) == MB_EDITVIEW){
747                         $parser2 = ParserFactory::getParser ( MB_DETAILVIEW, $_REQUEST [ 'view_module' ], isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
748                         $parser2->setUseTabs($parser->getUseTabs());
749                 $parser2->writeWorkingFile () ;
750                 }
751         }
752     }
753
754     function action_saveAndPublishLayout ()
755     {
756             $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
757             $this->view = 'layoutview' ;
758         $parser->handleSave () ;
759
760         if(!empty($_REQUEST [ 'sync_detail_and_edit' ]) && $_REQUEST['sync_detail_and_edit'] != false && $_REQUEST['sync_detail_and_edit'] != "false"){
761                 if(strtolower ($parser->_view) == MB_EDITVIEW){
762                         $parser2 = ParserFactory::getParser ( MB_DETAILVIEW, $_REQUEST [ 'view_module' ], isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
763                         $parser2->setUseTabs($parser->getUseTabs());
764                 $parser2->handleSave () ;
765                 }
766         }
767     }
768
769     function action_manageBackups ()
770     {
771
772     }
773
774     function action_listViewSave ()
775     {
776         $GLOBALS [ 'log' ]->info ( "action_listViewSave" ) ;
777
778         $packageName = (isset ( $_REQUEST [ 'view_package' ] ) && (strtolower($_REQUEST['view_package']) != 'studio')) ? $_REQUEST [ 'view_package' ] : null ;
779         $subpanelName = (! empty ( $_REQUEST [ 'subpanel' ] )) ? $_REQUEST [ 'subpanel' ] : null ;
780         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php' ;
781         $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], $packageName, $subpanelName ) ;
782         $this->view = 'listView' ;
783         $parser->handleSave () ;
784
785     }
786
787     function action_dashletSave () {
788         $this->view = 'dashlet' ;
789         $packageName = (isset ( $_REQUEST [ 'view_package' ] ) && (strtolower($_REQUEST['view_package']) != 'studio')) ? $_REQUEST [ 'view_package' ] : null ;
790         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php' ;
791         $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], $packageName ) ;
792         $parser->handleSave () ;
793     }
794
795         function action_popupSave(){
796                 $this->view = 'popupview' ;
797         $packageName = (isset ( $_REQUEST [ 'view_package' ] ) && (strtolower($_REQUEST['view_package']) != 'studio')) ? $_REQUEST [ 'view_package' ] : null ;
798         require_once 'modules/ModuleBuilder/parsers/ParserFactory.php' ;
799         $parser = ParserFactory::getParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], $packageName ) ;
800         $parser->handleSave () ;
801         if(empty($packageName)){
802                 include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
803                         global $mod_strings;
804                 $mod_strings['LBL_ALL_MODULES'] = 'all_modules';
805                 $repair = new RepairAndClear();
806                         $repair->show_output = false;
807                         $class_name = $GLOBALS [ 'beanList' ] [ $_REQUEST [ 'view_module' ] ] ;
808                         $repair->module_list = array($class_name);
809                         $repair->clearTpls();
810         }
811
812         }
813
814     function action_searchViewSave ()
815     {
816         $packageName = (isset ( $_REQUEST [ 'view_package' ] )) ? $_REQUEST [ 'view_package' ] : null ;
817         require_once 'modules/ModuleBuilder/parsers/views/SearchViewMetaDataParser.php' ;
818         $parser = new SearchViewMetaDataParser ( $_REQUEST [ 'view' ], $_REQUEST [ 'view_module' ], $packageName ) ;
819         $parser->handleSave () ;
820
821
822         //Repair or create a custom SearchFields.php file as needed
823         $module_name = $_REQUEST [ 'view_module' ] ;
824         global $beanList;
825         if (isset($beanList[$module_name]) && $beanList[$module_name]!="") {
826             $objectName = BeanFactory::getObjectName($module_name);
827
828             //Load the vardefs for the module to pass to TemplateRange
829             VardefManager::loadVardef($module_name, $objectName, true);
830             global $dictionary;
831             $vardefs = $dictionary[$objectName]['fields'];
832             require_once('modules/DynamicFields/templates/Fields/TemplateRange.php');
833             TemplateRange::repairCustomSearchFields($vardefs, $module_name, $packageName);
834         }
835         $this->view = 'searchView' ;
836     }
837
838     function action_editLabels ()
839     {
840         if (isset ( $_REQUEST [ 'view_package' ] )) //MODULE BUILDER
841         {
842             $this->view = 'modulelabels';
843         }else{ //STUDIO
844             $this->view = 'labels';
845         }
846     }
847
848     function action_get_app_list_string ()
849     {
850         require_once ('include/JSON.php') ;
851         $json = new JSON ( ) ;
852         if (isset ( $_REQUEST [ 'key' ] ) && ! empty ( $_REQUEST [ 'key' ] ))
853         {
854             $key = $_REQUEST [ 'key' ] ;
855             $value = array ( ) ;
856             if (! empty ( $GLOBALS [ 'app_list_strings' ] [ $key ] ))
857             {
858                 $value = $GLOBALS [ 'app_list_strings' ] [ $key ] ;
859             } else
860             {
861                 $package_strings = array ( ) ;
862                 if (! empty ( $_REQUEST [ 'view_package' ] ) && $_REQUEST [ 'view_package' ] != 'studio' && ! empty ( $_REQUEST [ 'view_module' ] ))
863                 {
864                     require_once ('modules/ModuleBuilder/MB/ModuleBuilder.php') ;
865                     $mb = new ModuleBuilder ( ) ;
866                     $module = & $mb->getPackageModule ( $_REQUEST [ 'view_package' ], $_REQUEST [ 'view_module' ] ) ;
867                     $lang = $GLOBALS [ 'current_language' ] ;
868                     $module->mblanguage->generateAppStrings ( false ) ;
869                     $package_strings = $module->mblanguage->appListStrings [ $lang . '.lang.php' ] ;
870                     if (isset ( $package_strings [ $key ] ) && is_array ( $package_strings [ $key ] ))
871                     {
872                         $value = $package_strings [ $key ] ;
873                     }
874                 }
875             }
876             echo $json->encode ( $value ) ;
877         }
878     }
879
880     function action_history ()
881     {
882         $this->view = 'history' ;
883     }
884
885     function resetmodule()
886     {
887         $this->view = 'resetmodule';
888     }
889
890
891     /**
892      * savetablesort
893      * This method handles saving the current user's tabling sorting preferences.  It is called when
894      * the user clicks on a column to sort from the fields layout table.
895      *
896      */
897     function action_savetablesort ()
898     {
899         $this->view = 'ajax';
900         global $current_user;
901
902         if(!empty($current_user) && isset($_REQUEST['column']) && isset($_REQUEST['direction']))
903         {
904             $direction = ($_REQUEST['direction'] == 'yui-dt-asc') ? 'ASC' : 'DESC';
905             $valid_columns = array('name', 'label', 'type', 'required', 'unified_search', 'custom');
906             $key = in_array($_REQUEST['column'], $valid_columns) ? $_REQUEST['column'] : 'name';
907             $val = array('key'=>$key, 'direction'=>$direction);
908             $current_user->setPreference('fieldsTableColumn', getJSONobj()->encode($val), 0, 'ModuleBuilder');
909         }
910     }
911
912 }
913 ?>