PageRenderTime 122ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/content/code/trunk/administrator/components/com_artofcontent/libraries/jxtended/form/formfield.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 213 lines | 95 code | 30 blank | 88 comment | 25 complexity | da7ecc6856ae0c35ffc30ba187c9502c MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: formfield.php 484 2010-12-20 23:40:27Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Form
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License
  8. * @link http://www.theartofjoomla.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. jimport('joomla.utilities.simplexml');
  12. /**
  13. * Abstract Form Field class for JXtended Libraries.
  14. *
  15. * @package JXtended.Libraries
  16. * @subpackage Form
  17. * @since 1.1
  18. */
  19. abstract class JFormField extends JObject
  20. {
  21. /**
  22. * The field type.
  23. *
  24. * @var string
  25. */
  26. protected $type;
  27. /**
  28. * A reference to the form object that the field belongs to.
  29. *
  30. * @var object
  31. */
  32. protected $_form;
  33. /**
  34. * Method to instantiate the form field.
  35. *
  36. * @param object $form A reference to the form that the field belongs to.
  37. * @return void
  38. */
  39. public function __construct($form = null)
  40. {
  41. $this->_form = $form;
  42. }
  43. /**
  44. * Method to get the form field type.
  45. *
  46. * @return string The field type.
  47. */
  48. public function getType()
  49. {
  50. return $this->type;
  51. }
  52. public function render(&$xml, $value, $formName, $groupName)
  53. {
  54. // Set the xml element object.
  55. $this->_element = $xml;
  56. // Set the id, name, and value.
  57. $this->id = $xml->attributes('id');
  58. $this->name = $xml->attributes('name');
  59. $this->value = $value;
  60. // Set the label and description text.
  61. $this->labelText = $xml->attributes('label') ? $xml->attributes('label') : $this->name;
  62. $this->descText = $xml->attributes('description');
  63. // Set the required and validate options.
  64. $this->required = ($xml->attributes('required') == 'true' || $xml->attributes('required') == 'required');
  65. $this->validate = $xml->attributes('validate');
  66. // Add the required class if the field is required.
  67. if ($this->required) {
  68. if (strpos($xml->attributes('class'), 'required') === false) {
  69. $xml->addAttribute('class', $xml->attributes('class').' required');
  70. }
  71. }
  72. // Set the field decorator.
  73. $this->decorator = $xml->attributes('decorator');
  74. // Set the visibility.
  75. $this->hidden = ($xml->attributes('type') == 'hidden' || $xml->attributes('hidden'));
  76. // Set the multiple values option.
  77. $this->multiple = ($xml->attributes('multiple') == 'true' || $xml->attributes('multiple') == 'multiple');
  78. // Set the form and group names.
  79. $this->formName = $formName;
  80. $this->groupName = $groupName;
  81. // Set the input name and id.
  82. $this->inputName = $this->_getInputName($this->name, $formName, $groupName, $this->multiple);
  83. $this->inputId = $this->_getInputId($this->id, $this->name, $formName, $groupName);
  84. // Set the actual label and input.
  85. $this->label = $this->_getLabel();
  86. $this->input = $this->_getInput();
  87. return $this;
  88. }
  89. /**
  90. * Method to get the field label.
  91. *
  92. * @return string The field label.
  93. */
  94. protected function _getLabel()
  95. {
  96. // Set the class for the label.
  97. $class = !empty($this->descText) ? 'hasTip' : '';
  98. $class = $this->required == true ? $class.' required' : $class;
  99. // If a description is specified, use it to build a tooltip.
  100. if (!empty($this->descText)) {
  101. $label = '<label id="'.$this->inputId.'-lbl" for="'.$this->inputId.'" class="'.$class.'" title="'.trim(JText::_($this->labelText, true), ':').'::'.JText::_($this->descText, true).'">';
  102. } else {
  103. $label = '<label id="'.$this->inputId.'-lbl" for="'.$this->inputId.'" class="'.$class.'">';
  104. }
  105. $label .= JText::_($this->labelText);
  106. $label .= '</label>';
  107. return $label;
  108. }
  109. /**
  110. * Method to get the field input.
  111. *
  112. * @return string The field input.
  113. */
  114. abstract protected function _getInput();
  115. /**
  116. * Method to get the name of the input field.
  117. *
  118. * @param string $fieldName The field name.
  119. * @param string $formName The form name.
  120. * @param string $groupName The group name.
  121. * @param boolean $multiple Whether the input should support multiple values.
  122. * @return string The input field id.
  123. */
  124. protected function _getInputName($fieldName, $formName = false, $groupName = false, $multiple = false)
  125. {
  126. // No form or group, just use the field name.
  127. if ($formName === false && $groupName === false) {
  128. $return = $fieldName;
  129. }
  130. // No group, use the form and field name.
  131. elseif ($formName !== false && $groupName === false) {
  132. $return = $formName.'['.$fieldName.']';
  133. }
  134. // No form, use the group and field name.
  135. elseif ($formName === false && $groupName !== false) {
  136. $return = $groupName.'['.$fieldName.']';
  137. }
  138. // Use the form, group, and field name.
  139. else {
  140. $return = $formName.'['.$groupName.']['.$fieldName.']';
  141. }
  142. // Check if the field should support multiple values.
  143. if ($multiple) {
  144. $return .= '[]';
  145. }
  146. return $return;
  147. }
  148. /**
  149. * Method to get the id of the input field.
  150. *
  151. * @param string $fieldId The field id.
  152. * @param string $fieldName The field name.
  153. * @param string $formName The form name.
  154. * @param string $groupName The group name.
  155. * @return string The input field id.
  156. */
  157. protected function _getInputId($fieldId, $fieldName, $formName = false, $groupName = false)
  158. {
  159. // Use the field name if no id is set.
  160. if (empty($fieldId)) {
  161. $fieldId = $fieldName;
  162. }
  163. // No form or group, just use the field name.
  164. if ($formName === false && $groupName === false) {
  165. $return = $fieldId;
  166. }
  167. // No group, use the form and field name.
  168. elseif ($formName !== false && $groupName === false) {
  169. $return = $formName.'_'.$fieldId;
  170. }
  171. // No form, use the group and field name.
  172. elseif ($formName === false && $groupName !== false) {
  173. $return = $groupName.'_'.$fieldId;
  174. }
  175. // Use the form, group, and field name.
  176. else {
  177. $return = $formName.'_'.$groupName.'_'.$fieldId;
  178. }
  179. // Clean up any invalid characters.
  180. $return = preg_replace('#\W#', '_', $return);
  181. return $return;
  182. }
  183. }