PageRenderTime 1227ms CodeModel.GetById 38ms RepoModel.GetById 7ms app.codeStats 1ms

/app/code/community/Fontis/Australia/Model/Getprice/Cron.php

https://bitbucket.org/acidel/buykoala
PHP | 241 lines | 167 code | 45 blank | 29 comment | 15 complexity | 3cab8d7628192b52cd39132d721d6080 MD5 | raw file
  1. <?php
  2. /**
  3. * Fontis Australia Extension
  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 and you will be sent a copy immediately.
  14. *
  15. * @category Fontis
  16. * @package Fontis_Australia
  17. * @author Tom Greenaway
  18. * @copyright Copyright (c) 2008 Fontis Pty. Ltd. (http://www.fontis.com.au)
  19. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. */
  21. // Callback used by the Magento resource iterator walk() method. Adds a
  22. // product ID to a static array and calls addProductXmlgetPrice() to
  23. // generate XML when the array reaches the batch size.
  24. function addProductXmlCallbackGetPrice($args) {
  25. Fontis_Australia_Model_GetPrice_Cron::$accumulator[] = $args['row']['entity_id'];
  26. $length = count(Fontis_Australia_Model_GetPrice_Cron::$accumulator);
  27. if($length >= Fontis_Australia_Model_GetPrice_Cron::BATCH_SIZE) {
  28. addProductXmlGetPrice();
  29. }
  30. }
  31. // Runs a subprocesss to create feed XML for the product IDs in the static
  32. // array, then empties the array.
  33. function addProductXmlGetPrice() {
  34. $length = count(Fontis_Australia_Model_GetPrice_Cron::$accumulator);
  35. if($length > 0) {
  36. Mage::log("Fontis/Australia_Model_Getprice_Cron: Processing product IDs " . Fontis_Australia_Model_GetPrice_Cron::$accumulator[0] .
  37. " to " . Fontis_Australia_Model_GetPrice_Cron::$accumulator[$length - 1]);
  38. $store_id = Fontis_Australia_Model_GetPrice_Cron::$store->getId();
  39. $data = shell_exec("php " . Mage::getBaseDir() . "/app/code/community/Fontis/Australia/Model/Getprice/Child.php " .
  40. Mage::getBaseDir() . " '" . serialize(Fontis_Australia_Model_GetPrice_Cron::$accumulator) .
  41. "' " . $store_id);
  42. Fontis_Australia_Model_GetPrice_Cron::$accumulator = array();
  43. $array = json_decode($data, true);
  44. if(is_array($array)) {
  45. $codes = array();
  46. foreach($array as $prod) {
  47. Fontis_Australia_Model_GetPrice_Cron::$debugCount += 1;
  48. $product_node = Fontis_Australia_Model_GetPrice_Cron::$root_node->addChild('product');
  49. foreach($prod as $key => $val) {
  50. if($key == 'Code') {
  51. $codes[] = $val;
  52. }
  53. $product_node->addChild($key, htmlspecialchars($val));
  54. }
  55. }
  56. if(!empty($codes)) {
  57. Mage::log("Fontis/Australia_Model_Getprice_Cron: Codes: ".implode(",", $codes));
  58. }
  59. } else {
  60. Mage::log("Fontis/Australia_Model_Getprice_Cron: Could not unserialize to array:");
  61. Mage::log($data);
  62. Mage::log($array);
  63. }
  64. Mage::log('Fontis/Australia_Model_Getprice_Cron: ' . strlen($data) . ' characters returned');
  65. }
  66. }
  67. class Fontis_Australia_Model_GetPrice_Cron {
  68. const BATCH_SIZE = 100;
  69. public static $doc;
  70. public static $root_node;
  71. public static $store;
  72. public static $accumulator;
  73. public static $debugCount = 0;
  74. protected function _construct() {
  75. self::$accumulator = array();
  76. }
  77. protected function getPath() {
  78. $path = "";
  79. $config_path = Mage::getStoreConfig('fontis_feeds/getpricefeed/output');
  80. if (substr($config_path, 0, 1) == "/") {
  81. $path = $config_path . '/';
  82. } else {
  83. $path = Mage::getBaseDir() . '/' . $config_path . '/';
  84. }
  85. return str_replace('//', '/', $path);
  86. }
  87. public function nonstatic() {
  88. self::update();
  89. }
  90. public static function update() {
  91. Mage::log('Fontis/Australia_Model_Getprice_Cron: Entered update function');
  92. if (Mage::getStoreConfig('fontis_feeds/getpricefeed/active')) {
  93. $io = new Varien_Io_File();
  94. $io->setAllowCreateFolders(true);
  95. $io->open(array('path' => self::getPath()));
  96. // Loop through all stores:
  97. foreach(Mage::app()->getStores() as $store) {
  98. Mage::log('Fontis/Australia_Model_Getprice_Cron: Processing store: ' . $store->getName());
  99. $clean_store_name = str_replace('+', '-', strtolower(urlencode($store->getName())));
  100. // Write the entire products xml file:
  101. Mage::log('Fontis/Australia_Model_Getprice_Cron: Generating All Products XML File');
  102. $products_result = self::getProductsXml($store);
  103. $io->write($clean_store_name . '-products.xml', $products_result['xml']);
  104. Mage::log('Fontis/Australia_Model_Getprice_Cron: Wrote to file: ' . $clean_store_name . '-products.xml', $products_result['xml']);
  105. // Write the leaf categories xml file:
  106. Mage::log('Fontis/Australia_Model_Getprice_Cron: Generating Categories XML File');
  107. $categories_result = self::getCategoriesXml($store);
  108. $io->write($clean_store_name . '-categories.xml', $categories_result['xml']);
  109. Mage::log('Fontis/Australia_Model_Getprice_Cron: Wrote to file: ' . $clean_store_name . '-categories.xml', $categories_result['xml']);
  110. // Write for each leaf category, their products xml file:
  111. foreach($categories_result['link_ids'] as $link_id) {
  112. Mage::log('Fontis/Australia_Model_Getprice_Cron: Generating Product Category XML File: ' . $link_id);
  113. $subcategory_products_result = self::getProductsXml($store, $link_id);
  114. $io->write($clean_store_name . '-products-'.$link_id.'.xml', $subcategory_products_result['xml']);
  115. Mage::log('Fontis/Australia_Model_Getprice_Cron: Wrote to file: ' . $clean_store_name . '-products-'.$link_id.'.xml', $subcategory_products_result['xml']);
  116. }
  117. }
  118. $io->close();
  119. } else {
  120. Mage::log('Fontis/Australia_Model_Getprice_Cron: Disabled');
  121. }
  122. }
  123. public function getCategoriesXml($store) {
  124. $clean_store_name = str_replace('+', '-', strtolower(urlencode($store->getName())));
  125. $result = array();
  126. $categories = Mage::getModel('catalog/category')->getCollection()
  127. ->setStoreId($store->getId())
  128. ->addAttributeToFilter('is_active', 1);
  129. $categories->load()->getItems();
  130. $full_categories = array();
  131. foreach($categories as $category) {
  132. $id = $category->getId();
  133. $category = Mage::getModel('catalog/category')->load($id);
  134. $children = $category->getAllChildren(true);
  135. if (count($children) <= 1) {
  136. $full_categories[] = $category;
  137. }
  138. }
  139. $storeUrl = $store->getBaseUrl();
  140. $shopName = $store->getName();
  141. $date = date("d-m-Y", Mage::getModel('core/date')->timestamp(time()));
  142. $time = date("h:i:s", Mage::getModel('core/date')->timestamp(time()));
  143. $doc = new SimpleXMLElement('<store url="' . $storeUrl. '" date="'.$date.'" time="'.$time.'" name="' . $shopName . '"></store>');
  144. foreach($full_categories as $category) {
  145. $category_node = $doc->addChild('cat');
  146. $title_node = $category_node->addChild('name');
  147. $title_node[0] = htmlspecialchars($category->getName());
  148. $link_node = $category_node->addChild('link');
  149. $link_node[0] = Mage::getStoreConfig('web/unsecure/base_url') .
  150. Mage::getStoreConfig('fontis_feeds/getpricefeed/output') . "/$clean_store_name-products-" . $category->getId() . '.xml';
  151. $result['link_ids'][] = $category->getId();
  152. }
  153. $result['xml'] = self::formatSimpleXML($doc);
  154. return $result;
  155. }
  156. public function getProductsXml($store, $cat_id = -1) {
  157. Fontis_Australia_Model_GetPrice_Cron::$store = $store;
  158. $result = array();
  159. $product = Mage::getModel('catalog/product');
  160. $products = $product->getCollection();
  161. $products->setStoreId($store);
  162. $products->addStoreFilter();
  163. $products->addAttributeToSelect('*');
  164. $products->addAttributeToSelect(array('name', 'price', 'image', 'status', 'manufacturer'), 'left');
  165. $products->addAttributeToFilter('status', 1);
  166. $products->addAttributeToFilter('visibility', 4);
  167. if ($cat_id != -1) {
  168. $products->getSelect()->where("e.entity_id IN (
  169. SELECT product_id FROM catalog_category_product WHERE category_id = ".$cat_id."
  170. )");
  171. }
  172. $products->getSelect()->order('product_id');
  173. $storeUrl = $store->getBaseUrl();
  174. $shopName = $store->getName();
  175. $date = date("d-m-Y", Mage::getModel('core/date')->timestamp(time()));
  176. $time = date("h:i:s", Mage::getModel('core/date')->timestamp(time()));
  177. self::$doc = new SimpleXMLElement('<store url="' . $storeUrl. '" date="'.$date.'" time="'.$time.'" name="' . $shopName . '"></store>');
  178. self::$root_node = self::$doc->addChild('products');
  179. Mage::log('Fontis/Australia_Model_Getprice_Cron: Iterating: ' . $products->getSize() . ' products...');
  180. Mage::getSingleton('core/resource_iterator')->walk($products->getSelect(), array('addProductXmlCallbackGetPrice'), array('product' => $product));
  181. // call XML generation function one last time to process remaining batch
  182. addProductXmlGetPrice();
  183. Mage::log('Fontis/Australia_Model_Getprice_Cron: Iteration complete');
  184. $result['xml'] = self::formatSimpleXML(self::$doc);
  185. return $result;
  186. }
  187. public static function formatSimpleXML($doc) {
  188. $dom = new DOMDocument('1.0');
  189. $dom->preserveWhiteSpace = false;
  190. $dom->formatOutput = true;
  191. $dom_sxml = dom_import_simplexml($doc);
  192. $dom_sxml = $dom->importNode($dom_sxml, true);
  193. $dom->appendChild($dom_sxml);
  194. return $dom->saveXML();
  195. }
  196. }