10 defined(
'JPATH_PLATFORM') or die;
44 protected $autocomplete =
'on';
52 protected $spellcheck =
true;
61 protected $autofocus =
false;
93 protected $hidden =
false;
101 protected $translateLabel =
true;
109 protected $translateDescription =
true;
117 protected $translateHint =
true;
150 protected $multiple =
false;
158 public $repeat =
false;
199 protected $required =
false;
208 protected $disabled =
false;
216 protected $readonly =
false;
297 protected static $count = 0;
305 protected static $generated_fieldname =
'__field';
314 public function __construct($form = null)
317 if ($form instanceof
JForm)
320 $this->formControl = $form->getFormControl();
324 if (!isset($this->type))
328 if ($parts[0] ==
'J')
348 public function __get($name)
379 if (empty($this->input))
381 $this->input = $this->getInput();
388 if (empty($this->label))
390 $this->label = $this->getLabel();
396 return $this->getTitle();
412 public function __set($name, $value)
418 $value = preg_replace(
'/\s+/',
' ', trim((
string) $value));
429 $this->$name = (string) $value;
433 $this->
id = $this->getId((
string) $value, $this->fieldname);
437 $this->fieldname = $this->getFieldName((
string) $value);
441 $this->fieldname = $this->getFieldName((
string) $value);
442 $this->name = $this->getName($this->fieldname);
447 $value = (string) $value;
448 $value = $value ===
'' && isset($this->forceMultiple) ? (string) $this->forceMultiple : $value;
455 $value = (string) $value;
456 $this->$name = ($value ===
'true' || $value === $name || $value ===
'1');
460 $value = (string) $value;
461 $value = ($value ==
'on' || $value ==
'') ?
'on' : $value;
462 $this->$name = ($value ===
'false' || $value ===
'off' || $value ===
'0') ?
false : $value;
466 case 'translateLabel':
467 case 'translateDescription':
468 case 'translateHint':
469 $value = (string) $value;
470 $this->$name = !($value ===
'false' || $value ===
'off' || $value ===
'0');
474 $this->$name = (int) $value;
478 if (property_exists(__CLASS__, $name))
480 JLog::add(
"Cannot access protected / private property $name of " . __CLASS__);
484 $this->$name = $value;
498 public function setForm(
JForm $form)
519 public function setup(SimpleXMLElement $element, $value, $group = null)
522 if ((
string) $element->getName() !=
'field')
532 $this->element = $element;
535 $this->group = $group;
538 'multiple',
'name',
'id',
'hint',
'class',
'description',
'labelClass',
'onchange',
539 'onclick',
'validate',
'pattern',
'default',
'required',
540 'disabled',
'readonly',
'autofocus',
'hidden',
'autocomplete',
'spellcheck',
541 'translateHint',
'translateLabel',
'translateDescription',
'size');
543 $this->
default = isset($element[
'value']) ? (string) $element[
'value'] : $this->
default;
546 $this->value = $value;
548 foreach ($attributes as $attributeName)
550 $this->__set($attributeName, $element[$attributeName]);
554 $repeat = (string) $element[
'repeat'];
555 $this->repeat = ($repeat ==
'true' || $repeat ==
'multiple' || (!empty($this->form->repeat) && $this->form->repeat == 1));
558 $this->hidden = ($this->hidden || (string) $element[
'type'] ==
'hidden');
572 public function setValue($value)
574 $this->value = $value;
587 protected function getId($fieldId, $fieldName)
592 if ($this->formControl)
594 $id .= $this->formControl;
603 $id .=
'_' . str_replace(
'.',
'_', $this->group);
607 $id .= str_replace(
'.',
'_', $this->group);
614 $id .=
'_' . ($fieldId ? $fieldId : $fieldName);
618 $id .= ($fieldId ? $fieldId : $fieldName);
622 $id = preg_replace(
'#\W#',
'_', $id);
627 $repeatCounter = empty($this->form->repeatCounter) ? 0 : $this->form->repeatCounter;
628 $id .=
'-' . $repeatCounter;
630 if (strtolower($this->type) ==
'radio')
646 abstract protected function getInput();
655 protected function getTitle()
665 $title = $this->element[
'label'] ? (string) $this->element[
'label'] : (
string) $this->element[
'name'];
666 $title = $this->translateLabel ?
JText::_($title) : $title;
678 protected function getLabel()
688 $text = $this->element[
'label'] ? (string) $this->element[
'label'] : (
string) $this->element[
'name'];
689 $text = $this->translateLabel ?
JText::_($text) : $text;
692 $class = !empty($this->description) ?
'hasTooltip' :
'';
693 $class = $this->required ==
true ? $class .
' required' : $class;
694 $class = !empty($this->labelClass) ? $class .
' ' . $this->labelClass : $class;
697 $label .=
'<label id="' . $this->
id .
'-lbl" for="' . $this->
id .
'" class="' . $class .
'"';
700 if (!empty($this->description))
702 JHtml::_(
'bootstrap.tooltip');
703 $label .=
' title="' . JHtml::tooltipText(trim($text,
':'), JText::_($this->description), 0) .
'"';
709 $label .=
'>' . $text .
'<span class="star"> *</span></label>';
713 $label .=
'>' . $text .
'</label>';
728 protected function getName($fieldName)
731 $repeatCounter = empty($this->form->repeatCounter) ? 0 : $this->form->repeatCounter;
736 if ($this->formControl)
738 $name .= $this->formControl;
745 $groups = explode(
'.', $this->group);
749 foreach ($groups as $group)
751 $name .=
'[' . $group .
']';
756 $name .= array_shift($groups);
758 foreach ($groups as $group)
760 $name .=
'[' . $group .
']';
768 $name .=
'[' . $fieldName .
']';
778 switch (strtolower((
string) $this->element[
'type']))
806 protected function getFieldName($fieldName)
814 self::$count = self::$count + 1;
816 return self::$generated_fieldname . self::$count;
830 public function getAttribute($name, $default = null)
832 if ($this->element instanceof SimpleXMLElement)
834 $attributes = $this->element->attributes();
837 if (property_exists($attributes, $name))
839 $value = $attributes->$name;
843 return (
string) $value;
858 public function getControlGroup()
862 return $this->getInput();
866 '<div class="control-group">'
867 .
'<div class="control-label">' . $this->getLabel() .
'</div>'
868 .
'<div class="controls">' . $this->getInput() .
'</div>'