PageRenderTime 46ms CodeModel.GetById 15ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/source/Plug-in/xajax/xajax_controls/form.inc.php

http://prosporous.googlecode.com/
PHP | 314 lines | 167 code | 34 blank | 113 comment | 28 complexity | c9acb898abd58f1fbfd55ba2b83739fb MD5 | raw file
  1<?php
  2/*
  3	File: form.inc.php
  4
  5	HTML Control Library - Form Level Tags
  6
  7	Title: xajax HTML control class library
  8
  9	Please see <copyright.inc.php> for a detailed description, copyright
 10	and license information.
 11*/
 12
 13/*
 14	@package xajax
 15	@version $Id: form.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
 16	@copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
 17	@license http://www.xajaxproject.org/bsd_license.txt BSD License
 18*/
 19
 20/*
 21	Section: Description
 22	
 23	This file contains the class declarations for the following HTML Controls:
 24	
 25	- form
 26	- input, textarea, select, optgroup, option
 27	- label
 28	- fieldset, legend
 29	
 30	The following tags are deprecated as of HTML 4.01, therefore, they will not
 31	be supported:
 32	
 33	- isindex
 34*/
 35
 36class clsForm extends xajaxControlContainer
 37{
 38	function clsForm($aConfiguration=array())
 39	{
 40		if (false == isset($aConfiguration['attributes']))
 41			$aConfiguration['attributes'] = array();
 42		if (false == isset($aConfiguration['attributes']['method']))
 43			$aConfiguration['attributes']['method'] = 'POST';
 44		if (false == isset($aConfiguration['attributes']['action']))
 45			$aConfiguration['attributes']['action'] = '#';
 46
 47		xajaxControlContainer::xajaxControlContainer('form', $aConfiguration);
 48	}
 49}
 50
 51class clsInput extends xajaxControl
 52{
 53	function clsInput($aConfiguration=array())
 54	{
 55		xajaxControl::xajaxControl('input', $aConfiguration);
 56	}
 57}
 58
 59class clsInputWithLabel extends clsInput
 60{
 61	var $objLabel;
 62	var $sWhere;
 63	var $objBreak;
 64	
 65	function clsInputWithLabel($sLabel, $sWhere, $aConfiguration=array())
 66	{
 67		clsInput::clsInput($aConfiguration);
 68		
 69		$this->objLabel =& new clsLabel(array(
 70			'child' => new clsLiteral($sLabel)
 71			));
 72		$this->objLabel->setControl($this);
 73		
 74		$this->sWhere = $sWhere;
 75		
 76		$this->objBreak =& new clsBr();
 77	}
 78	
 79	function printHTML($sIndent='')
 80	{
 81		if ('left' == $this->sWhere || 'above' == $this->sWhere)
 82			$this->objLabel->printHTML($sIndent);
 83		if ('above' == $this->sWhere)
 84			$this->objBreak->printHTML($sIndent);
 85		
 86		clsInput::printHTML($sIndent);
 87
 88		if ('below' == $this->sWhere)
 89			$this->objBreak->printHTML($sIndent);
 90		if ('right' == $this->sWhere || 'below' == $this->sWhere)
 91			$this->objLabel->printHTML($sIndent);
 92	}
 93}
 94
 95/*
 96	Class: clsSelect
 97	
 98	A <xajaxControlContainer> derived class that assists in the construction
 99	of an HTML select control.
100	
101	This control can only accept <clsOption> controls as children.
102*/
103class clsSelect extends xajaxControlContainer
104{
105	/*
106		Function: clsSelect
107		
108		Construct and initialize an instance of the class.  See <xajaxControlContainer>
109		for details regarding the aConfiguration parameter.
110	*/
111	function clsSelect($aConfiguration=array())
112	{
113		xajaxControlContainer::xajaxControlContainer('select', $aConfiguration);
114	}
115	
116	/*
117		Function: addOption
118		
119		Used to add a single option to the options list.
120		
121		sValue - (string):  The value that is returned as the form value
122			when this option is the selected option.
123		sText - (string):  The text that is displayed in the select box when
124			this option is the selected option.
125	*/
126	function addOption($sValue, $sText)
127	{
128		$optionNew =& new clsOption();
129		$optionNew->setValue($sValue);
130		$optionNew->setText($sText);
131		$this->addChild($optionNew);
132	}
133	
134	/*
135		Function: addOptions
136		
137		Used to add a list of options.
138		
139		aOptions - (associative array):  A list of key/value pairs that will
140			be passed to <clsSelect->addOption>.
141	*/
142	function addOptions($aOptions, $aFields=array())
143	{
144		if (0 == count($aFields))
145			foreach ($aOptions as $sValue => $sText)
146				$this->addOption($sValue, $sText);
147		else if (1 < count($aFields))
148			foreach ($aOptions as $aOption)
149				$this->addOption($aOption[$aFields[0]], $aOption[$aFields[1]]);
150		else
151			trigger_error('Invalid list of fields passed to clsSelect::addOptions; should be array of two strings.'
152				. $this->backtrace(),
153				E_USER_ERROR
154				);
155	}
156}
157
158/*
159	Class: clsOptionGroup
160	
161	A <xajaxControlContainer> derived class that can be used around a list of <clsOption>
162	objects to help the user find items in a select list.
163*/
164class clsOptionGroup extends xajaxControlContainer
165{
166	function clsOptionGroup($aConfiguration=array())
167	{
168		xajaxControlContainer::xajaxControlContainer('optgroup', $aConfiguration);
169	}
170	
171	/*
172		Function: addOption
173		
174		Used to add a single option to the options list.
175		
176		sValue - (string):  The value that is returned as the form value
177			when this option is the selected option.
178		sText - (string):  The text that is displayed in the select box when
179			this option is the selected option.
180	*/
181	function addOption($sValue, $sText)
182	{
183		$optionNew =& new clsOption();
184		$optionNew->setValue($sValue);
185		$optionNew->setText($sText);
186		$this->addChild($optionNew);
187	}
188	
189	/*
190		Function: addOptions
191		
192		Used to add a list of options.
193		
194		aOptions - (associative array):  A list of key/value pairs that will
195			be passed to <clsSelect->addOption>.
196	*/
197	function addOptions($aOptions, $aFields=array())
198	{
199		if (0 == count($aFields))
200			foreach ($aOptions as $sValue => $sText)
201				$this->addOption($sValue, $sText);
202		else if (1 < count($aFields))
203			foreach ($aOptions as $aOption)
204				$this->addOption($aOption[$aFields[0]], $aOption[$aFields[1]]);
205		else
206			trigger_error('Invalid list of fields passed to clsOptionGroup::addOptions; should be array of two strings.'
207				. $this->backtrace(),
208				E_USER_ERROR
209				);
210	}
211}
212
213/*
214	Class: clsOption
215	
216	A <xajaxControlContainer> derived class that assists with the construction
217	of HTML option tags that will be assigned to an HTML select tag.
218
219	This control can only accept <clsLiteral> objects as children.
220*/
221class clsOption extends xajaxControlContainer
222{
223	/*
224		Function: clsOption
225		
226		Constructs and initializes an instance of this class.  See <xajaxControlContainer>
227		for more information regarding the aConfiguration parameter.
228	*/
229	function clsOption($aConfiguration=array())
230	{
231		xajaxControlContainer::xajaxControlContainer('option', $aConfiguration);
232	}
233	
234	/*
235		Function: setValue
236		
237		Used to set the value associated with this option.  The value is sent as the
238		value of the select control when this is the selected option.
239	*/
240	function setValue($sValue)
241	{
242		$this->setAttribute('value', $sValue);
243	}
244	
245	/*
246		Function: setText
247		
248		Sets the text to be shown in the select control when this is the 
249		selected option.
250	*/
251	function setText($sText)
252	{
253		$this->clearChildren();
254		$this->addChild(new clsLiteral($sText));
255	}
256}
257
258class clsTextArea extends xajaxControlContainer
259{
260	function clsTextArea($aConfiguration=array())
261	{
262		xajaxControlContainer::xajaxControlContainer('textarea', $aConfiguration);
263		
264		$this->sClass = '%block';
265	}
266}
267
268class clsLabel extends xajaxControlContainer
269{
270	var $objFor;
271
272	function clsLabel($aConfiguration=array())
273	{
274		xajaxControlContainer::xajaxControlContainer('label', $aConfiguration);
275	}
276
277	function setControl(&$objControl)
278	{
279		if (false == is_a($objControl, 'xajaxControl'))
280			trigger_error(
281				'Invalid control passed to clsLabel::setControl(); should be xajaxControl.'
282				. $this->backtrace(),
283				E_USER_ERROR);
284
285		$this->objFor =& $objControl;
286	}
287
288	function printHTML($sIndent='')
289	{
290		$this->aAttributes['for'] = $this->objFor->aAttributes['id'];
291		
292		xajaxControlContainer::printHTML($sIndent);
293	}
294}
295
296class clsFieldset extends xajaxControlContainer
297{
298	function clsFieldset($aConfiguration=array())
299	{
300		xajaxControlContainer::xajaxControlContainer('fieldset', $aConfiguration);
301		
302		$this->sClass = '%block';
303	}
304}
305
306class clsLegend extends xajaxControlContainer
307{
308	function clsLegend($aConfiguration=array())
309	{
310		xajaxControlContainer::xajaxControlContainer('legend', $aConfiguration);
311		
312		$this->sClass = '%inline';
313	}
314}