PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/contentmanager/code/trunk/administrator/components/com_contentmanager/libraries/jxtended/form/form.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 924 lines | 394 code | 111 blank | 419 comment | 76 complexity | a214648258cb7ffebc881a64fc3e2e7f MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: form.php 160 2009-07-09 00:06:09Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Form
  6. * @copyright Copyright (C) 2008 - 2009 JXtended, LLC. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://jxtended.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Form Class for JXtended Libraries.
  13. *
  14. * This class implements a robust API for constructing, populating,
  15. * filtering, and validating forms. It uses XML definitions to
  16. * construct form fields and a variety of field and rule classes
  17. * to render and validate the form.
  18. *
  19. * <code>
  20. * <?php
  21. * jximport2('jxtended.form.form');
  22. * $form = &JXForm::getInstance({XML FILE}, {OPTIONS});
  23. * ?>
  24. * </code>
  25. *
  26. * @package JXtended.Libraries
  27. * @subpackage Forms
  28. * @version 2.0
  29. */
  30. class JXForm extends JObject
  31. {
  32. /**
  33. * The form action URL.
  34. *
  35. * @access private
  36. * @since 2.0
  37. * @var string
  38. */
  39. var $_action = null;
  40. /**
  41. * The form id.
  42. *
  43. * @access private
  44. * @since 2.0
  45. * @var string
  46. */
  47. var $_id = null;
  48. /**
  49. * The form name.
  50. *
  51. * @access private
  52. * @since 2.0
  53. * @var string
  54. */
  55. var $_name = null;
  56. /**
  57. * Flag to indicate a multi-part form for uploads.
  58. *
  59. * @access private
  60. * @since 2.0
  61. * @var boolean
  62. */
  63. var $_multipart = false;
  64. /**
  65. * The form fieldsets as XML objects.
  66. *
  67. * @access private
  68. * @since 2.0
  69. * @var array
  70. */
  71. var $_fieldsets = array();
  72. /**
  73. * Method to get an instance of a form.
  74. *
  75. * @access public
  76. * @param string $file The name of the form to load.
  77. * @param array $options An array of options to pass to the form.
  78. * @return object A JXForm instance.
  79. * @since 2.0
  80. */
  81. function &getInstance($file, $options = array())
  82. {
  83. static $instances = null;
  84. if ($instances == null) {
  85. $instances = array();
  86. }
  87. // Only load the form once.
  88. if (!isset($instances[$file]))
  89. {
  90. // Instantiate the form.
  91. $instances[$file] = new JXForm($options);
  92. $instances[$file]->load($file, true, true);
  93. }
  94. return $instances[$file];
  95. }
  96. /**
  97. * Method to construct the object on instantiation.
  98. *
  99. * @access protected
  100. * @param array $options An array of form options.
  101. * @return void
  102. * @since 2.0
  103. */
  104. function __construct($options = array())
  105. {
  106. // Set the options if specified.
  107. $this->_action = array_key_exists('action', $options) ? $options['action'] : null;
  108. $this->_id = array_key_exists('id', $options) ? $options['id'] : null;
  109. $this->_multipart = array_key_exists('multipart', $options) ? $options['multipart'] : false;
  110. $this->_name = array_key_exists('name', $options) ? $options['name'] : 'jxform';
  111. }
  112. /**
  113. * Method to bind data to the form fields.
  114. *
  115. * @access public
  116. * @param mixed $data An array or object of form values.
  117. * @param string $group The group to bind the fields to.
  118. * @return boolean True on success, false otherwise.
  119. * @since 2.0
  120. */
  121. function bind($data, $group = null)
  122. {
  123. if (!is_object($data) && !is_array($data)) {
  124. return false;
  125. }
  126. // Iterate through the fieldsets.
  127. foreach ($this->_fieldsets as $fieldset => $fields)
  128. {
  129. // Bind if no group is specified or if the group matches the current fieldset.
  130. if ($group === null || ($group !== null && $fieldset === $group))
  131. {
  132. // Iterate through the values.
  133. foreach((array)$data as $k => $v)
  134. {
  135. // Bind the value to the field if it exists.
  136. if (isset($this->_fieldsets[$fieldset][$k]) && is_object($this->_fieldsets[$fieldset][$k])) {
  137. $this->_fieldsets[$fieldset][$k]->setData($v);
  138. }
  139. }
  140. }
  141. }
  142. return true;
  143. }
  144. /**
  145. * Method to load the form description from a file or string.
  146. *
  147. * If $data is a file name, $file must be set to true. The reset option
  148. * works on a group basis. If the XML file or string references groups
  149. * that have already been created they will be replaced with the fields
  150. * in the new file unless the $reset parameter has been set to false.
  151. *
  152. * @access public
  153. * @param string $data The name of an XML file or an XML string.
  154. * @param string $file Flag to toggle whether the $data is a file path or a string.
  155. * @param string $reset Flag to toggle whether the form description should be reset.
  156. * @return boolean True on success, false otherwise.
  157. * @since 2.0
  158. */
  159. function load($data, $file = true, $reset = true)
  160. {
  161. $return = false;
  162. // Make sure we have data.
  163. if (!empty($data))
  164. {
  165. // Get the XML parser and load the data.
  166. $parser = &JFactory::getXMLParser('Simple');
  167. // If the data is a file, load the XML from the file.
  168. if ($file)
  169. {
  170. // If we were not given the absolute path of a form file, attempt to find one.
  171. if (!file_exists($data)) {
  172. jimport('joomla.filesystem.path');
  173. $data = JPath::find(JXForm::addFormPath(), strtolower($data).'.xml');
  174. }
  175. // Attempt to load the XML file.
  176. $loaded = $parser->loadFile($data);
  177. }
  178. // Load the data as a string.
  179. else {
  180. $loaded = $parser->loadString($data);
  181. }
  182. // Make sure the XML was loaded.
  183. if ($loaded)
  184. {
  185. // Check if any fieldsets exist.
  186. if (isset($parser->document->fields))
  187. {
  188. // Load the form fieldsets.
  189. foreach ($parser->document->fields as $fieldset)
  190. {
  191. $this->loadFieldsXML($fieldset, $reset);
  192. $return = true;
  193. }
  194. }
  195. // Check if an action is set.
  196. if ($parser->document->attributes('action') && $reset) {
  197. $this->setAction($parser->document->attributes('action'));
  198. }
  199. // Check if a name is set.
  200. if ($parser->document->attributes('name') && $reset) {
  201. $this->setName($parser->document->attributes('name'));
  202. }
  203. // Check if an id is set.
  204. if ($parser->document->attributes('id') && $reset) {
  205. $this->setId($parser->document->attributes('id'));
  206. }
  207. }
  208. }
  209. return $return;
  210. }
  211. /**
  212. * Method to filter an array of data based on the form fields.
  213. *
  214. * @access public
  215. * @param array $data The data to filter.
  216. * @param string $group An optional group to limit the filtering to.
  217. * @return array An array of filtered data.
  218. * @since 2.0
  219. */
  220. function filter($data, $group = null)
  221. {
  222. $return = array();
  223. // Static input filters for specific settings
  224. static $noHtmlFilter = null;
  225. static $safeHtmlFilter = null;
  226. // Get the safe HTML filter if not set.
  227. if (is_null($safeHtmlFilter)) {
  228. $safeHtmlFilter = &JFilterInput::getInstance(null, null, 1, 1);
  229. }
  230. // Get the no HTML filter if not set.
  231. if (is_null($noHtmlFilter)) {
  232. $noHtmlFilter = &JFilterInput::getInstance(/* $tags, $attr, $tag_method, $attr_method, $xss_auto */);
  233. }
  234. // Iterate through the fieldsets.
  235. foreach ($this->_fieldsets as $fieldset => $fields)
  236. {
  237. // Filter if no group is specified or if the group matches the current fieldset.
  238. if ($group === null || ($group !== null && $fieldset === $group))
  239. {
  240. // Filter the fields.
  241. foreach ($fields as $name => $field)
  242. {
  243. // Get the field information.
  244. $filter = $field->attributes('filter');
  245. // Check for a value to filter.
  246. if (isset($data[$name]))
  247. {
  248. // Handle the different filter options.
  249. switch (strtoupper($filter))
  250. {
  251. case 'RAW':
  252. // No Filter.
  253. $return[$name] = $data[$name];
  254. break;
  255. case 'SAFEHTML':
  256. // Filter safe HTML.
  257. $return[$name] = $safeHtmlFilter->clean($data[$name], $filter);
  258. break;
  259. default:
  260. // Check for a callback filter.
  261. if (function_exists($filter)) {
  262. // Filter using the callback.
  263. $return[$name] = call_user_func($filter, $data[$name]);
  264. } else {
  265. // Filter out HTML.
  266. $return[$name] = $noHtmlFilter->clean($data[$name], $filter);
  267. }
  268. break;
  269. }
  270. }
  271. }
  272. }
  273. }
  274. return $return;
  275. }
  276. /**
  277. * Method to validate form data.
  278. *
  279. * Validation warnings will be pushed into JXForm::_errors and should be
  280. * retrieved with JXForm::getErrors() when validate returns boolean false.
  281. *
  282. * @access public
  283. * @param array $data An array of field values to validate.
  284. * @param string $group An option group to limit the validation to.
  285. * @return mixed Boolean on success, JException on error.
  286. * @since 2.0
  287. */
  288. function validate($data, $group = null)
  289. {
  290. $return = true;
  291. $data = (array)$data;
  292. // Check if the group exists.
  293. if ($group !== null && !isset($this->_fieldsets[$group])) {
  294. // The group that was supposed to be filtered does not exist.
  295. return new JException(JText::sprintf('JX_LIBRARIES_FORM_VALIDATOR_GROUP_NOT_FOUND', $group), 0, E_ERROR);
  296. }
  297. // Get a validator object.
  298. jximport2('jxtended.form.validator');
  299. $validator = new JXFormValidator();
  300. // Iterate through the fieldsets.
  301. foreach ($this->_fieldsets as $fieldset => $fields)
  302. {
  303. // Filter if no group is specified or if the group matches the current fieldset.
  304. if ($group === null || ($group !== null && $fieldset === $group))
  305. {
  306. // Run the validator over the group.
  307. $results = $validator->validate($this->_fieldsets[$fieldset], $data);
  308. // Check for a error.
  309. if (JError::isError($results) && $results->level === E_ERROR) {
  310. return new JException($results->getMessage(), 0, E_ERROR);
  311. }
  312. // Check the validation results.
  313. if (count($results))
  314. {
  315. // Get the validation messages.
  316. foreach ($results as $result) {
  317. if (JError::isError($result) && $result->level === E_WARNING) {
  318. $this->setError($result);
  319. $return = false;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. return $return;
  326. }
  327. /**
  328. * Method to get the form action URL.
  329. *
  330. * @access public
  331. * @param string $default The default form action URL if it is not set.
  332. * @return string The form action URL.
  333. * @since 2.0
  334. */
  335. function getAction($default = null)
  336. {
  337. return !is_null($this->_action) ? $this->_action : $default;
  338. }
  339. /**
  340. * Method to set the form action URL.
  341. *
  342. * @access public
  343. * @param string $action The form action URL.
  344. * @return void
  345. * @since 2.0
  346. */
  347. function setAction($action)
  348. {
  349. $this->_action = $action;
  350. }
  351. /**
  352. * Method to get the form id.
  353. *
  354. * @access public
  355. * @param string $default The default form id if it is not set.
  356. * @return string The form id.
  357. * @since 2.0
  358. */
  359. function getId($default = null)
  360. {
  361. return !is_null($this->_id) ? $this->_id : $default;
  362. }
  363. /**
  364. * Method to set the form id.
  365. *
  366. * @access public
  367. * @param string $value The new form id.
  368. * @return void
  369. * @since 2.0
  370. */
  371. function setId($value)
  372. {
  373. $this->_id = $value;
  374. }
  375. /**
  376. * Method to get the form name.
  377. *
  378. * @access public
  379. * @param string $default The default form name if it is not set.
  380. * @return string The form name.
  381. * @since 2.0
  382. */
  383. function getName($default = null)
  384. {
  385. return !is_null($this->_name) ? $this->_name : $default;
  386. }
  387. /**
  388. * Method to set the form name.
  389. *
  390. * @access public
  391. * @param string $value The new form name.
  392. * @return void
  393. * @since 2.0
  394. */
  395. function setName($value)
  396. {
  397. $this->_name = $value;
  398. }
  399. /**
  400. * Method to add a field to a group.
  401. *
  402. * @access public
  403. * @param object $field The field object to add.
  404. * @param string $group The group to add the field to.
  405. * @return void
  406. * @since 2.0
  407. */
  408. function addField(&$field, $group = '_default')
  409. {
  410. // Add the field to the group.
  411. $this->_fieldsets[$group][$field->attributes('name')] = &$field;
  412. }
  413. /**
  414. * Method to add an array of fields to a group.
  415. *
  416. * @access public
  417. * @param array $fields An array of field objects to add.
  418. * @param string $group The group to add the fields to.
  419. * @return void
  420. * @since 2.0
  421. */
  422. function addFields(&$fields, $group = '_default')
  423. {
  424. // Add the fields to the group.
  425. foreach ($fields as $field) {
  426. $this->_fieldsets[$group][$field->attributes('name')] = $field;
  427. }
  428. }
  429. /**
  430. * Method to get a form field.
  431. *
  432. * @access public
  433. * @param string $name The name of the form field.
  434. * @param string $group The group the field is in.
  435. * @param mixed $controlName The control name of the field.
  436. * @return object Rendered Form Field object
  437. * @since 2.0
  438. */
  439. function getField($name, $group = '_default', $controlName = 'jxform')
  440. {
  441. // Get the XML node.
  442. $node = isset($this->_fieldsets[$group][$name]) ? $this->_fieldsets[$group][$name] : null;
  443. if (empty($node)) {
  444. return false;
  445. }
  446. // Get the field info.
  447. $type = $node->attributes('type');
  448. $data = $node->data();
  449. $value = !empty($data) ? $data : $node->attributes('default');
  450. // Load the field.
  451. $field = &$this->loadFieldType($type);
  452. // If the field could not be loaded, get a text field.
  453. if ($field === false) {
  454. $field = &$this->loadFieldType('text');
  455. }
  456. // Render the field.
  457. return $field->render($node, $value, $controlName);
  458. }
  459. /**
  460. * Method to replace a field in a group.
  461. *
  462. * @access public
  463. * @param object $field The field object to replace.
  464. * @param string $group The group to replace the field in.
  465. * @return void
  466. * @since 2.0
  467. */
  468. function setField(&$field, $group = '_default')
  469. {
  470. $return = false;
  471. // Add the fields to the group if it exists.
  472. if (isset($this->_fieldsets[$group][$field->attributes('name')])) {
  473. $this->_fieldsets[$group][$field->attributes('name')] = $field;
  474. $return = true;
  475. }
  476. return $return;
  477. }
  478. /**
  479. * Method to get the fields in a group.
  480. *
  481. * @access public
  482. * @param string $controlName The control name for the form field group
  483. * @param string $group The form field group
  484. * @return array Associative array of rendered Form Field object by field name
  485. * @since 2.0
  486. */
  487. function getFields($group = '_default', $controlName = 'jxform')
  488. {
  489. $results = array();
  490. // Check if the group exists.
  491. if (isset($this->_fieldsets[$group]))
  492. {
  493. // Get the fields in the group.
  494. foreach ($this->_fieldsets[$group] as $name => $node)
  495. {
  496. // Get the field info.
  497. $type = $node->attributes('type');
  498. $data = $node->data();
  499. $value = !empty($data) ? $data : $node->attributes('default');
  500. // Load the field.
  501. $field = &$this->loadFieldType($type);
  502. // If the field could not be loaded, get a text field.
  503. if ($field === false) {
  504. $field = &$this->loadFieldType('text');
  505. }
  506. // Render the field.
  507. $results[$name] = $field->render($node, $value, $controlName);
  508. }
  509. }
  510. return $results;
  511. }
  512. /**
  513. * Method to assign an array of fields to a group.
  514. *
  515. * @access public
  516. * @param array $fields An array of field objects to assign.
  517. * @param string $group The group to assign the fields to.
  518. * @return void
  519. * @since 2.0
  520. */
  521. function setFields(&$fields, $group = '_default')
  522. {
  523. // Reset the fields group,
  524. $this->_fieldsets[$group] = array();
  525. // Add the fields to the group.
  526. foreach ($fields as $field) {
  527. $this->_fieldsets[$group][$field->attributes('name')] = $field;
  528. }
  529. }
  530. /**
  531. * Method to get a list of fieldset groups.
  532. *
  533. * @access public
  534. * @return array An array of fieldset groups.
  535. * @since 2.0
  536. */
  537. function getGroups()
  538. {
  539. return array_keys($this->_fieldsets);
  540. }
  541. /**
  542. * Method to remove a fieldset group.
  543. *
  544. * @access public
  545. * @param string $group The fieldset group to remove.
  546. * @return void
  547. * @since 2.0
  548. */
  549. function removeGroup($group)
  550. {
  551. unset($this->_fieldsets[$group]);
  552. }
  553. /**
  554. * Method to get the input control for a field.
  555. *
  556. * @access public
  557. * @param string The field name.
  558. * @param string The control name for the form field.
  559. * @param string The group the field is in.
  560. * @param mixed The optional value to render as the default for the field.
  561. * @return string The form field input control.
  562. * @since 2.0
  563. */
  564. function getInput($name, $controlName = 'jxform', $group = '_default', $value = null)
  565. {
  566. // Get the XML node.
  567. $node = isset($this->_fieldsets[$group][$name]) ? $this->_fieldsets[$group][$name] : null;
  568. // If there is no XML node for the given field name, return false.
  569. if (empty($node)) {
  570. return false;
  571. }
  572. // Load the field type.
  573. $type = $node->attributes('type');
  574. $field = & $this->loadFieldType($type);
  575. // If the field could not be loaded, get a text field.
  576. if ($field === false) {
  577. $field = & $this->loadFieldType('text');
  578. }
  579. // Get the value for the form field.
  580. if ($value === null) {
  581. $data = $node->data();
  582. $value = !empty($data) ? $data : $node->attributes('default');
  583. }
  584. // Render the field label.
  585. $input = $field->fetchField($name, $value, $node, $controlName);
  586. return $input;
  587. }
  588. /**
  589. * Method to get the label for a field.
  590. *
  591. * @access public
  592. * @param string The field name.
  593. * @param string The control name for the form field.
  594. * @param string The group the field is in.
  595. * @return string The form field label.
  596. * @since 2.0
  597. */
  598. function getLabel($name, $controlName = 'jxform', $group = '_default')
  599. {
  600. // Get the XML node.
  601. $node = isset($this->_fieldsets[$group][$name]) ? $this->_fieldsets[$group][$name] : null;
  602. // If there is no XML node for the given field name, return false.
  603. if (empty($node)) {
  604. return false;
  605. }
  606. // Load the field type.
  607. $type = $node->attributes('type');
  608. $field = & $this->loadFieldType($type);
  609. // If the field could not be loaded, get a text field.
  610. if ($field === false) {
  611. $field = & $this->loadFieldType('text');
  612. }
  613. // Get some field attributes for rendering the label.
  614. $label = $node->attributes('label');
  615. $descr = $node->attributes('description');
  616. // Make sure we have a valid label.
  617. $label = $label ? $label : $name;
  618. // Render the field label.
  619. $label = $field->fetchLabel($label, $descr, $node, $controlName, $name);
  620. return $label;
  621. }
  622. /**
  623. * Method to get the value of a field.
  624. *
  625. * @access public
  626. * @param string $field The field to set.
  627. * @param mixed $default The default value of the field if empty.
  628. * @param string $group The group the field is in.
  629. * @return boolean The value of the field or the default value if empty.
  630. * @since 2.0
  631. */
  632. function getValue($field, $default = null, $group = '_default')
  633. {
  634. $return = null;
  635. // Get the field value if it exists.
  636. if (isset($this->_fieldsets[$group][$field]) && is_object($this->_fieldsets[$group][$field])) {
  637. $return = $this->_fieldsets[$group][$field]->data() ? $this->_fieldsets[$group][$field]->data() : $default;
  638. }
  639. return $return;
  640. }
  641. /**
  642. * Method to set the value of a field.
  643. *
  644. * @access public
  645. * @param string $field The field to set.
  646. * @param mixed $value The value to set the field to.
  647. * @param string $group The group the field is in.
  648. * @return boolean True if field exists, false otherwise.
  649. * @since 2.0
  650. */
  651. function setValue($field, $value, $group = '_default')
  652. {
  653. // Set the field if it exists.
  654. if (isset($this->_fieldsets[$group][$field]) && is_object($this->_fieldsets[$group][$field])) {
  655. $this->_fieldsets[$group][$field]->setData($value);
  656. return true;
  657. } else {
  658. return false;
  659. }
  660. }
  661. /**
  662. * Loads form fields from an XML fieldset element optionally reseting fields before loading new ones.
  663. *
  664. * @access public
  665. * @param object $xml The XML fieldset object.
  666. * @param boolean $reset Flag to toggle whether the form fieldset should be reset.
  667. * @return boolean True on success, false otherwise.
  668. * @since 2.0
  669. */
  670. function loadFieldsXML(&$xml, $reset = true)
  671. {
  672. // Check for an XML object.
  673. if (!is_object($xml)) {
  674. return false;
  675. }
  676. // Get the group name.
  677. $group = ($xml->attributes('group')) ? $xml->attributes('group') : '_default';
  678. if ($reset) {
  679. // Reset the field group.
  680. $this->setFields($xml->children(), $group);
  681. } else {
  682. // Add to the field group.
  683. $this->addFields($xml->children(), $group);
  684. }
  685. // Check if there is a field path to handle.
  686. if ($xml->attributes('addfieldpath'))
  687. {
  688. jimport('joomla.filesystem.folder');
  689. jimport('joomla.filesystem.path');
  690. $path = JPath::clean(JPATH_ROOT.DS.$xml->attributes('addfieldpath'));
  691. // Add the field path to the list if it exists.
  692. if (JFolder::exists($path)) {
  693. JXForm::addFieldPath($path);
  694. }
  695. }
  696. return true;
  697. }
  698. /**
  699. * Method to load a form field object.
  700. *
  701. * @access public
  702. * @param string $type The field type.
  703. * @param boolean $new Flag to toggle whether we should get a new instance of the object.
  704. * @return mixed Field object on success, false otherwise.
  705. * @since 2.0
  706. */
  707. function &loadFieldType($type, $new = false)
  708. {
  709. $false = false;
  710. $key = md5($type);
  711. $class = 'JXFieldType'.ucfirst($type);
  712. // Return the field object if it already exists and we don't need a new one.
  713. if (isset($this->_fieldTypes[$key]) && $new === false) {
  714. return $this->_fieldTypes[$key];
  715. }
  716. if (!class_exists('JXFormFieldList') || !class_exists('JXFormFieldList')) {
  717. jximport2('jxtended.form.formfield');
  718. jximport2('jxtended.form.fields.list');
  719. }
  720. // TODO: Is this needed?
  721. // // Check if the field is a complex type.
  722. // if ($pos = strpos($type, '_'))
  723. // {
  724. // // Load the base field type.
  725. // $base = substr($type, 0, $pos);
  726. // $this->loadFieldType($base);
  727. // }
  728. if(!class_exists($class))
  729. {
  730. $paths = JXForm::addFieldPath();
  731. // If the type is complex, add the base type to the paths.
  732. if ($pos = strpos($type, '_'))
  733. {
  734. // Add the complex type prefix to the paths.
  735. for ($i = 0, $n = count($paths); $i < $n; $i++)
  736. {
  737. // Derive the new path.
  738. $path = $paths[$i].DS.substr($type, 0, $pos);
  739. // If the path does not exist, add it.
  740. if (!in_array($path, $paths)) {
  741. array_unshift($paths, $path);
  742. }
  743. }
  744. // Break off the end of the complex type.
  745. $type = substr($type, $pos+1);
  746. }
  747. // Try to find the field file.
  748. jimport('joomla.filesystem.path');
  749. if ($file = JPath::find($paths, strtolower($type).'.php')) {
  750. require_once $file;
  751. } else {
  752. return $false;
  753. }
  754. // Check once and for all if the class exists.
  755. if (!class_exists($class)) {
  756. return $false;
  757. }
  758. }
  759. // Instantiate a new field object.
  760. $this->_fieldTypes[$key] = new $class($this);
  761. return $this->_fieldTypes[$key];
  762. }
  763. /**
  764. * Method to add a path to the list of form include paths.
  765. *
  766. * @access public
  767. * @param mixed $new A path or array of paths to add.
  768. * @return array The list of paths that have been added.
  769. * @since 2.0
  770. * @static
  771. */
  772. function addFormPath($new = null)
  773. {
  774. static $paths;
  775. if (!isset($paths)) {
  776. $paths = array(dirname(__FILE__).DS.'forms');
  777. }
  778. // Force path to an array.
  779. settype($new, 'array');
  780. // Add the new paths to the list if not already there.
  781. foreach ($new as $path) {
  782. if (!in_array($path, $paths)) {
  783. array_unshift($paths, trim($path));
  784. }
  785. }
  786. return $paths;
  787. }
  788. /**
  789. * Method to add a path to the list of field include paths.
  790. *
  791. * @access public
  792. * @param mixed $new A path or array of paths to add.
  793. * @return array The list of paths that have been added.
  794. * @since 2.0
  795. * @static
  796. */
  797. function addFieldPath($new = null)
  798. {
  799. static $paths;
  800. if (!isset($paths)) {
  801. $paths = array(dirname(__FILE__).DS.'fields');
  802. }
  803. // Force path to an array.
  804. settype($new, 'array');
  805. // Add the new paths to the list if not already there.
  806. foreach ($new as $path) {
  807. if (!in_array($path, $paths)) {
  808. array_unshift($paths, trim($path));
  809. }
  810. }
  811. return $paths;
  812. }
  813. }