Gifter - Version 1.0.0

Version Notes

Gifter

Download this release

Release Info

Developer adam
Extension Gifter
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/local/Bricks/Gifter/Block/Adminhtml/Gift.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Block_Adminhtml_Gift extends Mage_Adminhtml_Block_Widget_Grid_Container
13
+ {
14
+ public function __construct()
15
+ {
16
+ $this->_controller = 'promo_gifter';
17
+ $this->_headerText = $this->__('Gifts');
18
+
19
+ parent::__construct();
20
+
21
+ $this->removeButton('add');
22
+ }
23
+
24
+ protected function _prepareLayout()
25
+ {
26
+ return Mage_Adminhtml_Block_Widget_Container::_prepareLayout();
27
+ }
28
+ }
app/code/local/Bricks/Gifter/Block/Adminhtml/Gift/Grid.php ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Block_Adminhtml_Gift_Grid extends Mage_Adminhtml_Block_Widget_Grid
13
+ {
14
+ /**
15
+ * Initialize grid
16
+ * Set sort settings
17
+ */
18
+ public function __construct()
19
+ {
20
+ parent::__construct();
21
+ $this->setId('bricks_gifter_gift_grid');
22
+ $this->setDefaultSort('sort_order');
23
+ $this->setDefaultDir('ASC');
24
+ $this->setSaveParametersInSession(true);
25
+ }
26
+
27
+ /**
28
+ * Add websites to sales rules collection
29
+ * Set collection
30
+ *
31
+ * @return Mage_Adminhtml_Block_Promo_Quote_Grid
32
+ */
33
+ protected function _prepareCollection()
34
+ {
35
+ /** @var $collection bricks_Gifter_Resource_Gift_Collection */
36
+ $collection = Mage::getModel('Bricks_Gifter/Gift')->getCollection();
37
+
38
+ $collection->join(
39
+ array('order' => 'sales/order'),
40
+ 'main_table.order_id = order.entity_id',
41
+ array(
42
+ 'increment_id',
43
+ 'customer_firstname',
44
+ 'customer_lastname'
45
+ )
46
+ );
47
+
48
+ $collection->addExpressionFieldToSelect('sent', 'IF(ISNULL(sent_at), "no", "yes")', array());
49
+
50
+ $this->setCollection($collection);
51
+
52
+ return parent::_prepareCollection();
53
+ }
54
+
55
+ /**
56
+ * Add grid columns
57
+ *
58
+ * @return Mage_Adminhtml_Block_Promo_Quote_Grid
59
+ */
60
+ protected function _prepareColumns()
61
+ {
62
+ $this->addColumn(
63
+ 'increment_id',
64
+ array(
65
+ 'header' => $this->__('Order #'),
66
+ 'align' => 'left',
67
+ 'index' => 'increment_id',
68
+ 'filter_index' => 'order.increment_id'
69
+ )
70
+ );
71
+
72
+ $this->addColumn(
73
+ 'customer_firstname',
74
+ array(
75
+ 'header' => $this->__('Customer Firstname'),
76
+ 'align' => 'left',
77
+ 'index' => 'customer_firstname',
78
+ 'filter_index' => 'order.customer_firstname'
79
+ )
80
+ );
81
+
82
+ $this->addColumn(
83
+ 'customer_lastname',
84
+ array(
85
+ 'header' => $this->__('Customer Lastname'),
86
+ 'align' => 'left',
87
+ 'index' => 'customer_lastname',
88
+ 'filter_index' => 'order.customer_lastname'
89
+ )
90
+ );
91
+
92
+ $this->addColumn(
93
+ 'recipient_name',
94
+ array(
95
+ 'header' => $this->__('Recipient Name'),
96
+ 'align' => 'left',
97
+ 'index' => 'recipient_name',
98
+ )
99
+ );
100
+
101
+ $this->addColumn(
102
+ 'recipient_email',
103
+ array(
104
+ 'header' => $this->__('Recipient Email'),
105
+ 'align' => 'left',
106
+ 'index' => 'recipient_email',
107
+ )
108
+ );
109
+
110
+ $this->addColumn(
111
+ 'coupon',
112
+ array(
113
+ 'header' => $this->__('Coupon Code'),
114
+ 'align' => 'left',
115
+ 'width' => '150px',
116
+ 'index' => 'coupon',
117
+ )
118
+ );
119
+
120
+ $this->addColumn(
121
+ 'sent',
122
+ array(
123
+ 'header' => $this->__('Sent'),
124
+ 'align' => 'left',
125
+ 'width' => '30px',
126
+ 'type' => 'options',
127
+ 'index' => 'sent',
128
+ 'filter_condition_callback' => array($this, 'filterSent'),
129
+ 'options' => array(
130
+ 'yes' => $this->__('Yes'),
131
+ 'no' => $this->__('No'),
132
+ ),
133
+ )
134
+ );
135
+
136
+ $this->addColumn(
137
+ 'sent_at',
138
+ array(
139
+ 'header' => $this->__('Date Sent'),
140
+ 'align' => 'left',
141
+ 'width' => '120px',
142
+ 'type' => 'date',
143
+ 'index' => 'sent_at',
144
+ )
145
+ );
146
+
147
+ return parent::_prepareColumns();
148
+ }
149
+
150
+ public function getGridUrl()
151
+ {
152
+ return $this->getUrl('*/*/grid');
153
+ }
154
+
155
+ public function getAbsoluteGridUrl($params = array())
156
+ {
157
+ return $this->getUrl('*/*/grid', $params);
158
+ }
159
+
160
+ public function filterSent(Bricks_Gifter_Resource_Gift_Collection $collection, Mage_Adminhtml_Block_Widget_Grid_Column $column)
161
+ {
162
+ $filter = $column->getFilter()->getValue();
163
+ if ($filter === 'yes') {
164
+ $collection->addFieldToFilter('sent_at', array('notnull' => true));
165
+ } elseif ($filter === 'no') {
166
+ $collection->addFieldToFilter('sent_at', array('null' => true));
167
+ }
168
+ }
169
+ }
app/code/local/Bricks/Gifter/Block/Frontend/Checkout/GiftForm.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+ class Bricks_Gifter_Block_Frontend_Checkout_GiftForm extends Mage_Core_Block_Template
12
+ {
13
+ protected function _toHtml()
14
+ {
15
+ if (!Mage::helper('Bricks_Gifter/Data')->isStoreEnabled()) {
16
+ return '';
17
+ } else {
18
+ return parent::_toHtml();
19
+ }
20
+ }
21
+ }
app/code/local/Bricks/Gifter/Helper/Data.php ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Helper_Data extends Mage_SalesRule_Helper_Coupon
13
+ {
14
+ const XML_PATH_GIFTER_ENABLED = 'bricks/gifter/enabled';
15
+ const XML_PATH_GIFTER_COUPON_RULE = 'bricks/gifter/coupon_rule';
16
+ const XML_PATH_GIFTER_COUPON_BATCH = 'bricks/gifter/coupon_batch';
17
+ const XML_PATH_GIFTER_COUPON_LENGTH = 'bricks/gifter/coupon_code_length';
18
+ const XML_PATH_GIFTER_COUPON_FORMAT = 'bricks/gifter/coupon_code_format';
19
+ const XML_PATH_GIFTER_COUPON_PREFIX = 'bricks/gifter/coupon_code_prefix';
20
+ const XML_PATH_GIFTER_COUPON_SUFFIX = 'bricks/gifter/coupon_code_suffix';
21
+ const XML_PATH_GIFTER_COUPON_DASH_INTERVAL = 'bricks/gifter/coupon_code_dash_interval';
22
+ const XML_PATH_GIFTER_EMAIL_BATCH = 'bricks/gifter/email_batch';
23
+ const XML_PATH_GIFTER_EMAIL_SENDER = 'bricks/gifter/email_sender';
24
+ const XML_PATH_GIFTER_EMAIL_TEMPLATE = 'bricks/gifter/email_template';
25
+
26
+ public function getEnabledStores()
27
+ {
28
+ $stores = Mage::app()->getStores();
29
+
30
+ foreach ($stores as $id => $store) {
31
+ if (!$store->getIsActive() || !$this->isStoreEnabled($store)) {
32
+ unset($stores[$id]);
33
+ }
34
+ }
35
+
36
+ return $stores;
37
+ }
38
+
39
+ public function isStoreEnabled($store = null)
40
+ {
41
+ return Mage::getStoreConfigFlag(self::XML_PATH_GIFTER_ENABLED, $store);
42
+ }
43
+
44
+ public function getCouponBatch($store = null)
45
+ {
46
+ return max((int)Mage::getStoreConfig(self::XML_PATH_GIFTER_COUPON_BATCH, $store), 1);
47
+ }
48
+
49
+ /**
50
+ * @param null $store
51
+ *
52
+ * @return Mage_SalesRule_Model_Rule|null
53
+ */
54
+ public function getCouponRule($store = null)
55
+ {
56
+ $ruleId = Mage::getStoreConfig(self::XML_PATH_GIFTER_COUPON_RULE, $store);
57
+ $rule = Mage::getModel('salesrule/rule')->load($ruleId);
58
+ if ($rule->getId()) {
59
+ if($rule->getCouponType() == Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO) {
60
+ return $rule;
61
+ }
62
+ }
63
+
64
+ return null;
65
+ }
66
+
67
+ /**
68
+ * Get coupon code length
69
+ *
70
+ * @return int
71
+ */
72
+ public function getCouponCodeLength($store = null)
73
+ {
74
+ $value = Mage::getStoreConfig(self::XML_PATH_GIFTER_COUPON_LENGTH, $store);
75
+ if (!$value) {
76
+ $value = $this->getDefaultLength();
77
+ }
78
+ return (int)$value;
79
+ }
80
+
81
+ /**
82
+ * Get coupon code format
83
+ *
84
+ * @return string
85
+ */
86
+ public function getCouponCodeFormat($store = null)
87
+ {
88
+ $value = Mage::getStoreConfig(self::XML_PATH_GIFTER_COUPON_FORMAT, $store);
89
+ if (!$value) {
90
+ $value = $this->getDefaultFormat();
91
+ }
92
+ return $value;
93
+ }
94
+
95
+ /**
96
+ * Get coupon code prefix
97
+ *
98
+ * @return string
99
+ */
100
+ public function getCouponCodePrefix($store = null)
101
+ {
102
+ $value = Mage::getStoreConfig(self::XML_PATH_GIFTER_COUPON_PREFIX, $store);
103
+ if ($value === null) {
104
+ $value = $this->getDefaultPrefix();
105
+ }
106
+ return $value;
107
+ }
108
+
109
+ /**
110
+ * Get coupon code suffix
111
+ *
112
+ * @return string
113
+ */
114
+ public function getCouponCodeSuffix($store = null)
115
+ {
116
+ $value = Mage::getStoreConfig(self::XML_PATH_GIFTER_COUPON_SUFFIX, $store);
117
+ if ($value === null) {
118
+ $value = $this->getDefaultSuffix();
119
+ }
120
+ return $value;
121
+ }
122
+
123
+ /**
124
+ * Get dashes occurrences frequency in coupon code
125
+ *
126
+ * @return int
127
+ */
128
+ public function getCouponCodeDashInterval($store = null)
129
+ {
130
+ $value = Mage::getStoreConfig(self::XML_PATH_GIFTER_COUPON_DASH_INTERVAL, $store);
131
+ if (!$value) {
132
+ $value = $this->getDefaultDashInterval();
133
+ }
134
+ return (int)$value;
135
+ }
136
+
137
+ public function getEmailBatch($store = null)
138
+ {
139
+ return max((int)Mage::getStoreConfig(self::XML_PATH_GIFTER_EMAIL_BATCH, $store), 1);
140
+ }
141
+
142
+ public function getEmailSender($store = null)
143
+ {
144
+ return Mage::getStoreConfig(self::XML_PATH_GIFTER_EMAIL_SENDER, $store);
145
+ }
146
+
147
+ public function getEmailTemplate($store = null)
148
+ {
149
+ return Mage::getStoreConfig(self::XML_PATH_GIFTER_EMAIL_TEMPLATE, $store);
150
+ }
151
+ }
app/code/local/Bricks/Gifter/Model/Config/Source/CouponFormats.php ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Model_Config_Source_CouponFormats
13
+ {
14
+ /**
15
+ * Options getter
16
+ *
17
+ * @return array
18
+ */
19
+ public function toOptionArray()
20
+ {
21
+ foreach ($this->toOptionHash() as $value => $label) {
22
+ $coupons[] = array(
23
+ 'value' => $value,
24
+ 'label' => $label
25
+ );
26
+ }
27
+
28
+ return $coupons;
29
+ }
30
+
31
+ /**
32
+ * Get options in "key-value" format
33
+ *
34
+ * @return array
35
+ */
36
+ public function toOptionHash()
37
+ {
38
+ /** @var $helper Bricks_Gifter_Helper_Data */
39
+ $helper = Mage::helper('Bricks_Gifter/Data');
40
+
41
+ return $helper->getFormatsList();
42
+ }
43
+ }
app/code/local/Bricks/Gifter/Model/Config/Source/CouponRules.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Model_Config_Source_CouponRules
13
+ {
14
+ /**
15
+ * Options getter
16
+ *
17
+ * @return array
18
+ */
19
+ public function toOptionArray()
20
+ {
21
+ $coupons = array();
22
+
23
+ foreach ($this->_getData() as $entry) {
24
+ $coupons[] = array(
25
+ 'value' => $entry['rule_id'],
26
+ 'label' => $entry['name']
27
+ );
28
+ }
29
+
30
+ return $coupons;
31
+ }
32
+
33
+ /**
34
+ * Get options in "key-value" format
35
+ *
36
+ * @return array
37
+ */
38
+ public function toOptionHash()
39
+ {
40
+ $coupons = array();
41
+
42
+ foreach ($this->_getData() as $entry) {
43
+ $coupons[$entry['rule_id']] = $entry['name'];
44
+ }
45
+
46
+ return $coupons;
47
+ }
48
+
49
+ protected function _getData()
50
+ {
51
+ /** @var $collection Mage_SalesRule_Model_Resource_Rule_Collection */
52
+ $collection = Mage::getModel('salesrule/rule')->getCollection()
53
+ ->addFieldToFilter('coupon_type', Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO);
54
+
55
+ $data = $collection->toArray(array('rule_id', 'name'));
56
+
57
+ return $data['items'];
58
+ }
59
+ }
app/code/local/Bricks/Gifter/Model/CouponGenerator.php ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Model_CouponGenerator extends Mage_SalesRule_Model_Coupon_Codegenerator
13
+ {
14
+ const CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
15
+ const MAX_GENERATE_ATTEMPTS = 10;
16
+
17
+ protected $_generatedCodes = array();
18
+
19
+ /**
20
+ * Count of generated Coupons
21
+ *
22
+ * @var int
23
+ */
24
+ protected $_generatedCount = 0;
25
+
26
+ /**
27
+ * Generate coupon code
28
+ *
29
+ * @return string
30
+ */
31
+ public function generateCode()
32
+ {
33
+ $length = max(1, (int)$this->getLength());
34
+ $split = max(0, (int)$this->getDash());
35
+ $suffix = $this->getSuffix();
36
+ $prefix = $this->getPrefix();
37
+
38
+ $splitChar = $this->getDelimiter();
39
+ $charset = (string)self::CHARSET;
40
+
41
+ $code = '';
42
+ $charsetSize = strlen($charset);
43
+ for ($i = 0; $i < $length; $i++) {
44
+ $char = $charset[mt_rand(0, $charsetSize - 1)];
45
+ if ($split > 0 && ($i % $split) == 0 && $i != 0) {
46
+ $char = $splitChar . $char;
47
+ }
48
+ $code .= $char;
49
+ }
50
+
51
+ $code = $prefix . $code . $suffix;
52
+ return $code;
53
+ }
54
+
55
+ /**
56
+ * Retrieve delimiter
57
+ *
58
+ * @return string
59
+ */
60
+ public function getDelimiter()
61
+ {
62
+ if ($this->getData('delimiter')) {
63
+ return $this->getData('delimiter');
64
+ } else {
65
+ return '-';
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Generate Coupons Pool
71
+ *
72
+ * @return Bricks_Gifter_Model_CouponGenerator
73
+ */
74
+ public function generatePool()
75
+ {
76
+ $this->_generatedCodes = array();
77
+ $this->_generatedCount = 0;
78
+ $size = $this->getQty();
79
+
80
+ $maxAttempts = $this->getMaxAttempts() ? $this->getMaxAttempts() : self::MAX_GENERATE_ATTEMPTS;
81
+
82
+ /** @var $coupon Mage_SalesRule_Model_Coupon */
83
+ $coupon = Mage::getModel('salesrule/coupon');
84
+
85
+ for ($i = 0; $i < $size; $i++) {
86
+ $attempt = 0;
87
+ do {
88
+ if ($attempt >= $maxAttempts) {
89
+ Mage::throwException('Unable to create requested Coupon Qty. Please check settings and try again.');
90
+ }
91
+ $code = $this->generateCode();
92
+ $attempt++;
93
+ } while ($this->_exists($code));
94
+
95
+ $expirationDate = $this->getToDate();
96
+ if ($expirationDate instanceof Zend_Date) {
97
+ $expirationDate = $expirationDate->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
98
+ }
99
+
100
+ $coupon->setId(null)
101
+ ->setRuleId($this->getRuleId())
102
+ ->setUsageLimit($this->getUsesPerCoupon())
103
+ ->setUsagePerCustomer($this->getUsesPerCustomer())
104
+ ->setExpirationDate($expirationDate)
105
+ ->setCode($code)
106
+ ->save();
107
+
108
+ $this->_generatedCodes[] = $code;
109
+ $this->_generatedCount++;
110
+ }
111
+
112
+ return $this;
113
+ }
114
+
115
+ /**
116
+ * @return array
117
+ */
118
+ public function getGeneratedCodes()
119
+ {
120
+ return $this->_generatedCodes;
121
+ }
122
+
123
+ /**
124
+ * Retrieve count of generated Coupons
125
+ *
126
+ * @return int
127
+ */
128
+ public function getGeneratedCount()
129
+ {
130
+ return $this->_generatedCount;
131
+ }
132
+
133
+ /**
134
+ * Validate input
135
+ *
136
+ * @param array $data
137
+ *
138
+ * @return bool
139
+ */
140
+ public function validateData($data)
141
+ {
142
+ return !empty($data) && !empty($data['qty']) && !empty($data['rule_id'])
143
+ && !empty($data['length'])
144
+ && (int)$data['qty'] > 0
145
+ && (int)$data['rule_id'] > 0
146
+ && (int)$data['length'] > 0;
147
+ }
148
+
149
+ protected function _exists($code)
150
+ {
151
+ /** @var $resource Mage_SalesRule_Model_Resource_Coupon */
152
+ $resource = Mage::getResourceModel('salesrule/coupon');
153
+ $read = $resource->getReadConnection();
154
+ $select = $read->select();
155
+ $select->from($resource->getMainTable(), 'code');
156
+ $select->where('code = :code');
157
+ if ($read->fetchOne($select, array('code' => $code)) === false) {
158
+ return false;
159
+ }
160
+ return true;
161
+ }
162
+ }
app/code/local/Bricks/Gifter/Model/Cron.php ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Model_Cron
13
+ {
14
+ public function assignCoupons()
15
+ {
16
+ /** @var $helper Bricks_Gifter_Helper_Data */
17
+ $helper = Mage::helper('Bricks_Gifter/Data');
18
+
19
+ $stores = $helper->getEnabledStores();
20
+ if (empty($stores)) {
21
+ return;
22
+ }
23
+
24
+ // Get the resource object so we can use it for a transaction
25
+ $resource = Mage::getModel('Bricks_Gifter/Gift')->getResource();
26
+
27
+ /** @var $generator Bricks_Gifter_Model_CouponGenerator */
28
+ $generator = Mage::getSingleton('Bricks_Gifter/CouponGenerator');
29
+
30
+ foreach ($stores as $store) {
31
+ $rule = $helper->getCouponRule($store);
32
+ if (!$rule || !$rule->getIsActive()) {
33
+ continue;
34
+ }
35
+
36
+ // Start transaction
37
+ $resource->beginTransaction();
38
+
39
+ try {
40
+ /** @var $gifts Bricks_Gifter_Resource_Gift_Collection */
41
+ $gifts = Mage::getModel('Bricks_Gifter/Gift')->getCollection()
42
+ ->join('sales/order', 'main_table.order_id=`sales/order`.entity_id', 'store_id')
43
+ ->addFieldToFilter('store_id', $store->getId())
44
+ ->addFieldToFilter('coupon', array('null' => true))
45
+ ->addFieldToFilter('sent_at', array('null' => true))
46
+ ->setPageSize($helper->getCouponBatch($store))
47
+ ->addOrder('id', 'ASC');
48
+
49
+ if ($gifts->getSize() > 0) {
50
+ // Create and save coupon code batch
51
+ $generationData = array(
52
+ 'rule_id' => $rule->getId(),
53
+ 'qty' => $gifts->getSize(),
54
+ 'length' => $helper->getCouponCodeLength($store),
55
+ 'prefix' => $helper->getCouponCodePrefix($store),
56
+ 'suffix' => $helper->getCouponCodeSuffix($store),
57
+ 'dash' => $helper->getCouponCodeDashInterval($store),
58
+ );
59
+ if (!$generator->validateData($generationData)) {
60
+ Mage::log('Invalid gifter automatic coupon generation data');
61
+ continue;
62
+ }
63
+ $generator->setData($generationData);
64
+ $generator->generatePool();
65
+ $generatedCodes = $generator->getGeneratedCodes();
66
+ if (count($generatedCodes) < $gifts->getSize()) {
67
+ Mage::log('Generated too few coupon codes!', Zend_Log::WARN);
68
+ }
69
+
70
+ // Assign coupons to gifts, oldest first, until there are no more coupons
71
+ foreach ($gifts as $gift) {
72
+ if (empty($generatedCodes)) {
73
+ break;
74
+ }
75
+
76
+ // Assign coupon and save
77
+ $gift->setCoupon(array_shift($generatedCodes))->save();
78
+ }
79
+ }
80
+
81
+ // Commit Transaction
82
+ $resource->commit();
83
+ }
84
+ catch (Exception $e) {
85
+ // Log the exception
86
+ Mage::logException($e);
87
+
88
+ // Revert Transaction
89
+ $resource->rollBack();
90
+ }
91
+ }
92
+ }
93
+
94
+ public function sendEmails()
95
+ {
96
+ /** @var $helper Bricks_Gifter_Helper_Data */
97
+ $helper = Mage::helper('Bricks_Gifter/Data');
98
+
99
+ $translate = Mage::getSingleton('core/translate');
100
+ /* @var $translate Mage_Core_Model_Translate */
101
+ $translate->setTranslateInline(false);
102
+
103
+ foreach ($helper->getEnabledStores() as $store) {
104
+ $this->_sendEmailsForStore($store);
105
+ }
106
+
107
+ $translate->setTranslateInline(true);
108
+ }
109
+
110
+ protected function _sendEmailsForStore(Mage_Core_Model_Store $store)
111
+ {
112
+ /** @var $helper Bricks_Gifter_Helper_Data */
113
+ $helper = Mage::helper('Bricks_Gifter/Data');
114
+
115
+ /** @var $gifts Bricks_Gifter_Resource_Gift_Collection */
116
+ $gifts = Mage::getModel('Bricks_Gifter/Gift')->getCollection()
117
+ ->join('sales/order', 'main_table.order_id=`sales/order`.entity_id', 'store_id')
118
+ ->join('salesrule/coupon', 'main_table.coupon=`salesrule/coupon`.code', '')
119
+ ->join('salesrule/rule', '`salesrule/coupon`.rule_id=`salesrule/rule`.rule_id', 'is_active')
120
+ ->addFieldToFilter('store_id', $store->getId())
121
+ ->addFieldToFilter('is_active', 1)
122
+ ->addFieldToFilter('coupon', array('notnull' => true))
123
+ ->addFieldToFilter('sent_at', array('null' => true))
124
+ ->setPageSize($helper->getEmailBatch($store))
125
+ ->addOrder('id', 'ASC');
126
+
127
+ if ($gifts->getSize() > 0) {
128
+ foreach ($gifts as $gift) {
129
+ try {
130
+ $this->_sendEmailForGift($store, $gift);
131
+ }
132
+ catch (Exception $e) {
133
+ // Log the exception
134
+ Mage::logException($e);
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ protected function _sendEmailForGift(Mage_Core_Model_Store $store, Bricks_Gifter_Model_Gift $gift)
141
+ {
142
+ /** @var $helper Bricks_Gifter_Helper_Data */
143
+ $helper = Mage::helper('Bricks_Gifter/Data');
144
+
145
+ $order = Mage::getModel('sales/order')->load($gift->getOrderId());
146
+ if (!$order->getId()) {
147
+ Mage::throwException('Invalid order referenced from gift record');
148
+ }
149
+
150
+ /** @var $mailTemplate Mage_Core_Model_Email_Template */
151
+ $mailTemplate = Mage::getModel('core/email_template')
152
+ ->setDesignConfig(array('area' => 'frontend', 'store' => $store->getId()));
153
+
154
+ try {
155
+ $mailTemplate->emulateDesign($store);
156
+
157
+ $mailTemplate->sendTransactional(
158
+ $helper->getEmailTemplate($store),
159
+ $helper->getEmailSender($store),
160
+ $gift->getRecipientEmail(),
161
+ $gift->getRecipientName(),
162
+ array('gift' => $gift, 'order' => $order)
163
+ );
164
+ $mailTemplate->revertDesign();
165
+ }
166
+ catch (Exception $e) {
167
+ $mailTemplate->revertDesign();
168
+ }
169
+
170
+ if ($mailTemplate->getSentSuccess()) {
171
+ // Commit Transaction
172
+ $gift->setSentAt(new Zend_Date())->save();
173
+ } else {
174
+ Mage::throwException('Failed to send email for gift: ' . $gift->getId());
175
+ }
176
+ }
177
+ }
app/code/local/Bricks/Gifter/Model/Gift.php ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Model_Gift extends Mage_Core_Model_Abstract
13
+ {
14
+ /**
15
+ * @var string
16
+ */
17
+ protected $_eventPrefix = 'bricks_gifter_gift';
18
+
19
+ /**
20
+ * @var string
21
+ */
22
+ protected $_eventObject = 'gift';
23
+
24
+ /**
25
+ * Initialize Model
26
+ */
27
+ protected function _construct()
28
+ {
29
+ $this->_init('Bricks_Gifter/Gift');
30
+ }
31
+ }
app/code/local/Bricks/Gifter/Model/Observer.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Model_Observer
13
+ {
14
+ public function activateAutoCoupons(Varien_Event_Observer $observer)
15
+ {
16
+ /** @var $order Varien_Object */
17
+ $transport = $observer->getTransport();
18
+ if (!$transport instanceof Varien_Object) {
19
+ return;
20
+ }
21
+
22
+ $transport->setIsCouponTypeAutoVisible(true);
23
+ }
24
+
25
+ public function processGiftRecipient(Varien_Event_Observer $observer)
26
+ {
27
+ /** @var $helper Bricks_Gifter_Helper_Data */
28
+ $helper = Mage::helper('Bricks_Gifter/Data');
29
+
30
+ if (!$helper->isStoreEnabled()) {
31
+ return;
32
+ }
33
+
34
+ /** @var $order Mage_Sales_Model_Order */
35
+ $order = $observer->getOrder();
36
+ if (!$order instanceof Mage_Sales_Model_Order) {
37
+ return;
38
+ }
39
+
40
+ $data = Mage::app()->getRequest()->getPost('bricks-gifter', array());
41
+ if (!empty($data['recipient_name']) && !empty($data['recipient_email'])) {
42
+ $gift = Mage::getModel('Bricks_Gifter/Gift')->load($order->getId(), 'order_id');
43
+ if ($gift && !$gift->getId()) {
44
+ $gift->setOrderId($order->getId())
45
+ ->setRecipientName($data['recipient_name'])
46
+ ->setRecipientEmail($data['recipient_email'])
47
+ ->save();
48
+ }
49
+ }
50
+ }
51
+ }
app/code/local/Bricks/Gifter/Resource/Gift.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Resource_Gift extends Mage_Core_Model_Resource_Db_Abstract
13
+ {
14
+ /**
15
+ * Set table and primary key
16
+ */
17
+ protected function _construct()
18
+ {
19
+ $this->_init('Bricks_Gifter/Gift', 'id');
20
+ }
21
+ }
app/code/local/Bricks/Gifter/Resource/Gift/Collection.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Resource_Gift_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
13
+ {
14
+ /**
15
+ * Set resource model
16
+ */
17
+ protected function _construct()
18
+ {
19
+ $this->_init('Bricks_Gifter/Gift');
20
+ }
21
+ }
app/code/local/Bricks/Gifter/controllers/Admin/Promo/GifterController.php ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ class Bricks_Gifter_Admin_Promo_GifterController extends Mage_Adminhtml_Controller_Action
13
+ {
14
+ public function indexAction()
15
+ {
16
+ $this->loadLayout()->renderLayout();
17
+ }
18
+
19
+ public function gridAction()
20
+ {
21
+ if ($this->getRequest()->isAjax()) {
22
+ $this->loadLayout()->renderLayout();
23
+ } else {
24
+ $this->_forward('noroute');
25
+ }
26
+ }
27
+
28
+ public function assignCouponsAction()
29
+ {
30
+ Mage::getModel('Bricks_Gifter/Cron')->assignCoupons();
31
+ $this->_getSession()->addSuccess('Manually ran the Assign Coupons cron job.');
32
+ $this->_redirect('*/*');
33
+ }
34
+
35
+ public function sendEmailsAction()
36
+ {
37
+ Mage::getModel('Bricks_Gifter/Cron')->sendEmails();
38
+ $this->_getSession()->addSuccess('Manually ran the Send Emails cron job.');
39
+ $this->_redirect('*/*');
40
+ }
41
+
42
+ protected function _isAllowed()
43
+ {
44
+ return Mage::getSingleton('admin/session')->isAllowed('promo/gifter');
45
+ }
46
+ }
app/code/local/Bricks/Gifter/etc/adminhtml.xml ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * NOTICE OF LICENSE
5
+ *
6
+ *
7
+ * @category Mage
8
+ * @package Bricks_Gifter
9
+ * @license GPL
10
+ * @author Adam <adam@bricksandmortarweb.com>
11
+ */
12
+ -->
13
+ <config>
14
+ <menu>
15
+ <promo>
16
+ <children>
17
+ <bricks_gifter translate="title">
18
+ <title>Gifter</title>
19
+ <action>adminhtml/promo_gifter/</action>
20
+ <children>
21
+ <assign_coupons translate="title">
22
+ <title>Assign Coupons (Force)</title>
23
+ <action>adminhtml/promo_gifter/assignCoupons</action>
24
+ </assign_coupons>
25
+ <send_emails translate="title">
26
+ <title>Send Emails (Force)</title>
27
+ <action>adminhtml/promo_gifter/sendEmails</action>
28
+ </send_emails>
29
+ </children>
30
+ </bricks_gifter>
31
+ </children>
32
+ </promo>
33
+ </menu>
34
+ <acl>
35
+ <resources>
36
+ <admin>
37
+ <children>
38
+ <promo>
39
+ <children>
40
+ <bricks_gifter translate="title">
41
+ <title>Gifter</title>
42
+ </bricks_gifter>
43
+ </children>
44
+ </promo>
45
+ <system>
46
+ <children>
47
+ <config>
48
+ <children>
49
+ <checkout>
50
+ <children>
51
+ <bricks_gifter translate="title">
52
+ <title>Bricks Gifter</title>
53
+ </bricks_gifter>
54
+ </children>
55
+ </checkout>
56
+ </children>
57
+ </config>
58
+ </children>
59
+ </system>
60
+ </children>
61
+ </admin>
62
+ </resources>
63
+ </acl>
64
+ </config>
app/code/local/Bricks/Gifter/etc/config.xml ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * NOTICE OF LICENSE
5
+ *
6
+ *
7
+ * @category Mage
8
+ * @package Bricks_Gifter
9
+ * @license GPL
10
+ * @author Adam <adam@bricksandmortarweb.com>
11
+ */
12
+ -->
13
+ <config>
14
+ <modules>
15
+ <Bricks_Gifter>
16
+ <version>1.0.0</version>
17
+ </Bricks_Gifter>
18
+ </modules>
19
+ <global>
20
+ <helpers>
21
+ <Bricks_Gifter>
22
+ <class>Bricks_Gifter_Helper</class>
23
+ </Bricks_Gifter>
24
+ </helpers>
25
+ <blocks>
26
+ <Bricks_Gifter>
27
+ <class>Bricks_Gifter_Block</class>
28
+ </Bricks_Gifter>
29
+ </blocks>
30
+ <models>
31
+ <Bricks_Gifter>
32
+ <class>Bricks_Gifter_Model</class>
33
+ <resourceModel>Bricks_Gifter_Resource</resourceModel>
34
+ </Bricks_Gifter>
35
+ <Bricks_Gifter_Resource>
36
+ <class>Bricks_Gifter_Resource</class>
37
+ <entities>
38
+ <Gift>
39
+ <table>bricks_gifter_gift</table>
40
+ </Gift>
41
+ </entities>
42
+ </Bricks_Gifter_Resource>
43
+ </models>
44
+ <resources>
45
+ <Bricks_Gifter>
46
+ <setup>
47
+ <module>Bricks_Gifter</module>
48
+ </setup>
49
+ </Bricks_Gifter>
50
+ </resources>
51
+ <template>
52
+ <email>
53
+ <bricks_gifter_email_template translate="label">
54
+ <label>Gift Coupon Code</label>
55
+ <file>bricks/gifter/gift.html</file>
56
+ <type>html</type>
57
+ </bricks_gifter_email_template>
58
+ </email>
59
+ </template>
60
+ </global>
61
+ <frontend>
62
+ <layout>
63
+ <updates>
64
+ <Bricks_Gifter>
65
+ <file>Bricks/Gifter.xml</file>
66
+ </Bricks_Gifter>
67
+ </updates>
68
+ </layout>
69
+ <translate>
70
+ <modules>
71
+ <Bricks_Gifter>
72
+ <files>
73
+ <default>Bricks/Gifter.csv</default>
74
+ </files>
75
+ </Bricks_Gifter>
76
+ </modules>
77
+ </translate>
78
+ <events>
79
+ <sales_model_service_quote_submit_success>
80
+ <observers>
81
+ <Bricks_Gifter>
82
+ <class>Bricks_Gifter/Observer</class>
83
+ <method>processGiftRecipient</method>
84
+ </Bricks_Gifter>
85
+ </observers>
86
+ </sales_model_service_quote_submit_success>
87
+ </events>
88
+ </frontend>
89
+ <adminhtml>
90
+ <layout>
91
+ <updates>
92
+ <Bricks_Gifter>
93
+ <file>Bricks/Gifter.xml</file>
94
+ </Bricks_Gifter>
95
+ </updates>
96
+ </layout>
97
+ <translate>
98
+ <modules>
99
+ <Bricks_Gifter>
100
+ <files>
101
+ <default>Bricks_Gifter.csv</default>
102
+ </files>
103
+ </Bricks_Gifter>
104
+ </modules>
105
+ </translate>
106
+ <events>
107
+ <salesrule_rule_get_coupon_types>
108
+ <observers>
109
+ <Bricks_Gifter>
110
+ <class>Bricks_Gifter/Observer</class>
111
+ <method>activateAutoCoupons</method>
112
+ </Bricks_Gifter>
113
+ </observers>
114
+ </salesrule_rule_get_coupon_types>
115
+ </events>
116
+ </adminhtml>
117
+ <admin>
118
+ <routers>
119
+ <adminhtml>
120
+ <args>
121
+ <modules>
122
+ <Bricks_Gifter before="Mage_Adminhtml">Bricks_Gifter_Admin</Bricks_Gifter>
123
+ </modules>
124
+ </args>
125
+ </adminhtml>
126
+ </routers>
127
+ </admin>
128
+ <crontab>
129
+ <jobs>
130
+ <Bricks_Gifter_assignCoupons>
131
+ <schedule>
132
+ <config_path>Bricks/gifter/cron/assign_coupons</config_path>
133
+ </schedule>
134
+ <run>
135
+ <model>Bricks_Gifter/Cron::assignCoupons</model>
136
+ </run>
137
+ </Bricks_Gifter_assignCoupons>
138
+ <Bricks_Gifter_sendEmails>
139
+ <schedule>
140
+ <config_path>bricks/gifter/cron/send_emails</config_path>
141
+ </schedule>
142
+ <run>
143
+ <model>Bricks_Gifter/Cron::sendEmails</model>
144
+ </run>
145
+ </Bricks_Gifter_sendEmails>
146
+ </jobs>
147
+ </crontab>
148
+ <default>
149
+ <bricks>
150
+ <gifter>
151
+ <enabled>0</enabled>
152
+ <coupon_batch>100</coupon_batch>
153
+ <email_batch>100</email_batch>
154
+ <cron>
155
+ <assign_coupons>*/15 * * * *</assign_coupons>
156
+ <send_emails>*/15 * * * *</send_emails>
157
+ </cron>
158
+ </gifter>
159
+ </bricks>
160
+ </default>
161
+ </config>
app/code/local/Bricks/Gifter/etc/system.xml ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * NOTICE OF LICENSE
5
+ *
6
+ *
7
+ * @category Mage
8
+ * @package Bricks_Gifter
9
+ * @license GPL
10
+ * @author Adam <adam@bricksandmortarweb.com>
11
+ */
12
+ -->
13
+ <config>
14
+ <sections>
15
+ <checkout>
16
+ <groups>
17
+ <bricks_gifter translate="label">
18
+ <label>Gifter</label>
19
+ <frontend_type>text</frontend_type>
20
+ <sort_order>10000</sort_order>
21
+ <show_in_default>1</show_in_default>
22
+ <show_in_website>1</show_in_website>
23
+ <show_in_store>1</show_in_store>
24
+ <fields>
25
+ <enabled translate="label">
26
+ <label>Enabled</label>
27
+ <frontend_type>select</frontend_type>
28
+ <source_model>adminhtml/system_config_source_yesno</source_model>
29
+ <config_path>bricks/gifter/enabled</config_path>
30
+ <show_in_default>1</show_in_default>
31
+ <show_in_website>1</show_in_website>
32
+ <show_in_store>1</show_in_store>
33
+ <sort_order>1000</sort_order>
34
+ </enabled>
35
+ <!-- Add Selector for Static Block to display on checkout -->
36
+ <coupon_rule translate="label">
37
+ <label>Coupon Rule</label>
38
+ <frontend_type>select</frontend_type>
39
+ <source_model>Bricks_Gifter/Config_Source_CouponRules</source_model>
40
+ <config_path>bricks/gifter/coupon_rule</config_path>
41
+ <show_in_default>1</show_in_default>
42
+ <show_in_website>1</show_in_website>
43
+ <show_in_store>1</show_in_store>
44
+ <depends>
45
+ <enabled>1</enabled>
46
+ </depends>
47
+ <sort_order>10000</sort_order>
48
+ </coupon_rule>
49
+ <coupon_code_length>
50
+ <label>Generated Coupon Length</label>
51
+ <comment>Excluding prefix, suffix and separators.</comment>
52
+ <frontend_type>text</frontend_type>
53
+ <config_path>bricks/gifter/coupon_code_length</config_path>
54
+ <show_in_default>1</show_in_default>
55
+ <show_in_website>1</show_in_website>
56
+ <show_in_store>1</show_in_store>
57
+ <depends>
58
+ <enabled>1</enabled>
59
+ </depends>
60
+ <sort_order>10100</sort_order>
61
+ </coupon_code_length>
62
+ <coupon_code_prefix>
63
+ <label>Generated Coupon Prefix</label>
64
+ <frontend_type>text</frontend_type>
65
+ <config_path>bricks/gifter/coupon_code_prefix</config_path>
66
+ <show_in_default>1</show_in_default>
67
+ <show_in_website>1</show_in_website>
68
+ <show_in_store>1</show_in_store>
69
+ <depends>
70
+ <enabled>1</enabled>
71
+ </depends>
72
+ <sort_order>10300</sort_order>
73
+ </coupon_code_prefix>
74
+ <min_checkout_amount>
75
+ <label>Min. Checkout Amount</label>
76
+ <comment>Min. amount for checkout that allows the customer to include a friend's email address to "send a gift to a friend"</comment>
77
+ <show_in_default>1</show_in_default>
78
+ <show_in_website>1</show_in_website>
79
+ <show_in_store>1</show_in_store>
80
+ <depends>
81
+ <enabled>1</enabled>
82
+ </depends>
83
+ <sort_order>10100</sort_order>
84
+ </min_checkout_amount>
85
+ <coupon_code_suffix>
86
+ <label>Generated Coupon Suffix</label>
87
+ <frontend_type>text</frontend_type>
88
+ <config_path>bricks/gifter/coupon_code_suffix</config_path>
89
+ <show_in_default>1</show_in_default>
90
+ <show_in_website>1</show_in_website>
91
+ <show_in_store>1</show_in_store>
92
+ <depends>
93
+ <enabled>1</enabled>
94
+ </depends>
95
+ <sort_order>10400</sort_order>
96
+ </coupon_code_suffix>
97
+ <coupon_code_dash_interval>
98
+ <label>Generated Coupon Dash Interval</label>
99
+ <comment>If empty no separation.</comment>
100
+ <frontend_type>text</frontend_type>
101
+ <config_path>bricks/gifter/coupon_code_dash_interval</config_path>
102
+ <show_in_default>1</show_in_default>
103
+ <show_in_website>1</show_in_website>
104
+ <show_in_store>1</show_in_store>
105
+ <depends>
106
+ <enabled>1</enabled>
107
+ </depends>
108
+ <sort_order>10500</sort_order>
109
+ </coupon_code_dash_interval>
110
+ <email_sender translate="label">
111
+ <label>Email Sender</label>
112
+ <frontend_type>select</frontend_type>
113
+ <source_model>adminhtml/system_config_source_email_identity</source_model>
114
+ <config_path>bricks/gifter/email_sender</config_path>
115
+ <show_in_default>1</show_in_default>
116
+ <show_in_website>1</show_in_website>
117
+ <show_in_store>1</show_in_store>
118
+ <depends>
119
+ <enabled>1</enabled>
120
+ </depends>
121
+ <sort_order>15100</sort_order>
122
+ </email_sender>
123
+ <email_template translate="label">
124
+ <label>Email Template</label>
125
+ <frontend_type>select</frontend_type>
126
+ <source_model>adminhtml/system_config_source_email_template</source_model>
127
+ <config_path>bricks/gifter/email_template</config_path>
128
+ <show_in_default>1</show_in_default>
129
+ <show_in_website>1</show_in_website>
130
+ <show_in_store>1</show_in_store>
131
+ <depends>
132
+ <enabled>1</enabled>
133
+ </depends>
134
+ <sort_order>15200</sort_order>
135
+ </email_template>
136
+ </fields>
137
+ </bricks_gifter>
138
+ </groups>
139
+ </checkout>
140
+ </sections>
141
+ </config>
app/code/local/Bricks/Gifter/sql/Bricks_Gifter/install-1.0.0.php ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ /* @var $setup Mage_Core_Model_Resource_Setup */
13
+ $setup = $this;
14
+
15
+ // Begin setup
16
+ $setup->startSetup();
17
+
18
+ // Create new table object
19
+ $table = $setup->getConnection()->newTable($setup->getTable('Bricks_Gifter/Gift'));
20
+
21
+ // Columns
22
+ $table->addColumn(
23
+ 'id',
24
+ Varien_Db_Ddl_Table::TYPE_INTEGER,
25
+ null,
26
+ array(
27
+ 'identity' => true,
28
+ 'unsigned' => true,
29
+ 'nullable' => false,
30
+ 'primary' => true,
31
+ )
32
+ );
33
+ $table->addColumn(
34
+ 'order_id',
35
+ Varien_Db_Ddl_Table::TYPE_INTEGER,
36
+ null,
37
+ array(
38
+ 'unsigned' => true,
39
+ 'nullable' => false,
40
+ )
41
+ );
42
+ $table->addColumn(
43
+ 'recipient_name',
44
+ Varien_Db_Ddl_Table::TYPE_TEXT,
45
+ 255,
46
+ array()
47
+ );
48
+ $table->addColumn(
49
+ 'recipient_email',
50
+ Varien_Db_Ddl_Table::TYPE_TEXT,
51
+ 255,
52
+ array()
53
+ );
54
+ $table->addColumn(
55
+ 'coupon',
56
+ Varien_Db_Ddl_Table::TYPE_TEXT,
57
+ 255,
58
+ array(
59
+ 'nullable' => true,
60
+ )
61
+ );
62
+ $table->addColumn(
63
+ 'sent_at',
64
+ Varien_Db_Ddl_Table::TYPE_DATETIME,
65
+ null,
66
+ array(
67
+ 'nullable' => true,
68
+ )
69
+ );
70
+
71
+ // Indexes
72
+ $table->addIndex(
73
+ $setup->getIdxName('Bricks_Gifter/Gift', array('order_id'), Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE),
74
+ array('order_id'),
75
+ array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE)
76
+ );
77
+ $table->addIndex(
78
+ $setup->getIdxName('Bricks_Gifter/Gift', array('recipient_email')),
79
+ array('recipient_email')
80
+ );
81
+ $table->addIndex(
82
+ $setup->getIdxName('Bricks_Gifter/Gift', array('coupon'), Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE),
83
+ array('coupon'),
84
+ array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE)
85
+ );
86
+
87
+ // Constraints
88
+ $table->addForeignKey(
89
+ $setup->getFkName('Bricks_Gifter/Gift', 'order_id', 'sales/order', 'entity_id'),
90
+ 'order_id',
91
+ $setup->getTable('sales/order'),
92
+ 'entity_id',
93
+ Varien_Db_Ddl_Table::ACTION_CASCADE,
94
+ Varien_Db_Ddl_Table::ACTION_CASCADE
95
+ );
96
+ $table->addForeignKey(
97
+ $setup->getFkName('Bricks_Gifter/Gift', 'coupon', 'salesrule/coupon', 'code'),
98
+ 'coupon',
99
+ $setup->getTable('salesrule/coupon'),
100
+ 'code',
101
+ Varien_Db_Ddl_Table::ACTION_CASCADE,
102
+ Varien_Db_Ddl_Table::ACTION_CASCADE
103
+ );
104
+
105
+ // Turn the table object into DDL and execute
106
+ $setup->getConnection()->createTable($table);
107
+
108
+ // Finish up setup
109
+ $setup->endSetup();
app/design/adminhtml/default/default/layout/Bricks/Gifter.xml ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * NOTICE OF LICENSE
5
+ *
6
+ *
7
+ * @category Mage
8
+ * @package Bricks_Gifter
9
+ * @license GPL
10
+ * @author Adam <adam@bricksandmortarweb.com>
11
+ */
12
+ -->
13
+ <layout>
14
+ <adminhtml_promo_gifter_index>
15
+ <reference name="content">
16
+ <block type="Bricks_Gifter/Adminhtml_Gift" name="bricks.gifter.gift">
17
+ <block type="Bricks_Gifter/Adminhtml_Gift_Grid" name="bricks.gifter.gift.grid" as="grid">
18
+ <action method="setUseAjax">
19
+ <flag>1</flag>
20
+ </action>
21
+ </block>
22
+ </block>
23
+ </reference>
24
+ </adminhtml_promo_gifter_index>
25
+ <adminhtml_promo_gifter_grid>
26
+ <remove name="root"/>
27
+ <block type="Bricks_Gifter/Adminhtml_Gift_Grid" name="bricks.gifter.gift.grid" output="toHtml"/>
28
+ </adminhtml_promo_gifter_grid>
29
+ </layout>
app/design/frontend/base/default/layout/Bricks/Gifter.xml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * NOTICE OF LICENSE
5
+ *
6
+ *
7
+ * @category Mage
8
+ * @package Bricks_Gifter
9
+ * @license GPL
10
+ * @author Adam <adam@bricksandmortarweb.com>
11
+ */
12
+ -->
13
+ <layout>
14
+ <checkout_onepage_review>
15
+ <reference name="checkout.onepage.review.info.items.after">
16
+ <block type="Bricks_Gifter/Frontend_Checkout_GiftForm" name="bricks.gifter.checkout.gift-form" template="Bricks/Gifter/checkout/gift-form.phtml" />
17
+ </reference>
18
+ </checkout_onepage_review>
19
+ </layout>
app/design/frontend/base/default/template/Bricks/Gifter/checkout/gift-form.phtml ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+
12
+ /** @var $this bricks_Gifter_Block_Frontend_Checkout_GiftForm */
13
+ $subtotal = Mage::helper('checkout/cart')->getQuote()->getSubtotal();
14
+ $min_amount_check = Mage::getStoreConfig('checkout/bricks_gifter/min_checkout_amount');
15
+ // exit;
16
+ if($subtotal>$min_amount_check)
17
+ {
18
+ ?>
19
+ <div class="checkout-agreements">
20
+ <?php echo $this->getBeforeContent() ?>
21
+ <form action="" id="bricks-gifter" onsubmit="return false;">
22
+ <label for="bricks-gifter_recipient_name">
23
+ <?php echo $this->__('Name') ?>
24
+ <input type="text" id="bricks-gifter_recipient_name" name="bricks-gifter[recipient_name]" />
25
+ </label>
26
+ <label for="bricks-gifter_recipient_email">
27
+ <?php echo $this->__('Email Address') ?>
28
+ <input type="text" id="bricks-gifter_recipient_email" name="bricks-gifter[recipient_email]" />
29
+ </label>
30
+ </form>
31
+ <?php echo $this->getAfterContent() ?>
32
+ </div>
33
+ <script type="text/javascript">
34
+ Review.prototype.save = function () {
35
+ if (checkout.loadWaiting != false) {
36
+ return;
37
+ }
38
+ checkout.setLoadWaiting('review');
39
+ var params = Form.serialize(payment.form);
40
+ if (this.agreementsForm) {
41
+ params += '&' + Form.serialize(this.agreementsForm);
42
+ }
43
+
44
+ params += '&' + Form.serialize($('bricks-gifter'))
45
+
46
+ params.save = true;
47
+ var request = new Ajax.Request(
48
+ this.saveUrl,
49
+ {
50
+ method :'post',
51
+ parameters:params,
52
+ onComplete:this.onComplete,
53
+ onSuccess :this.onSave,
54
+ onFailure :checkout.ajaxFailure.bind(checkout)
55
+ }
56
+ );
57
+ };
58
+ </script>
59
+ <?php
60
+ }
61
+ ?>
app/etc/modules/Bricks_Gifter.xml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * NOTICE OF LICENSE
5
+ *
6
+ *
7
+ * @category Mage
8
+ * @package Bricks_Gifter
9
+ * @license GPL
10
+ * @author Adam <adam@bricksandmortarweb.com>
11
+ */
12
+ -->
13
+ <config>
14
+ <modules>
15
+ <Bricks_Gifter>
16
+ <active>true</active>
17
+ <codePool>local</codePool>
18
+ <depends>
19
+ <Mage_Core/>
20
+ <Mage_Adminhtml/>
21
+ <Mage_Sales/>
22
+ <Mage_SalesRule/>
23
+ </depends>
24
+ </Bricks_Gifter>
25
+ </modules>
26
+ </config>
app/locale/en_US/template/email/bricks/gifter/gift.html ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!--
2
+ /**
3
+ * NOTICE OF LICENSE
4
+ *
5
+ *
6
+ * @category Mage
7
+ * @package Bricks_Gifter
8
+ * @license GPL
9
+ * @author Adam <adam@bricksandmortarweb.com>
10
+ */
11
+ -->
12
+ <!--@subject Gift for {{var gift.recipientName}} @-->
13
+ <body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
14
+ <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
15
+ <table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%">
16
+ <tr>
17
+ <td align="center" valign="top" style="padding:20px 0 20px 0">
18
+ <!-- [ header starts here] -->
19
+ <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
20
+ <tr>
21
+ <td valign="top">
22
+ <a href="{{store url=''}}" style="color:#1E7EC8;"><img src="{{var logo_url}}" alt="{{var logo_alt}}" border="0" /></a>
23
+ </td>
24
+ </tr>
25
+ <!-- [ middle starts here] -->
26
+ <tr>
27
+ <td valign="top">
28
+ <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;">
29
+ Dear {{htmlescape var=$gift.recipientName}},
30
+ </h1>
31
+
32
+ <p style="font-size:12px; line-height:16px; margin:0 0 8px 0;">
33
+ <strong>Your gift coupon code is:</strong>
34
+ {{htmlescape var=$gift.coupon}}
35
+ </p>
36
+
37
+ <p style="font-size:12px; line-height:16px; margin:0;">
38
+ <a href="{{store url=''}}" style="color:#1E7EC8;">
39
+ You can use this free gift at our site.
40
+ </a>
41
+ <p>
42
+ </td>
43
+ </tr>
44
+ <tr>
45
+ <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;">
46
+ <center><p style="font-size:12px; margin:0;">Thank you again, <strong>{{var store.getFrontendName()}}</strong></p></center>
47
+ </td>
48
+ </tr>
49
+ </table>
50
+ </td>
51
+ </tr>
52
+ </table>
53
+ </div>
54
+ </body>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Gifter</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>OSL-3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Gifter</summary>
10
+ <description>Gifter</description>
11
+ <notes>Gifter</notes>
12
+ <authors><author><name>adam</name><user>adam</user><email>adam@bricksandmortarweb.com</email></author></authors>
13
+ <date>2016-02-08</date>
14
+ <time>14:16:05</time>
15
+ <contents><target name="magelocal"><dir name="Bricks"><dir name="Gifter"><dir name="Block"><dir name="Adminhtml"><dir name="Gift"><file name="Grid.php" hash="cd77314b07f51b1acdcbe4ce8997fe78"/></dir><file name="Gift.php" hash="8c2c222bf1ae921d61eac62108c5f725"/></dir><dir name="Frontend"><dir name="Checkout"><file name="GiftForm.php" hash="54acc51fe0b072c96dca27ec234340eb"/></dir></dir></dir><dir name="Helper"><file name="Data.php" hash="a0e452c5d23ae7cad623c52d15a6118d"/></dir><dir name="Model"><dir name="Config"><dir name="Source"><file name="CouponFormats.php" hash="7438cfcd8fbf2062e62495f59967a7bf"/><file name="CouponRules.php" hash="135b84acf613328796d1c2eae0543734"/></dir></dir><file name="CouponGenerator.php" hash="ca121b7f205cb05e03ecfdf229c431b4"/><file name="Cron.php" hash="6dd51a4f15ca62382c978e143c9a869d"/><file name="Gift.php" hash="f1569c386e730dd174417066c419423f"/><file name="Observer.php" hash="772d3fb152c6829eff20b5acb09c0736"/></dir><dir name="Resource"><dir name="Gift"><file name="Collection.php" hash="4404e2f7021b6683d0ceab2b9e687af6"/></dir><file name="Gift.php" hash="6ab2c2e31f60d3e8be45545cf4e2fabd"/></dir><dir name="controllers"><dir name="Admin"><dir name="Promo"><file name="GifterController.php" hash="34cf174e2d9241d7eb3c03de360ff03c"/></dir></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="cf2e63b3e3f403c91b52393f5fea2b14"/><file name="config.xml" hash="b16782e6ea0554796bd8e9844e361297"/><file name="system.xml" hash="7d27a8bbcc1ce7827327635fbb479259"/></dir><dir name="sql"><dir name="Bricks_Gifter"><file name="install-1.0.0.php" hash="6272b303f9e635fb3a53ed9b378d2a72"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Bricks_Gifter.xml" hash="7c4851a4ad353bbaf0f3fe0cecf0223d"/></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="layout"><dir name="Bricks"><file name="Gifter.xml" hash="2a3abc47e56bba863bd5712d4bf4abfe"/></dir></dir></dir></dir></dir><dir name="frontend"><dir name="base"><dir name="default"><dir name="template"><dir name="Bricks"><dir name="Gifter"><dir name="checkout"><file name="gift-form.phtml" hash="7e4c513fa0c0b14c77570d79b425bd5d"/></dir></dir></dir></dir><dir name="layout"><dir name="Bricks"><file name="Gifter.xml" hash="d17963e57250b04e8442d043352f9d15"/></dir></dir></dir></dir></dir></target><target name="magelocale"><dir name="en_US"><dir name="template"><dir name="email"><dir name="bricks"><dir name="gifter"><file name="gift.html" hash="1ea15790892174ee8a48fa67e1c053e8"/></dir></dir></dir></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>2.0.0.0</min><max>6.0.0.0</max></php></required></dependencies>
18
+ </package>