cart.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 3 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, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. use utf8;
  17. AddModuleDescription('cart.pl', 'Cart Extension');
  18. our ($q, %Action, $UserGotoBar, $CookieName);
  19. our ($CartPic, $CartName, %Cart, $ShowCart, @CartOrdered);
  20. my $LOADED_CART_JS;
  21. # ============
  22. # = cart-bfc =
  23. # ============
  24. # This is a simple shopping cart for pages!
  25. # Requires searchpaged-bfc.pl.
  26. # We make a checkbox that onChange, uses Yahoo! UI Cookie 2.6 (note we need 2.6!) routines to set a subcookie.
  27. # We have the cookie "$CartName" (by default $Cookiename . "Cart")
  28. # which holds the actual cart and is managed almost entirely
  29. # in client-side javascript. That means the checkboxes directly control the cookie.
  30. # If you want a little picture of a cart, you can set the URL at $CartPic.
  31. # InitCart loads the cookie values into %Cart. $Cart->{$pagename}=1 if it's in the cart.
  32. # 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.
  33. # We'll need the cookie values for
  34. # action=cart;subaction=show; along with other future subactions (download in latex, bibtex)
  35. # we'll feed this display to a variant of search display.
  36. # I'll have to check oddmuse.pl.
  37. # load Yahoo UI code bit to manage subcookies.
  38. $Action{cart} = \&DoCart;
  39. sub DoCart {
  40. # foreach $key (keys %Cart) {
  41. # push @cart, $key if ($Cart{"$key"});
  42. # }
  43. DoSearch(\@CartOrdered);
  44. }
  45. $UserGotoBar .= '<a href="?action=cart;cache=0">View Cart</a>';
  46. # Manage Cart Routines
  47. #push @MyPrintSearchResultsPrefix, \&PrintCheckboxTableStart;
  48. #push @MyPrintSearchResultsSuffix, \&PrintCheckboxTableEnd;
  49. # I can't hack into Init, so let's tap into InitCookie.
  50. # We also tap into Cookie() to arrange writing out our cleaned up Cart.
  51. *OldInitCookie = *InitCookie;
  52. *InitCookie = *InitCookieAndCart;
  53. # To get a checkbox in the titles of pages, we patch GetHeader.
  54. *OldGetHeader = *GetHeader;
  55. *GetHeader = *GetHeaderAndCart;
  56. sub InitCookieAndCart {
  57. OldInitCookie();
  58. InitCart();
  59. }
  60. sub GetHeaderAndCart {
  61. my ($id, $title, $oldId, $nocache, $status) = @_;
  62. my $result = OldGetHeader(@_);
  63. return ($result) unless ($id);
  64. my $checkbox = MakeCheckbox($id);
  65. $checkbox = qq(<span class="cart-checkbox" style="float:right">$checkbox</span>);
  66. $result =~ s/(<\/h1>)/$checkbox$1/;
  67. return ($result);
  68. }
  69. # We load the contents of our Cart cookie into the global %Cart and @CartOrdered
  70. sub InitCart {
  71. $CartName = $CookieName . "Cart" unless (defined ($CartName) );
  72. my @pairs;
  73. %Cart = ();
  74. @CartOrdered = ();
  75. if ($q->cookie($CartName)) {
  76. # @pairs = split(/&/, $q->cookie($CartName));
  77. @pairs = $q->cookie($CartName);
  78. foreach my $pair (@pairs) {
  79. # my $encodedequals = UrlEncode("=");
  80. my ($name, $val)= split(/\=/, $pair);
  81. $Cart{"$name"}=$val;
  82. push @CartOrdered, $name;
  83. }
  84. }
  85. }
  86. sub PrintCheckboxTableStart {
  87. my ($name, $regex, $text, $type) = @_;
  88. my $html;
  89. $html .= "<table><tr>";
  90. my $checkbox = MakeCheckbox(@_);
  91. $html .= qq(<td valign=top>$checkbox</td>);
  92. $html .= "<td valign=top>";
  93. print $html;
  94. }
  95. sub PrintCheckboxTableEnd {
  96. my ($name, $regex, $text, $type) = @_;
  97. my $html;
  98. $html .= "</td>";
  99. $html .= "</tr></table>";
  100. print $html;
  101. }
  102. sub MakeCheckbox {
  103. my ($name, $regex, $text, $type) = @_;
  104. my $html;
  105. return unless ($ShowCart);
  106. unless ($LOADED_CART_JS) {
  107. $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>';
  108. $LOADED_CART_JS=1;
  109. }
  110. my $selected = qq(checked="yes") if ($Cart{"$name"});
  111. $html .=<<HTMLEND;
  112. $CartPic<input type="checkbox" value="cart" id="$name-set" title="Add To Cart" $selected/> <br>
  113. <script type="text/javascript">
  114. (function(){
  115. YAHOO.util.Event.on("$name-set", "change", function(){
  116. var value = YAHOO.util.Cookie.getSub("$CartName", "$name");
  117. if (value == 1 ) { YAHOO.util.Cookie.removeSub("$CartName", "$name"); }
  118. else { YAHOO.util.Cookie.setSub("$CartName", "$name", 1 ); }
  119. });
  120. })();
  121. </script>
  122. HTMLEND
  123. return $html unless ($q->param('action') eq 'edit' || $q->param('Preview'));
  124. # no checkboxes for edit pages.
  125. return;
  126. }
  127. __END__
  128. =
  129. (0.7) Load JS libraries on first checkbox (so won't load if we are editing).
  130. (0.6) Changed the JS source to be Yahoo's CDN.
  131. (0.51) Use CSS class cart-checkbox for the cart checkbox! That way, we can remove them for printouts, for instance.
  132. (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!
  133. (0.4) Now every page title has a checkbox floated to the right, which controls the cart status.
  134. (0.3) Allow cart editing from cart display. Currently, doesn't seem to affect the cart.
  135. (0.2) Cart now displays.
  136. (0.1) Cart is now persistent and is edited by the checkboxes.