/libraries/fof/form/header.php

https://github.com/elinw/joomla-cms · PHP · 578 lines · 250 code · 77 blank · 251 comment · 23 complexity · dee54c3f5c609d45f6c01667d879bfab MD5 · raw file

  1. <?php
  2. /**
  3. * @package FrameworkOnFramework
  4. * @copyright Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // Protect from unauthorized access
  8. defined('_JEXEC') or die;
  9. /**
  10. * An interface for FOFFormHeader fields, used to define the filters and the
  11. * elements of the header row in repeatable (browse) views
  12. *
  13. * @package FrameworkOnFramework
  14. * @since 2.0
  15. */
  16. abstract class FOFFormHeader
  17. {
  18. /**
  19. * The description text for the form field. Usually used in tooltips.
  20. *
  21. * @var string
  22. * @since 2.0
  23. */
  24. protected $description;
  25. /**
  26. * The SimpleXMLElement object of the <field /> XML element that describes the header field.
  27. *
  28. * @var SimpleXMLElement
  29. * @since 2.0
  30. */
  31. protected $element;
  32. /**
  33. * The FOFForm object of the form attached to the header field.
  34. *
  35. * @var FOFForm
  36. * @since 2.0
  37. */
  38. protected $form;
  39. /**
  40. * The label for the header field.
  41. *
  42. * @var string
  43. * @since 2.0
  44. */
  45. protected $label;
  46. /**
  47. * The header HTML.
  48. *
  49. * @var string|null
  50. * @since 2.0
  51. */
  52. protected $header;
  53. /**
  54. * The filter HTML.
  55. *
  56. * @var string|null
  57. * @since 2.0
  58. */
  59. protected $filter;
  60. /**
  61. * The buttons HTML.
  62. *
  63. * @var string|null
  64. * @since 2.0
  65. */
  66. protected $buttons;
  67. /**
  68. * The options for a drop-down filter.
  69. *
  70. * @var array|null
  71. * @since 2.0
  72. */
  73. protected $options;
  74. /**
  75. * The name of the form field.
  76. *
  77. * @var string
  78. * @since 2.0
  79. */
  80. protected $name;
  81. /**
  82. * The name of the field.
  83. *
  84. * @var string
  85. * @since 2.0
  86. */
  87. protected $fieldname;
  88. /**
  89. * The group of the field.
  90. *
  91. * @var string
  92. * @since 2.0
  93. */
  94. protected $group;
  95. /**
  96. * The form field type.
  97. *
  98. * @var string
  99. * @since 2.0
  100. */
  101. protected $type;
  102. /**
  103. * The value of the filter.
  104. *
  105. * @var mixed
  106. * @since 2.0
  107. */
  108. protected $value;
  109. /**
  110. * The intended table data width (in pixels or percent).
  111. *
  112. * @var mixed
  113. * @since 2.0
  114. */
  115. protected $tdwidth;
  116. /**
  117. * The key of the filter value in the model state.
  118. *
  119. * @var mixed
  120. * @since 2.0
  121. */
  122. protected $filterSource;
  123. /**
  124. * Is this a sortable column?
  125. *
  126. * @var bool
  127. * @since 2.0
  128. */
  129. protected $sortable = false;
  130. /**
  131. * Method to instantiate the form field object.
  132. *
  133. * @param FOFForm $form The form to attach to the form field object.
  134. *
  135. * @since 2.0
  136. */
  137. public function __construct(FOFForm $form = null)
  138. {
  139. // If there is a form passed into the constructor set the form and form control properties.
  140. if ($form instanceof FOFForm)
  141. {
  142. $this->form = $form;
  143. }
  144. }
  145. /**
  146. * Method to get certain otherwise inaccessible properties from the form field object.
  147. *
  148. * @param string $name The property name for which to the the value.
  149. *
  150. * @return mixed The property value or null.
  151. *
  152. * @since 2.0
  153. */
  154. public function __get($name)
  155. {
  156. switch ($name)
  157. {
  158. case 'description':
  159. case 'name':
  160. case 'type':
  161. case 'fieldname':
  162. case 'group':
  163. case 'tdwidth':
  164. return $this->$name;
  165. break;
  166. case 'label':
  167. if (empty($this->label))
  168. {
  169. $this->label = $this->getLabel();
  170. }
  171. return $this->label;
  172. case 'value':
  173. if (empty($this->value))
  174. {
  175. $this->value = $this->getValue();
  176. }
  177. return $this->value;
  178. break;
  179. case 'header':
  180. if (empty($this->header))
  181. {
  182. $this->header = $this->getHeader();
  183. }
  184. return $this->header;
  185. break;
  186. case 'filter':
  187. if (empty($this->filter))
  188. {
  189. $this->filter = $this->getFilter();
  190. }
  191. return $this->filter;
  192. break;
  193. case 'buttons':
  194. if (empty($this->buttons))
  195. {
  196. $this->buttons = $this->getButtons();
  197. }
  198. return $this->buttons;
  199. break;
  200. case 'options':
  201. if (empty($this->options))
  202. {
  203. $this->options = $this->getOptions();
  204. }
  205. return $this->options;
  206. break;
  207. case 'sortable':
  208. if (empty($this->sortable))
  209. {
  210. $this->sortable = $this->getSortable();
  211. }
  212. return $this->sortable;
  213. break;
  214. }
  215. return null;
  216. }
  217. /**
  218. * Method to attach a JForm object to the field.
  219. *
  220. * @param FOFForm $form The JForm object to attach to the form field.
  221. *
  222. * @return FOFFormHeader The form field object so that the method can be used in a chain.
  223. *
  224. * @since 2.0
  225. */
  226. public function setForm(FOFForm $form)
  227. {
  228. $this->form = $form;
  229. return $this;
  230. }
  231. /**
  232. * Method to attach a FOFForm object to the field.
  233. *
  234. * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
  235. * @param mixed $value The form field value to validate.
  236. * @param string $group The field name group control value. This acts as as an array container for the field.
  237. * For example if the field has name="foo" and the group value is set to "bar" then the
  238. * full field name would end up being "bar[foo]".
  239. *
  240. * @return boolean True on success.
  241. *
  242. * @since 2.0
  243. */
  244. public function setup(SimpleXMLElement $element, $value, $group = null)
  245. {
  246. // Make sure there is a valid JFormField XML element.
  247. if ((string) $element->getName() != 'header')
  248. {
  249. return false;
  250. }
  251. // Reset the internal fields
  252. $this->label = null;
  253. $this->header = null;
  254. $this->filter = null;
  255. $this->buttons = null;
  256. $this->options = null;
  257. $this->value = null;
  258. $this->filterSource = null;
  259. // Set the XML element object.
  260. $this->element = $element;
  261. // Get some important attributes from the form field element.
  262. $class = (string) $element['class'];
  263. $id = (string) $element['id'];
  264. $name = (string) $element['name'];
  265. $filterSource = (string) $element['filter_source'];
  266. $tdwidth = (string) $element['tdwidth'];
  267. // Set the field description text.
  268. $this->description = (string) $element['description'];
  269. // Set the group of the field.
  270. $this->group = $group;
  271. // Set the td width of the field.
  272. $this->tdwidth = $tdwidth;
  273. // Set the field name and id.
  274. $this->fieldname = $this->getFieldName($name);
  275. $this->name = $this->getName($this->fieldname);
  276. $this->id = $this->getId($id, $this->fieldname);
  277. $this->filterSource = $this->getFilterSource($filterSource);
  278. // Set the field default value.
  279. $this->value = $this->getValue();
  280. return true;
  281. }
  282. /**
  283. * Method to get the id used for the field input tag.
  284. *
  285. * @param string $fieldId The field element id.
  286. * @param string $fieldName The field element name.
  287. *
  288. * @return string The id to be used for the field input tag.
  289. *
  290. * @since 2.0
  291. */
  292. protected function getId($fieldId, $fieldName)
  293. {
  294. $id = '';
  295. // If the field is in a group add the group control to the field id.
  296. if ($this->group)
  297. {
  298. // If we already have an id segment add the group control as another level.
  299. if ($id)
  300. {
  301. $id .= '_' . str_replace('.', '_', $this->group);
  302. }
  303. else
  304. {
  305. $id .= str_replace('.', '_', $this->group);
  306. }
  307. }
  308. // If we already have an id segment add the field id/name as another level.
  309. if ($id)
  310. {
  311. $id .= '_' . ($fieldId ? $fieldId : $fieldName);
  312. }
  313. else
  314. {
  315. $id .= ($fieldId ? $fieldId : $fieldName);
  316. }
  317. // Clean up any invalid characters.
  318. $id = preg_replace('#\W#', '_', $id);
  319. return $id;
  320. }
  321. /**
  322. * Method to get the name used for the field input tag.
  323. *
  324. * @param string $fieldName The field element name.
  325. *
  326. * @return string The name to be used for the field input tag.
  327. *
  328. * @since 2.0
  329. */
  330. protected function getName($fieldName)
  331. {
  332. $name = '';
  333. // If the field is in a group add the group control to the field name.
  334. if ($this->group)
  335. {
  336. // If we already have a name segment add the group control as another level.
  337. $groups = explode('.', $this->group);
  338. if ($name)
  339. {
  340. foreach ($groups as $group)
  341. {
  342. $name .= '[' . $group . ']';
  343. }
  344. }
  345. else
  346. {
  347. $name .= array_shift($groups);
  348. foreach ($groups as $group)
  349. {
  350. $name .= '[' . $group . ']';
  351. }
  352. }
  353. }
  354. // If we already have a name segment add the field name as another level.
  355. if ($name)
  356. {
  357. $name .= '[' . $fieldName . ']';
  358. }
  359. else
  360. {
  361. $name .= $fieldName;
  362. }
  363. return $name;
  364. }
  365. /**
  366. * Method to get the field name used.
  367. *
  368. * @param string $fieldName The field element name.
  369. *
  370. * @return string The field name
  371. *
  372. * @since 2.0
  373. */
  374. protected function getFieldName($fieldName)
  375. {
  376. return $fieldName;
  377. }
  378. /**
  379. * Method to get the field label.
  380. *
  381. * @return string The field label.
  382. *
  383. * @since 2.0
  384. */
  385. protected function getLabel()
  386. {
  387. // Get the label text from the XML element, defaulting to the element name.
  388. $title = $this->element['label'] ? (string) $this->element['label'] : '';
  389. if (empty($title))
  390. {
  391. $view = $this->form->getView();
  392. $params = $view->getViewOptionAndName();
  393. $title = $params['option'] . '_' .
  394. FOFInflector::pluralize($params['view']) . '_FIELD_' .
  395. (string) $this->element['name'];
  396. $title = strtoupper($title);
  397. $result = JText::_($title);
  398. if ($result === $title)
  399. {
  400. $title = ucfirst((string) $this->element['name']);
  401. }
  402. }
  403. return $title;
  404. }
  405. /**
  406. * Get the filter value for this header field
  407. *
  408. * @return mixed The filter value
  409. */
  410. protected function getValue()
  411. {
  412. $model = $this->form->getModel();
  413. return $model->getState($this->filterSource);
  414. }
  415. /**
  416. * Return the key of the filter value in the model state or, if it's not set,
  417. * the name of the field.
  418. *
  419. * @param string $filterSource The filter source value to return
  420. *
  421. * @return string
  422. */
  423. protected function getFilterSource($filterSource)
  424. {
  425. if ($filterSource)
  426. {
  427. return $filterSource;
  428. }
  429. else
  430. {
  431. return $this->name;
  432. }
  433. }
  434. /**
  435. * Is this a sortable field?
  436. *
  437. * @return boolean True if it's sortable
  438. */
  439. protected function getSortable()
  440. {
  441. $sortable = ($this->element['sortable'] != 'false');
  442. if ($sortable)
  443. {
  444. if (empty($this->header))
  445. {
  446. $this->header = $this->getHeader();
  447. }
  448. $sortable = !empty($this->header);
  449. }
  450. return $sortable;
  451. }
  452. /**
  453. * Returns the HTML for the header row, or null if this element should
  454. * render no header element
  455. *
  456. * @return string|null HTML code or null if nothing is to be rendered
  457. *
  458. * @since 2.0
  459. */
  460. protected function getHeader()
  461. {
  462. return null;
  463. }
  464. /**
  465. * Returns the HTML for a text filter to be rendered in the filter row,
  466. * or null if this element should render no text input filter.
  467. *
  468. * @return string|null HTML code or null if nothing is to be rendered
  469. *
  470. * @since 2.0
  471. */
  472. protected function getFilter()
  473. {
  474. return null;
  475. }
  476. /**
  477. * Returns the HTML for the buttons to be rendered in the filter row,
  478. * next to the text input filter, or null if this element should render no
  479. * text input filter buttons.
  480. *
  481. * @return string|null HTML code or null if nothing is to be rendered
  482. *
  483. * @since 2.0
  484. */
  485. protected function getButtons()
  486. {
  487. return null;
  488. }
  489. /**
  490. * Returns the JHtml options for a drop-down filter. Do not include an
  491. * empty option, it is added automatically.
  492. *
  493. * @return array The JHtml options for a drop-down filter
  494. *
  495. * @since 2.0
  496. */
  497. protected function getOptions()
  498. {
  499. return array();
  500. }
  501. }