2015-08-07 02:26:10 +03:00
# Copyright (C) 2008 Eric Hsu <apricotan@gmail.com>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
2016-08-16 14:59:13 +02:00
# the Free Software Foundation; either version 3 of the License, or
2015-08-07 02:26:10 +03:00
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2016-08-16 14:59:13 +02:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-08-07 02:26:10 +03:00
2016-08-16 16:43:21 +02:00
use strict ;
use v5 .10 ;
use utf8 ;
2015-08-07 02:43:46 +03:00
AddModuleDescription ( 'cart.pl' , 'Cart Extension' ) ;
2015-08-07 02:26:10 +03:00
2015-08-07 02:43:46 +03:00
our ( $ q , % Action , $ UserGotoBar , $ CookieName ) ;
our ( $ CartPic , $ CartName , % Cart , $ ShowCart , @ CartOrdered ) ;
my $ LOADED_CART_JS ;
2015-08-07 02:26:10 +03:00
# ============
# = cart-bfc =
# ============
2015-08-07 02:36:35 +03:00
# This is a simple shopping cart for pages!
2015-08-07 02:26:10 +03:00
# Requires searchpaged-bfc.pl.
# We make a checkbox that onChange, uses Yahoo! UI Cookie 2.6 (note we need 2.6!) routines to set a subcookie.
# We have the cookie "$CartName" (by default $Cookiename . "Cart")
2015-08-07 02:36:35 +03:00
# which holds the actual cart and is managed almost entirely
# in client-side javascript. That means the checkboxes directly control the cookie.
2015-08-07 02:26:10 +03:00
# If you want a little picture of a cart, you can set the URL at $CartPic.
2015-08-07 02:36:35 +03:00
# InitCart loads the cookie values into %Cart. $Cart->{$pagename}=1 if it's in the cart.
# 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.
2015-08-07 02:26:10 +03:00
2015-08-07 02:36:35 +03:00
# We'll need the cookie values for
2015-08-07 02:26:10 +03:00
# action=cart;subaction=show; along with other future subactions (download in latex, bibtex)
2015-08-07 02:36:35 +03:00
# we'll feed this display to a variant of search display.
2015-08-07 02:26:10 +03:00
# I'll have to check oddmuse.pl.
2015-08-07 02:36:35 +03:00
# load Yahoo UI code bit to manage subcookies.
2015-08-07 02:26:10 +03:00
2015-08-07 02:36:35 +03:00
$ Action { cart } = \ & DoCart ;
sub DoCart {
# foreach $key (keys %Cart) {
# push @cart, $key if ($Cart{"$key"});
# }
DoSearch ( \ @ CartOrdered ) ;
2015-08-07 02:26:10 +03:00
}
2015-08-07 02:32:07 +03:00
$ UserGotoBar . = '<a href="?action=cart;cache=0">View Cart</a>' ;
2015-08-07 02:26:10 +03:00
# Manage Cart Routines
2015-08-07 02:43:46 +03:00
#push @MyPrintSearchResultsPrefix, \&PrintCheckboxTableStart;
#push @MyPrintSearchResultsSuffix, \&PrintCheckboxTableEnd;
2015-08-07 02:26:10 +03:00
2015-08-07 02:36:35 +03:00
# I can't hack into Init, so let's tap into InitCookie.
2015-08-07 02:26:10 +03:00
# We also tap into Cookie() to arrange writing out our cleaned up Cart.
* OldInitCookie = * InitCookie ;
* InitCookie = * InitCookieAndCart ;
2015-08-07 02:32:07 +03:00
# To get a checkbox in the titles of pages, we patch GetHeader.
* OldGetHeader = * GetHeader ;
* GetHeader = * GetHeaderAndCart ;
2015-08-07 02:26:10 +03:00
sub InitCookieAndCart {
2015-08-07 02:36:35 +03:00
OldInitCookie ( ) ;
InitCart ( ) ;
2015-08-07 02:26:10 +03:00
}
2015-08-07 02:36:35 +03:00
sub GetHeaderAndCart {
my ( $ id , $ title , $ oldId , $ nocache , $ status ) = @ _ ;
my $ result = OldGetHeader ( @ _ ) ;
2015-08-07 02:26:10 +03:00
2015-08-07 02:36:35 +03:00
return ( $ result ) unless ( $ id ) ;
2015-08-07 02:26:10 +03:00
2015-08-07 02:36:35 +03:00
my $ checkbox = MakeCheckbox ( $ id ) ;
$ checkbox = qq( <span class="cart-checkbox" style="float:right">$checkbox</span> ) ;
$ result =~ s/(<\/h1>)/$checkbox$1/ ;
return ( $ result ) ;
2015-08-07 02:26:10 +03:00
}
# We load the contents of our Cart cookie into the global %Cart and @CartOrdered
sub InitCart {
2015-08-07 02:36:35 +03:00
$ CartName = $ CookieName . "Cart" unless ( defined ( $ CartName ) ) ;
my @ pairs ;
% Cart = ( ) ;
@ CartOrdered = ( ) ;
if ( $ q - > cookie ( $ CartName ) ) {
# @pairs = split(/&/, $q->cookie($CartName));
@ pairs = $ q - > cookie ( $ CartName ) ;
2015-08-07 02:43:46 +03:00
foreach my $ pair ( @ pairs ) {
2015-08-07 02:36:35 +03:00
# my $encodedequals = UrlEncode("=");
my ( $ name , $ val ) = split ( /\=/ , $ pair ) ;
$ Cart { "$name" } = $ val ;
push @ CartOrdered , $ name ;
}
}
2015-08-07 02:26:10 +03:00
}
sub PrintCheckboxTableStart {
2015-08-07 02:36:35 +03:00
my ( $ name , $ regex , $ text , $ type ) = @ _ ;
my $ html ;
$ html . = "<table><tr>" ;
my $ checkbox = MakeCheckbox ( @ _ ) ;
$ html . = qq( <td valign=top>$checkbox</td> ) ;
$ html . = "<td valign=top>" ;
print $ html ;
2015-08-07 02:26:10 +03:00
}
sub PrintCheckboxTableEnd {
2015-08-07 02:36:35 +03:00
my ( $ name , $ regex , $ text , $ type ) = @ _ ;
my $ html ;
$ html . = "</td>" ;
$ html . = "</tr></table>" ;
print $ html ;
2015-08-07 02:26:10 +03:00
}
sub MakeCheckbox {
2015-08-07 02:36:35 +03:00
my ( $ name , $ regex , $ text , $ type ) = @ _ ;
my $ html ;
return unless ( $ ShowCart ) ;
unless ( $ LOADED_CART_JS ) {
$ 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>' ;
$ LOADED_CART_JS = 1 ;
}
my $ selected = qq( checked="yes" ) if ( $ Cart { "$name" } ) ;
2015-08-07 02:26:10 +03:00
$ html . = << HTMLEND ;
2015-08-07 02:32:07 +03:00
$ CartPic < input type = "checkbox" value = "cart" id = "$name-set" title = "Add To Cart" $ selected / > <br>
2015-08-07 02:26:10 +03:00
< script type = "text/javascript" >
( function ( ) {
YAHOO . util . Event . on ( "$name-set" , "change" , function ( ) {
var value = YAHOO . util . Cookie . getSub ( "$CartName" , "$name" ) ;
if ( value == 1 ) { YAHOO . util . Cookie . removeSub ( "$CartName" , "$name" ) ; }
2015-08-07 02:36:35 +03:00
else { YAHOO . util . Cookie . setSub ( "$CartName" , "$name" , 1 ) ; }
2015-08-07 02:26:10 +03:00
} ) ;
} ) ( ) ;
</script>
2015-08-07 02:43:46 +03:00
HTMLEND
2015-08-07 02:36:35 +03:00
return $ html unless ( $ q - > param ( 'action' ) eq 'edit' || $ q - > param ( 'Preview' ) ) ;
# no checkboxes for edit pages.
return ;
2015-08-07 02:26:10 +03:00
}
2015-08-07 02:36:35 +03:00
2015-08-07 02:26:10 +03:00
__END__
2015-08-07 02:32:07 +03:00
2015-08-07 02:36:35 +03:00
=
2015-08-07 02:32:07 +03:00
( 0.7 ) Load JS libraries on first checkbox ( so won ' t load if we are editing ) .
2015-08-07 02:36:35 +03:00
( 0.6 ) Changed the JS source to be Yahoo ' s CDN .
2015-08-07 02:32:07 +03:00
( 0.51 ) Use CSS class cart - checkbox for the cart checkbox ! That way , we can remove them for printouts , for instance .
2015-08-07 02:36:35 +03:00
( 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 !
( 0.4 ) Now every page title has a checkbox floated to the right , which controls the cart status .
2015-08-07 02:26:10 +03:00
( 0.3 ) Allow cart editing from cart display . Currently , doesn ' t seem to affect the cart .
( 0.2 ) Cart now displays .
2015-08-07 02:36:35 +03:00
( 0.1 ) Cart is now persistent and is edited by the checkboxes .