Thursday, January 19, 2012

dynamically get url or form parameters and set variables or query strings for cflayout calls, ajax binds, iframe or cfincludes, links or in-page use.

I have some 'main' pages which use cflayout to actually hold LOTS of pages. Some are content 'in' the main page inside the cflayout code and some are 'bound' within a cflayoutarea source or ajax call and some are 'cf includes'.

Some might even be a secondary situation, like an iframe inside a cfinclude inside a cflayoutarea bind which is inside the main page which had params passed to it from somewhere. Got all that?! Not saying I'm going to do that, but I might want to.

Because (thanks to cflayout) this holds what amounts to a whole website, directly or indirectly, all being called via the same page technically, it gets complicated.

There are three things I don't 'know' for this page. Partly because there are so many different files involved and calls to them coming from everywhere. Partly because I'm unlikely to remember later every possible detail in the whole site, which is what happens when you do hobby sites at 2am 'cause it ain't your real job. All these unknowns and possibilities need to be handled right up top, generically no matter what their situation, so it's simply all available to me in any form I need it later on in the page, and can even be applied to everything 'generically'.

That way, whatever it is that particular thing needs, will be included in there somewhere (and extra stuff is ignored by what we call).

Things I need to know:

Q: What parameters were passed when I loaded this page and what are their values?

Q: Did those parameters come a form or a url?

Q: Do those need to be written into an inpage var, or conjoined on a url-query string?

Bear in mind var names should be distinct in the site (so one thing coming in for page X, if applied to a query string for page Y, would not affect it), and that any bound/included/called pages using the vars we have here of course must use default cfparams and/or check for existence of their needed params.


<!--- get any and all parameters passed on the url --->
<cfset paramsurl = structKeyList(url)>

<!--- put a parameter in place in a new var, this will be ignored (like 1=1 in SQL) --->
<cfset paramsurllist = "x=0">

<!--- loop through the list of params --->
<cfloop index="ndx01" list="#paramsurl#">

<!-- see it for debug: <cfoutput><div>#ndx01# = #url[ndx01]#</div></cfoutput>-->

<!--- build the query string for use in links if needed --->
<cfset paramsurllist = '#variables.paramsurllist#' & '&#ndx01#=#url[ndx01]#'>

<!--- set the var for use in-page if needed --->
<cfset #ndx01# = "#url[ndx01]#">

</cfloop>

<!-- see it for debug: show me my query string <cfoutput>#paramsurllist#</cfoutput>-->


<!--- get any and all parameters passed on the form --->
<cfset paramsform = structKeyList(form)>

<!--- put a parameter in place in a new var, this will be ignored --->
<cfset paramsformlist = "x=0">

<!--- loop through the list of params --->
<cfloop index="ndx02" list="#paramsform#">

<!-- see it for debug: <cfoutput><div>#ndx02# = #form[ndx02]#</div></cfoutput> -->

<!--- build the query string for use in links if needed --->
<cfset paramsformlist = '#variables.paramsformlist#' & '&#ndx02#=#form[ndx02]#'>

<!--- set the var for use in-page if needed --->
<cfset #ndx02# = "#url[ndx02]#">

</cfloop>

<!-- see it for debug: show me my query string <cfoutput>#paramsformlist#</cfoutput> -->

<!--- now, work out generic all-inclusive string to add as needed to anything "pulled in" --->

<!--- if incoming url params existed --->
<cfif variables.paramsurllist is not 'x=0'>

<!--- make a generic caller param string from the url stuff. --->
<cfset addcallerparams = "#variables.paramsurllist#">

<!--- if instead any params came from a form --->
<cfelseif variables.paramsformlist is not 'x=0'>

<!--- make a generic caller param string from the form stuff. --->
<cfset addcallerparams = "#variables.paramsformlist#">

<!--- worst case just put our default param on there. --->
<cfelse><cfset addcallerparams = "x=0">
</cfif>

Now, every iframe, or cflayoutarea source, or other ajax bind, or a regular link, can simply have "?#variables.addcallerparams#" added to it.

<cfset mylink = "_2012a_daily.cfm?#variables.addcallerparams#">
(then use in context)

And in the cases when using that doesn't help at all, because passing these vars on a source parameter in cflayoutarea apparently doesn't work (you'd think they could mention that...), we already have the variables set in the page in that case. So any incoming content that is bound or cfinclude, will pick up the var's existence and value from there instead.

PJ

Saturday, January 7, 2012

Validate Amazon URL in ColdFusion

All my images are on Amazon S3. Some of my records don't have images there -- or they do, but they don't have thumbnails. In listings, I tried the following things, with the following results.

1. Verify file exists. Doesn't work unless you do relative URL it appears.

2. Use HTTP 'trace'. That didn't work either. Got a 'forbidden' response to everything.

3. Use "IsImage" with the full URL. That didn't work either, it believed everything to be ok.

4. Use HTTP 'head'. Then check for 200 OK response code. That worked!

<cfset myfileref="full-path-to-image-filename-goes-here">
<cfhttp method="head" url="#variables.myfileref#">
<cfif cfhttp.statusCode is not '200 OK'>
<cfset myfileref="#variables.path-to-file#/icon-spacer.gif">

So when variables.myfileref is called it will either be the valid image or the spacer replacement image I have, if needed.


PJ