Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Rule_Block_Actions
  • Mage_Rule_Block_Conditions
  • Mage_Rule_Block_Editable
  • Mage_Rule_Block_Newchild
  • Mage_Rule_Block_Rule
  • Mage_Rule_Helper_Data
  • Mage_Rule_Model_Abstract
  • Mage_Rule_Model_Action_Abstract
  • Mage_Rule_Model_Action_Collection
  • Mage_Rule_Model_Condition_Abstract
  • Mage_Rule_Model_Condition_Combine
  • Mage_Rule_Model_Condition_Product_Abstract
  • Mage_Rule_Model_Environment
  • Mage_Rule_Model_Mysql4_Rule
  • Mage_Rule_Model_Mysql4_Rule_Collection
  • Mage_Rule_Model_Renderer_Actions
  • Mage_Rule_Model_Renderer_Conditions
  • Mage_Rule_Model_Resource_Abstract
  • Mage_Rule_Model_Resource_Rule_Collection_Abstract

Interfaces

  • Mage_Rule_Model_Action_Interface
  • Mage_Rule_Model_Condition_Interface
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  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 so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Rule
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: /**
 28:  * Abstract Rule entity resource model
 29:  *
 30:  * @category Mage
 31:  * @package Mage_Rule
 32:  * @author Magento Core Team <core@magentocommerce.com>
 33:  */
 34: abstract class Mage_Rule_Model_Resource_Abstract extends Mage_Core_Model_Resource_Db_Abstract
 35: {
 36:     /**
 37:      * Store associated with rule entities information map
 38:      *
 39:      * Example:
 40:      * array(
 41:      *    'entity_type1' => array(
 42:      *        'associations_table' => 'table_name',
 43:      *        'rule_id_field'      => 'rule_id',
 44:      *        'entity_id_field'    => 'entity_id'
 45:      *    ),
 46:      *    'entity_type2' => array(
 47:      *        'associations_table' => 'table_name',
 48:      *        'rule_id_field'      => 'rule_id',
 49:      *        'entity_id_field'    => 'entity_id'
 50:      *    )
 51:      *    ....
 52:      * )
 53:      *
 54:      * @var array
 55:      */
 56:     protected $_associatedEntitiesMap = array();
 57: 
 58:     /**
 59:      * Prepare rule's active "from" and "to" dates
 60:      *
 61:      * @param Mage_Core_Model_Abstract $object
 62:      *
 63:      * @return Mage_Rule_Model_Resource_Abstract
 64:      */
 65:     public function _beforeSave(Mage_Core_Model_Abstract $object)
 66:     {
 67:         $fromDate = $object->getFromDate();
 68:         if ($fromDate instanceof Zend_Date) {
 69:             $object->setFromDate($fromDate->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
 70:         } elseif (!is_string($fromDate) || empty($fromDate)) {
 71:             $object->setFromDate(null);
 72:         }
 73: 
 74:         $toDate = $object->getToDate();
 75:         if ($toDate instanceof Zend_Date) {
 76:             $object->setToDate($toDate->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
 77:         } elseif (!is_string($toDate) || empty($toDate)) {
 78:             $object->setToDate(null);
 79:         }
 80: 
 81:         parent::_beforeSave($object);
 82:         return $this;
 83:     }
 84: 
 85:     /**
 86:      * Bind specified rules to entities
 87:      *
 88:      * @param array|int|string $ruleIds
 89:      * @param array|int|string $entityIds
 90:      * @param string $entityType
 91:      *
 92:      * @return Mage_Rule_Model_Resource_Abstract
 93:      */
 94:     public function bindRuleToEntity($ruleIds, $entityIds, $entityType)
 95:     {
 96:         if (empty($ruleIds) || empty($entityIds)) {
 97:             return $this;
 98:         }
 99:         $adapter    = $this->_getWriteAdapter();
100:         $entityInfo = $this->_getAssociatedEntityInfo($entityType);
101: 
102:         if (!is_array($ruleIds)) {
103:             $ruleIds = array((int) $ruleIds);
104:         }
105:         if (!is_array($entityIds)) {
106:             $entityIds = array((int) $entityIds);
107:         }
108: 
109:         $data  = array();
110:         $count = 0;
111: 
112:         $adapter->beginTransaction();
113: 
114:         try {
115:             foreach ($ruleIds as $ruleId) {
116:                 foreach ($entityIds as $entityId) {
117:                     $data[] = array(
118:                         $entityInfo['entity_id_field'] => $entityId,
119:                         $entityInfo['rule_id_field'] => $ruleId
120:                     );
121:                     $count++;
122:                     if (($count % 1000) == 0) {
123:                         $adapter->insertOnDuplicate(
124:                             $this->getTable($entityInfo['associations_table']),
125:                             $data,
126:                             array($entityInfo['rule_id_field'])
127:                         );
128:                         $data = array();
129:                     }
130:                 }
131:             }
132:             if (!empty($data)) {
133:                 $adapter->insertOnDuplicate(
134:                     $this->getTable($entityInfo['associations_table']),
135:                     $data,
136:                     array($entityInfo['rule_id_field'])
137:                 );
138:             }
139: 
140:             $adapter->delete($this->getTable($entityInfo['associations_table']),
141:                 $adapter->quoteInto($entityInfo['rule_id_field']   . ' IN (?) AND ', $ruleIds) .
142:                 $adapter->quoteInto($entityInfo['entity_id_field'] . ' NOT IN (?)',  $entityIds)
143:             );
144:         } catch (Exception $e) {
145:             $adapter->rollback();
146:             throw $e;
147: 
148:         }
149: 
150:         $adapter->commit();
151: 
152:         return $this;
153:     }
154: 
155:     /**
156:      * Unbind specified rules from entities
157:      *
158:      * @param array|int|string $ruleIds
159:      * @param array|int|string $entityIds
160:      * @param string $entityType
161:      *
162:      * @return Mage_Rule_Model_Resource_Abstract
163:      */
164:     public function unbindRuleFromEntity($ruleIds = array(), $entityIds = array(), $entityType)
165:     {
166:         $writeAdapter = $this->_getWriteAdapter();
167:         $entityInfo   = $this->_getAssociatedEntityInfo($entityType);
168: 
169:         if (!is_array($entityIds)) {
170:             $entityIds = array((int) $entityIds);
171:         }
172:         if (!is_array($ruleIds)) {
173:             $ruleIds = array((int) $ruleIds);
174:         }
175: 
176:         $where = array();
177:         if (!empty($ruleIds)) {
178:             $where[] = $writeAdapter->quoteInto($entityInfo['rule_id_field'] . ' IN (?)', $ruleIds);
179:         }
180:         if (!empty($entityIds)) {
181:             $where[] = $writeAdapter->quoteInto($entityInfo['entity_id_field'] . ' IN (?)', $entityIds);
182:         }
183: 
184:         $writeAdapter->delete($this->getTable($entityInfo['associations_table']), implode(' AND ', $where));
185: 
186:         return $this;
187:     }
188: 
189:     /**
190:      * Retrieve rule's associated entity Ids by entity type
191:      *
192:      * @param int $ruleId
193:      * @param string $entityType
194:      *
195:      * @return array
196:      */
197:     public function getAssociatedEntityIds($ruleId, $entityType)
198:     {
199:         $entityInfo = $this->_getAssociatedEntityInfo($entityType);
200: 
201:         $select = $this->_getReadAdapter()->select()
202:             ->from($this->getTable($entityInfo['associations_table']), array($entityInfo['entity_id_field']))
203:             ->where($entityInfo['rule_id_field'] . ' = ?', $ruleId);
204: 
205:         return $this->_getReadAdapter()->fetchCol($select);
206:     }
207: 
208:     /**
209:      * Retrieve website ids of specified rule
210:      *
211:      * @param int $ruleId
212:      * @return array
213:      */
214:     public function getWebsiteIds($ruleId)
215:     {
216:         return $this->getAssociatedEntityIds($ruleId, 'website');
217:     }
218: 
219:     /**
220:      * Retrieve customer group ids of specified rule
221:      *
222:      * @param int $ruleId
223:      * @return array
224:      */
225:     public function getCustomerGroupIds($ruleId)
226:     {
227:         return $this->getAssociatedEntityIds($ruleId, 'customer_group');
228:     }
229: 
230:     /**
231:      * Retrieve correspondent entity information (associations table name, columns names)
232:      * of rule's associated entity by specified entity type
233:      *
234:      * @param string $entityType
235:      *
236:      * @return array
237:      */
238:     protected function _getAssociatedEntityInfo($entityType)
239:     {
240:         if (isset($this->_associatedEntitiesMap[$entityType])) {
241:             return $this->_associatedEntitiesMap[$entityType];
242:         }
243: 
244:         $e = Mage::exception(
245:             'Mage_Core',
246:             Mage::helper('rule')->__(
247:                 'There is no information about associated entity type "%s".', $entityType
248:             )
249:         );
250:         throw $e;
251:     }
252: }
253: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0