Tuesday, January 27, 2009

OT: Sometimes I like spam (Yes, really)

Like most people, I get a moderate amount of spam. Usually annoying, but occasionally I find the subjects amusing and start dreaming up imaginary responses. (Do not ask me why, my sense of humor is just weird that way).

Subject: You Have Won the British Lottery of £x,xxxx,xxx GBP
Response: Wow. So given the economic climate, the current exchange rate, the fact that this is a total scam, how much money are you trying to steal from me?


Subject: Trusting in your urgent reply
Response: My advice, try not to be so gullible. You are bound to be disappointed - a lot.


Subject: I have a problem. Needing your help
Response: Let me guess. You are addicted to spam. You know it is wrong, but you just cannot seem to help yourself.


Subject:
70% off Brand name pharmaceuticals
Response: Sorry. I promised my mother I would never accept pharmaceutical candy from strangers

Subject: Your urgent response needed
Response: Stop spamming me. This instant! Hurry! Hurry!


Subject:
** SPAM** WINNING NOTIFICATION FOR YEAR xxxx
Response: Congratulations. Being selected the top spammer of the year must be quite an honor for you. Your parents must be so proud! (Okay, so maybe that was not the original subject ...but close enough ;-)

Though I do wonder why a large percentage of spam is in capital letters. It is not bad enough they are "spamming" me, they have to shout too? Perhaps they are just excitable people that are a little too in touch with their inner caps lock key.

...Read More

Monday, January 12, 2009

ColdFusion 8 - Multiple Date Selection Experiment with CFCALENDAR (HTML) and FLEX

For some time I have been wondering whether it was possible to select multiple dates with a cfcalendar. Technically, the answer is no.

After working with flash forms, I realized cfcalendar is based on the DateChooser component in Flex 1.5. Unfortunately, some of the niceties like selecting multiple dates or setting a minimum and maximum year displayed, were not introduced until Flex 2. I did eventually manage to simulate multiple date selections in flash forms, through a series of bizarre code gyrations, numerous expletives and light incantations. But having seen the calendar in Flex 2, I knew it was just a poor re-invention of the wheel.

But being the curious sort, I got to wondering if there was a way to use the Flex 2 calendar, from within ColdFusion 8. I began to realize it might be possible after reading a very interesting tip on Sam Farmer's blog about ColdFusion 8 and its Cool Ability to Compile Flex Applications. I could use it to create a swf containing a Flex 2 calendar. Then embed the calendar in my html form. The same way cfform does with cfcalendar. But I was not sure what to do from there.

It turns out there are several methods of communicating with javascript from Flash, such as the ExternalInterface introduced with Flash 8. As I understand it Flex 2 requires Flash 9+ anyway, so no problems there. Using my rudimentary Flex skills, I wrote a simple example that creates a calendar, enables multiple selections, and finally exposes the selectedRanges property to javascript using the addCallback method. (Loosely translated, selectedRanges is an array of structures. Each structure contains two keys: rangeStart and rangeEnd.) I then compiled the flex code, using the tip from Sam Farmer's blog.

<cfimport prefix="cfmxml" taglib="/WEB-INF/lib/cf-bootstrap-for-flex.jar">
<cfmxml:mxml>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:local="*"
backgroundAlpha="0"
backgroundGradientAlphas="[0,0]"
backgroundColor="0xffffff"
creationComplete="onApplicationCreated();"
>
<mx:Script>
<![CDATA[
import flash.external.*;
import mx.controls.Alert;

protected function onApplicationCreated():void
{
if (!ExternalInterface.available)
{
Alert.show("JavaScript is required to use this calendar.", "Error");
return;
}

multiCalendar.allowMultipleSelection = true;

// this allows the actionscript function to be accessed from javascript
ExternalInterface.addCallback("getRanges", getSelectedRanges);
}

public function getSelectedRanges():Array
{
return multiCalendar.selectedRanges;
}
]]>
</mx:Script>

<mx:Style>
Application
{
background-color:"";
background-image:"";
padding: 0px;
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
paddingBottom: 0;
paddingLeft: 0;
paddingRight: 0;
paddingTop: 0;
horizontalAlign: Left;
}
</mx:Style>

<mx:DateChooser id="multiCalendar" width="100%" height="100%"/>
</mx:Application>
</cfmxml:mxml>

That created a randomly named swf in my current directory. With the help of SWFObject, I embedded that swf in my html form. All that was left was to add a javascript function that retrieved and displayed the calendar dates in a javascript alert.


<html>
<head>
<script type="text/javascript" src="swfobject/swfobject.js"></script>
<script type="text/javascript">
// display the selected ranges from the cfcalendar object
function showRanges(id)
{
var ranges = document.getElementById(id).getRanges();
var arr = new Array();

arr.push("Selected ranges for: "+ id);
arr.push(" ");
for (var x in ranges)
{
arr.push("range["+ x +"]===========");
var obj = ranges[x];
for (var y in obj)
{
arr.push(y +"="+ obj[y]);
}
}
alert(arr.join("\n"));
}
</script>
</head>
<body>
<cfset calendarID = "myFlex2Calendar">

<cfoutput>
<script type="text/javascript">
swfobject.embedSWF("jspxxxxxxxxxxxxxx.swf","#calendarID#", "163","181", "9.0.0", "swfobject/expressInstall.swf");
</script>

<!--- container that will hold the final calendar --->
<div id="#calendarID#">
<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" border="0"/></a></p>
</div>
<form>
<input type="button" value="Show Dates" onClick="showRanges('#calendarID#');">
</form>
</cfoutput>
</body>
</html>


Voila! A calendar that allows multiple date selections.




Ultimately, I put the code in a cfc to make it completely dynamic. Then added in all of the basic cfcalendar settings, but also Flex 2 specific properties like the year navigation arrows and disabling multiple date ranges. Then passed those properties to flesh through flashVars. Here is an example of the end result. Ignore the ugly test form. If you look closesly, the calendar on the right has year navigation buttons, which I have always missed in cfcalendar.


Bear in mind my Flex skills are basic at best. So this was more of an experiment than anything else. But overall it was a great learning experience about one of the many things you can do with CF and Flex.

...Read More

Wednesday, January 7, 2009

OT: Favorite twitter of the day

I do not always pay attention to Twitter but a link posted by Todd Rafferty had me shaking my head. Now that is just sad ...

Weak Password Brings 'Happiness' to Twitter Hacker

...Read More

Thursday, January 1, 2009

CFCalendar Tip (Flash): Disable Multiple Date Ranges

Talk about missing the obvious. In a previous entry I mentioned a small tip about disabling individual dates in a cfcalendar using actionscript and the disabledRanges property. As the disabledRanges property is an array, it can also be used to disable multiple date ranges. Now I either missed that fact last time or more likely just could not get it to work at the time ;-)

In any case, the disabledRanges property expects an array of structures. The structure keys being rangeStart and rangeEnd. Here is a simple example that disables both the first and fourth week of December.


<!---
Create two sample ranges
--->
<cfset rangesToDisable = arrayNew(1)>
<cfset range = { start = "2008-12-01", end = "2008-12-05"} >
<cfset arrayAppend(rangesToDisable, range)>
<cfset range = { start = "2008-12-22", end = "2008-12-26"} >
<cfset arrayAppend(rangesToDisable, range)>


<cfoutput>
<cfsavecontent variable="disableRanges">
<!--- convert the CF variable into an actionscript array of Date objects --->
var rangesToDisable:Array = [];
var range:Object;

<!--- loop through each range --->
<cfloop from="1" to="#arrayLen(rangesToDisable)#" index="x">
<cfset currRange = rangesToDisable[x]>
<cfif structKeyExists(currRange, "start") OR structKeyExists(currRange, "end")>
range = {};
<!--- convert the start date to a date/time object --->
<cfif structKeyExists(currRange, "start")>
#ToScript( parseDateTime(currRange.start), "range.rangeStart", false, true )#
</cfif>
<!--- convert the end date to a date/time object --->
<cfif structKeyExists(currRange, "end")>
#ToScript( parseDateTime(currRange.end), "range.rangeEnd", false, true )#
</cfif>
<!--- save the new range --->
rangesToDisable.push(range);
</cfif>
</cfloop>
testCalendar.disabledRanges = rangesToDisable;
</cfsavecontent>
</cfoutput>

<cfform name="testForm" format="flash" onLoad="#disableRanges#">
<cfformitem type="text">Disable multiple Date Ranges</cfformitem>
<cfcalendar name="testCalendar" selecteddate="2008-12-01">
</cfform>


I originally thought it could be done in just two lines of code. The ToScript function is capable of converting arrays, structures and date/time objects. So I thought I could convert the CF variable directly to actionscript like this:


var #toScript(datesToDisable, "ranges", false, true)#;
testCalendar.disabledRanges = ranges;


Unfortunately that did not work because the ToScript function converts the structure keys to lowercase: rangestart and rangeend. That is problematic because the calendar component expects the keys to be in mixed case: rangeStart and rangeEnd. So ultimately the code did nothing because the component could not locate any date ranges. Oh well, I guess that would have been too easy.

...Read More

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep