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