PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/joomla/form/formfield.php

https://github.com/nikosdion/Akeeba-Example
PHP | 583 lines | 248 code | 72 blank | 263 comment | 43 complexity | 844b95870076654781ae147767d1b4ee MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. /**
  11. * Abstract Form Field class for the Joomla Framework.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Form
  15. * @since 11.1
  16. */
  17. abstract class JFormField
  18. {
  19. /**
  20. * The description text for the form field. Usually used in tooltips.
  21. *
  22. * @var string
  23. * @since 11.1
  24. */
  25. protected $description;
  26. /**
  27. * The JXMLElement object of the <field /> XML element that describes the form field.
  28. *
  29. * @var object
  30. * @since 11.1
  31. */
  32. protected $element;
  33. /**
  34. * The JForm object of the form attached to the form field.
  35. *
  36. * @var object
  37. * @since 11.1
  38. */
  39. protected $form;
  40. /**
  41. * The form control prefix for field names from the JForm object attached to the form field.
  42. *
  43. * @var string
  44. * @since 11.1
  45. */
  46. protected $formControl;
  47. /**
  48. * The hidden state for the form field.
  49. *
  50. * @var boolean
  51. * @since 11.1
  52. */
  53. protected $hidden = false;
  54. /**
  55. * True to translate the field label string.
  56. *
  57. * @var boolean
  58. * @since 11.1
  59. */
  60. protected $translateLabel = true;
  61. /**
  62. * True to translate the field description string.
  63. *
  64. * @var boolean
  65. * @since 11.1
  66. */
  67. protected $translateDescription = true;
  68. /**
  69. * The document id for the form field.
  70. *
  71. * @var string
  72. * @since 11.1
  73. */
  74. protected $id;
  75. /**
  76. * The input for the form field.
  77. *
  78. * @var string
  79. * @since 11.1
  80. */
  81. protected $input;
  82. /**
  83. * The label for the form field.
  84. *
  85. * @var string
  86. * @since 11.1
  87. */
  88. protected $label;
  89. /**
  90. * The multiple state for the form field. If true then multiple values are allowed for the
  91. * field. Most often used for list field types.
  92. *
  93. * @var boolean
  94. * @since 11.1
  95. */
  96. protected $multiple = false;
  97. /**
  98. * The name of the form field.
  99. *
  100. * @var string
  101. * @since 11.1
  102. */
  103. protected $name;
  104. /**
  105. * The name of the field.
  106. *
  107. * @var string
  108. * @since 11.1
  109. */
  110. protected $fieldname;
  111. /**
  112. * The group of the field.
  113. *
  114. * @var string
  115. * @since 11.1
  116. */
  117. protected $group;
  118. /**
  119. * The required state for the form field. If true then there must be a value for the field to
  120. * be considered valid.
  121. *
  122. * @var boolean
  123. * @since 11.1
  124. */
  125. protected $required = false;
  126. /**
  127. * The form field type.
  128. *
  129. * @var string
  130. * @since 11.1
  131. */
  132. protected $type;
  133. /**
  134. * The validation method for the form field. This value will determine which method is used
  135. * to validate the value for a field.
  136. *
  137. * @var string
  138. * @since 11.1
  139. */
  140. protected $validate;
  141. /**
  142. * The value of the form field.
  143. *
  144. * @var mixed
  145. * @since 11.1
  146. */
  147. protected $value;
  148. /**
  149. * The count value for generated name field
  150. *
  151. * @var integer
  152. * @since 11.1
  153. */
  154. protected static $count = 0;
  155. /**
  156. * The string used for generated fields names
  157. *
  158. * @var integer
  159. * @since 11.1
  160. */
  161. protected static $generated_fieldname = '__field';
  162. /**
  163. * Method to instantiate the form field object.
  164. *
  165. * @param object $form The form to attach to the form field object.
  166. *
  167. * @return JFormField
  168. *
  169. * @since 11.1
  170. */
  171. public function __construct($form = null)
  172. {
  173. // If there is a form passed into the constructor set the form and form control properties.
  174. if ($form instanceof JForm)
  175. {
  176. $this->form = $form;
  177. $this->formControl = $form->getFormControl();
  178. }
  179. }
  180. /**
  181. * Method to get certain otherwise inaccessible properties from the form field object.
  182. *
  183. * @param string $name The property name for which to the the value.
  184. *
  185. * @return mixed The property value or null.
  186. *
  187. * @since 11.1
  188. */
  189. public function __get($name)
  190. {
  191. switch ($name)
  192. {
  193. case 'class':
  194. case 'description':
  195. case 'formControl':
  196. case 'hidden':
  197. case 'id':
  198. case 'multiple':
  199. case 'name':
  200. case 'required':
  201. case 'type':
  202. case 'validate':
  203. case 'value':
  204. case 'fieldname':
  205. case 'group':
  206. return $this->$name;
  207. break;
  208. case 'input':
  209. // If the input hasn't yet been generated, generate it.
  210. if (empty($this->input))
  211. {
  212. $this->input = $this->getInput();
  213. }
  214. return $this->input;
  215. break;
  216. case 'label':
  217. // If the label hasn't yet been generated, generate it.
  218. if (empty($this->label))
  219. {
  220. $this->label = $this->getLabel();
  221. }
  222. return $this->label;
  223. break;
  224. case 'title':
  225. return $this->getTitle();
  226. break;
  227. }
  228. return null;
  229. }
  230. /**
  231. * Method to attach a JForm object to the field.
  232. *
  233. * @param object $form The JForm object to attach to the form field.
  234. *
  235. * @return object The form field object so that the method can be used in a chain.
  236. *
  237. * @since 11.1
  238. */
  239. public function setForm(JForm $form)
  240. {
  241. $this->form = $form;
  242. $this->formControl = $form->getFormControl();
  243. return $this;
  244. }
  245. /**
  246. * Method to attach a JForm object to the field.
  247. *
  248. * @param object &$element The JXmlElement object representing the <field /> tag for the form field object.
  249. * @param mixed $value The form field value to validate.
  250. * @param string $group The field name group control value. This acts as as an array container for the field.
  251. * For example if the field has name="foo" and the group value is set to "bar" then the
  252. * full field name would end up being "bar[foo]".
  253. *
  254. * @return boolean True on success.
  255. *
  256. * @since 11.1
  257. */
  258. public function setup(&$element, $value, $group = null)
  259. {
  260. // Make sure there is a valid JFormField XML element.
  261. if (!($element instanceof JXMLElement) || (string) $element->getName() != 'field')
  262. {
  263. return false;
  264. }
  265. // Reset the input and label values.
  266. $this->input = null;
  267. $this->label = null;
  268. // Set the XML element object.
  269. $this->element = $element;
  270. // Get some important attributes from the form field element.
  271. $class = (string) $element['class'];
  272. $id = (string) $element['id'];
  273. $multiple = (string) $element['multiple'];
  274. $name = (string) $element['name'];
  275. $required = (string) $element['required'];
  276. // Set the required and validation options.
  277. $this->required = ($required == 'true' || $required == 'required' || $required == '1');
  278. $this->validate = (string) $element['validate'];
  279. // Add the required class if the field is required.
  280. if ($this->required)
  281. {
  282. if ($class)
  283. {
  284. if (strpos($class, 'required') === false)
  285. {
  286. $this->element['class'] = $class . ' required';
  287. }
  288. }
  289. else
  290. {
  291. $this->element->addAttribute('class', 'required');
  292. }
  293. }
  294. // Set the multiple values option.
  295. $this->multiple = ($multiple == 'true' || $multiple == 'multiple');
  296. // Allow for field classes to force the multiple values option.
  297. if (isset($this->forceMultiple))
  298. {
  299. $this->multiple = (bool) $this->forceMultiple;
  300. }
  301. // Set the field description text.
  302. $this->description = (string) $element['description'];
  303. // Set the visibility.
  304. $this->hidden = ((string) $element['type'] == 'hidden' || (string) $element['hidden'] == 'true');
  305. // Determine whether to translate the field label and/or description.
  306. $this->translateLabel = !((string) $this->element['translate_label'] == 'false' || (string) $this->element['translate_label'] == '0');
  307. $this->translateDescription = !((string) $this->element['translate_description'] == 'false'
  308. || (string) $this->element['translate_description'] == '0');
  309. // Set the group of the field.
  310. $this->group = $group;
  311. // Set the field name and id.
  312. $this->fieldname = $this->getFieldName($name);
  313. $this->name = $this->getName($this->fieldname);
  314. $this->id = $this->getId($id, $this->fieldname);
  315. // Set the field default value.
  316. $this->value = $value;
  317. return true;
  318. }
  319. /**
  320. * Method to get the id used for the field input tag.
  321. *
  322. * @param string $fieldId The field element id.
  323. * @param string $fieldName The field element name.
  324. *
  325. * @return string The id to be used for the field input tag.
  326. *
  327. * @since 11.1
  328. */
  329. protected function getId($fieldId, $fieldName)
  330. {
  331. // Initialise variables.
  332. $id = '';
  333. // If there is a form control set for the attached form add it first.
  334. if ($this->formControl)
  335. {
  336. $id .= $this->formControl;
  337. }
  338. // If the field is in a group add the group control to the field id.
  339. if ($this->group)
  340. {
  341. // If we already have an id segment add the group control as another level.
  342. if ($id)
  343. {
  344. $id .= '_' . str_replace('.', '_', $this->group);
  345. }
  346. else
  347. {
  348. $id .= str_replace('.', '_', $this->group);
  349. }
  350. }
  351. // If we already have an id segment add the field id/name as another level.
  352. if ($id)
  353. {
  354. $id .= '_' . ($fieldId ? $fieldId : $fieldName);
  355. }
  356. else
  357. {
  358. $id .= ($fieldId ? $fieldId : $fieldName);
  359. }
  360. // Clean up any invalid characters.
  361. $id = preg_replace('#\W#', '_', $id);
  362. return $id;
  363. }
  364. /**
  365. * Method to get the field input markup.
  366. *
  367. * @return string The field input markup.
  368. *
  369. * @since 11.1
  370. */
  371. abstract protected function getInput();
  372. /**
  373. * Method to get the field title.
  374. *
  375. * @return string The field title.
  376. *
  377. * @since 11.1
  378. */
  379. protected function getTitle()
  380. {
  381. // Initialise variables.
  382. $title = '';
  383. if ($this->hidden)
  384. {
  385. return $title;
  386. }
  387. // Get the label text from the XML element, defaulting to the element name.
  388. $title = $this->element['label'] ? (string) $this->element['label'] : (string) $this->element['name'];
  389. $title = $this->translateLabel ? JText::_($title) : $title;
  390. return $title;
  391. }
  392. /**
  393. * Method to get the field label markup.
  394. *
  395. * @return string The field label markup.
  396. *
  397. * @since 11.1
  398. */
  399. protected function getLabel()
  400. {
  401. // Initialise variables.
  402. $label = '';
  403. if ($this->hidden)
  404. {
  405. return $label;
  406. }
  407. // Get the label text from the XML element, defaulting to the element name.
  408. $text = $this->element['label'] ? (string) $this->element['label'] : (string) $this->element['name'];
  409. $text = $this->translateLabel ? JText::_($text) : $text;
  410. // Build the class for the label.
  411. $class = !empty($this->description) ? 'hasTip' : '';
  412. $class = $this->required == true ? $class . ' required' : $class;
  413. // Add the opening label tag and main attributes attributes.
  414. $label .= '<label id="' . $this->id . '-lbl" for="' . $this->id . '" class="' . $class . '"';
  415. // If a description is specified, use it to build a tooltip.
  416. if (!empty($this->description))
  417. {
  418. $label .= ' title="'
  419. . htmlspecialchars(
  420. trim($text, ':') . '::' . ($this->translateDescription ? JText::_($this->description) : $this->description),
  421. ENT_COMPAT, 'UTF-8'
  422. ) . '"';
  423. }
  424. // Add the label text and closing tag.
  425. if ($this->required)
  426. {
  427. $label .= '>' . $text . '<span class="star">&#160;*</span></label>';
  428. }
  429. else
  430. {
  431. $label .= '>' . $text . '</label>';
  432. }
  433. return $label;
  434. }
  435. /**
  436. * Method to get the name used for the field input tag.
  437. *
  438. * @param string $fieldName The field element name.
  439. *
  440. * @return string The name to be used for the field input tag.
  441. *
  442. * @since 11.1
  443. */
  444. protected function getName($fieldName)
  445. {
  446. // Initialise variables.
  447. $name = '';
  448. // If there is a form control set for the attached form add it first.
  449. if ($this->formControl)
  450. {
  451. $name .= $this->formControl;
  452. }
  453. // If the field is in a group add the group control to the field name.
  454. if ($this->group)
  455. {
  456. // If we already have a name segment add the group control as another level.
  457. $groups = explode('.', $this->group);
  458. if ($name)
  459. {
  460. foreach ($groups as $group)
  461. {
  462. $name .= '[' . $group . ']';
  463. }
  464. }
  465. else
  466. {
  467. $name .= array_shift($groups);
  468. foreach ($groups as $group)
  469. {
  470. $name .= '[' . $group . ']';
  471. }
  472. }
  473. }
  474. // If we already have a name segment add the field name as another level.
  475. if ($name)
  476. {
  477. $name .= '[' . $fieldName . ']';
  478. }
  479. else
  480. {
  481. $name .= $fieldName;
  482. }
  483. // If the field should support multiple values add the final array segment.
  484. if ($this->multiple)
  485. {
  486. $name .= '[]';
  487. }
  488. return $name;
  489. }
  490. /**
  491. * Method to get the field name used.
  492. *
  493. * @param string $fieldName The field element name.
  494. *
  495. * @return string The field name
  496. *
  497. * @since 11.1
  498. */
  499. protected function getFieldName($fieldName)
  500. {
  501. if ($fieldName)
  502. {
  503. return $fieldName;
  504. }
  505. else
  506. {
  507. self::$count = self::$count + 1;
  508. return self::$generated_fieldname . self::$count;
  509. }
  510. }
  511. }