Source for file form.php
Documentation is available at form.php
* @package FrameworkOnFramework
* @copyright Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
// Protect from unauthorized access
* FOFForm is an extension to JForm which support not only edit views but also
* browse (record list) and read (single record display) views based on XML
* @package FrameworkOnFramework
* The model attached to this view
* The view used to render this form
* Method to get an instance of a form.
* @param string $name The name of the form.
* @param string $data The name of an XML file or string to load as the form definition.
* @param array $options An array of form options.
* @param string $replace Flag to toggle whether form fields should be replaced if a field
* already exists with the same group/name.
* @param string $xpath An optional xpath to search for the fields.
* @return object FOFForm instance.
* @throws InvalidArgumentException if no data provided.
* @throws RuntimeException if the form could not be loaded.
public static function getInstance($name, $data =
null, $options =
array(), $replace =
true, $xpath =
false)
// Reference to array with form instances
// Only instantiate the form if it does not already exist.
if (!isset
($forms[$name]))
throw
new InvalidArgumentException(sprintf('FOFForm::getInstance(name, *%s*)', gettype($data)));
$forms[$name] =
new FOFForm($name, $options);
if ($forms[$name]->load($data, $replace, $xpath) ==
false)
throw
new RuntimeException('FOFForm::getInstance could not load form');
if ($forms[$name]->loadFile($data, $replace, $xpath) ==
false)
throw
new RuntimeException('FOFForm::getInstance could not load file');
* Returns the value of an attribute of the form itself
* @param string $attribute The name of the attribute
* @param mixed $default Optional default value to return
$value =
$this->xml->attributes()->$attribute;
* Loads the CSS files defined in the form, based on its cssfiles attribute
$cssfiles =
explode(',', $cssfiles);
foreach ($cssfiles as $cssfile)
// Support for LESS files
$lessfiles =
explode(',', $lessfiles);
foreach ($lessfiles as $def)
$alt =
(count($parts) >
1) ?
trim($parts[1]) :
null;
* Loads the Javascript files defined in the form, based on its jsfiles attribute
foreach ($jsfiles as $jsfile)
* Returns a reference to the protected $data object, allowing direct
* access to and manipulation of the form's data.
* @return JRegistry The form's data registry
* Attaches a FOFModel to this form
* @param FOFModel &$model The model to attach to the form
public function setModel(FOFModel &$model)
* Returns the FOFModel attached to this form
* Attaches a FOFView to this form
* @param FOFView &$view The view to attach to the form
public function setView(FOFView &$view)
* Returns the FOFView attached to this form
* Method to get an array of FOFFormHeader objects in the headerset.
* @return array The array of FOFFormHeader objects in the headerset.
// If no field elements were found return empty.
// Build the result array from the found field elements.
foreach ($elements as $element)
// Get the field groups for the element.
$attrs =
$element->xpath('ancestor::headers[@name]/@name');
$groups =
array_map('strval', $attrs ?
$attrs :
array());
// If the field is successfully loaded add it to the result array.
$fields[$field->id] =
$field;
* Method to get an array of <header /> elements from the form XML document which are
* in a control group by name.
* @param mixed $group The optional dot-separated form group path on which to find the fields.
* Null will return all fields. False will return fields not in a group.
* @param boolean $nested True to also include fields in nested groups that are inside of the
* group for which to find fields.
* @return mixed Boolean false on error or array of SimpleXMLElement objects.
// Make sure there is a valid JForm XML document.
if (!($this->xml instanceof
SimpleXMLElement))
// Get only fields in a specific group?
// Get the fields elements for a given group.
// Get all of the field elements for the fields elements.
foreach ($elements as $element)
// If there are field elements add them to the return result.
if ($tmp =
$element->xpath('descendant::header'))
// If we also want fields in nested groups then just merge the arrays.
// If we want to exclude nested groups then we need to check each field.
$groupNames =
explode('.', $group);
// Get the names of the groups that the field is in.
$attrs =
$field->xpath('ancestor::headers[@name]/@name');
$names =
array_map('strval', $attrs ?
$attrs :
array());
// If the field is in the specific group then add it to the return list.
if ($names == (array)
$groupNames)
elseif ($group ===
false)
// Get only field elements not in a group.
$fields =
$this->xml->xpath('descendant::headers[not(@name)]/header | descendant::headers[not(@name)]/headerset/header ');
// Get an array of all the <header /> elements.
$fields =
$this->xml->xpath('//header');
* Method to get a header field represented as a FOFFormHeader object.
* @param string $name The name of the header field.
* @param string $group The optional dot-separated form group path on which to find the field.
* @param mixed $value The optional value to use as the default for the field.
* @return mixed The FOFFormHeader object for the field or boolean false on error.
public function getHeader($name, $group =
null, $value =
null)
// Make sure there is a valid FOFForm XML document.
if (!($this->xml instanceof
SimpleXMLElement))
// Attempt to find the field by name and group.
// If the field element was not found return false.
return $this->loadHeader($element, $group, $value);
* Method to get a header field represented as an XML element object.
* @param string $name The name of the form field.
* @param string $group The optional dot-separated form group path on which to find the field.
* @return mixed The XML element object for the field or boolean false on error.
protected function findHeader($name, $group =
null)
// Make sure there is a valid JForm XML document.
if (!($this->xml instanceof
SimpleXMLElement))
// Let's get the appropriate field element based on the method arguments.
// Get the fields elements for a given group.
// Get all of the field elements with the correct name for the fields elements.
foreach ($elements as $element)
// If there are matching field elements add them to the fields array.
if ($tmp =
$element->xpath('descendant::header[@name="' .
$name .
'"]'))
// Make sure something was found.
// Use the first correct match in the given group.
$groupNames =
explode('.', $group);
foreach ($fields as &$field)
// Get the group names as strings for ancestor fields elements.
$attrs =
$field->xpath('ancestor::headerfields[@name]/@name');
$names =
array_map('strval', $attrs ?
$attrs :
array());
// If the field is in the exact group use it and break out of the loop.
if ($names == (array)
$groupNames)
// Get an array of fields with the correct name.
$fields =
$this->xml->xpath('//header[@name="' .
$name .
'"]');
// Make sure something was found.
// Search through the fields for the right one.
foreach ($fields as &$field)
// If we find an ancestor fields element with a group name then it isn't what we want.
if ($field->xpath('ancestor::headerfields[@name]'))
* Method to load, setup and return a FOFFormHeader object based on field data.
* @param string $element The XML element object representation of the form field.
* @param string $group The optional dot-separated form group path on which to find the field.
* @param mixed $value The optional value to use as the default for the field.
* @return mixed The FOFFormHeader object for the field or boolean false on error.
protected function loadHeader($element, $group =
null, $value =
null)
// Make sure there is a valid SimpleXMLElement.
if (!($element instanceof
SimpleXMLElement))
$type =
$element['type'] ? (string)
$element['type'] :
'field';
// Load the JFormField object for the field.
// If the object could not be loaded, get a text field object.
// Setup the FOFFormHeader object.
if ($field->setup($element, $value, $group))
* Proxy for {@link FOFFormHelper::loadFieldType()}.
* @param string $type The field type.
* @param boolean $new Flag to toggle whether we should get a new instance of the object.
* @return mixed FOFFormField object on success, false otherwise.
* Proxy for {@link FOFFormHelper::loadHeaderType()}.
* @param string $type The field type.
* @param boolean $new Flag to toggle whether we should get a new instance of the object.
* @return mixed FOFFormHeader object on success, false otherwise.
* Proxy for {@link FOFFormHelper::loadRuleType()}.
* @param string $type The rule type.
* @param boolean $new Flag to toggle whether we should get a new instance of the object.
* @return mixed JFormRule object on success, false otherwise.
* @see FOFFormHelper::loadRuleType()
* Proxy for {@link FOFFormHelper::addFieldPath()}.
* @param mixed $new A path or array of paths to add.
* @return array The list of paths that have been added.
* Proxy for {@link FOFFormHelper::addHeaderPath()}.
* @param mixed $new A path or array of paths to add.
* @return array The list of paths that have been added.
* Proxy for FOFFormHelper::addFormPath().
* @param mixed $new A path or array of paths to add.
* @return array The list of paths that have been added.
* @see FOFFormHelper::addFormPath()
* Proxy for FOFFormHelper::addRulePath().
* @param mixed $new A path or array of paths to add.
* @return array The list of paths that have been added.
* @see FOFFormHelper::addRulePath()
Documentation generated on Tue, 19 Nov 2013 15:03:36 +0100 by phpDocumentor 1.4.3