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

Contents of /contributions/modules/order/order.module

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


Revision 1.24 - (show annotations) (download) (as text)
Wed Aug 5 19:02:15 2009 UTC (3 months, 3 weeks ago) by vauxia
Branch: MAIN
CVS Tags: HEAD
Changes since 1.23: +94 -79 lines
File MIME type: text/x-php
Beginning to refactor:  An ecommerce Order is an instance of pay_form_order,
which is a subclass of pay_form.  Thus, all of the configuration, available
payment methods, etc. are managed by the payment API, while 'cartiness'
is Order's sole domain.

Also, decoupling order_product items from nodes.  There's no longer a 1:1
relationship between nodes and products: Product definitions will be class-based
and have an (optional) one-to-many relationship from nodes. For my current needs
I will write a "simple" 1:1 node:product handler, but there's no reason you
couldn't write some fancy sub-product backend later.

P.S. There is no - and will be no - upgrade path from any previous/current
version, until there's a downloadable dev release.
1 <?php // $Id: order.module,v 1.23 2009/07/30 23:14:36 vauxia Exp $
2
3 /**
4 * Implementation of hook_menu().
5 */
6 function order_menu() {
7 module_load_include('menu.inc', 'order', 'includes/order');
8 return order_menu_menu();
9 }
10
11 /**
12 * An access callback for orders.
13 */
14 function order_access($order = NULL) {
15 return user_access('use cart') || user_access('administer orders');
16 }
17
18 /**
19 * A load callback for orders.
20 * Orders are just fancy pay_transaction objects, so use the pay API.
21 */
22 function order_load($pxid) {
23 return pay_transaction_load($pxid);
24 }
25
26 /**
27 * A load callback for order products.
28 */
29 function order_product_load($opid) {
30 if (is_object($opid)) return $opid;
31 }
32
33 /**
34 * Implementation of hook_user().
35 */
36 function order_user($op, &$edit, &$account, $category = NULL) {
37 global $user;
38 switch ($op) {
39 case 'insert':
40 if (($user->uid == 0) && $cart = order_cart()) {
41 // the user has a cart we should save with their new information
42 // update the cart with the new UID
43 db_query("UPDATE {order_cart} SET uid = %d WHERE sid = '%s'", $account->uid, $cart->sid);
44 if ($cart = order_cart()) {
45 // alert the user of their changes
46 drupal_set_message(t('Your shopping cart has been saved to your new user account. You may continue to shop and your changes will be saved.'));
47 }
48 else { // an error occurred while saving the cart
49 drupal_set_message(t('An error has occured while saving your shopping cart. Please contact the site administrator to complete your order.', 'error'));
50 watchdog(WATCHDOG_ERROR, t('An error occurred saving a cart during user registration for %link', array('%link' => l($account->name, 'user/'. $account->uid))));
51 }
52 }
53 break;
54
55 case 'login':
56 // check to see if the user has started a cart
57 $carts = db_result(db_query("SELECT count(*) FROM {order_cart} WHERE sid = '%s'", session_id()));
58
59 // this check prevents deleting old carts if the user hasn't started a new one
60 if ($carts) {
61 // delete any old carts
62 db_query("DELETE FROM {order_cart} WHERE uid = %d", $account->uid);
63 // update the existing cart with the user's id
64 db_query("UPDATE {order_cart} SET uid = %d WHERE sid = '%s'", $account->uid, session_id());
65 }
66 // TODO merge existing carts instead of deleting them
67 break;
68 }
69 }
70
71 /**
72 * Implementation of hook_nodeapi().
73 */
74 function order_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
75
76 switch ($op) {
77
78 case 'load':
79 // Append product information to this node.
80 if (user_access('use cart')) {
81 $node->order_product = order_node_products($node);
82 }
83 return;
84
85 case 'view':
86 if (isset($node->order_product)) {
87 $node->content['order_product'] = array(
88 '#theme' => 'order_item_view',
89 '#items' => $node->order_product,
90 );
91 }
92 return;
93
94 case 'prepare':
95 // Include ALL linked products on the node editing form, not just active.
96 $node->order_product = order_node_products($node, FALSE);
97 return;
98
99 case 'insert':
100 case 'update':
101 // Save any product information related to this node.
102 if (isset($node->order_product)) {
103 foreach ($node->order_product as $product) {
104 $product = order_product_load($product);
105 $product->save();
106 }
107 }
108 return;
109
110 case 'delete':
111 // Invalidate any product information related to this node.
112 if (isset($node->order_product)) {
113 foreach ($node->order_product as $opid => $product) {
114 $product->set_nid(0);
115 $product->disable(0);
116 }
117 }
118 return;
119 }
120 }
121
122 /**
123 * Implementation of hook_form_alter().
124 */
125 function order_form_alter(&$form, $form_state, $form_id) {
126 if ($node = $form['#node']) {
127 module_load_include('product.inc', 'order', 'includes/order');
128 $form['order_product'] = order_product_node_form($form, $form_state);
129 }
130 }
131
132 /**
133 * Helper function: return all products related to a node.
134 */
135 function order_node_products($node, $active_only = TRUE) {
136 $products = array();
137
138 $res = db_query("SELECT * FROM {order_product}
139 WHERE nid = %d ". ($active_only ? "AND status = 1" : '') ."
140 ORDER BY weight", $node->nid);
141
142 while ($row = db_fetch_object($res)) {
143 $products[$row->opid] = order_product_load($row);
144 }
145 return $products;
146 }
147
148 /**
149 * Implementation of hook_theme().
150 */
151 function order_theme() {
152 module_load_include('theme.inc', 'order', 'theme/order');
153 return order_theme_theme();
154 }
155
156 /**
157 * Implementation of hook_pay_transaction_handler_info().
158 */
159 function order_pay_transaction_handler_info() {
160 $path = drupal_get_path('module', 'order') .'/includes/handlers';
161 return array(
162 'order_transaction' => array(
163 'title' => t('Orders'),
164 'description' => t('E-commerce product orders on your site.'),
165 'parent' => 'pay_transaction',
166 'module' => 'order',
167 'path' => $path,
168 ),
169 );
170 }
171
172 /**
173 * Implementation of hook_pay_transaction_handler_info().
174 */
175 function order_order_product_handler_info() {
176 $path = drupal_get_path('module', 'order') .'/includes/handlers';
177 return array(
178 'order_product' => array(
179 'title' => t('Simple product'),
180 'description' => t('Simple product handling.'),
181 'module' => 'order',
182 'path' => $path,
183 ),
184 );
185 }

  ViewVC Help
Powered by ViewVC 1.1.2