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