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_SalesRule
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: /**
29: * SalesRule Model Resource Coupon_Collection
30: *
31: * @category Mage
32: * @package Mage_SalesRule
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_SalesRule_Model_Resource_Coupon_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Constructor
39: *
40: */
41: protected function _construct()
42: {
43: parent::_construct();
44: $this->_init('salesrule/coupon');
45: }
46:
47: /**
48: * Add rule to filter
49: *
50: * @param Mage_SalesRule_Model_Rule|int $rule
51: *
52: * @return Mage_SalesRule_Model_Resource_Coupon_Collection
53: */
54: public function addRuleToFilter($rule)
55: {
56: if ($rule instanceof Mage_SalesRule_Model_Rule) {
57: $ruleId = $rule->getId();
58: } else {
59: $ruleId = (int)$rule;
60: }
61:
62: $this->addFieldToFilter('rule_id', $ruleId);
63:
64: return $this;
65: }
66:
67: /**
68: * Add rule IDs to filter
69: *
70: * @param array $ruleIds
71: *
72: * @return Mage_SalesRule_Model_Resource_Coupon_Collection
73: */
74: public function addRuleIdsToFilter(array $ruleIds)
75: {
76: $this->addFieldToFilter('rule_id', array('in' => $ruleIds));
77: return $this;
78: }
79:
80: /**
81: * Filter collection to be filled with auto-generated coupons only
82: *
83: * @return Mage_SalesRule_Model_Resource_Coupon_Collection
84: */
85: public function addGeneratedCouponsFilter()
86: {
87: $this->addFieldToFilter('is_primary', array('null' => 1));
88: return $this;
89: }
90:
91: /**
92: * Callback function that filters collection by field "Used" from grid
93: *
94: * @param Mage_Core_Model_Resource_Db_Collection_Abstract $collection
95: * @param Mage_Adminhtml_Block_Widget_Grid_Column $column
96: */
97: public function addIsUsedFilterCallback($collection, $column)
98: {
99: $filterValue = $column->getFilter()->getCondition();
100: $collection->addFieldToFilter(
101: $this->getConnection()->getCheckSql('main_table.times_used > 0', 1, 0),
102: array('eq' => $filterValue)
103: );
104: }
105: }
106: