cart.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Copyright (C) 2008 Eric Hsu <apricotan@gmail.com>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the
  14. # Free Software Foundation, Inc.
  15. # 59 Temple Place, Suite 330
  16. # Boston, MA 02111-1307 USA
  17. use strict;
  18. use v5.10;
  19. use utf8;
  20. AddModuleDescription('cart.pl', 'Cart Extension');
  21. our ($q, %Action, $UserGotoBar, $CookieName);
  22. our ($CartPic, $CartName, %Cart, $ShowCart, @CartOrdered);
  23. my $LOADED_CART_JS;
  24. # ============
  25. # = cart-bfc =
  26. # ============
  27. # This is a simple shopping cart for pages!
  28. # Requires searchpaged-bfc.pl.
  29. # We make a checkbox that onChange, uses Yahoo! UI Cookie 2.6 (note we need 2.6!) routines to set a subcookie.
  30. # We have the cookie "$CartName" (by default $Cookiename . "Cart")
  31. # which holds the actual cart and is managed almost entirely
  32. # in client-side javascript. That means the checkboxes directly control the cookie.
  33. # If you want a little picture of a cart, you can set the URL at $CartPic.
  34. # InitCart loads the cookie values into %Cart. $Cart->{$pagename}=1 if it's in the cart.
  35. # In theory cookies are capped at 4K. Our page names are capped around 90ish chars. That leaves room for 40 maximal names in the cart. Probably enough.
  36. # We'll need the cookie values for
  37. # action=cart;subaction=show; along with other future subactions (download in latex, bibtex)
  38. # we'll feed this display to a variant of search display.
  39. # I'll have to check oddmuse.pl.
  40. # load Yahoo UI code bit to manage subcookies.
  41. $Action{cart} = \&DoCart;
  42. sub DoCart {
  43. # foreach $key (keys %Cart) {
  44. # push @cart, $key if ($Cart{"$key"});
  45. # }
  46. DoSearch(\@CartOrdered);
  47. }
  48. $UserGotoBar .= '<a href="?action=cart;cache=0">View Cart</a>';
  49. # Manage Cart Routines
  50. #push @MyPrintSearchResultsPrefix, \&PrintCheckboxTableStart;
  51. #push @MyPrintSearchResultsSuffix, \&PrintCheckboxTableEnd;
  52. # I can't hack into Init, so let's tap into InitCookie.
  53. # We also tap into Cookie() to arrange writing out our cleaned up Cart.
  54. *OldInitCookie = *InitCookie;
  55. *InitCookie = *InitCookieAndCart;
  56. # To get a checkbox in the titles of pages, we patch GetHeader.
  57. *OldGetHeader = *GetHeader;
  58. *GetHeader = *GetHeaderAndCart;
  59. sub InitCookieAndCart {
  60. OldInitCookie();
  61. InitCart();
  62. }
  63. sub GetHeaderAndCart {
  64. my ($id, $title, $oldId, $nocache, $status) = @_;
  65. my $result = OldGetHeader(@_);
  66. return ($result) unless ($id);
  67. my $checkbox = MakeCheckbox($id);
  68. $checkbox = qq(<span class="cart-checkbox" style="float:right">$checkbox</span>);
  69. $result =~ s/(<\/h1>)/$checkbox$1/;
  70. return ($result);
  71. }
  72. # We load the contents of our Cart cookie into the global %Cart and @CartOrdered
  73. sub InitCart {
  74. $CartName = $CookieName . "Cart" unless (defined ($CartName) );
  75. my @pairs;
  76. %Cart = ();
  77. @CartOrdered = ();
  78. if ($q->cookie($CartName)) {
  79. # @pairs = split(/&/, $q->cookie($CartName));
  80. @pairs = $q->cookie($CartName);
  81. foreach my $pair (@pairs) {
  82. # my $encodedequals = UrlEncode("=");
  83. my ($name, $val)= split(/\=/, $pair);
  84. $Cart{"$name"}=$val;
  85. push @CartOrdered, $name;
  86. }
  87. }
  88. }
  89. sub PrintCheckboxTableStart {
  90. my ($name, $regex, $text, $type) = @_;
  91. my $html;
  92. $html .= "<table><tr>";
  93. my $checkbox = MakeCheckbox(@_);
  94. $html .= qq(<td valign=top>$checkbox</td>);
  95. $html .= "<td valign=top>";
  96. print $html;
  97. }
  98. sub PrintCheckboxTableEnd {
  99. my ($name, $regex, $text, $type) = @_;
  100. my $html;
  101. $html .= "</td>";
  102. $html .= "</tr></table>";
  103. print $html;
  104. }
  105. sub MakeCheckbox {
  106. my ($name, $regex, $text, $type) = @_;
  107. my $html;
  108. return unless ($ShowCart);
  109. unless ($LOADED_CART_JS) {
  110. $html .= '<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo/yahoo-min.js&2.7.0/build/cookie/cookie-min.js&2.7.0/build/event/event-min.js"></script>';
  111. $LOADED_CART_JS=1;
  112. }
  113. my $selected = qq(checked="yes") if ($Cart{"$name"});
  114. $html .=<<HTMLEND;
  115. $CartPic<input type="checkbox" value="cart" id="$name-set" title="Add To Cart" $selected/> <br>
  116. <script type="text/javascript">
  117. (function(){
  118. YAHOO.util.Event.on("$name-set", "change", function(){
  119. var value = YAHOO.util.Cookie.getSub("$CartName", "$name");
  120. if (value == 1 ) { YAHOO.util.Cookie.removeSub("$CartName", "$name"); }
  121. else { YAHOO.util.Cookie.setSub("$CartName", "$name", 1 ); }
  122. });
  123. })();
  124. </script>
  125. HTMLEND
  126. return $html unless ($q->param('action') eq 'edit' || $q->param('Preview'));
  127. # no checkboxes for edit pages.
  128. return;
  129. }
  130. __END__
  131. =
  132. (0.7) Load JS libraries on first checkbox (so won't load if we are editing).
  133. (0.6) Changed the JS source to be Yahoo's CDN.
  134. (0.51) Use CSS class cart-checkbox for the cart checkbox! That way, we can remove them for printouts, for instance.
  135. (0.5) Our hack of cookies was not working cross-platform. We have a mismatch because our attempts to send out a cookie from oddmuse were getting the contents encoded and unreadable for the YUI routines. Instead,we will use removeSub to avoid ever having to send the cookie back from our server!
  136. (0.4) Now every page title has a checkbox floated to the right, which controls the cart status.
  137. (0.3) Allow cart editing from cart display. Currently, doesn't seem to affect the cart.
  138. (0.2) Cart now displays.
  139. (0.1) Cart is now persistent and is edited by the checkboxes.