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