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

Contents of /contributions/modules/cart_theme/cart_theme.module

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


Revision 1.6 - (show annotations) (download) (as text)
Thu Nov 1 01:06:38 2007 UTC (2 years ago) by suydam
Branch: MAIN
CVS Tags: DRUPAL-5--1-4, HEAD
Changes since 1.5: +29 -10 lines
File MIME type: text/x-php
Fixed bug with /store/payment/authorize_net URLs.  Please upgrade to this version.
1 <?php
2 // $Id: cart_theme.module,v 1.5 2007/10/16 18:51:42 suydam Exp $
3
4 /**
5 * @file
6 * Theme the checkout experience based on settings from taxonomy theme and items in the user's cart
7 */
8
9 // hook_menu()
10 function cart_theme_menu($may_cache) {
11 $items = array();
12 if ( !$may_cache ) {
13 if ( preg_match( "/cart\/checkout/",$_GET['q'] )
14 || preg_match( "/cart\/review/", $_GET['q'] )
15 || preg_match( "/authorize_net/", $_GET['q'] )
16 || preg_match( "/skipcart/", $_GET['q'] )
17 || ( isset( $_GET['destination'] ) && preg_match( "/^cart/", $_GET['destination'] ) )
18 ) {
19 watchdog("cart_theme","in hook_menu, attempting to perform cart_get_items()");
20 $our_items = array();
21 $cart_items = cart_get_items();
22 foreach ( array_keys( $cart_items ) as $key ) {
23 if ( is_numeric( $key ) ) {
24 $our_items[] = $key;
25 }
26 }
27 if ( count( $our_items ) ) {
28 watchdog( "cart_theme","Checking for custom theme on nid=" . $our_items[0]);
29 watchdog('cart_theme',"themeing from within hook_menu() using cart_get_items");
30 _cart_themer( $our_items[0] );
31 }
32 elseif ( preg_match('/store\/payment\/authorize_net/', $_GET['q'] ) ) {
33 // in this situation, arg(3) contains the txnid.
34 // get the items from that transaction and cart theme based on that.
35 $txn = store_transaction_load( arg(3) );
36 //drupal_set_message('<PRE>' . sprint_r( $txn ) . '</pre>');
37 foreach ( array_keys( $txn->items ) as $key ) {
38 if ( is_numeric( $key ) ) {
39 $our_items[] = $txn->items[$key]->nid;
40 }
41 }
42 if ( count( $our_items ) ) {
43 watchdog('cart_theme',"themeing from within hook_menu() using store_transaction_load");
44 watchdog( "cart_theme","Checking for custom theme on nid=" . $our_items[0]);
45 _cart_themer( $our_items[0] );
46 }
47 }
48 else {
49 watchdog('cart_theme','Uh-oh. Nothing in the cart. PLAN B!');
50 }
51 }
52 }
53 return $items;
54 }
55
56 // create the "should be part of PHP's core" sprint_r function...but only if I haven't
57 // already added it elsewhere...
58 if (!function_exists('sprint_r')) {
59 function sprint_r($var) {
60 ob_start();
61 print_r($var);
62 $ret = ob_get_contents();
63 ob_end_clean();
64 return $ret;
65 }
66 }
67
68 function _cart_theme_by_nodetype( $nid, &$custom_theme ) {
69 // code taken largely from _taxonomy_theme_select_term
70 $nodetype = db_fetch_object(db_query('SELECT type FROM {node} WHERE nid = %d', $nid));
71 if ( $nodetype->type ) {
72 $custom_theme = variable_get('taxonomy_theme_nodetype|'.$nodetype->type, $default_theme);
73 }
74 }
75
76 function _cart_theme_by_alltaxo( $nid, &$custom_theme ) {
77 // code taken largely from _taxonomy_theme_select_term
78 $terms = taxonomy_node_get_terms($nid);
79 $term = current($terms);
80 $vid = $term->vid;
81 $custom_theme = variable_get('taxonomy_theme_all|'.$term->tid, $default_theme);
82 }
83 function _cart_theme_by_vocab( $nid, &$custom_theme ) {
84 // code taken largely from _taxonomy_theme_select_term
85 $terms = taxonomy_node_get_terms( $nid );
86 $term = current( $terms ); // currnt takes the first element of the array (vs. pop which takes the last)
87 $vid = $term->vid;
88 //drupal_set_message("<h1>TERMS</h1><PRE>" . sprint_r( $terms ) . "</pre>");
89 $custom_theme = variable_get('taxonomy_theme_vocab|'.$vid, $default_theme);
90 }
91 function _cart_theme_by_term( $nid, &$custom_theme ) {
92 // code taken largely from _taxonomy_theme_select_term
93 $selector_vid = variable_get( 'taxonomy_theme_selector','');
94 $terms = taxonomy_node_get_terms_by_vocabulary($nid, $selector_vid);
95 if ($terms) {
96 $term = current($terms);
97 $custom_theme = variable_get('taxonomy_theme_term|'.$term->tid, $default_theme);
98 }
99 }
100
101 function _cart_themer( $nid ) {
102 // we borrow code from (and call functions directly from) taxonomY-theme.module here
103 // module tested with taxonomy_theme v5.x-1.1
104 global $custom_theme;
105 $default_theme = variable_get('theme_default', 'bluemarine');
106
107 // this one is set by taxonomy_theme module
108 $theme_method = variable_get('taxonomy_theme_method',0);
109
110 watchdog("cart_theme","_cart_themer setting theme based on nid=$nid");
111 watchdog("cart_theme","Default theme is $default_theme");
112 watchdog("cart_theme","BEFORE: custom_theme = $custom_theme");
113 //drupal_set_message("BEFORE: custom_theme = $custom_theme");
114
115 // call out to taxonomy_theme.module for help themeing the nid we need.
116 require_once (drupal_get_path('module', 'taxonomy_theme').'/taxonomy_theme_taxonomy.inc');
117
118 // code pasted from taxonomy_theme module directly..
119 // couldn't use their functins though...despite asking for $uri (which we could alter to be node/$nid)
120 // , it still used arg(x) to determine theme.. boooo...
121 if ( $theme_method ) {
122 switch($theme_method) {
123 case TAXOTHEME_TERM:
124 watchdog("cart_theme","theme based on term ($theme_method)");
125 _cart_theme_by_term( $nid, $custom_theme );
126 break;
127 case TAXOTHEME_VOCAB:
128 watchdog("cart_theme","theme based on vocab ($theme_method)");
129 _cart_theme_by_vocab($nid, $custom_theme );
130 break;
131 case TAXOTHEME_ALLTAXO:
132 watchdog("cart_theme","theme based on all taxo ($theme_method)");
133 _cart_theme_by_alltaxo($nid, $custom_theme);
134 break;
135 case TAXOTHEME_NODETYPE:
136 //drupal_set_message("theme based on all Node type ($theme_method)");
137 _cart_theme_by_nodetype($nid, $custom_theme);
138 break;
139 default:
140 // do nothing
141 watchdog("cart_theme","theme method case didn't happen.... theme_method=$theme_method");
142 //drupal_set_message("theme method case didn't happen.... theme_method=$theme_method");
143 }
144 watchdog("cart_theme","AFTER: custom_theme = $custom_theme");
145 //drupal_set_message("AFTER: custom_theme = $custom_theme");
146 }
147 elseif ( variable_get('taxonomy_theme_extended_enable',0) ) {
148 watchdog("cart_theme","themeing by path ($uri)");
149 //drupal_set_message("themeing by path");
150 _cart_theme_by_path( $nid, $custom_theme );
151 //drupal_set_message("AFTER: custom_theme = $custom_theme");
152 watchdog('cart_theme',"AFTER: custom_theme = $custom_theme");
153 }
154 else {
155 watchdog("cart_theme","ALERT: NO THEME METHOD FOUND (theme_method = $theme_method)");
156 //drupal_set_message("ALERT: NO THEME METHOD FOUND (theme_method = $theme_method)");
157 }
158
159 if ( $custom_theme && ($custom_theme != $default_theme ) ) {
160 watchdog("cart_theme","calling init_theme... with $custom_theme");
161 //drupal_set_message("calling init_theme... with $custom_theme");
162 init_theme();
163 }
164 }
165
166 function _cart_theme_by_path( $nid, $custom_theme ) {
167 // normally they theme by the URI...we need to get the first item out of the cart, construct a URI for that and theme with that instead.
168 global $custom_theme;
169 $uri = "node/$nid";
170 watchdog("cart_theme","path URI = $uri");
171
172 $offset = (variable_get('clean_url', 0) ? 0 : 3) + strlen(base_path());
173 watchdog("cart_theme","path OFFSET = $offset");
174 //drupal_set_message("OFFSET = $offset");
175 $alias_uri = $uri;
176 if (module_exists('i18n')) {
177 i18n_get_lang_prefix($alias_uri, true);
178 }
179 $alias_uri = $alias_uri ? $alias_uri : '/';
180 //drupal_set_message("ALIAS URI = $alias_uri");
181 watchdog("cart_theme","path ALIAS URI = $alias_uri");
182 $themes = list_themes();
183 foreach ($themes as $themex) {
184 //drupal_set_message("THEMEX = <PRE>" . sprint_r($themex) . '</pre>');
185 $paths_regexp = _taxonomy_theme_expcache($themex->name);
186 //drupal_set_message("Paths regexp = $paths_regexp");
187 watchdog('cart_theme',"Paths regexp = $paths_regexp");
188 if (preg_grep($paths_regexp, array($uri, $alias_uri))) {
189 watchdog('cart_theme',"PATH setting custom theme to " . $themex->name);
190 //drupal_set_message("setting custom theme to " . $themex->name);
191 $custom_theme = $themex->name;
192 }
193 else {
194 //drupal_set_message("NO match on the $paths_regexp with this one");
195 watchdog('cart_theme',"PATH no match on the $paths_regexp with this one");
196 }
197 }
198 }
199
200 function cart_theme_form_alter( $form_id, &$form ) {
201 //watchdog("cart_theme","FORM ALTER Form id = $form_id");
202 //drupal_set_message("FORM ALTER Form id = $form_id");
203 if ( $form_id == 'cart_view_form' ) {
204 $our_items = array();
205 if (isset( $form['items'] ) && count( $form['items'] ) ) {
206 foreach ( array_keys( $form['items'] ) as $key ) {
207 if ( is_numeric( $key ) ) {
208 $our_items[] = $key;
209 }
210 }
211 }
212 else {
213 $cart_items = cart_get_items();
214 foreach ( array_keys( $cart_items ) as $key ) {
215 if ( is_numeric( $key ) ) {
216 $our_items[] = $key;
217 }
218 }
219 }
220
221 watchdog("cart_theme","our items = " . sprint_r( $our_items ));
222
223 if ( count( $our_items ) ) {
224 // call our themer with the lightest item in the cart (only the nid is required)
225 watchdog('cart_theme',"Theming from within hook_form_alter()");
226 _cart_themer( $our_items[0] );
227 }
228 }
229 }
230
231 function cart_theme_ecommerceapi( &$txn, $op ) {
232 //watchdog("cart_theme","Ecommerce api op = $op");
233 }
234
235 function cart_theme_productapi( &$node, $op, $arg3=NULL, $arg4=NULL) {
236 //watchdog("cart_theme","Product api op = $op");
237 //watchdog("cart_theme","h1>NODE</h1><PRE>" . sprint_r( $node ) . "</pre>");
238 }
239
240 function cart_theme_transactionapi( &$txn, $op, $a3=NULL, $a4 ) {
241 //watchdog("cart_theme","Txn api op = $op");
242 }
243
244 function cart_theme_nodeapi(&$node, $op) {
245 //watchdog("cart_theme","Node api op = $op" );
246 //watchdog("cart_thtme","nid = " . $node->nid );
247 }
248
249 function cart_theme_checkoutapi(&$txn, $op, $arg3=NULL, $arg4=NULL) {
250 //watchdog("cart_theme","checkoutapi op = $op");
251 //watchdog("cart_theme","h1>TXN</h1>" . sprint_r( $txn ));
252 }
253
254

  ViewVC Help
Powered by ViewVC 1.1.2