1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Toolbar
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 /**
13 * Renders a modal window button
14 *
15 * @since 3.0
16 */
17 class JToolbarButtonPopup extends JToolbarButton
18 {
19 /**
20 * Button type
21 *
22 * @var string
23 */
24 protected $_name = 'Popup';
25
26 /**
27 * Fetch the HTML for the button
28 *
29 * @param string $type Unused string, formerly button type.
30 * @param string $name Modal name, used to generate element ID
31 * @param string $text The link text
32 * @param string $url URL for popup
33 * @param integer $width Width of popup
34 * @param integer $height Height of popup
35 * @param integer $top Top attribute. [@deprecated Unused, will be removed in 4.0]
36 * @param integer $left Left attribute. [@deprecated Unused, will be removed in 4.0]
37 * @param string $onClose JavaScript for the onClose event.
38 * @param string $title The title text
39 * @param string $footer The footer html
40 *
41 * @return string HTML string for the button
42 *
43 * @since 3.0
44 */
45 public function fetchButton($type = 'Modal', $name = '', $text = '', $url = '', $width = 640, $height = 480, $top = 0, $left = 0,
46 $onClose = '', $title = '', $footer = null)
47 {
48 // If no $title is set, use the $text element
49 if ($title === '')
50 {
51 $title = $text;
52 }
53
54 // Store all data to the options array for use with JLayout
55 $options = array();
56 $options['name'] = $name;
57 $options['text'] = JText::_($text);
58 $options['title'] = JText::_($title);
59 $options['class'] = $this->fetchIconClass($name);
60 $options['doTask'] = $this->_getCommand($url);
61
62 // Instantiate a new JLayoutFile instance and render the layout
63 $layout = new JLayoutFile('joomla.toolbar.popup');
64
65 $html = array();
66 $html[] = $layout->render($options);
67
68 // Place modal div and scripts in a new div
69 $html[] = '<div class="btn-group" style="width: 0; margin: 0">';
70
71 // Build the options array for the modal
72 $params = array();
73 $params['title'] = $options['title'];
74 $params['url'] = $options['doTask'];
75 $params['height'] = $height;
76 $params['width'] = $width;
77
78 if (isset($footer))
79 {
80 $params['footer'] = $footer;
81 }
82
83 $html[] = JHtml::_('bootstrap.renderModal', 'modal-' . $name, $params);
84
85 // If an $onClose event is passed, add it to the modal JS object
86 if ($onClose !== '')
87 {
88 $html[] = '<script>'
89 . 'jQuery(\'#modal-' . $name . '\').on(\'hide\', function () {' . $onClose . ';});'
90 . '</script>';
91 }
92
93 $html[] = '</div>';
94
95 return implode("\n", $html);
96 }
97
98 /**
99 * Get the button id
100 *
101 * @param string $type Button type
102 * @param string $name Button name
103 *
104 * @return string Button CSS Id
105 *
106 * @since 3.0
107 */
108 public function fetchId($type, $name)
109 {
110 return $this->_parent->getName() . '-popup-' . $name;
111 }
112
113 /**
114 * Get the JavaScript command for the button
115 *
116 * @param string $url URL for popup
117 *
118 * @return string JavaScript command string
119 *
120 * @since 3.0
121 */
122 private function _getCommand($url)
123 {
124 if (strpos($url, 'http') !== 0)
125 {
126 $url = JUri::base() . $url;
127 }
128
129 return $url;
130 }
131 }
132