PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/local/MageWorx/Adminhtml/controllers/Seosuite/Report/ProductController.php

https://bitbucket.org/kdms/sh-magento
PHP | 233 lines | 149 code | 33 blank | 51 comment | 14 complexity | 77effa279b3a5a410dfffeb89fc5ca72 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_Report_ProductController extends Mage_Adminhtml_Controller_Action {
  31. public function indexAction() {
  32. if (!Mage::helper('seosuite')->getProductReportStatus()) Mage::getSingleton('adminhtml/session')->addWarning(Mage::helper('seosuite')->__('You need to generate the report due to recent changes.'));
  33. $this->_title($this->__('SEO Suite'))->_title($this->__('Products Report'));
  34. $this->loadLayout()
  35. ->_setActiveMenu('report/seosuit')
  36. ->_addBreadcrumb($this->__('SEO Suite'), $this->__('SEO Suite'))
  37. ->_addBreadcrumb($this->__('Products Report'), $this->__('Products Report'))
  38. ->renderLayout();
  39. }
  40. public function gridAction() {
  41. $this->loadLayout(false);
  42. $this->renderLayout();
  43. }
  44. public function generateAction() {
  45. $this->loadLayout();
  46. $this->renderLayout();
  47. }
  48. public function runGenerateAction() {
  49. $action = $this->getRequest()->getParam('action', '');
  50. $current = intval($this->getRequest()->getParam('current', 0));
  51. if (!$action) return false;
  52. $result = array();
  53. // 'start', 'preparation', 'calculation'
  54. switch ($action) {
  55. case 'start':
  56. // truncate report table
  57. $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
  58. $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
  59. $connection->truncate($tablePrefix.'seosuite_report_product');
  60. $action = 'preparation';
  61. case 'preparation':
  62. $limit = 500;
  63. $total = $this->_getTotalProductCount();
  64. $result = array();
  65. if ($current<$total) {
  66. $this->_prepareProducts($current, $limit);
  67. $current += $limit;
  68. if ($current>=$total) {
  69. $current = $total;
  70. //$result['stop'] = 1;
  71. $action = 'calculation';
  72. }
  73. $result['text'] = $this->__('Total %1$s, processed %2$s records (%3$s%%)...', $total, $current, round($current*100/$total, 2));
  74. $result['url'] = $this->getUrl('*/*/runGenerate/', array('action'=>$action, 'current'=>($action=='preparation'?$current:0)));
  75. }
  76. break;
  77. case 'calculation':
  78. $limit = 1;
  79. $stories = $this->_getStores();
  80. $total = count($stories)+1;
  81. $result = array();
  82. if ($current<$total) {
  83. if (count($stories)>=$total) $storeId = 0; else $storeId = $stories[$current];
  84. $this->_calculateProducts($storeId);
  85. $current += $limit;
  86. if ($current>=$total) {
  87. $current = $total;
  88. $result['stop'] = 1;
  89. Mage::helper('seosuite')->setProductReportStatus(1);
  90. $this->_getSession()->addSuccess(Mage::helper('seosuite')->__('Report has been successfully generated.'));
  91. }
  92. if ($current<=$limit) {
  93. $result['text'] = $this->__('Starting to calculate store\'s product data...');
  94. } else {
  95. $result['text'] = $this->__('Total %1$s, processed %2$s records (%3$s%%)...', $total-1, $current-1, round(($current-1)*100/($total-1), 2));
  96. }
  97. $result['url'] = $this->getUrl('*/*/runGenerate/', array('action'=>$action, 'current'=>$current));
  98. }
  99. break;
  100. }
  101. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  102. }
  103. protected function _getProductCollection($store, $from=0, $limit=900000000) {
  104. $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
  105. $collection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect(array('sku', 'url_path', 'type_id', 'meta_title', 'meta_description'));
  106. $collection->addStoreFilter($store);
  107. $collection->joinAttribute('name', 'catalog_product/name', 'entity_id', null, 'inner', $adminStore);
  108. if ($store) $collection->joinAttribute('custom_name', 'catalog_product/name', 'entity_id', null, 'inner', $store->getId());
  109. $collection->addAttributeToFilter('visibility', array(2,3,4));
  110. $collection->addAttributeToFilter('status', 1);
  111. $collection->getSelect()->limit($limit, $from);
  112. return $collection;
  113. }
  114. protected function _getTotalProductCount() {
  115. $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
  116. $select = $this->_getProductCollection(null)->getSelectCountSql();
  117. $total = $connection->fetchOne($select);
  118. return $total;
  119. }
  120. protected function _prepareProducts($from, $limit) {
  121. $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
  122. $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
  123. $helper = Mage::helper('seosuite');
  124. $connection->beginTransaction();
  125. // default store = 0
  126. /* $collection = $this->_getProductCollection(null, $from, $limit);
  127. foreach ($collection as $item) {
  128. $connection->insert($tablePrefix.'seosuite_report_product', array(
  129. 'entity_id' => $item->getId(),
  130. 'store_id' => 0,
  131. 'sku' => $item->getSku(),
  132. 'url_path' => $item->getUrlPath(),
  133. 'type_id' => $item->getTypeId(),
  134. 'name' => $item->getName(),
  135. 'prepared_name' => $helper->_prepareText($item->getName()),
  136. 'meta_title' => $helper->_trimText($item->getMetaTitle()),
  137. 'prepared_meta_title' => $helper->_prepareText($item->getMetaTitle()),
  138. 'meta_title_len' => strlen($helper->_trimText($item->getMetaTitle())),
  139. 'meta_descr_len' => strlen($helper->_trimText($item->getMetaDescription())),
  140. ));
  141. }
  142. */
  143. // no default stories
  144. $stores = $this->_getStores();
  145. foreach ($stores as $storeId){
  146. if ($storeId==0) continue;
  147. $store = Mage::app()->getStore($storeId);
  148. $collection = $this->_getProductCollection($store, $from, $limit);
  149. foreach ($collection as $item) {
  150. $connection->insert($tablePrefix.'seosuite_report_product', array(
  151. 'id' => null,
  152. 'entity_id' => $item->getId(),
  153. 'store_id' => $storeId,
  154. 'sku' => $item->getSku(),
  155. 'url_path' => $item->getUrlPath(),
  156. 'type_id' => $item->getTypeId(),
  157. 'name' => $item->getCustomName(),
  158. 'prepared_name' => $helper->_prepareText($item->getCustomName()),
  159. 'meta_title' => $helper->_trimText($item->getMetaTitle()),
  160. 'prepared_meta_title' => $helper->_prepareText($item->getMetaTitle()),
  161. 'meta_title_len' => strlen($helper->_trimText($item->getMetaTitle())),
  162. 'meta_descr_len' => strlen($helper->_trimText($item->getMetaDescription()))
  163. ));
  164. }
  165. }
  166. $connection->commit();
  167. }
  168. protected function _getStores() {
  169. return Mage::getModel('core/store')->getCollection()->load()->getAllIds();
  170. }
  171. protected function _calculateProducts($storeId) {
  172. $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
  173. $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
  174. $sql = "UPDATE `".$tablePrefix."seosuite_report_product` AS srp,
  175. (SELECT `prepared_name`, `store_id`, COUNT(*) AS dupl_count FROM `".$tablePrefix."seosuite_report_product` WHERE `store_id`=".intval($storeId)." AND `prepared_name`!='' GROUP BY `prepared_name`) AS srpr
  176. SET srp.`name_dupl` = srpr.dupl_count
  177. WHERE srp.`prepared_name`=srpr.`prepared_name` AND srp.`store_id`=srpr.`store_id` AND srp.`prepared_name`!='' AND srp.`store_id`=".intval($storeId);
  178. $connection->query($sql);
  179. $sql = "UPDATE `".$tablePrefix."seosuite_report_product` AS srp,
  180. (SELECT `prepared_meta_title`, `store_id`, COUNT(*) AS dupl_count FROM `".$tablePrefix."seosuite_report_product` WHERE `store_id`=".intval($storeId)." AND `prepared_meta_title`!='' GROUP BY `prepared_meta_title`) AS srpr
  181. SET srp.`meta_title_dupl` = srpr.dupl_count
  182. WHERE srp.`prepared_meta_title`=srpr.`prepared_meta_title` AND srp.`store_id`=srpr.`store_id` AND srp.`prepared_meta_title`!='' AND srp.`store_id`=".intval($storeId);
  183. $connection->query($sql);
  184. }
  185. public function duplicateViewAction() {
  186. if (!Mage::helper('seosuite')->getProductReportStatus()) Mage::getSingleton('adminhtml/session')->addWarning(Mage::helper('seosuite')->__('You need to generate the report due to recent changes.'));
  187. $this->_title($this->__('SEO Suite'))->_title($this->__('Products Report'))->_title($this->__('View Duplicates'));
  188. $this->loadLayout()
  189. ->_setActiveMenu('report/seosuit')
  190. ->_addBreadcrumb($this->__('SEO Suite'), $this->__('SEO Suite'))
  191. ->_addBreadcrumb($this->__('Products Report'), $this->__('Products Report'))
  192. ->_addBreadcrumb($this->__('View Duplicates'), $this->__('View Duplicates'))
  193. ->renderLayout();
  194. }
  195. public function duplicateViewGridAction() {
  196. $this->loadLayout(false);
  197. $this->renderLayout();
  198. }
  199. }