1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 JFormHelper::loadFieldClass('list');
13
14 /**
15 * Field to load a list of posible item count limits
16 *
17 * @since 3.2
18 */
19 class JFormFieldLimitbox extends JFormFieldList
20 {
21 /**
22 * The form field type.
23 *
24 * @var string
25 * @since 3.2
26 */
27 public $type = 'Limitbox';
28
29 /**
30 * Cached array of the category items.
31 *
32 * @var array
33 * @since 3.2
34 */
35 protected static $options = array();
36
37 /**
38 * Default options
39 *
40 * @var array
41 */
42 protected $defaultLimits = array(5, 10, 15, 20, 25, 30, 50, 100, 200, 500);
43
44 /**
45 * Method to get the options to populate to populate list
46 *
47 * @return array The field option objects.
48 *
49 * @since 3.2
50 */
51 protected function getOptions()
52 {
53 // Accepted modifiers
54 $hash = md5($this->element);
55
56 if (!isset(static::$options[$hash]))
57 {
58 static::$options[$hash] = parent::getOptions();
59
60 $options = array();
61 $limits = $this->defaultLimits;
62
63 // Limits manually specified
64 if (isset($this->element['limits']))
65 {
66 $limits = explode(',', $this->element['limits']);
67 }
68
69 // User wants to add custom limits
70 if (isset($this->element['append']))
71 {
72 $limits = array_unique(array_merge($limits, explode(',', $this->element['append'])));
73 }
74
75 // User wants to remove some default limits
76 if (isset($this->element['remove']))
77 {
78 $limits = array_diff($limits, explode(',', $this->element['remove']));
79 }
80
81 // Order the options
82 asort($limits);
83
84 // Add an option to show all?
85 $showAll = isset($this->element['showall']) ? $this->element['showall'] === 'true' : true;
86
87 if ($showAll)
88 {
89 $limits[] = 0;
90 }
91
92 if (!empty($limits))
93 {
94 foreach ($limits as $value)
95 {
96 $options[] = (object) array(
97 'value' => $value,
98 'text' => ($value != 0) ? JText::_('J' . $value) : JText::_('JALL'),
99 );
100 }
101
102 static::$options[$hash] = array_merge(static::$options[$hash], $options);
103 }
104 }
105
106 return static::$options[$hash];
107 }
108 }
109