Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Intelligently send people back and forth between different versions of
- ## the same page on mobile and full domains depending on
- ## 1) their devices, 2) whether or not the URL has "?siteversion=full" or ?siteversion=mobile
- ## and 3) whether cookies have been set by the other two conditions.
- ##
- ## Logic flowchart (rough) - http://twitpic.com/6e4woa
- ##
- ## This code makes use of ISAAC's mobile detection plugin:
- ## http://geekyplugins.com/dot-cms/mobile-visitor-detection.dot
- ## Your site domains
- #set($fullDomain = "www6.oc.edu")
- #set($mobileDomain = "m.oc.edu")
- ###################### SET VARIABLES DEPENDENT ON CURRENT PAGE ADDRESS
- ## Domain
- #set($requestURL = "$request.getRequestURL()")
- ## Detect if you're on the mobile domain by analyzing the site URL (does it contain my mobile domain?)
- #set($onMobileDomain = $requestURL.matches("^https?:\/\/($mobileDomain).*"))
- #if($onMobileDomain == true)
- #set($otherVersion = "full")
- #set($currentVersion = "mobile")
- #set($otherDomain = $fullDomain)
- #else
- #set($otherVersion = "mobile")
- #set($currentVersion = "full")
- #set($otherDomain = $mobileDomain)
- #end
- ## Page URL
- ## Some kung fu to find the page address if we're in URL mapped content
- #if($URLMapContent)
- #set($currentURLMapContent = $dotcontent.find($URLMapContent.identifier))
- #set($pageUri = $currentURLMapContent.getUrlMap())
- #else
- #set($pageUri = $request.getRequestURI())
- #end
- ## QUERY STRING
- #set($queryString = $request.getQueryString())
- ## Remove "siteversion" variable from query string before passing to another site (to prevent repetition)
- #set($queryString = $queryString.replaceAll("(siteversion=[a-zA-Z]*&?)|(&?siteversion=[a-zA-Z]*)$",""))
- #if($UtilMethods.isSet($queryString))
- #set($queryString = "?$queryString") ## Add the question mark
- #end
- ## If there's already a query string, add siteversion onto it, otherwise make it just siteversion
- #if($UtilMethods.isSet($queryString))
- #set($siteversionquery = "&siteversion=${otherVersion}")
- #else
- #set($siteversionquery = "?siteversion=${otherVersion}")
- #end
- #set($otherUrl = "http://${otherDomain}${pageUri}$!{queryString}") ## The mirror of this page on the other server
- #set($otherUrlwSiteversion = "$otherUrl$!{siteversionquery}") ## The mirror of this page, with siteversion=??? to set a cookie there
- ###################### MACROS
- #macro(redirectme $url)
- ## Keep browsers from remembering this redirect in case people change their mind on the site version
- $response.setHeader("Pragma", "no-cache")
- $response.setHeader("Cache-Control","no-store")
- ## Send me somewhere
- $response.sendRedirect($url)
- #end
- #macro(siteversioncookie $cookievalue)
- ## Get a date 30 days from now to feed to cookie
- #set($now = $date.format('MM/dd/yyyy HH:mm:ss', $date.getDate()))
- #set($now30= $date.getDate().getTime() + 2592000000) ## Add 30 days
- #set($now30 = $date.format('E, dd-MMM-yyyy HH:mm:ss zzz', $now30))
- ## Set the cookie
- $response.setHeader("Set-Cookie","siteversion=$cookievalue;expires=$!{now30};path=/;domain=oc.edu")
- #end
- ## Example usage
- ## #redirectme("http://www.garfield.com")
- ## #siteversioncookie("mobile")
- ## $cookietool.siteversion.value
- ###################### START REDIRECTING
- ## Based on QUERY STRING
- #if($UtilMethods.isSet($request.getParameter("siteversion")))
- #if($request.getParameter("siteversion")==$otherVersion)
- #siteversioncookie($otherVersion)
- #redirectme($otherUrl)
- #elseif($request.getParameter("siteversion")==$currentVersion)
- #siteversioncookie($currentVersion)
- #end
- #end
- ## Based on COOKIE
- #if($cookietool.siteversion.value==$otherVersion && $UtilMethods.isSet($request.getParameter("siteversion")) != true)
- #redirectme($otherUrl)
- #end
- ## Based on device detection
- ## Only detect if we're not doing something fancy with cookies or query strings
- #if($UtilMethods.isSet($cookietool.siteversion.value) != true && $UtilMethods.isSet($request.getParameter("siteversion")) != true)
- ## If it's a mobile device that isn't a tablet, serve mobile
- #if($mobileSite.device.getCapability('is_wireless_device')==true && $mobileSite.device.getCapability('is_tablet')==false)
- #siteversioncookie("mobile")
- #if($currentVersion != "mobile")
- #redirectme($otherUrlwSiteversion)
- #end
- #else
- #siteversioncookie("full")
- #if($currentVersion != "full")
- #redirectme($otherUrlwSiteversion)
- #end
- #end
- #end
- ###################### SET VARIABLE THAT CHANGES TEMPLATE
- ## If we're on the mobile domain, show the mobile version of the site
- #set($useMobileVersion = $onMobileDomain)
- ###################### EXAMPLE USAGE IN PAGE
- #*
- #if($useMobileVersion)
- Mobile version template code here
- <a href="$otherUrlwSiteversion">Full Site</a>
- #else
- Full version template code here
- <a href="$otherUrlwSiteversion">Mobile Site</a>
- #end
- *#
Advertisement
Add Comment
Please, Sign In to add comment