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