/[drupal]/contributions/modules/paypalnode/nodepromocode.module
ViewVC logotype

Contents of /contributions/modules/paypalnode/nodepromocode.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Wed Aug 29 23:33:42 2007 UTC (2 years, 3 months ago) by budda
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +33 -5 lines
File MIME type: text/x-php
Fixed broken promocode price calculations on submission of the node.
1 <?php
2 /*
3 * @todo: store the promo code against the newly created node
4 */
5 function nodepromocode_form_alter($form_id, &$form) {
6 switch($form_id) {
7 case 'taxonomy_form_term':
8
9 // Mark Taxonomy Form Menus Which are related to price calculation
10 /*$vocabs = variable_get('paypalnode_vocabularies', array());
11 $attribs['class'] = 'paypernode';
12 foreach($vocabs as $vocab_id) {
13 */
14
15 $form['name']['#weight'] = -10;
16
17 // Add promo code fieldset
18 $values = nodepromocode_term_promo_code($form['tid']['#value']);
19 $form = array_merge($form, nodepromocode_taxonomy_form($values));
20 break;
21
22
23 case variable_get('paypalnode_content_type', PAYPALNODE_CHARGE_TYPE) . '_node_form':
24 $form['promocode'] = array(
25 '#type' => 'textfield',
26 '#title' => t('Promo code'),
27 '#description' => t('If you have a promotional code, enter it here to receive a discount'),
28 '#size' => 10,
29 '#maxlength' => 10,
30 );
31
32 drupal_add_js("$('#edit-promocode').blur(function() { paypalnode_pricecalc(); });", 'inline', 'footer');
33
34 break;
35 }
36 }
37
38 /*
39 nodepromocode_taxonomy_form
40 */
41 function nodepromocode_taxonomy_form($values) {
42 $form['promocode'] = array(
43 '#type' => 'fieldset',
44 '#title' => t('Promotion Code'),
45 '#description' => t('Enter an alphanumeric string for your promo code. It will be automatically converted to UPPERCASE upon submission. Use a percentage value to describe the amount of discount associated with the code. 100% = FREE'),
46 '#weight' => 0,
47 );
48
49 $form['promocode']['code'] = array(
50 '#type' => 'textfield',
51 '#title' => t('Promo code'),
52 '#size' => 10,
53 '#maxlength' => 10,
54 '#default_value' => isset($values['promocode']) ? $values['promocode'] : '',
55 '#prefix' => '<div class="container-inline">',
56 );
57
58 $form['promocode']['discount'] = array(
59 '#type' => 'textfield',
60 '#size' => 4,
61 '#maxlength' => 3,
62 '#default_value' => isset($values['discount']) ? $values['discount'] : '100',
63 '#weight' => 1,
64 '#suffix' => '% </div>',
65 );
66
67 $form['#validate']['nodepromocode_taxonomy_validate'] = array();
68 $form['#submit']['nodepromocode_taxonomy_submit'] = array();
69
70 return $form;
71 }
72
73
74 function nodepromocode_taxonomy_validate($form_id, $form_values) {
75 // promo codes can only have letters and numbers
76 if($form_values['code'] != '' && !preg_match('/^[A-Za-z0-9]/', $form_values['code'])) {
77 form_set_error('code', t('A promotion code can only contain letters and numbers.'));
78 }
79
80 // make sure the discount percentage is numeric and between 0 & 100
81 if($form_values['discount'] != '' && !is_numeric($form_values['discount'])) {
82 form_set_error('discount', t('The discount value must be a number.'));
83 } elseif($form_values['discount'] < 0 || $form_values['discount'] > 100) {
84 form_set_error('discount', t('The discount value must be between 0% and 100%'));
85 }
86
87 if($form_values['code'] && !$form_values['discount']) {
88 form_set_error('discount', t('If you are setting a promo code you need to specify a discount percentage too.'));
89 }
90
91 if(nodepromocode_get_discount($form_values['code'])) {
92 form_set_error('code', t('A promotion code with the name %name already exists. Codes must be unique.', array('%name' => $form_values['code'])));
93 }
94 }
95
96
97 /*
98 nodepromocode_taxonomy_submit
99 */
100 function nodepromocode_taxonomy_submit($form_id, $form_values) {
101 if($form_values['code'] != '') {
102
103 // Force promo code to all uppercase for easier reading
104 $form_values['code'] = strtoupper($form_values['code']);
105
106 db_query("UPDATE {nodepromocode} SET promocode = '%s', discount = '%s' WHERE tid = %d", $form_values['code'], $form_values['discount'], $form_values['tid']);
107
108 if(db_affected_rows() == 0) {
109 db_query("INSERT INTO {nodepromocode} (tid, promocode, discount) VALUES (%d, '%s', '%s')", $form_values['tid'], $form_values['code'], $form_values['discount']);
110 }
111 }
112 }
113
114
115 /*
116 * Returns the promo code and discount percentage based on a taxonomy term id
117 */
118 function nodepromocode_term_promo_code($tid) {
119 $result = db_query("SELECT * FROM {nodepromocode} WHERE tid = %d", $tid);
120 return db_fetch_array($result);
121 }
122
123 /*
124 nodepromocode_get_discount
125 */
126 function nodepromocode_get_discount($code) {
127 static $promocodes;
128
129 if(!$promocodes[$code] && $result = db_query("SELECT * FROM {nodepromocode} WHERE promocode = '%s'", $code)) {
130 $promocodes[$code] = db_fetch_array($result);
131 }
132
133 return $promocodes[$code];
134 }
135
136 /*
137 nodepromocode_nodeprice
138 */
139 function nodepromocode_nodeprice($price, $tids, $args) {
140
141 if($args['code']) {
142 $promocode = nodepromocode_get_discount($args['code']);
143
144 /* make sure the code matches with any selected terms on the form
145 * otherwise the promo code is not valid. */
146 if(in_array($promocode['tid'], $tids)) {
147 $price -= ($price / 100) * $promocode['discount'];
148 }
149 }
150
151 return array('price' => $price);
152 }
153
154 /*
155 * Stores a snapshot of the promocode used and the percentage discount at the time the node was published. Useful for audit trail reporting.
156 * This also allows for a site admin to change the promocode and percentage at any time without messing up reports.
157 */
158 function nodepromocode_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
159 if($node->type == variable_get('paypalnode_content_type', PAYPALNODE_CHARGE_TYPE)) {
160 switch ($op) {
161 case 'insert':
162 $promo = nodepromocode_get_discount($node->promocode);
163 db_query("INSERT INTO {nodepromocode_node} (nid, promocode, discount) VALUES (%d, '%s', %d)", $node->nid, strtoupper($node->promocode), $promo['discount']);
164 break;
165
166 case 'load':
167 return db_fetch_array(db_query("SELECT promocode, discount FROM {nodepromocode_node} WHERE nid = %d", $node->nid));
168 break;
169 }
170 }
171 }

  ViewVC Help
Powered by ViewVC 1.1.2