PageRenderTime 46ms CodeModel.GetById 2ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/ModuleBuilder/views/view.resetmodule.php

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