/[drupal]/contributions/modules/wishlist/wishlist.page.inc
ViewVC logotype

Contents of /contributions/modules/wishlist/wishlist.page.inc

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


Revision 1.1 - (show annotations) (download) (as text)
Sun Nov 25 03:34:04 2007 UTC (2 years ago) by smclewin
Branch: MAIN
CVS Tags: DRUPAL-6--2-1-BETA, DRUPAL-6--2-1, HEAD
Branch point for: DRUPAL-6--2
File MIME type: text/x-php
First pass port to Drupal 6. All appears to be well, but it could use some other eyes to test and verify.
1 <?php
2 /* $Id:$ */
3 // Wishlist management module for Drupal
4 // Written by Scott McLewin and Melanie Paul-McLewin
5 // drupal AT mclewin DOT com
6
7 /**
8 * Menu callback function to handle the "wishlist" URL
9 *
10 * @return
11 * theme("page",...) ready to display the wishlist page result
12 */
13 function wishlist_page() {
14 $arg1 = arg(1);
15
16 if (!isset($arg1)) {
17 print theme("page", _wishlist_show_all_lists());
18 } else if (is_numeric($arg1)){
19 print theme("page", _wishlist_list_items($arg1));
20 } else {
21 switch ($arg1) {
22 case "item":
23 print theme("page", _wishlist_item_action_handler(arg(2), arg(3), arg(4)));
24 break;
25 default:
26 print theme("page", "Invalid argument to wishlist_page [".$arg1."]");
27 break;
28 }
29 }
30 }
31
32
33 /**
34 * Function to display every wishlist in the system
35 *
36 * @return
37 * A theme("page", ...) with display content showing all available wishlists.
38 */
39 function _wishlist_show_all_lists() {
40 $output = "";
41
42 $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, n.uid, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.type='%s' GROUP BY u.uid ORDER BY u.name DESC"), 'wishlist');
43
44 $output .= "<div class='wishlist'><div class='all_lists'>";
45 $output .= '<p>'.t("Wishlists allow their owners to maintain a running list of items they may want to purchase or have purchased for them on a birthday or special occasion. The items on these lists are not sold through this web site, but point to other stores and vendors who do sell them. This list is the on-line version of the birthday list you may have given to your mother growing up. It was her job to make sure that no two people purchased the same gift for you.").'</p>';
46 $has_rows = false;
47 while ($account = db_fetch_object($result)) {
48 $items[] = format_wishlists($account, t("'s wishlist"));
49 $has_rows = true;
50 }
51 if(false == $has_rows) {
52 return t("No wishlists exist on this site");
53 } else {
54
55 $output .= theme('item_list', $items);
56 $output .= "</div></div>";
57
58 return $output;
59 }
60 }
61
62 /**
63 * Lists out the items for the user $uid
64 *
65 * @param $uid
66 * User for whom we should list out wishlist items.
67 * @return
68 * $output for display
69 */
70 function _wishlist_list_items($uid) {
71 global $user;
72
73 if(!is_numeric($uid)) {
74 return theme("page", "Invalid argument to wishlist_list_items [".$uid."]");
75 }
76
77 $showcolumns = variable_get('wishlist_showcolumn', _wishlist_get_default_showcolumn_settings_array());
78
79 $header = array();
80 if($showcolumns['wishlist_show_action'] == true) {
81 $header[] = array('data' => t("Action"));
82 }
83 if($showcolumns['wishlist_show_title'] == true) {
84 $header[] = array('data' => t("Item Name"), "field" => "n.title");
85 }
86 if($showcolumns['wishlist_show_description'] == true) {
87 $header[] = array('data' => t("Description"));
88 }
89 if($showcolumns['wishlist_show_priority'] == true) {
90 $header[] = array('data' => t("Priority"), "field" => "w.item_priority", "sort" => "asc");
91 }
92 if($showcolumns['wishlist_show_cost'] == true) {
93 $header[] = array('data' => t("Cost"), "field" => "w.item_est_cost");
94 }
95 if($showcolumns['wishlist_show_quantity'] == true) {
96 $header[] = array('data' => t("Quantity"), "field" => "w.item_quantity_requested");
97 }
98 if($showcolumns['wishlist_show_urls'] == true) {
99 $header[] = array('data' => t("URLs"));
100 }
101 if($showcolumns['wishlist_show_updated'] == true) {
102 $header[] = array('data' => t("Last Updated"), "field" => "n.changed");
103 }
104
105 if(empty($header)) {
106 drupal_set_message('The site administrator has removed all columns from this display. No wishlist information will appear.', 'error');
107 }
108
109 // Load up the user account information for the giftee so that we can include it in the display and in the breadcrumb
110 $giftee_account = user_load(array('uid' => $uid));
111
112 $breadcrumb[] = l(t('Home'), NULL);
113 $breadcrumb[] = l(t('All wishlists'), 'wishlist');
114 $breadcrumb[] = l(t("@name's wishlist", array('@name' => $giftee_account->name)), 'wishlist/'.$giftee_account->uid);
115 drupal_set_breadcrumb($breadcrumb);
116
117 $result = pager_query(db_rewrite_sql("SELECT n.nid, n.title, n.uid, w.item_priority, w.item_est_cost, w.item_currency, r.body, n.changed, n.created, w.item_quantity_requested, w.item_url1, w.item_url2, w.item_is_public FROM {node} n INNER JOIN {node_revisions} r ON n.vid = r.vid LEFT JOIN {wishlist} w ON n.nid = w.nid WHERE n.type='wishlist' AND n.uid=".$uid." AND w.item_is_public ".tablesort_sql($header)), variable_get('wishlist_display_count', 10), 0);
118
119 $rows = _wishlist_fill_list_table_array($result);
120
121
122 if (!$rows) {
123 $rows[] = array(array("data" => t("No wishlist items found."), "colspan" => "5"));
124 }
125
126 // Add a pager control to the bottom of the table (if necessary)
127 $pager = theme("pager", NULL, variable_get('wishlist_display_count', 10), 0);
128 if (!empty($pager)) {
129 $rows[] = array(array("data" => $pager, "colspan" => "8"));
130 }
131
132 // Build up the output buffer.
133 $output .= '<div class="wishlist">';
134 $output .= t("<p>The list below shows all of the items on @giftee_name's wishlist.</p><p>Once you have decided on an item to purchase for @giftee_name, click on 'get this gift' and follow the instructions on the screen that comes up. This will indicate that you have purchased the gift and will prevent somebody else from getting a duplicate. Once you have clicked on 'get this gift' you still need to actually purchase the gift. You don't do that here at this site. You need to visit the store or website described by @giftee_name and complete your transaction there. Please always 'get this gift' here on this website before you purchase it at the store. Somebody else might decide to buy one too while you are out shopping.</p>", array('@giftee_name' => check_plain($giftee_account->name)));
135 $output .= t("<p>If you change your mind about purchasing a particular gift you can put the item back on @giftee_name's wishlist. View the item (by clicking on the 'Item Name' field) and you will be able to return the item to the list. You need to be logged in to this web site to see the 'return' option.</p>", array('@giftee_name' => $giftee_account->name));
136 $output .= _wishlist_get_reveal_form($uid);
137 $output .= theme('table', $header, $rows) .'</div>';
138
139 // If the wishlist being shown belongs to the user who is currently logged in, then also
140 // display a table of the private items below the public ones
141 if($uid == $user->uid) {
142 $output .= "<div class='wishlist_private'>".t("Items which are private and not shown to others who view your wishlist")."</div>";
143 $result = pager_query(db_rewrite_sql("SELECT n.nid, n.title, n.uid, w.item_priority, w.item_est_cost, w.item_currency, r.body, n.changed, n.created, w.item_quantity_requested, w.item_url1, w.item_url2, w.item_is_public FROM {node} n INNER JOIN {node_revisions} r ON n.vid = r.vid LEFT JOIN {wishlist} w ON n.nid = w.nid WHERE n.type='wishlist' AND n.uid=".$uid." AND NOT w.item_is_public ".tablesort_sql($header)), variable_get('wishlist_display_count', 10), 1);
144
145 $rows = _wishlist_fill_list_table_array($result);
146 if (!$rows) {
147 $rows[] = array(array("data" => t("No private wishlist items found."), "colspan" => "5"));
148 }
149
150 // Add a pager control to the bottom of the table (if necessary)
151 $pager = theme("pager", NULL, variable_get('wishlist_display_count', 10), 1);
152 if (!empty($pager)) {
153 $rows[] = array(array("data" => $pager, "colspan" => "8"));
154 }
155
156 $output .= '<div class="wishlist">'. theme('table', $header, $rows) .'</div>';
157 }
158
159 return $output;
160 }
161
162
163
164
165
166 /**
167 * Fills an associative array for use with a theme('table' call as the $rows argument.
168 *
169 * @param result
170 * Result of a db_query() for a series of node records of type wishlist
171 *
172 * @return
173 * An associative array ready for use with theme('table', ...) call as the $rows argument.
174 */
175 function _wishlist_fill_list_table_array($result) {
176 global $user;
177 $priority_str = _wishlist_get_item_priority_array();
178
179 $showcolumns = variable_get('wishlist_showcolumn', _wishlist_get_default_showcolumn_settings_array());
180
181
182 while($node = db_fetch_object($result)) {
183 $node->item_quantity_purchased = _wishlist_get_node_quantity_purchased($node->nid);
184
185 if (drupal_strlen($node->body) > 200) {
186 $body = truncate_utf8($node->body, 200).l("...(more)", "node/$node->nid");
187 } else {
188 $body = $node->body;
189 }
190 $body = check_markup($body, $node->format);
191
192 // If the module is configured to toss links open in new windows, then generate the appropriate attributes for the link
193 if(variable_get("wishlist_url_in_new_window", FALSE)) {
194 $url_link_target["target"] = "_wishlist_url_window";
195 }
196
197
198 // In links column display links to the purchase point if there are still items to purchase.
199 // If all have been purchased, don't put the links in the list. This is to cut down
200 // on the folks who will visit, ignore the column about how many are left, click through
201 // to the shop and then end up purchasing a duplicate. We also hide the links when
202 // the module is configured to hide purchase information from anonymous users.
203 if((($node->item_quantity_requested != 0) && ($node->item_quantity_purchased >= $node->item_quantity_requested))
204 || _wishlist_hide_purchase_info($node)) {
205 $links = _wishlist_url($node->item_url1, 20) . "<BR>" . _wishlist_url($node->item_url2, 20);
206 } else {
207 $links = l(_wishlist_url($node->item_url1, 20), $node->item_url1, array('attributes' => array('target' => $url_link_target)))."<BR>".l(_wishlist_url($node->item_url2, 20), $node->item_url2, array('attributes' => array('target' => $url_link_target)));
208 }
209
210 $rowdata = array();
211
212 if($showcolumns['wishlist_show_action'] == true) {
213 if(_wishlist_hide_purchase_info($node)) {
214 if($user->uid == $node->uid) {
215 $rowdata[] = array('data' => t('Hidden from you'));
216 } else {
217 $rowdata[] = array('data' => l(t('Login for information on this gift'), "user/login"));
218 }
219 } else {
220 $rowdata[] = array("data" =>
221 (user_access('access wishlists') && $node->item_quantity_requested > $node->item_quantity_purchased) ?
222 l(t('Get this gift'), "wishlist/item/$node->nid/purchase")
223 : (($node->item_quantity_purchased >= $node->item_quantity_requested) ? t("(none left to purchase)")
224 : l(t("Login to get this gift"), "user/login")));
225 }
226 }
227
228 $newtext = '';
229 if($user->uid) {
230 $last_viewed = node_last_viewed($node->nid);
231 if(($last_viewed == 0) && ($node->created > NODE_NEW_LIMIT)) {
232 $newtext = '<span class="newitem">'.t('new').'</span>';
233 }
234 }
235
236 if($showcolumns['wishlist_show_title'] == true) {
237 $rowdata[] = array("data" => l($node->title, "node/$node->nid").$newtext);
238 }
239 if($showcolumns['wishlist_show_description'] == true) {
240 $rowdata[] = array("data" => $body);
241 }
242 if($showcolumns['wishlist_show_priority'] == true) {
243 $rowdata[] = array("data" => $priority_str[$node->item_priority]);
244 }
245 if($showcolumns['wishlist_show_cost'] == true) {
246 $rowdata[] = array("data" => _wishlist_currency_str($node->item_currency).$node->item_est_cost);
247 }
248 if($showcolumns['wishlist_show_quantity'] == true) {
249 $rowdata[] = array("data" => $node->item_quantity_requested);
250 }
251 if($showcolumns['wishlist_show_urls'] == true) {
252 $rowdata[] = array("data" => $links);
253 }
254 if($showcolumns['wishlist_show_updated'] == true) {
255 $rowdata[] = array("data" => format_date($node->changed, 'small'));
256 }
257
258 $rows[] = $rowdata;
259 }
260
261 return $rows;
262 }
263
264 /**
265 * Retrieves the default values array for the show-column settings array
266 */
267 function _wishlist_get_default_showcolumn_settings_array() {
268 $default_values['wishlist_show_action'] = true;
269 $default_values['wishlist_show_title'] = true;
270 $default_values['wishlist_show_description'] = true;
271 $default_values['wishlist_show_priority'] = true;
272 $default_values['wishlist_show_cost'] = true;
273 $default_values['wishlist_show_quantity'] = true;
274 $default_values['wishlist_show_urls'] = true;
275 $default_values['wishlist_show_updated'] = true;
276 return $default_values;
277 }
278
279
280 /**
281 *
282 * Handler function for actions take on an item - purchase and return
283 * @param $nid
284 * ID of the node on which we are going to act
285 * @param $action
286 * Action to be taken. "purchase" or "return".
287 * @param $wishlist_purch_id
288 * For the return action only, the ID of the wishlist_purchase record that is being returned.
289 * @return
290 * output for display (via theme('page', ...))
291 */
292 function _wishlist_item_action_handler($nid, $action, $wishlist_purch_id) {
293
294 if(!is_numeric($nid)) {
295 watchdog('error', "Invalid node argument to wishlist_item_action_handler[".$nid."]");
296 return theme("page", "Invalid node argument to wishlist_item_action_handler[".$nid."]");
297 }
298
299 switch($action) {
300 case "purchase":
301 return _wishlist_item_action_purchase($nid);
302 break;
303 case "return":
304 return _wishlist_item_action_return($nid, $wishlist_purch_id);
305 break;
306
307 default:
308 watchdog("error", "Invalid action argument to wishlist_item_action_handler[".$action."]");
309 return "Invalid action argument to wishlist_item_action_handler[".$action."]";
310 }
311 }
312
313 /**
314 * Builds the page to display when a user wants to purchase an item from a wishlist
315 *
316 * @param $nid
317 * ID of the node on which to act. We assume the user that is currently logged in will be making
318 * the purchase. Purchases by user 0 - anonymous - are not allowed. Purchases off your own list are allowed.
319 * @return
320 * $output for display.
321 */
322 function _wishlist_item_action_purchase($nid) {
323 global $user;
324
325 if($user->uid == 0) {
326 watchdog('error', 'Anonymous user attempted to purchase item off wishlist: nid=@nid. You can prevent this error by removing wishlist permissions from anonymous accounts on your site', array('@nid' => $nid));
327 return t('Anonymous wishlist purchases are not allowed');
328 }
329
330 $node = node_load($nid);
331
332 drupal_set_title(t('Purchase wishlist item - @title', array('@title' => $node->title)));
333
334 $output = _wishlist_render_view($node, TRUE);
335
336 $output .= drupal_get_form("wishlist_action_purchase_form", $node);
337
338 return $output;
339 }
340
341 /**
342 * Builds the page to display when a user wants to return an item they already pulled off of a user's wishlist
343 *
344 * @param $nid
345 * The node ID for the item where some of the purchased quantity will be returned
346 * @param $wpid
347 * The ID of the wishlist_purchased record to delete.
348 *
349 * @return
350 * a theme("page", ...) ready for display.
351 */
352 function _wishlist_item_action_return($nid, $wpid) {
353 global $user;
354
355 if($user->uid == 0) {
356 watchdog('error', 'Anonymous user attempted to return an item from a wishlist: nid=@nid. You can prevent this error by removing wishlist permissions from anonymous accounts on your site', array('@nid' => $nid));
357 return t("Anonymous wishlist returns are not allowed");
358 }
359
360 $result = db_query(db_rewrite_sql("SELECT p.wishlist_purch_nid, p.wishlist_purch_wid, p.wishlist_purch_buyer_uid, p.wishlist_purch_quantity FROM {wishlist_purchased} p WHERE p.wishlist_purch_wid=%d", "p", "wishlist_purch_wid"), $wpid);
361 if($wishlist_purch = db_fetch_object($result)) {
362 if($nid != $wishlist_purch->wishlist_purch_nid) {
363 return "Inputs do not agree. Node ".$nid." is not related to wpid=".$wpid;
364 }
365 if($user->uid != $wishlist_purch->wishlist_purch_buyer_uid) {
366 return "Inputs do not agree. Node ".$nid."/wpid ".$wpid." was not purchased by user ".$user->uid;
367 }
368 } else {
369 return "Invalid record - item return failed for wpid=".$wpid;
370 }
371
372 // Load all this up just so that we can print a nice information message
373 $node = node_load($nid);
374 $purchaser_account = user_load(array('uid' => $user->uid));
375 $giftee_account = user_load(array('uid' => $node->uid));
376 watchdog('content', '@purchaser_name returned @purchase_quantity of "@node_title" for @giftee_name', array('@purchaser_name' => $purchaser_account->name, '@purchase_quantity' => $wishlist_purch->wishlist_purch_quantity, '@node_title' => $node->title, '@giftee_name' => $giftee_account->name));
377
378 db_query('DELETE FROM {wishlist_purchased} WHERE wishlist_purch_wid = %d', $wpid);
379
380 // Set a successful return and then go back to the node display for this item.
381 drupal_set_message(t('You have placed @purchase_quantity of <i>@node_title</i> back on the wishlist.', array('@purchase_quantity' => $wishlist_purch->wishlist_purch_quantity, '@node_title' => $node->title)));
382 drupal_goto('node/'.$nid);
383 }
384
385 function wishlist_action_purchase_form_validate($form, &$form_state) {
386 $item_action_purchase_quantity = check_plain($form_state['values']['item_action_purchase_quantity']);
387
388 if(!is_numeric($item_action_purchase_quantity)) {
389 form_set_error('item_action_purchase_quantity', t('The purchase quantity must be numeric.'));
390 }
391
392 if($item_action_purchase_quantity < 0) {
393 form_set_error('item_action_purchase_quantity', t('Your purchase quantity must be a positive number, and not zero.'));
394 }
395
396 if($item_action_purchase_quantity == 0) {
397 form_set_error('item_action_purchase_quantity', t('Be serious. Zero of them?'));
398 }
399
400 $node = node_load($form_state['values']['nid']);
401 $items_remaining = $node->item_quantity_requested-_wishlist_get_node_quantity_purchased($form_state['values']['nid']);
402 if($item_action_purchase_quantity > $items_remaining) {
403 form_set_error('item_action_purchase_quantity', t('You cannot purchase more than @items_remaining of this item', array('@items_remaining' => $items_remaining)));
404 }
405
406 }
407
408 /**
409 * Submit handler for for the wishlist purchase form
410 *
411 */
412 function wishlist_action_purchase_form_submit($form, &$form_state) {
413 global $user;
414
415 $node = node_load($form_state['values']['nid']);
416 $purchaser_account = user_load(array('uid' => $user->uid));
417 $giftee_account = user_load(array('uid' => $node->uid));
418
419 // Create the node that tracks what was purchased.
420 db_query("INSERT INTO {wishlist_purchased} (wishlist_purch_nid, wishlist_purch_buyer_uid, wishlist_purch_quantity, purch_date) VALUES
421 (%d, %d, %d, %d)",
422 $node->nid, $purchaser_account->uid, check_plain($form_state['values']['item_action_purchase_quantity']), time());
423
424 // Set a successful return and then go back to the node display for this item.
425 watchdog('content', '@purchaser_name purchased @purchase_quantity of "@node_title" for @giftee_name', array('@purchaser_name' => $purchaser_account->name, '@purchase_quantity' => $item_action_purchase_quantity, '@node_title' => $node->title, '@giftee_name' => $giftee_account->name));
426 drupal_set_message(t('Your intent to buy @item_purchase_quantity of this item has been recorded. Next you should buy the described item at the store/website that is selling it.', array('@item_purchase_quantity' => $item_action_purchase_quantity)));
427 $form_state['redirect'] = 'node/'.$node->nid;
428 }
429
430
431 /*
432 * Returns the form array for the form we display when the user
433 * wants to purchase an item.
434 *
435 * @param $node The node being acted on
436 */
437 function wishlist_action_purchase_form(&$form_state, $node) {
438 $giftee_account = user_load(array('uid' => $node->uid));
439
440 $form["item_action_purchase_quantity"] = array(
441 '#type' => 'textfield',
442 '#title' => t("Quantity that you will purchase"),
443 '#default_value' => $node->item_quantity_requested-$node->item_quantity_purchased,
444 '#size' => 3,
445 '#maxlength' => 3,
446 '#description' => t("Enter the quantity that you intend to purchase for @giftee_name. You should 'purchase' items off the wishlist here before you actually buy the items from the store/website. If you do not, you create the chance for somebody else to get these items instead. Remember, you can always put an item you 'purchase' back on the wishlist.", array('@giftee_name' => $giftee_account->name)),
447 );
448
449 $form['nid'] = array(
450 '#type' => 'hidden',
451 '#value' => $node->nid
452 );
453
454 $form[] = array(
455 '#type' => 'submit',
456 '#value' => t('Purchase'),
457 );
458
459 return $form;
460 }
461
462
463
464
465 ?>

  ViewVC Help
Powered by ViewVC 1.1.2