PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/controllers/System/CacheController.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 341 lines | 259 code | 28 blank | 54 comment | 18 complexity | 2f25eaa24d2f1dc1720b99eaba0d131e MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * config controller
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_System_CacheController extends Mage_Adminhtml_Controller_Action
  34. {
  35. /**
  36. * Retrieve session model
  37. *
  38. * @return Mage_Adminhtml_Model_Session
  39. */
  40. protected function _getSession()
  41. {
  42. return Mage::getSingleton('adminhtml/session');
  43. }
  44. /**
  45. * Display cache management form
  46. */
  47. public function indexAction()
  48. {
  49. $this->loadLayout()
  50. ->_setActiveMenu('system/cache')
  51. ->_addContent($this->getLayout()->createBlock('adminhtml/system_cache_edit')->initForm())
  52. ->renderLayout();
  53. }
  54. /**
  55. * Seva cache settings
  56. */
  57. public function saveAction()
  58. {
  59. /**
  60. * Protect empty post data
  61. */
  62. $postData = $this->getRequest()->getPost();
  63. if (empty($postData)) {
  64. $this->_redirect('*/*');
  65. return;
  66. }
  67. /**
  68. * Process cache settings
  69. */
  70. $allCache = $this->getRequest()->getPost('all_cache');
  71. if ($allCache=='disable' || $allCache=='refresh') {
  72. Mage::app()->cleanCache();
  73. }
  74. $e = $this->getRequest()->getPost('enable');
  75. $enable = array();
  76. $clean = array();
  77. $cacheTypes = array_keys(Mage::helper('core')->getCacheTypes());
  78. foreach ($cacheTypes as $type) {
  79. $flag = $allCache!='disable' && (!empty($e[$type]) || $allCache=='enable');
  80. $enable[$type] = $flag ? 1 : 0;
  81. if ($allCache=='' && !$flag) {
  82. $clean[] = $type;
  83. }
  84. }
  85. // beta cache enabler (?)
  86. $beta = $this->getRequest()->getPost('beta');
  87. $betaCache = array_keys(Mage::helper('core')->getCacheBetaTypes());
  88. foreach ($betaCache as $type) {
  89. if (empty($beta[$type])) {
  90. $clean[] = $type;
  91. } else {
  92. $enable[$type] = 1;
  93. }
  94. }
  95. // clean all requested system cache and update cache usage
  96. if (!empty($clean)) {
  97. Mage::app()->cleanCache($clean);
  98. }
  99. Mage::app()->saveUseCache($enable);
  100. // clean javascript/css cache
  101. if ($this->getRequest()->getPost('jscss_action')) {
  102. if (Mage::getDesign()->cleanMergedJsCss()) {
  103. $this->_getSession()->addSuccess(
  104. Mage::helper('adminhtml')->__('The JavaScript/CSS cache has been cleared.')
  105. );
  106. } else {
  107. $this->_getSession()->addError(Mage::helper('adminhtml')->__('Failed to clear the JavaScript/CSS cache.'));
  108. }
  109. }
  110. /**
  111. * Run catalog actions
  112. */
  113. if ($catalogAction = $this->getRequest()->getPost('catalog_action')) {
  114. switch ($catalogAction) {
  115. case 'refresh_catalog_rewrites':
  116. try {
  117. Mage::getSingleton('catalog/url')->refreshRewrites();
  118. $this->_getSession()->addSuccess(
  119. Mage::helper('adminhtml')->__('The Catalog Rewrites were refreshed.')
  120. );
  121. }
  122. catch (Mage_Core_Exception $e) {
  123. $this->_getSession()->addError($e->getMessage());
  124. }
  125. catch (Exception $e) {
  126. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while refreshing the Catalog Rewrites.'));
  127. }
  128. break;
  129. case 'clear_images_cache':
  130. try {
  131. Mage::getModel('catalog/product_image')->clearCache();
  132. $this->_getSession()->addSuccess(
  133. Mage::helper('adminhtml')->__('The image cache was cleared.')
  134. );
  135. }
  136. catch (Mage_Core_Exception $e) {
  137. $this->_getSession()->addError($e->getMessage());
  138. }
  139. catch (Exception $e) {
  140. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while clearing the image cache.'));
  141. }
  142. break;
  143. case 'refresh_layered_navigation_now':
  144. try {
  145. $flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();
  146. if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) {
  147. $kill = Mage::getModel('catalogindex/catalog_index_kill_flag')->loadSelf();
  148. $kill->setFlagData($flag->getFlagData())->save();
  149. }
  150. $flag->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED)->save();
  151. Mage::getSingleton('catalogindex/indexer')->plainReindex();
  152. $this->_getSession()->addSuccess(
  153. Mage::helper('adminhtml')->__('Layered Navigation Indices were refreshed.')
  154. );
  155. }
  156. catch (Mage_Core_Exception $e) {
  157. $this->_getSession()->addError($e->getMessage());
  158. }
  159. catch (Exception $e) {
  160. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while refreshing the Layered Navigation indices.'));
  161. }
  162. break;
  163. case 'refresh_layered_navigation':
  164. try {
  165. $flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();
  166. switch ($flag->getState()) {
  167. case Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED:
  168. $flag->delete();
  169. $this->_getSession()->addSuccess(
  170. Mage::helper('adminhtml')->__('The Layered Navigation indexing queue has been canceled.')
  171. );
  172. break;
  173. case Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING:
  174. $kill = Mage::getModel('catalogindex/catalog_index_kill_flag')->loadSelf();
  175. $kill->setFlagData($flag->getFlagData())->save();
  176. $this->_getSession()->addSuccess(
  177. Mage::helper('adminhtml')->__('The Layered Navigation process has been queued to be killed.')
  178. );
  179. break;
  180. default:
  181. $flag->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED)->save();
  182. $this->_getSession()->addSuccess(
  183. Mage::helper('adminhtml')->__('The Layered Navigation indexing has been queued.')
  184. );
  185. break;
  186. }
  187. }
  188. catch (Mage_Core_Exception $e) {
  189. $this->_getSession()->addError($e->getMessage());
  190. }
  191. catch (Exception $e) {
  192. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while refreshing the Layered Navigation indices.'));
  193. }
  194. break;
  195. case 'rebuild_search_index':
  196. try {
  197. Mage::getSingleton('catalogsearch/fulltext')->rebuildIndex();
  198. $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__('The search index has been rebuilt.'));
  199. }
  200. catch (Mage_Core_Exception $e) {
  201. $this->_getSession()->addError($e->getMessage());
  202. }
  203. catch (Exception $e) {
  204. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while rebuilding the search index.'));
  205. }
  206. break;
  207. case 'rebuild_inventory_stock_status':
  208. try {
  209. Mage::getSingleton('cataloginventory/stock_status')->rebuild();
  210. $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__('The CatalogInventory Stock Status has been rebuilt.'));
  211. }
  212. catch (Mage_Core_Exception $e) {
  213. $this->_getSession()->addError($e->getMessage());
  214. }
  215. catch (Exception $e) {
  216. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while rebuilding the CatalogInventory Stock Status.'));
  217. }
  218. break;
  219. case 'rebuild_catalog_index':
  220. try {
  221. Mage::getSingleton('catalog/index')->rebuild();
  222. $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__('The catalog index has been rebuilt.'));
  223. }
  224. catch (Mage_Core_Exception $e) {
  225. $this->_getSession()->addError($e->getMessage());
  226. }
  227. catch (Exception $e) {
  228. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while rebuilding the catalog index.'));
  229. }
  230. break;
  231. case 'rebuild_flat_catalog_category':
  232. try {
  233. Mage::getResourceModel('catalog/category_flat')->rebuild();
  234. $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__('The flat catalog category has been rebuilt.'));
  235. }
  236. catch (Mage_Core_Exception $e) {
  237. $this->_getSession()->addError($e->getMessage());
  238. }
  239. catch (Exception $e) {
  240. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while rebuilding the flat catalog category.'));
  241. }
  242. break;
  243. case 'rebuild_flat_catalog_product':
  244. try {
  245. Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();
  246. $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__('The Flat Catalog Product was rebuilt'));
  247. }
  248. catch (Mage_Core_Exception $e) {
  249. $this->_getSession()->addError($e->getMessage());
  250. }
  251. catch (Exception $e) {
  252. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while rebuilding the flat product catalog.'));
  253. }
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. $this->_redirect('*/*');
  260. }
  261. public function refreshCatalogRewritesAction()
  262. {
  263. try {
  264. Mage::getSingleton('catalog/url')->refreshRewrites();
  265. $this->_getSession()->addSuccess(
  266. Mage::helper('adminhtml')->__('The catalog rewrites have been refreshed.')
  267. );
  268. }
  269. catch (Mage_Core_Exception $e) {
  270. $this->_getSession()->addError($e->getMessage());
  271. }
  272. catch (Exception $e) {
  273. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while refreshing the catalog rewrites.'));
  274. }
  275. $this->_redirect('*/*');
  276. }
  277. public function clearImagesCacheAction()
  278. {
  279. try {
  280. Mage::getModel('catalog/product_image')->clearCache();
  281. $this->_getSession()->addSuccess(
  282. Mage::helper('adminhtml')->__('The image cache was cleared.')
  283. );
  284. }
  285. catch (Mage_Core_Exception $e) {
  286. $this->_getSession()->addError($e->getMessage());
  287. }
  288. catch (Exception $e) {
  289. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while clearing the image cache.'));
  290. }
  291. $this->_redirect('*/*');
  292. }
  293. public function refreshLayeredNavigationAction()
  294. {
  295. try {
  296. Mage::getSingleton('catalogindex/indexer')->plainReindex();
  297. $this->_getSession()->addSuccess(
  298. Mage::helper('adminhtml')->__('The Layered Navigation indices were refreshed.')
  299. );
  300. }
  301. catch (Mage_Core_Exception $e) {
  302. $this->_getSession()->addError($e->getMessage());
  303. }
  304. catch (Exception $e) {
  305. $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while refreshing the layered navigation indices.'));
  306. }
  307. $this->_redirect('*/*');
  308. }
  309. protected function _isAllowed()
  310. {
  311. return Mage::getSingleton('admin/session')->isAllowed('system/cache');
  312. }
  313. }