/*
File: BulletinBoard.as
Revision: 2.0.0
Purpose: Reads and posts entries from mySQL database via PHP Remoting
Authors: Jesse Stratford, jesse@actionscript.org - AS1 version
Patrick Mineault, patrick@5etdemi.com - AS2 version
Created: 1 March, 2003
Edited: 18 January, 2006
License: Free for commercial and non-commercial use provided this information
remains unchanged.
Notes: For more information on how to use this file, see the relevant tutorial
at www.actionscript.org
Functions are named in the following way:
do{something} : call a remote function
handle{something} : Remote function callback
on{something} : UI callback (button clicks, etc.)
*/
//Import Remoting classes
import mx.remoting.*;
import mx.remoting.debug.*;
import mx.rpc.*;
//The helpful Delegate for UI component event listeners
import mx.utils.Delegate;
//For customizing the datagrid columns
import mx.controls.gridclasses.DataGridColumn;
//For data verification
import mx.controls.Alert;
class BulletinBoard
{
//A reference to our root movie
var root:MovieClip;
//The offset the currently viewed post
var offset:Number = 0;
//The location of the gateway and the name of the service
//static var GATEWAY_URL:String = "http://localhost/amfphp/gateway.php"
//static var GATEWAY_URL:String = "http://flashservices/gateway.php"
//Note: if running into issues with your local service, try amfphp's
//public service instead
static var GATEWAY_URL:String = "/amfphp/gateway.php"
static var SERVICE_NAME:String = "BulletinBoard"
//Max number of posts per request
static var MESSAGES_PER_PAGE:Number = 10;
//The service reference
var service:Service;
function BulletinBoard(root:MovieClip)
{
//Remember the root
this.root = root;
//Start doing the magic
init();
}
///////////////////////////////////////////////////////////////////////////
// Remoting-related methods (interesting stuff)
///////////////////////////////////////////////////////////////////////////
//This function creates the service, calls doRead and waits for results
function init()
{
//Start the NetConnection debugger
NetDebug.initialize();
//Create the service
service = new Service(GATEWAY_URL, null, SERVICE_NAME);
//Read the first set of posts
doRead(0);
//Make the UI work (boring)
initUI();
}
//Ask the remote server to read some posts
function doRead(start:Number)
{
//arg1: message start, arg2: message offset
var pc:PendingCall = service.doRead(start, MESSAGES_PER_PAGE);
pc.responder = new RelayResponder(this, 'handleDoRead', null);
//Remember the offset
offset = start;
}
//Ask the remote server to insert a post
function doPost(info:Object)
{
var pc:PendingCall = service.doPost(info);
pc.responder = new RelayResponder(this, 'handleDoPost', null);
}
//Get data back from server, show the post
function handleDoRead(re:ResultEvent)
{
//Step 1: Take the returned RecordSet, and bind it to the datagrid
root.readMessages.dgMessages.dataProvider = RecordSet(re.result);
//There is no step 2.... Mouhahah!
trace (RecordSet(re.result));
//Reset the selectedIndex of the datagrid to 0
root.readMessages.dgMessages.selectedIndex = 0;
//Fake as though the datagrid was clicked
onDgChange({target:root.readMessages.dgMessages});
//Enable/disable next and previous buttons
root.readMessages.btnPrev.enabled = offset > 0;
root.readMessages.btnNext.enabled = re.result.length == MESSAGES_PER_PAGE;
}
//Insert request was answered, hide the post interface
function handleDoPost(re:ResultEvent)
{
resetPost(); //Clear for next time
hidePost(); //Hide interface
//Refresh posts
doRead(0);
}
//Called when datagrid is clicked. Inspect the data and show
//it in the textarea below
function onDgChange(evtObj:Object)
{
var datagrid = evtObj.target;
var data = datagrid.dataProvider.getItemAt(datagrid.selectedIndex);
if(data == null)
{
root.readMessages.txtMessage.text = ""; //If empty, clear textarea
}
else
{
root.readMessages.txtMessage.text =
'' + data.message + '
----------------------------------------
' +
'Posted by ' + data.name + '' +
' of ' + data.url + '' +
' at '+data.date + ''; //Fill in the textarea
}
}
//Submit a post to the remote server
function onPost(evtObj:Object)
{
//Gather info from UI
var message = new Object();
message.name = root.postMessages.txtName.text;
message.email = root.postMessages.txtEmail.text;
message.theurl = root.postMessages.txtUrl.text;
message.message = root.postMessages.txtMessage.text;
//Check if data was input
if(message.name == '' || message.email == '')
{
Alert.show('Please input name and email', 'Validation Error', Alert.OK);
return;
}
//Now submit
doPost(message);
}
///////////////////////////////////////////////////////////////////////////
// UI-related methods (not-so interesting stuff)
///////////////////////////////////////////////////////////////////////////
function onPrev()
{
doRead(offset - MESSAGES_PER_PAGE); //Read the previous set of posts
}
function onNext()
{
doRead(offset + MESSAGES_PER_PAGE); //Read the next set of posts
}
function onRefresh()
{
doRead(offset); //Refresh the UI
}
function onNewPost()
{
root.postMessages._visible = true; //Make the post interface appear
}
function onReset()
{
resetPost(); //Reset the post interface
}
function onCancel()
{
hidePost(); //Hide the post interface on cancel
}
function initUI()
{
//Hide the insert insterface
root.postMessages._visible = false;
//Add an event listener so clicking the datagrid will show the
//message in the textarea below
root.readMessages.dgMessages.addEventListener('change', Delegate.create(this, onDgChange) );
//Add various button click handlers
root.readMessages.btnPrev.addEventListener('click', Delegate.create(this, onPrev));
root.readMessages.btnNext.addEventListener('click', Delegate.create(this, onNext));
root.readMessages.btnRefresh.addEventListener('click', Delegate.create(this, onRefresh));
root.readMessages.btnNew.addEventListener('click', Delegate.create(this, onNewPost));
//Same for the post interface
root.postMessages.btnPost.addEventListener('click', Delegate.create(this, onPost));
root.postMessages.btnReset.addEventListener('click', Delegate.create(this, onReset));
root.postMessages.btnCancel.addEventListener('click', Delegate.create(this, onCancel));
//Stop clicks from going through background
root.postMessages.background.onPress = Delegate.create(this, null);
root.postMessages.background.useHandCursor = false;
//Set some styles
_global.style.setStyle('themeColor', 'haloBlue');
_global.style.setStyle('fontFamily', 'Verdana');
_global.style.setStyle('fontSize', 10);
//Set the columns for the datagrid
var dgc:DataGridColumn = new DataGridColumn();
dgc.headerText = 'Posted';
dgc.columnName = 'date';
dgc.width = 140;
root.readMessages.dgMessages.addColumn(dgc);
var dgc:DataGridColumn = new DataGridColumn();
dgc.headerText = 'Author';
dgc.columnName = 'name';
dgc.width = 120;
root.readMessages.dgMessages.addColumn(dgc);
var dgc:DataGridColumn = new DataGridColumn();
dgc.headerText = 'Snippet';
dgc.columnName = 'message';
dgc.width = 240;
root.readMessages.dgMessages.addColumn(dgc);
root.readMessages.dgMessages.setStyle('hGridLines', true);
root.readMessages.dgMessages.setStyle('hGridLineColor', 0xdddddd);
//Set the message text field to html mode, set the font
var styles:TextField.StyleSheet = new TextField.StyleSheet();
styles.setStyle("html", {fontFamily:"Verdana,Arial,Helvetica,sans-serif", fontSize:"11px"});
root.readMessages.txtMessage.html = true;
root.readMessages.txtMessage.styleSheet = styles;
}
//Clear the post interface fields
function resetPost()
{
root.postMessages.txtName.text = "";
root.postMessages.txtEmail.text = "";
root.postMessages.txtUrl.text = "http://";
root.postMessages.txtMessage.text = "";
}
//Hide the post interface
function hidePost()
{
root.postMessages._visible = false;
}
}