00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_CatalogRule_Model_Rule extends Mage_Rule_Model_Rule
00029 {
00030 protected $_productIds = array();
00031
00032 protected $_now;
00033
00034 protected function _construct()
00035 {
00036 parent::_construct();
00037 $this->_init('catalogrule/rule');
00038 $this->setIdFieldName('rule_id');
00039 }
00040
00041 public function getConditionsInstance()
00042 {
00043 return Mage::getModel('catalogrule/rule_condition_combine');
00044 }
00045
00046 public function getActionsInstance()
00047 {
00048 return Mage::getModel('catalogrule/rule_action_collection');
00049 }
00050
00051 public function getNow()
00052 {
00053 if (!$this->_now) {
00054 return now();
00055 }
00056 return $this->_now;
00057 }
00058
00059 public function setNow($now)
00060 {
00061 $this->_now = $now;
00062 }
00063
00064 public function toString($format='')
00065 {
00066 $str = Mage::helper('catalogrule')->__("Name: %s", $this->getName()) ."\n"
00067 . Mage::helper('catalogrule')->__("Start at: %s", $this->getStartAt()) ."\n"
00068 . Mage::helper('catalogrule')->__("Expire at: %s", $this->getExpireAt()) ."\n"
00069 . Mage::helper('catalogrule')->__("Customer registered: %s", $this->getCustomerRegistered()) ."\n"
00070 . Mage::helper('catalogrule')->__("Customer is new buyer: %s", $this->getCustomerNewBuyer()) ."\n"
00071 . Mage::helper('catalogrule')->__("Description: %s", $this->getDescription()) ."\n\n"
00072 . $this->getConditions()->toStringRecursive() ."\n\n"
00073 . $this->getActions()->toStringRecursive() ."\n\n";
00074 return $str;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 public function toArray(array $arrAttributes = array())
00090 {
00091 $out = parent::toArray($arrAttributes);
00092 $out['customer_registered'] = $this->getCustomerRegistered();
00093 $out['customer_new_buyer'] = $this->getCustomerNewBuyer();
00094
00095 return $out;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 public function getResourceCollection()
00127 {
00128 return Mage::getResourceModel('catalogrule/rule_collection');
00129 }
00130
00131 protected function _afterSave()
00132 {
00133 $this->_getResource()->updateRuleProductData($this);
00134 parent::_afterSave();
00135 }
00136
00137 public function getMatchingProductIds()
00138 {
00139 if (empty($this->_productIds)) {
00140 $productCollection = Mage::getResourceModel('catalog/product_collection');
00141 $websiteIds = explode(',', $this->getWebsiteIds());
00142 if (!empty($websiteIds)) {
00143 $productCollection->addWebsiteFilter($websiteIds);
00144 }
00145
00146 $this->setCollectedAttributes(array());
00147 $this->getConditions()->collectValidatedAttributes($productCollection);
00148
00149 $this->_productIds = array();
00150 Mage::getSingleton('core/resource_iterator')->walk(
00151 $productCollection->getSelect(),
00152 array(array($this, 'callbackValidateProduct')),
00153 array(
00154 'attributes'=>$this->getCollectedAttributes(),
00155 'product'=>Mage::getModel('catalog/product'),
00156 )
00157 );
00158 }
00159 return $this->_productIds;
00160 }
00161
00162 public function callbackValidateProduct($args)
00163 {
00164 $product = $args['product']->setData($args['row']);
00165 if (!empty($args['attributes']['category_ids'])) {
00166 $categoryCollection = $product->getCategoryCollection()->load();
00167 $categories = array();
00168 foreach ($categoryCollection as $category) {
00169 $categories[] = $category->getId();
00170 }
00171 $product->setCategoryIds($categories);
00172 }
00173 if ($this->getConditions()->validate($product)) {
00174 $this->_productIds[] = $product->getId();
00175 }
00176 }
00177
00178 public function applyToProduct($product, $websiteIds=null)
00179 {
00180 if (is_numeric($product)) {
00181 $product = Mage::getModel('catalog/product')->load($product);
00182 }
00183 if (is_null($websiteIds)) {
00184 $websiteIds = explode(',', $this->getWebsiteIds());
00185 }
00186 $this->getResource()->applyToProduct($this, $product, $websiteIds);
00187 }
00188
00189 public function getCustomerGroupIds()
00190 {
00191 $ids = $this->getData('customer_group_ids');
00192 if (($ids && !$this->getCustomerGroupChecked()) || is_string($ids)) {
00193 if (is_string($ids)) {
00194 $ids = explode(',', $ids);
00195 }
00196
00197 $groupIds = Mage::getModel('customer/group')->getCollection()->getAllIds();
00198 $ids = array_intersect($ids, $groupIds);
00199 $this->setData('customer_group_ids', $ids);
00200 $this->setCustomerGroupChecked(true);
00201 }
00202 return $ids;
00203 }
00204 }