]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/views/view.resetmodule.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / views / view.resetmodule.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/AjaxCompose.php') ;
38 require_once ('modules/ModuleBuilder/MB/ModuleBuilder.php') ;
39 require_once ('modules/ModuleBuilder/Module/StudioModule.php') ;
40 require_once ('modules/ModuleBuilder/Module/StudioBrowser.php') ;
41 require_once ('modules/DynamicFields/DynamicField.php') ;
42 require_once 'modules/ModuleBuilder/Module/StudioModuleFactory.php' ;
43 require_once 'modules/ModuleBuilder/parsers/views/DeployedMetaDataImplementation.php';
44
45 class ViewResetmodule extends SugarView
46 {
47     /**
48          * @see SugarView::_getModuleTitleParams()
49          */
50         protected function _getModuleTitleParams($browserTitle = false)
51         {
52             global $mod_strings;
53             
54         return array(
55            translate('LBL_MODULE_NAME','Administration'),
56            ModuleBuilderController::getModuleTitle(),
57            );
58     }
59
60         function display()
61     {
62         $moduleName = $this->module = $_REQUEST['view_module'];
63         if (isset($_REQUEST['handle']) && $_REQUEST['handle'] == "execute") {
64             return $this->handleSave();
65         }
66         
67         $ajax = new AjaxCompose ( ) ;
68         $ajax->addCrumb ( translate('LBL_STUDIO'), 'ModuleBuilder.main("studio")' ) ;
69         $ajax->addCrumb ( translate($moduleName), 'ModuleBuilder.getContent("module=ModuleBuilder&action=wizard&view_module=' . $moduleName . '")' ) ;
70         $ajax->addCrumb ( translate('LBL_RESET') . " " . translate($moduleName) , '') ;
71         
72         $smarty = new Sugar_Smarty ( ) ;
73         $smarty->assign("module", $moduleName);
74         $smarty->assign("actions", array(
75             array("name" => "relationships", "label" => translate("LBL_CLEAR_RELATIONSHIPS")),
76             array("name" => "fields", "label" => translate("LBL_REMOVE_FIELDS")),
77             array("name" => "layouts", "label" => translate("LBL_RESET_LAYOUTS")),
78             array("name" => "labels", "label" => translate("LBL_RESET_LABELS")),
79                         array("name" => "extensions", "label" => translate("LBL_CLEAR_EXTENSIONS")),
80         ));
81         
82         $ajax->addSection ( 
83             'center', 
84             "Reset ". translate($moduleName) , 
85             $smarty->fetch('modules/ModuleBuilder/tpls/resetModule.tpl') //"This works now" 
86         ) ;
87         
88         echo $ajax->getJavascript () ;
89     }
90     
91     function handleSave() 
92     {
93         $out = "<script>ajaxStatus.flashStatus(SUGAR.language.get('app_strings', 'LBL_REQUEST_PROCESSED'), 2000);</script>";
94         
95         if (!empty($_REQUEST['relationships']))
96             $out .= $this->removeCustomRelationships();
97             
98         if (!empty($_REQUEST['fields']))
99             $out .= $this->removeCustomFields();
100             
101         if (!empty($_REQUEST['layouts']))
102             $out .= $this->removeCustomLayouts();
103                         
104                 if (!empty($_REQUEST['labels']))
105             $out .= $this->removeCustomLabels();
106                         
107                 if (!empty($_REQUEST['extensions']))
108             $out .= $this->removeCustomExtensions();    
109                         
110         
111         $out .= "Complete!";
112         
113         $ajax = new AjaxCompose ( ) ;
114         
115         $ajax->addCrumb ( translate('LBL_STUDIO'), 'ModuleBuilder.main("studio")' ) ;
116         $ajax->addCrumb ( translate($this->module), 'ModuleBuilder.getContent("module=ModuleBuilder&action=wizard&view_module=' . $this->module . '")' ) ;
117         $ajax->addCrumb ( "Reset ". translate($this->module) , '') ;
118         
119         
120         $ajax->addSection ( 
121             'center', 
122             "Reset ". translate($this->module) , 
123             $out
124         ) ;
125         
126         echo $ajax->getJavascript () ;
127     }
128     
129     /**
130      * Removes all custom fields created in studio
131      * 
132      * @return html output record of the field deleted
133      */
134     function removeCustomFields() 
135     {    
136         $moduleName = $this->module;
137         $class_name = $GLOBALS [ 'beanList' ] [ $moduleName ] ;
138         require_once ($GLOBALS [ 'beanFiles' ] [ $class_name ]) ;
139         $seed = new $class_name ( ) ;
140         $df = new DynamicField ( $moduleName ) ;
141         $df->setup ( $seed ) ;
142         
143         
144         $module = StudioModuleFactory::getStudioModule( $moduleName ) ;
145         $customFields = array();
146         foreach($seed->field_defs as $def) {
147             if(isset($def['source']) && $def['source'] == 'custom_fields') {
148                $field = $df->getFieldWidget($moduleName, $def['name']);
149                $field->delete ( $df ) ;
150                
151                $module->removeFieldFromLayouts( $def['name'] );
152                $customFields[] = $def['name'];
153             }
154         }
155         $out = "";
156         foreach ($customFields as $field) {
157             $out .= "Removed field $field<br/>";
158         }
159         return ($out);
160     }
161     
162     /**
163      * Removes the metadata files for all known studio layouts.
164      * 
165      * @return html output record of the files deleted
166      */
167     function removeCustomLayouts() 
168     {
169         $module = StudioModuleFactory::getStudioModule( $this->module ) ;
170         $sources = $module->getViewMetadataSources();
171         
172         $out = "";
173         foreach($sources as $view)
174         {
175             $file = DeployedMetaDataImplementation::getFileName($view['type'], $this->module);
176             if (file_exists($file)) {
177                 unlink($file);
178                 $out .= "Removed layout {$view['type']}.php<br/>";
179             }
180         }
181         
182         // now clear the cache
183         include_once ('include/TemplateHandler/TemplateHandler.php') ;
184         TemplateHandler::clearCache ( $this->module ) ;
185         
186         return $out;
187     }
188     
189     /**
190      * Removes all custom relationships containing this module
191      * 
192      * @return html output record of the files deleted
193      */
194     function removeCustomRelationships() 
195     {
196         require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php' ;
197         $out = "";
198         $madeChanges = false;
199         $relationships = new DeployedRelationships ( $this->module ) ;
200         
201         foreach ( $relationships->getRelationshipList () as $relationshipName )
202         {
203             $rel = $relationships->get ( $relationshipName )->getDefinition () ;
204             if ($rel [ 'is_custom' ] || (isset($rel [ 'from_studio' ]) && $rel [ 'from_studio' ])) {
205                 $relationships->delete ($relationshipName);
206                 $out .= "Removed relationship $relationshipName<br/>";
207             }
208         }
209         if ($madeChanges)
210            $relationships->save () ;
211         
212         return $out;
213     }
214     
215     function removeCustomLabels() 
216     {
217         $out = "";
218                 $languageDir = "custom/modules/{$this->module}/language";
219         if (is_dir($languageDir)) {
220             $files = scandir($languageDir);
221             foreach ($files as $langFile) {
222                 if (substr($langFile, 0 ,1) == '.') continue;
223                                 $language = substr($langFile, 0, strlen($langFile) - 9);
224                                 unlink($languageDir . "/" . $langFile);
225                                 
226                                 LanguageManager::clearLanguageCache ( $this->module, $language ) ;
227                                 $out .= "Removed language file $langFile<br/>";
228             }
229         }
230                 
231                 return $out;
232     }
233         
234         function removeCustomExtensions() 
235         {
236         $out = "";
237         $extDir = "custom/Extension/modules/{$this->module}";
238         if (is_dir($extDir)) {
239                 rmdir_recursive($extDir);
240                 require_once ('modules/Administration/QuickRepairAndRebuild.php') ;
241             $rac = new RepairAndClear ( ) ;
242             $rac->repairAndClearAll ( array ( 'clearAll' ), array ( $this->module ), true, false ) ;
243                         $rac->rebuildExtensions();
244                 $out .= "Cleared extensions for {$this->module}<br/>";
245         }
246                 
247         return $out;
248     }
249 }