/[drupal]/contributions/modules/skipcart/skipcart.module
ViewVC logotype

Contents of /contributions/modules/skipcart/skipcart.module

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


Revision 1.16 - (show annotations) (download) (as text)
Mon Sep 24 20:39:59 2007 UTC (2 years, 2 months ago) by suydam
Branch: MAIN
CVS Tags: HEAD
Changes since 1.15: +32 -54 lines
File MIME type: text/x-php
Port of Skipcart v1.7 to Drupal 4.7 by mariano.barcia
1 <?php
2 // $Id: skipcart.module,v 1.12 2007/08/09 15:49:37 suydam Exp $
3
4 /**
5 * @file
6 * Skip the cart review page and proceed directly to checkout after adding this item to my cart
7 */
8
9
10 // hook_menu()
11 function skipcart_menu($may_cache) {
12 $items = array();
13 if ( !$may_cache ) {
14 $items[] = array(
15 'path' => 'skipcart/donate',
16 'title' => t('Skipcart this item'),
17 'description' => t('Menu call back for when a user clicks our button'),
18 'type' => MENU_CALLBACK,
19 'callback' => 'skipcart_donation',
20 'access' => TRUE,
21 'weight' => 10
22 );
23
24 $items[] = array(
25 'path' => 'skipcart',
26 'title' => t('Skipcart this item'),
27 'description' => t('Menu call back for when a user clicks our button'),
28 'type' => MENU_CALLBACK,
29 'callback' => 'skipcart_item',
30 'access' => TRUE,
31 'weight' => 10
32 );
33 }
34 return $items;
35 }
36
37 function skipcart_donation() {
38 // after a donation, send them to checkout
39 $form = drupal_get_form('checkout_form');
40 drupal_set_message( variable_get('skipcart_introtext','') );
41 return $form;
42 }
43
44 function skipcart_item() {
45 if ( !arg(1)) {
46 drupal_set_message("No arg passed to skipcart.",'error');
47 return 'Exiting';
48 }
49 $product_nid = arg(1);
50
51 // empty the cart
52 // cart.module line 1058
53 // function cart_empty($cookie_id = null) {
54 // this only happens if they have it turned on
55 if ( variable_get('skipcart_clearcart','0')) {
56 cart_empty();
57 watchdog('Skipcart',"Cart emptied in preparation for skipcarting $product_nid");
58 }
59
60 // add this item
61 // cart.module line 946
62 // function cart_add_item($nid, $qty = NULL, $data = NULL) {
63 cart_add_item($product_nid,1);
64 watchdog('Skipcart',"NID $product_nid added to new cart");
65
66 // send them to the checkout screen instead of cart-review
67 //$screens = checkout_get_screens();
68 //watchdog('Skipcart',"Screenlist fetched");
69 //drupal_set_message("<PRE>" . sprint_r( $screens ) . "</pre>");
70
71 // build a friendly intro to the page
72 // the checkout_form is automagically set to the correct "screen" from the screenlist
73 // so we can skip that step..hooray.
74 $form = checkout_form();
75 drupal_set_message( variable_get('skipcart_introtext','') );
76 return $form;
77 }
78
79 function _skipcart_eligible( $type ) {
80 $allowed_list = variable_get('skipcart_eligibles',array());
81 if ( count( preg_grep("/^$type$/", $allowed_list))) {
82 return true;
83 }
84 else {
85 return false;
86 }
87
88 }
89
90 function skipcart_nodeapi(&$node,$op) {
91 switch($op) {
92
93 case 'insert':
94 case 'update':
95 // get a list of those node-types for which we want to store skipcart settings
96 $eligibles = variable_get("skipcart_eligibles",array());
97
98 // if this is one of those nodes AND if the hook_form_alter worked (sending us variables for the 2 skipcart vars) then proceed to store data
99 //if ( in_array( $node->type,$elibibles ) && isset( $node->skipcart_enabled ) && isset( $node->skipcart_buytext) ) {
100 if ( in_array( $node->type,$eligibles ) && isset( $node->skipcart_enabled ) && isset( $node->skipcart_buytext) ) {
101 if ( $node->skipcart_buytext == 'DEFAULTTEXT' ) {
102 $node->skipcart_buytext = variable_get('skipcart_buybutton','SKIPCART MODULE LINK');
103 }
104 db_query( "DELETE FROM {skipcart_nodes} WHERE nid=%d", $node->nid );
105 db_query("INSERT INTO {skipcart_nodes} (nid,skipcart_enabled,skipcart_buytext)
106 VALUES
107 (" . $node->nid . "," . $node->skipcart_enabled . ",'" . $node->skipcart_buytext . "')"
108 );
109 if ( $node->skipcart_enabled ) {
110 drupal_set_message('<I>SKIPCART ANNOUNCEMENT:</i> Make sure to add $node->content[\'skipcart\'] to the ' . l('body template for product nodes','admin/content/templates/product') . '. Otherwise you won\'t see the Skipcart button');
111 }
112 }
113 break;
114
115 case 'delete':
116 db_query("DELETE FROM skipcart_nodes WHERE nid = %d", $node->nid );
117 break;
118
119 case 'load':
120 $thearray = array();
121 // check this node-type to see if it's skipcart enabled.
122 // if it's not, don't even get any vars.
123 // skipcart_eligibles is an array
124 $eligibles = variable_get("skipcart_eligibles",array());
125 if ( in_array( $node->type, $eligibles ) ) {
126 //drupal_set_message($node->type . "is eligible for skipcart...get vars");
127 $thearray = _skipcart_getvars( $node->nid );
128 }
129 //else {
130 //drupal_set_message($node->type . "is not eligible for skipcart");
131 //}
132 return $thearray;
133 break;
134
135 case 'prepare':
136 $eligibles = variable_get("skipcart_eligibles",array());
137 if ( in_array( $node->type, $eligibles ) ) {
138 if ( !isset( $node->skipcart_enabled ) ) {
139 $node->skipcart_enabled = '1';
140 }
141 if ( !isset( $node->skipcart_buytext ) ) {
142 $node->skipcart_buytext = 'SKIPCART MODULE LINK';
143 }
144 }
145 break;
146
147 case 'view':
148 // for donations, we have to use a button (to submit the form with donation amounts in tact)
149 if ( $node->ptype == 'donate' && isset( $node->skipcart_enabled ) && $node->skipcart_enabled ) {
150 $node->body .= "\n\n<!-- skipcart content --><!-- skipcarts hides itself for donations, altering the submit button instead -->";
151 $node->body .= "<!-- end skipcart content -->\n\n";
152 }
153 else if ( isset( $node->skipcart_enabled ) && $node->skipcart_enabled) {
154 //drupal_set_message("skipcart is enabled");
155 $node->body .= "\n\n<!-- skipcart content --><div class='skipcart_button'>";
156 $button_text = $node->skipcart_buytext;
157 $node->body .= theme('skipcart_buy', $node->nid, $button_text);
158 $node->body .= "</div><!-- end skipcart content -->\n\n";
159 }
160 //drupal_set_message("<HR><HR><HR><PRE>" . sprint_r($node) . "</pre><HR><HR><HR>");
161 }
162 }
163
164 function theme_skipcart_buy($nid, $button_text) {
165 $output = "<br />";
166 $output .= l($button_text, "skipcart/$nid");
167 return $output;
168 }
169
170 function _skipcart_getvars( $nid ) {
171 $statement = "SELECT * FROM skipcart_nodes WHERE nid = $nid";
172 //drupal_set_message($statement);
173 $sth = db_query( $statement );
174 $row = array();
175 if ( db_num_rows( $sth ) ) {
176 $row = db_fetch_array( $sth );
177
178 // If it's empty string, use default text for the link
179 if ( $row['skipcart_buytext'] == '' ) {
180 $row['skipcart_buytext'] = variable_get('skipcart_buybutton','SKIPCART MODULE LINK');
181 }
182
183 }
184 else {
185 $row = array('skipcart_enabled' => 0, 'skipcart_buytext' => variable_get('skipcart_buybutton','SKIPCART MODULE LINK'), 'skipcart_donatebugoverride' => 0 );
186 }
187 //drupal_set_message("<PRE>" . sprint_r($row) . "</pre>");
188 return $row;
189 }
190
191 // remove a specific item from an array
192 if ( !function_exists('array_remval') ) {
193 function array_remval($val, &$arr) {
194 $array_remval = $arr;
195 for($x=0;$x<count($array_remval);$x++) {
196 $i=array_search($val,$array_remval);
197 if (is_numeric($i)) {
198 $array_temp = array_slice($array_remval, 0, $i );
199 $array_temp2 = array_slice($array_remval, $i+1, count($array_remval)-1 );
200 $array_remval = array_merge($array_temp, $array_temp2);
201 }
202 }
203 return $array_remval;
204 }
205 }
206
207
208 // create the "should be part of PHP's core" sprint_r function...but only if I haven't
209 // already added it elsewhere...
210 if (!function_exists('sprint_r')) {
211 function sprint_r($var) {
212 ob_start();
213 print_r($var);
214 $ret = ob_get_contents();
215 ob_end_clean();
216 return $ret;
217 }
218 }
219
220
221 function skipcart_form_alter( $form_id, &$form ) {
222 if ( $form_id == 'donate_product_form' ) {
223 $skipcart_vars = _skipcart_getvars( arg(1) );
224
225 // for donations alter the submit button instead of adding our own link.
226 if ( $skipcart_vars['skipcart_enabled'] == '1' ) {
227
228 //drupal_set_message("BEFORE: <PRE>" . sprint_r( $form ) . "</pre>",error);
229 if ( $form['0']['#type'] == 'submit' && $form['0']['#value'] == 'Add to cart') {
230
231 $form['0']['#value'] = $skipcart_vars['skipcart_buytext'];
232 $form['#redirect'] = 'skipcart/donate';
233 //$form['#description'] = "HELLO THERE";
234
235 }
236 //drupal_set_message("AFTER: <PRE>" . sprint_r( $form ) . "</pre>",error);
237
238 }
239
240 }
241 else if ( $form_id == 'product_node_form' ) {
242 $skipcart_vars = _skipcart_getvars( arg(1) );
243 $yesno = array('1' => ('Yes'), '0' => t('No') );
244
245 //drupal_set_message("getting skipcart vars....");
246 // why doesn't global $node;$node->nid work here????
247 //drupal_set_message('$skipcart_vars[\'skipcart_enabled\'] = ' . $skipcart_vars['skipcart_enabled'] );
248
249 $form['skipcart'] = array(
250 '#type' => 'fieldset',
251 '#title' => 'Skipcart Configuration',
252 '#collapsible' => TRUE,
253 '#collapsed' => FALSE,
254 '#weight' => 100
255 );
256 $form['skipcart']['skipcart_enabled'] = array(
257 '#type' => 'radios',
258 '#title' => t('Enable Skipcart for this product?'),
259 '#options' => $yesno,
260 '#default_value' => $skipcart_vars['skipcart_enabled'],
261 '#description' => t('If this is checked then "Skipcart" button will be displayed with this product'),
262 );
263
264 // use "DEFAULT" to use the sitewide default
265 $buytext = $skipcart_vars['skipcart_buytext'];
266 if ( $buytext == variable_get('skipcart_buybutton','SKIPCART MODULE LINK') || $buytext == '') {
267 $buytext = 'DEFAULTTEXT';
268 }
269 $form['skipcart']['skipcart_buytext'] = array(
270 '#type' => 'textfield',
271 '#title' => t('Buy Button Text'),
272 '#default_value' => $buytext,
273 '#description' => t('What should the buy-it-now link say? Enter DEFAULTTEXT to use the sitewide default.'),
274 '#maxlength' => 255,
275 '#required' => TRUE,
276 '#siez' => 25,
277 '#weight' => 300
278 );
279 }
280 }
281
282 // admin settings function
283 function skipcart_settings() {
284 $yesno = array('1' => t('Yes'), '0' => t('No') );
285 $form['skipcart_intro'] = array(
286 '#prefix'=> '<p><strong>Skipcart Settings</strong></P>',
287 '#value'=>'<P><strong>NOTE:</strong> Skipcart is nearly useless without contemplate.module. We create $node->content[\'skipcart\'] and the rest is up to you.</p>'
288 );
289
290 $form['skipcart_clearcart'] = array(
291 '#type' => 'radios',
292 '#title' => t('Clear cart on click'),
293 '#options' => $yesno,
294 '#default_value' => variable_get('skipcart_clearcart','1'),
295 '#description' => t('If this is checked then skipcart will remove other items from the cart when adding the item that is being skip-carted.'),
296 );
297
298 /* $form['skipcart_donatebugoverride'] = array(
299 '#type' => 'radios',
300 '#title' => t('Attempt to fix donate.module \'Add to Cart\' removal bug?'),
301 '#options' => $yesno,
302 '#default_value' => variable_get('skipcart_donatebugoverride','0'),
303 '#description' => t('Some versions of donate.module (March 2007 and earlier for sure) do not listen to the Add to Cart invisibility toggle. Skipcart\'s override resolves this (for some versions at least). Because this attempts to fix a bug, you cannot over-ride this at the product level. Rather, use the already-provided "Add to Cart" link: Visible/Hidden toggle on the node/edit page.'),
304 ); */
305
306 $form['skipcart_eligibles'] = array(
307 '#type' => 'checkboxes',
308 '#title' => t('These node types should store a skipcart yes/no setting'),
309 '#options' => node_get_types('names'),
310 '#default_value' => variable_get('skipcart_eligibles',array('product')),
311 '#description' => t('These node-types can have a skipcart yes/no setting. This is intentionally *not* synchronized with those nodes that can be products.'),
312 );
313
314 $form['skipcart_buybutton'] = array(
315 '#type' => 'textarea',
316 '#title' => t('Buy now button text'),
317 '#description' => t('The skipcart button is a text-link by default. You can put whatever you want in here (you can over-ride this at each node as well). It will get wrapped in an href tag and be available in contemplate as $node->content[\'skipcart\'].'),
318 '#default_value' => variable_get('skipcart_buybutton','Buy This Now'),
319 '#cols' => 40,
320 '#rows' => 3,
321 '#resiable' => TRUE
322 );
323 $form['skipcart_introtext'] = array(
324 '#type' => 'textarea',
325 '#title' => t('Intro Text on Checkout'),
326 '#description' => t('It can be disconcerting to click "Buy this now" and then end up on a lone page with an email address and no introductory copy. You can enter introductory copy here. This content will be shoved to the top of the first checkout screen (whatever it is as configured by Drupal).'),
327 '#default_value'=> variable_get('skipcart_introtext','<h1>Checkout</h1>'),
328 '#cols' => 40,
329 '#rows' => 3,
330 '#resizable' => TRUE,
331 );
332 $form['array_filter'] = array('#type' => 'hidden' );
333 return $form;
334 }
335
336 /* permissons for access_control */
337 function skipcart_perm() {
338 return array('administer skipcart settings');
339 }

  ViewVC Help
Powered by ViewVC 1.1.2