1<?php
2/**
3 * @package     Joomla.Libraries
4 * @subpackage  HTML
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10defined('JPATH_PLATFORM') or die;
11
12/**
13 * HTML utility class for building a dropdown menu
14 *
15 * @since  3.0
16 */
17abstract class JHtmlDropdown
18{
19	/**
20	 * @var    array  Array containing information for loaded files
21	 * @since  3.0
22	 */
23	protected static $loaded = array();
24
25	/**
26	 * @var    string  HTML markup for the dropdown list
27	 * @since  3.0
28	 */
29	protected static $dropDownList = null;
30
31	/**
32	 * Method to inject needed script
33	 *
34	 * @return  void
35	 *
36	 * @since   3.0
37	 */
38	public static function init()
39	{
40		// Only load once
41		if (isset(static::$loaded[__METHOD__]))
42		{
43			return;
44		}
45
46		// Depends on Bootstrap
47		JHtml::_('bootstrap.framework');
48
49		JFactory::getDocument()->addScriptDeclaration("
50			(function($){
51				$(document).ready(function (){
52					$('.has-context')
53					.mouseenter(function (){
54						$('.btn-group',$(this)).show();
55					})
56					.mouseleave(function (){
57						$('.btn-group',$(this)).hide();
58						$('.btn-group',$(this)).removeClass('open');
59					});
60
61					contextAction =function (cbId, task)
62					{
63						$('input[name=\"cid[]\"]').removeAttr('checked');
64						$('#' + cbId).attr('checked','checked');
65						Joomla.submitbutton(task);
66					}
67				});
68			})(jQuery);
69			"
70		);
71
72		// Set static array
73		static::$loaded[__METHOD__] = true;
74
75		return;
76	}
77
78	/**
79	 * Method to start a new dropdown menu
80	 *
81	 * @return  void
82	 *
83	 * @since   3.0
84	 */
85	public static function start()
86	{
87		// Only start once
88		if (isset(static::$loaded[__METHOD__]) && static::$loaded[__METHOD__] == true)
89		{
90			return;
91		}
92
93		$dropDownList = '<div class="btn-group" style="margin-left:6px;display:none">
94							<a href="#" data-toggle="dropdown" class="dropdown-toggle btn btn-mini">
95								<span class="caret"></span>
96							</a>
97							<ul class="dropdown-menu">';
98		static::$dropDownList = $dropDownList;
99		static::$loaded[__METHOD__] = true;
100
101		return;
102	}
103
104	/**
105	 * Method to render current dropdown menu
106	 *
107	 * @return  string  HTML markup for the dropdown list
108	 *
109	 * @since   3.0
110	 */
111	public static function render()
112	{
113		$dropDownList  = static::$dropDownList;
114		$dropDownList .= '</ul></div>';
115
116		static::$dropDownList = null;
117		static::$loaded['JHtmlDropdown::start'] = false;
118
119		return $dropDownList;
120	}
121
122	/**
123	 * Append an edit item to the current dropdown menu
124	 *
125	 * @param   integer  $id          Record ID
126	 * @param   string   $prefix      Task prefix
127	 * @param   string   $customLink  The custom link if dont use default Joomla action format
128	 *
129	 * @return  void
130	 *
131	 * @since   3.0
132	 */
133	public static function edit($id, $prefix = '', $customLink = '')
134	{
135		static::start();
136
137		if (!$customLink)
138		{
139			$option = JFactory::getApplication()->input->getCmd('option');
140			$link = 'index.php?option=' . $option;
141		}
142		else
143		{
144			$link = $customLink;
145		}
146
147		$link .= '&task=' . $prefix . 'edit&id=' . $id;
148		$link = JRoute::_($link);
149
150		static::addCustomItem(JText::_('JACTION_EDIT'), $link);
151
152		return;
153	}
154
155	/**
156	 * Append a publish item to the current dropdown menu
157	 *
158	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
159	 * @param   string  $prefix      The task prefix
160	 *
161	 * @return  void
162	 *
163	 * @since   3.0
164	 */
165	public static function publish($checkboxId, $prefix = '')
166	{
167		$task = $prefix . 'publish';
168		static::addCustomItem(JText::_('JTOOLBAR_PUBLISH'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
169
170		return;
171	}
172
173	/**
174	 * Append an unpublish item to the current dropdown menu
175	 *
176	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
177	 * @param   string  $prefix      The task prefix
178	 *
179	 * @return  void
180	 *
181	 * @since   3.0
182	 */
183	public static function unpublish($checkboxId, $prefix = '')
184	{
185		$task = $prefix . 'unpublish';
186		static::addCustomItem(JText::_('JTOOLBAR_UNPUBLISH'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
187
188		return;
189	}
190
191	/**
192	 * Append a featured item to the current dropdown menu
193	 *
194	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
195	 * @param   string  $prefix      The task prefix
196	 *
197	 * @return  void
198	 *
199	 * @since   3.0
200	 */
201	public static function featured($checkboxId, $prefix = '')
202	{
203		$task = $prefix . 'featured';
204		static::addCustomItem(JText::_('JFEATURED'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
205
206		return;
207	}
208
209	/**
210	 * Append an unfeatured item to the current dropdown menu
211	 *
212	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
213	 * @param   string  $prefix      The task prefix
214	 *
215	 * @return  void
216	 *
217	 * @since   3.0
218	 */
219	public static function unfeatured($checkboxId, $prefix = '')
220	{
221		$task = $prefix . 'unfeatured';
222		static::addCustomItem(JText::_('JUNFEATURED'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
223
224		return;
225	}
226
227	/**
228	 * Append an archive item to the current dropdown menu
229	 *
230	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
231	 * @param   string  $prefix      The task prefix
232	 *
233	 * @return  void
234	 *
235	 * @since   3.0
236	 */
237	public static function archive($checkboxId, $prefix = '')
238	{
239		$task = $prefix . 'archive';
240		static::addCustomItem(JText::_('JTOOLBAR_ARCHIVE'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
241
242		return;
243	}
244
245	/**
246	 * Append an unarchive item to the current dropdown menu
247	 *
248	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
249	 * @param   string  $prefix      The task prefix
250	 *
251	 * @return  void
252	 *
253	 * @since   3.0
254	 */
255	public static function unarchive($checkboxId, $prefix = '')
256	{
257		$task = $prefix . 'unpublish';
258		static::addCustomItem(JText::_('JTOOLBAR_UNARCHIVE'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
259
260		return;
261	}
262
263	/**
264	 * Append a trash item to the current dropdown menu
265	 *
266	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
267	 * @param   string  $prefix      The task prefix
268	 *
269	 * @return  void
270	 *
271	 * @since   3.0
272	 */
273	public static function trash($checkboxId, $prefix = '')
274	{
275		$task = $prefix . 'trash';
276		static::addCustomItem(JText::_('JTOOLBAR_TRASH'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
277
278		return;
279	}
280
281	/**
282	 * Append an untrash item to the current dropdown menu
283	 *
284	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
285	 * @param   string  $prefix      The task prefix
286	 *
287	 * @return  void
288	 *
289	 * @since   3.0
290	 */
291	public static function untrash($checkboxId, $prefix = '')
292	{
293		$task = $prefix . 'publish';
294		static::addCustomItem(JText::_('JTOOLBAR_UNTRASH'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
295
296		return;
297	}
298
299	/**
300	 * Append a checkin item to the current dropdown menu
301	 *
302	 * @param   string  $checkboxId  ID of corresponding checkbox of the record
303	 * @param   string  $prefix      The task prefix
304	 *
305	 * @return  void
306	 *
307	 * @since   3.0
308	 */
309	public static function checkin($checkboxId, $prefix = '')
310	{
311		$task = $prefix . 'checkin';
312		static::addCustomItem(JText::_('JTOOLBAR_CHECKIN'), 'javascript:void(0)', 'onclick="contextAction(\'' . $checkboxId . '\', \'' . $task . '\')"');
313
314		return;
315	}
316
317	/**
318	 * Writes a divider between dropdown items
319	 *
320	 * @return  void
321	 *
322	 * @since   3.0
323	 */
324	public static function divider()
325	{
326		static::$dropDownList .= '<li class="divider"></li>';
327
328		return;
329	}
330
331	/**
332	 * Append a custom item to current dropdown menu
333	 *
334	 * @param   string   $label           The label of item
335	 * @param   string   $link            The link of item
336	 * @param   string   $linkAttributes  Custom link attributes
337	 * @param   string   $className       Class name of item
338	 * @param   boolean  $ajaxLoad        True if using ajax load when item clicked
339	 * @param   string   $jsCallBackFunc  Javascript function name, called when ajax load successfully
340	 *
341	 * @return  void
342	 *
343	 * @since   3.0
344	 */
345	public static function addCustomItem($label, $link = 'javascript:void(0)', $linkAttributes = '', $className = '', $ajaxLoad = false,
346		$jsCallBackFunc = null)
347	{
348		static::start();
349
350		if ($ajaxLoad)
351		{
352			$href = ' href = "javascript:void(0)" onclick="loadAjax(\'' . $link . '\', \'' . $jsCallBackFunc . '\')"';
353		}
354		else
355		{
356			$href = ' href = "' . $link . '" ';
357		}
358
359		$dropDownList = static::$dropDownList;
360		$dropDownList .= '<li class="' . $className . '"><a ' . $linkAttributes . $href . ' >';
361		$dropDownList .= $label;
362		$dropDownList .= '</a></li>';
363		static::$dropDownList = $dropDownList;
364
365		return;
366	}
367}
368