1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage HTML
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 use Joomla\Registry\Registry;
13
14 /**
15 * Searchtools elements.
16 *
17 * @since 3.2
18 */
19 abstract class JHtmlSearchtools
20 {
21 /**
22 * @var array Array containing information for loaded files
23 * @since 3.2
24 */
25 protected static $loaded = array();
26
27 /**
28 * Load the main Searchtools libraries
29 *
30 * @return void
31 *
32 * @since 3.2
33 */
34 public static function main()
35 {
36 // Only load once
37 if (empty(static::$loaded[__METHOD__]))
38 {
39 // Requires jQuery but allows to skip its loading
40 if ($loadJquery = (!isset($options['loadJquery']) || $options['loadJquery'] != 0))
41 {
42 JHtml::_('jquery.framework');
43 }
44
45 // Load the jQuery plugin && CSS
46 JHtml::_('script', 'jui/jquery.searchtools.min.js', array('version' => 'auto', 'relative' => true));
47 JHtml::_('stylesheet', 'jui/jquery.searchtools.css', array('version' => 'auto', 'relative' => true));
48
49 static::$loaded[__METHOD__] = true;
50 }
51
52 return;
53 }
54
55 /**
56 * Load searchtools for a specific form
57 *
58 * @param mixed $selector Is debugging mode on? [optional]
59 * @param array $options Optional array of parameters for search tools
60 *
61 * @return void
62 *
63 * @since 3.2
64 */
65 public static function form($selector = '.js-stools-form', $options = array())
66 {
67 $sig = md5(serialize(array($selector, $options)));
68
69 // Only load once
70 if (!isset(static::$loaded[__METHOD__][$sig]))
71 {
72 // Include Bootstrap framework
73 static::main();
74
75 // Add the form selector to the search tools options
76 $options['formSelector'] = $selector;
77
78 // Generate options with default values
79 $options = static::optionsToRegistry($options);
80
81 $doc = JFactory::getDocument();
82 $script = "
83 (function($){
84 $(document).ready(function() {
85 $('" . $selector . "').searchtools(
86 " . $options->toString() . "
87 );
88 });
89 })(jQuery);
90 ";
91 $doc->addScriptDeclaration($script);
92
93 static::$loaded[__METHOD__][$sig] = true;
94 }
95
96 return;
97 }
98
99 /**
100 * Function to receive & pre-process javascript options
101 *
102 * @param mixed $options Associative array/Registry object with options
103 *
104 * @return Registry Options converted to Registry object
105 */
106 private static function optionsToRegistry($options)
107 {
108 // Support options array
109 if (is_array($options))
110 {
111 $options = new Registry($options);
112 }
113
114 if (!($options instanceof Registry))
115 {
116 $options = new Registry;
117 }
118
119 return $options;
120 }
121
122 /**
123 * Method to sort a column in a grid
124 *
125 * @param string $title The link title
126 * @param string $order The order field for the column
127 * @param string $direction The current direction
128 * @param mixed $selected The selected ordering
129 * @param string $task An optional task override
130 * @param string $new_direction An optional direction for the new column
131 * @param string $tip An optional text shown as tooltip title instead of $title
132 * @param string $icon Icon to show
133 * @param string $formName Name of the form to submit
134 *
135 * @return string
136 */
137 public static function sort($title, $order, $direction = 'asc', $selected = 0, $task = null, $new_direction = 'asc', $tip = '', $icon = null,
138 $formName = 'adminForm')
139 {
140 $direction = strtolower($direction);
141 $orderIcons = array('icon-arrow-up-3', 'icon-arrow-down-3');
142 $index = (int) ($direction === 'desc');
143
144 if ($order !== $selected)
145 {
146 $direction = $new_direction;
147 }
148 else
149 {
150 $direction = $direction === 'desc' ? 'asc' : 'desc';
151 }
152
153 // Create an object to pass it to the layouts
154 $data = new stdClass;
155 $data->order = $order;
156 $data->direction = $direction;
157 $data->selected = $selected;
158 $data->task = $task;
159 $data->tip = $tip;
160 $data->title = $title;
161 $data->orderIcon = $orderIcons[$index];
162 $data->icon = $icon;
163 $data->formName = $formName;
164
165 return JLayoutHelper::render('joomla.searchtools.grid.sort', $data);
166 }
167 }
168