creating control arrays with qcodo

Lately I’ve been playing around with the qcodo framework. There’s a lot of things to love about qcodo, but the 4 or 5 lines of code required to put together a single control is not one of them.

For instance, if I wanted to create a text box on a page. I could something like this.

class MyCoolPage extends QForm {
protected $txtYourNameGoesHere;
function Form_Create{
$txtYourNameGoesHere = new QLabel($this);
$txtYourNameGoesHere->Name = “NameBox”;
$txtYourNameGoesHere->Text = “Write your name here”;
$txtYourNameGoesHere->Width = 100;
$txtYourNameGoesHere->CssClass = “name-box”;
$txtYourNameGoesHere->Required = TRUE;
}
}

If you just have one or two controls on a page, this is not big deal, but if you have a full page of text boxes and labels this gets unwieldy quickly. One solution for this is to first create an array with details about all the controls you’d like to have on your pages, then create a second control array that contains all of your controls. For instance, you can do something like this:

protected $arrListOfControls = Array();
protected $arrControls = Array();
function Form_Create() {
$arrListOfControls = Array(
‘BillName’ => Array(’Name’ => ‘BillName’, ‘Text’=>’Default Name’, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’addressee’),
‘BillAddress1′ => Array(’Name’ => ‘BillAddress1′, ‘Text’=>’default Address1′, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’street-address’),
‘BillAddress2′ => Array(’Name’ => ‘BillAddress2′, ‘Text’=>’default Address2′, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’street-address’),
‘BillCity’ => Array(’Name’ => ‘BillCity’, ‘Text’=>’default City’, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’locality’),
‘BillZip’ => Array(’Name’ => ‘BillZip’, ‘Text’=>’99999′, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’postal-code’),
‘BillState’ => Array(’Name’ => ‘BillState’, ‘Text’=>’ST’, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’region’),
‘BillPhone’ => Array(’Name’ => ‘BillPhone’, ‘Text’=>’123-452-4444′, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’country-name’),
‘ShipName’ => Array(’Name’ => ‘ShipName’, ‘Text’=>’Default Name’, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’addressee’),
‘ShipAddress1′ => Array(’Name’ => ‘ShipAddress1′, ‘Text’=>’default Address1′, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’street-address’),
‘ShipAddress2′ => Array(’Name’ => ‘ShipAddress2′, ‘Text’=>’default Address2′, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’street-address’),
‘ShipCity’ => Array(’Name’ => ‘ShipCity’, ‘Text’=>’default City’, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’locality’),
‘ShipZip’ => Array(’Name’ => ‘ShipZip’, ‘Text’=>’99998′, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’postal-code’),
‘ShipState’ => Array(’Name’ => ‘ShipState’, ‘Text’=>’TS’, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’region’),
‘ShipPhone’ => Array(’Name’ => ‘ShipPhone’, ‘Text’=>’777-777-7777′, ‘Width’ => 100, ‘Required’ => false, ‘Type’ => ‘Label’, ‘CssClass’=>’country-name’)
foreach ($arrListOfControls as $ctlname => $properties) {
buildTextBoxes ( $arrControls[$ctlname] );
}
}
function buildTextBoxes(&$ctl , array $properties) {
$ctl = new QTextBox($this, $properties['Name']);
$ctl->CssClass = $properties['CssClass'];
$ctl->Width = $properties['Width'];
$ctl->Text = $properties['Text'];
$ctl->BlahBlahBlah = $properties['BlahBlahBlah'];
}

Notice two things of importance here…in the buildTextBoxes function, there is an ampersand…this informs php to pass the whole variable, instead of just the value of the variable. When the function is complete, the new control object dressed up in the properties you provided. Another thing to note is when we create the QTextBox, we specifiy a name for the control. This is important to distinguish the contols in your html source when you view them page…it makes formatting your template easier!

Post a Comment

Your email is never published nor shared. Required fields are marked *