00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Rule 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 /** 00028 * Abstract rules collection to be extended 00029 * 00030 * @category Mage 00031 * @package Mage_Rule 00032 * @author Magento Core Team <core@magentocommerce.com> 00033 */ 00034 class Mage_Rule_Model_Mysql4_Rule_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract 00035 { 00036 /** 00037 * Quote rule environment 00038 * 00039 * @var Mage_Rule_Model_Environment 00040 */ 00041 protected $_env; 00042 00043 protected function _construct() 00044 { 00045 $this->_init('rule/rule'); 00046 } 00047 00048 /** 00049 * Initialize resource collection variables 00050 * 00051 * Example: 00052 */ 00053 /* 00054 public function __construct() 00055 { 00056 parent::__construct(Mage::getSingleton('core/resource')->getConnection('sales_read')); 00057 00058 $ruleTable = Mage::getSingleton('core/resource')->getTableName('sales/quote_rule'); 00059 $this->_select->from($ruleTable)->order('sort_order'); 00060 00061 $this->setItemObjectClass(Mage::getConfig()->getModelClassName('sales/quote_rule')); 00062 } 00063 */ 00064 00065 /** 00066 * Set environment for all rules in collection 00067 * 00068 * @param Mage_Rule_Model_Environment $env 00069 * @return Mage_Rule_Model_Mysql4_Rule_Collection 00070 */ 00071 public function setEnv(Mage_Rule_Model_Environment $env=null) 00072 { 00073 $this->_env = $env; 00074 return $this; 00075 } 00076 00077 /** 00078 * Retrieve environment for the rules in collection 00079 * 00080 * @return Mage_Rule_Model_Mysql4_Rule_Collection 00081 */ 00082 public function getEnv() 00083 { 00084 if (!$this->_env) { 00085 $this->_env = Mage::getModel('core/rule_environment'); 00086 $this->_env->collect(); 00087 } 00088 return $this->_env; 00089 } 00090 00091 /** 00092 * Overload default addItem method to set environment for the rules 00093 * 00094 * @param Mage_Rule_Model_Abstract $rule 00095 * @return Mage_Rule_Model_Mysql4_Rule_Collection 00096 */ 00097 public function addItem(Varien_Object $rule) 00098 { 00099 $rule->setEnv($this->getEnv())->setIsCollectionValidated(true); 00100 parent::addItem($rule); 00101 return $this; 00102 } 00103 00104 /** 00105 * Set filter for the collection based on the environment 00106 * 00107 * @return Mage_Rule_Model_Mysql4_Rule_Collection 00108 */ 00109 public function setActiveFilter() 00110 { 00111 $e = $this->getEnv()->getData(); 00112 00113 $this->_select->where("is_active=1"); 00114 00115 if (!empty($e['now'])) { 00116 if (!is_numeric($e['now'])) { 00117 $e['now'] = strtotime($e['now']); 00118 } 00119 $now = date("Y-m-d H:i:s", $e['now']); 00120 } else { 00121 $now = date("Y-m-d H:i:s"); 00122 } 00123 $this->_select->where("start_at<='$now' and expire_at>='$now'"); 00124 00125 return $this; 00126 } 00127 00128 /** 00129 * Process the quote with all the rules in collection 00130 * 00131 * @return Mage_Rule_Model_Mysql4_Rule_Collection 00132 */ 00133 public function process() 00134 { 00135 $rules = $this->getItems(); 00136 foreach ($rules as $rule) { 00137 $rule->process(); 00138 if ($rule->getStopProcessingRules()) { 00139 break; 00140 } 00141 } 00142 return $this; 00143 } 00144 00145 protected function _afterLoad() 00146 { 00147 $this->walk('afterLoad'); 00148 } 00149 }