PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/MageWorx/Adminhtml/controllers/Seosuite/TemplateController.php

https://bitbucket.org/kdms/sh-magento
PHP | 199 lines | 132 code | 27 blank | 40 comment | 15 complexity | e712ae3d794e7bd0a79ca3bd2a65b14a MD5 | raw file
  1. <?php
  2. /**
  3. * MageWorx
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the MageWorx EULA that is bundled with
  8. * this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.mageworx.com/LICENSE-1.0.html
  11. *
  12. * DISCLAIMER
  13. *
  14. * Do not edit or add to this file if you wish to upgrade the extension
  15. * to newer versions in the future. If you wish to customize the extension
  16. * for your needs please refer to http://www.mageworx.com/ for more information
  17. *
  18. * @category MageWorx
  19. * @package MageWorx_SeoSuite
  20. * @copyright Copyright (c) 2012 MageWorx (http://www.mageworx.com/)
  21. * @license http://www.mageworx.com/LICENSE-1.0.html
  22. */
  23. /**
  24. * SEO Suite extension
  25. *
  26. * @category MageWorx
  27. * @package MageWorx_SeoSuite
  28. * @author MageWorx Dev Team
  29. */
  30. class MageWorx_Adminhtml_Seosuite_TemplateController extends Mage_Adminhtml_Controller_Action {
  31. private $_currentModel;
  32. private $_currentModelCode;
  33. protected function _getTotalCount($entity = 'product') {
  34. $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
  35. $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
  36. $select = $connection->select()->from($tablePrefix.'catalog_'.$entity.'_entity', 'COUNT(*)');
  37. $total = $connection->fetchOne($select);
  38. return intval($total);
  39. }
  40. public function indexAction() {
  41. $this->_title($this->__('SEO Suite'))->_title($this->__('Manage SEO Templates'));
  42. $this->loadLayout()
  43. ->_setActiveMenu('catalog/seo_templates')
  44. ->_addBreadcrumb($this->__('SEO Suite'), $this->__('SEO Suite'))
  45. ->_addBreadcrumb($this->__('Templates'), $this->__('Templates'))
  46. ->renderLayout();
  47. }
  48. public function gridAction() {
  49. $this->loadLayout(false);
  50. $this->renderLayout();
  51. }
  52. public function editAction() {
  53. $templateId = $this->getRequest()->getParam('template_id');
  54. $storeId = $this->getRequest()->getParam('store',0);
  55. if (!$templateId)
  56. {
  57. return $this->_forward('grid');
  58. }
  59. $template = Mage::getModel('seosuite/template')->load($templateId);
  60. Mage::register('seosuite_template_edit', $template, true);
  61. $this->loadLayout();
  62. $this->renderLayout();
  63. }
  64. public function saveAction() {
  65. $params = $this->getRequest()->getParams();
  66. $templateModel = Mage::getModel('seosuite/template')->load($params['template_id']);
  67. try {
  68. $templateModel->setLastUpdate(time())->setStoreData($params)->save();
  69. } catch (Exception $e){
  70. Mage::getSingleton('adminhtml/session')->addError(
  71. Mage::helper('catalog')->__('%s',$e->getMessage()));
  72. $this->_redirect('*/*/');
  73. return;
  74. }
  75. Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Template successfully saved'));
  76. // return $this->_redirect('*/*/edit',array('store'=>$params['store_template']['store_id'],'template_id'=>$params['template_id']));
  77. return $this->_redirect('*/*/index');
  78. }
  79. public function applyAction() {
  80. $this->loadLayout();
  81. $templateId = $this->getRequest()->getParam('template_id');
  82. $model = Mage::getModel('seosuite/template')->load($templateId);
  83. $storeModelCollection = Mage::getModel('seosuite/template_store')->getCollection()->filterTemplateId($templateId);
  84. if(!$storeModelCollection->getSize()) {
  85. Mage::getSingleton('adminhtml/session')->addError($this->__('Template key is empty'));
  86. return $this->_redirect('*/*/index');
  87. }
  88. $canApply = false;
  89. foreach ($storeModelCollection as $item) {
  90. if(Mage::app()->getStore($item->getStoreId())->isAdmin()) {
  91. $canApply = true;
  92. continue;
  93. }
  94. // if ($this->getRequest()->getParam('store') == $item->getStoreId()) {
  95. // $canApply = true;
  96. // continue;
  97. // }
  98. }
  99. if(!$canApply) {
  100. Mage::getSingleton('adminhtml/session')->addError($this->__('Default Template key is empty. You can\'t continue.'));
  101. return $this->_redirect('*/*/index');
  102. }
  103. Mage::register('seosuite_template_current_model_id', $model->getId(),true);
  104. Mage::register('seosuite_template_current_model_name',$model->getTemplateName(),true);
  105. $this->renderLayout();
  106. }
  107. public function runApplyAction() {
  108. // @ini_set('max_execution_time', 1800);
  109. // @ini_set('memory_limit', 734003200);
  110. $storeId = $this->getRequest()->getParam('store',0);
  111. $model = Mage::getModel('seosuite/template')->load($this->getRequest()->getParam('model_id'));
  112. $templateCodeArray = explode('_',$model->getTemplateCode());
  113. $currentCode = array_shift($templateCodeArray)."_".join('',$templateCodeArray);
  114. Mage::register('seosuite_template_current_model_code', $currentCode,true);
  115. Mage::register('seosuite_template_current_model', $model,true);
  116. $aIndex = array(1,3,4,5,6,7,10);
  117. //$aIndex = array(1,2,3,4,5,6,7,8,9,10); # full list of index
  118. $limit = Mage::getStoreConfig('mageworx_seo/seosuite/template_limit',$storeId);
  119. $current = intval($this->getRequest()->getParam('current', array_shift($aIndex)));
  120. $reindex = $this->getRequest()->getParam('reindex', '');
  121. $result = array();
  122. if ($reindex) {
  123. // exit;
  124. // make reindex
  125. if ($reindex=='start') {
  126. $result['url'] = $this->getUrl('*/*/runApply/', array('reindex'=>'run'));
  127. $result['text'] = $this->__('Starting reindex product data...');
  128. } elseif ($reindex=='run') {
  129. $_index = Mage::getModel('index/process')->load($current);
  130. $current = $_index->getId() +1;
  131. while(!in_array($current,$aIndex) && $current<10)
  132. {
  133. $current++;
  134. }
  135. if(!$_index->getId())
  136. {
  137. Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Template was successfully applied.'));
  138. die($this->getUrl('*/*/index/',array('_secure'=>true)));
  139. } else {
  140. try {
  141. $_index->reindexAll();
  142. $result['text'] = $_index->getIndexer()->getDescription().$this->__('... 100%. Done.');
  143. } catch(Mage_Core_Exception $e) {
  144. $result['text'] = $e->getMessage();
  145. }
  146. $result['url'] = $this->getUrl('*/*/runApply/', array('reindex'=>'run', 'current'=>$current,'model_id'=>$model->getId()));
  147. }
  148. }
  149. } else {
  150. // applyUrl
  151. $entity = "product";
  152. if($model->getTemplateCode() == 'category_page')
  153. {
  154. $entity = 'category';
  155. }
  156. $total = $this->_getTotalCount($entity);
  157. // $result['test'] = "$total += $current += $limit";
  158. if ($current<$total) {
  159. Mage::getSingleton('seosuite/template_adapter_'.$currentCode)->setModel($model)->apply($current, $limit);
  160. $current += $limit;
  161. if ($current>=$total) {
  162. $current = $total;
  163. $result['url'] = $this->getUrl('*/*/runApply/', array('reindex'=>'start','model_id'=>$model->getId()));
  164. } else {
  165. $result['url'] = $this->getUrl('*/*/runApply/', array('current'=>$current,'model_id'=>$model->getId()));
  166. }
  167. $result['text'] = $this->__('Total %1$s, processed %2$s records (%3$s%%)...', $total, $current, round($current*100/$total, 2));
  168. }
  169. }
  170. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  171. }
  172. }