helper.php
389 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.toolbar |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | JLoader::import('adapter.toolbar.toolbar'); |
| 15 | |
| 16 | /** |
| 17 | * Utility class for the button bar. |
| 18 | * |
| 19 | * @since 10.0 |
| 20 | */ |
| 21 | abstract class JToolbarHelper |
| 22 | { |
| 23 | /** |
| 24 | * The list containing the buttons to render. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $buttons = array(); |
| 29 | |
| 30 | /** |
| 31 | * Title cell. |
| 32 | * For the title and toolbar to be rendered correctly, |
| 33 | * this title function must be called before the starttable function and the toolbars icons |
| 34 | * this is due to the nature of how the css has been used to position the title in respect to the toolbar. |
| 35 | * |
| 36 | * @param string $title The title. |
| 37 | * @param string $icon The space-separated names of the image. |
| 38 | * |
| 39 | * @return void |
| 40 | */ |
| 41 | public static function title($title, $icon = null) |
| 42 | { |
| 43 | $bar = JToolbar::getInstance(); |
| 44 | |
| 45 | $bar->setTitle($title); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Writes a spacer cell. |
| 50 | * |
| 51 | * @param string $width The width for the cell. |
| 52 | * |
| 53 | * @return void |
| 54 | */ |
| 55 | public static function spacer($width = null) |
| 56 | { |
| 57 | $bar = JToolbar::getInstance(); |
| 58 | |
| 59 | $bar->appendButton('Separator', 'spacer', $width); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Writes a divider between menu buttons. |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public static function divider() |
| 68 | { |
| 69 | $bar = JToolbar::getInstance(); |
| 70 | |
| 71 | $bar->appendButton('Separator', 'divider'); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Writes a custom option and task button for the button bar. |
| 76 | * |
| 77 | * @param string $task The task to perform (picked up by the switch($task) blocks). |
| 78 | * @param string $icon The image to display. |
| 79 | * @param string $iconOver The image to display when moused over. |
| 80 | * @param string $alt The alt text for the icon image. |
| 81 | * @param bool $listSelect True if required to check that a standard list item is checked. |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | public static function custom($task = '', $icon = '', $iconOver = '', $alt = '', $listSelect = true) |
| 86 | { |
| 87 | $bar = JToolbar::getInstance(); |
| 88 | |
| 89 | $bar->appendButton('Standard', $icon, $alt, $task, $listSelect); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Writes a cancel button that will go back to the previous page without doing |
| 94 | * any other operation. |
| 95 | * |
| 96 | * @param string $alt Alternative text. |
| 97 | * @param string $href URL of the href attribute. |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | public static function back($alt = 'JTOOLBAR_BACK', $href = 'javascript:history.back();') |
| 102 | { |
| 103 | $bar = JToolbar::getInstance(); |
| 104 | |
| 105 | $bar->appendButton('Link', 'back', $alt, $href); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Writes the common 'new' icon for the button bar. |
| 110 | * |
| 111 | * @param string $task An override for the task. |
| 112 | * @param string $alt An override for the alt text. |
| 113 | * @param boolean $check True if required to check that a standard list item is checked. |
| 114 | * |
| 115 | * @return void |
| 116 | * |
| 117 | * @uses custom() |
| 118 | */ |
| 119 | public static function addNew($task = 'add', $alt = 'JTOOLBAR_NEW', $check = false) |
| 120 | { |
| 121 | $bar = JToolbar::getInstance(); |
| 122 | |
| 123 | $bar->appendButton('Standard', 'new', $alt, $task, $check); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Writes a common 'publish' button for a list of records. |
| 128 | * |
| 129 | * @param string $task An override for the task. |
| 130 | * @param string $alt An override for the alt text. |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
| 134 | public static function publishList($task = 'publish', $alt = 'JTOOLBAR_PUBLISH') |
| 135 | { |
| 136 | $bar = JToolbar::getInstance(); |
| 137 | |
| 138 | $bar->appendButton('Standard', 'publish', $alt, $task, true); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Writes a common 'unpublish' button for a list of records. |
| 143 | * |
| 144 | * @param string $task An override for the task. |
| 145 | * @param string $alt An override for the alt text. |
| 146 | * |
| 147 | * @return void |
| 148 | */ |
| 149 | public static function unpublishList($task = 'unpublish', $alt = 'JTOOLBAR_UNPUBLISH') |
| 150 | { |
| 151 | $bar = JToolbar::getInstance(); |
| 152 | |
| 153 | $bar->appendButton('Standard', 'unpublish', $alt, $task, true); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Writes a common 'archive' button for a list of records. |
| 158 | * |
| 159 | * @param string $task An override for the task. |
| 160 | * @param string $alt An override for the alt text. |
| 161 | * |
| 162 | * @return void |
| 163 | */ |
| 164 | public static function archiveList($task = 'archive', $alt = 'JTOOLBAR_ARCHIVE') |
| 165 | { |
| 166 | $bar = JToolbar::getInstance(); |
| 167 | |
| 168 | $bar->appendButton('Standard', 'archive', $alt, $task, true); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Writes an unarchive button for a list of records. |
| 173 | * |
| 174 | * @param string $task An override for the task. |
| 175 | * @param string $alt An override for the alt text. |
| 176 | * |
| 177 | * @return void |
| 178 | */ |
| 179 | public static function unarchiveList($task = 'unarchive', $alt = 'JTOOLBAR_UNARCHIVE') |
| 180 | { |
| 181 | $bar = JToolbar::getInstance(); |
| 182 | |
| 183 | $bar->appendButton('Standard', 'unarchive', $alt, $task, true); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Writes a common 'edit' button for a list of records. |
| 188 | * |
| 189 | * @param string $task An override for the task. |
| 190 | * @param string $alt An override for the alt text. |
| 191 | * |
| 192 | * @return void |
| 193 | */ |
| 194 | public static function editList($task = 'edit', $alt = 'JTOOLBAR_EDIT') |
| 195 | { |
| 196 | $bar = JToolbar::getInstance(); |
| 197 | |
| 198 | $bar->appendButton('Standard', 'edit', $alt, $task, true); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Writes a common 'delete' button for a list of records. |
| 203 | * |
| 204 | * @param string $msg Postscript for the 'are you sure' message. |
| 205 | * @param string $task An override for the task. |
| 206 | * @param string $alt An override for the alt text. |
| 207 | * |
| 208 | * @return void |
| 209 | */ |
| 210 | public static function deleteList($msg = '', $task = 'remove', $alt = 'JTOOLBAR_DELETE') |
| 211 | { |
| 212 | $bar = JToolbar::getInstance(); |
| 213 | |
| 214 | if ($msg) |
| 215 | { |
| 216 | $bar->appendButton('Confirm', $msg, 'remove', $alt, $task, true); |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | $bar->appendButton('Standard', 'remove', $alt, $task, true); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Writes a common 'trash' button for a list of records. |
| 226 | * |
| 227 | * @param string $task An override for the task. |
| 228 | * @param string $alt An override for the alt text. |
| 229 | * @param bool $check True to allow lists. |
| 230 | * |
| 231 | * @return void |
| 232 | */ |
| 233 | public static function trash($task = 'remove', $alt = 'JTOOLBAR_TRASH', $check = true) |
| 234 | { |
| 235 | $bar = JToolbar::getInstance(); |
| 236 | |
| 237 | $bar->appendButton('Standard', 'trash', $alt, $task, $check); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Writes a save button for a given option. |
| 242 | * Apply operation leads to a save action only (does not leave edit mode). |
| 243 | * |
| 244 | * @param string $task An override for the task. |
| 245 | * @param string $alt An override for the alt text. |
| 246 | * |
| 247 | * @return void |
| 248 | */ |
| 249 | public static function apply($task = 'apply', $alt = 'JTOOLBAR_APPLY') |
| 250 | { |
| 251 | $bar = JToolbar::getInstance(); |
| 252 | |
| 253 | $bar->appendButton('Standard', 'apply', $alt, $task, false); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Writes a save button for a given option. |
| 258 | * Save operation leads to a save and then close action. |
| 259 | * |
| 260 | * @param string $task An override for the task. |
| 261 | * @param string $alt An override for the alt text. |
| 262 | * |
| 263 | * @return void |
| 264 | */ |
| 265 | public static function save($task = 'save', $alt = 'JTOOLBAR_SAVE') |
| 266 | { |
| 267 | $bar = JToolbar::getInstance(); |
| 268 | |
| 269 | $bar->appendButton('Standard', 'save', $alt, $task, false); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Writes a save and create new button for a given option. |
| 274 | * Save and create operation leads to a save and then add action. |
| 275 | * |
| 276 | * @param string $task An override for the task. |
| 277 | * @param string $alt An override for the alt text. |
| 278 | * |
| 279 | * @return void |
| 280 | */ |
| 281 | public static function save2new($task = 'save2new', $alt = 'JTOOLBAR_SAVE_AND_NEW') |
| 282 | { |
| 283 | $bar = JToolbar::getInstance(); |
| 284 | |
| 285 | $bar->appendButton('Standard', 'savenew', $alt, $task, false); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Writes a save as copy button for a given option. |
| 290 | * Save as copy operation leads to a save after clearing the key, |
| 291 | * then returns user to edit mode with new key. |
| 292 | * |
| 293 | * @param string $task An override for the task. |
| 294 | * @param string $alt An override for the alt text. |
| 295 | * |
| 296 | * @return void |
| 297 | * |
| 298 | * @since 1.6 |
| 299 | */ |
| 300 | public static function save2copy($task = 'save2copy', $alt = 'JTOOLBAR_SAVE_AS_COPY') |
| 301 | { |
| 302 | $bar = JToolbar::getInstance(); |
| 303 | |
| 304 | $bar->appendButton('Standard', 'savecopy', $alt, $task, false); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Writes a cancel button and invokes a cancel operation (eg a checkin). |
| 309 | * |
| 310 | * @param string $task An override for the task. |
| 311 | * @param string $alt An override for the alt text. |
| 312 | * |
| 313 | * @return void |
| 314 | */ |
| 315 | public static function cancel($task = 'cancel', $alt = 'JTOOLBAR_CANCEL') |
| 316 | { |
| 317 | $bar = JToolbar::getInstance(); |
| 318 | |
| 319 | $bar->appendButton('Standard', 'cancel', $alt, $task, false); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Writes a configuration button and invokes a cancel operation (eg a checkin). |
| 324 | * |
| 325 | * @param string $component The name of the component, eg, com_content. |
| 326 | * |
| 327 | * @return void |
| 328 | */ |
| 329 | public static function preferences($component) |
| 330 | { |
| 331 | $component = urlencode($component); |
| 332 | |
| 333 | $uri = JUri::current(); |
| 334 | $return = urlencode(base64_encode($uri)); |
| 335 | |
| 336 | // Add a button linking to the ACL configuration page of the component |
| 337 | static::link( |
| 338 | 'admin.php?option=' . $component . '&view=acl&return=' . $return, |
| 339 | 'JTOOLBAR_OPTIONS', |
| 340 | 'options' |
| 341 | ); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Writes a button to create shortcodes. |
| 346 | * |
| 347 | * @param string $component The name of the component, eg, com_content. |
| 348 | * |
| 349 | * @return void |
| 350 | */ |
| 351 | public static function shortcodes($component) |
| 352 | { |
| 353 | $component = urlencode($component); |
| 354 | |
| 355 | $uri = JUri::current(); |
| 356 | $return = urlencode(base64_encode($uri)); |
| 357 | |
| 358 | // Add a button linking to the shortcodes management page of the component |
| 359 | static::link( |
| 360 | 'admin.php?option=' . $component . '&view=shortcodes&return=' . $return, |
| 361 | 'JTOOLBAR_SHORTCODES', |
| 362 | 'codes' |
| 363 | ); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Creates a button to redirect to a link. |
| 368 | * |
| 369 | * @param string $url The link url. |
| 370 | * @param string $text The Bìbutton text. |
| 371 | * @param string $name Name to be used as apart of the id. |
| 372 | * |
| 373 | * @return void |
| 374 | * |
| 375 | * @since 10.1.48 |
| 376 | */ |
| 377 | public static function link($url, $text, $name = 'link') |
| 378 | { |
| 379 | $bar = JToolbar::getInstance(); |
| 380 | |
| 381 | $bar->appendButton( |
| 382 | 'Link', |
| 383 | $name, |
| 384 | $text, |
| 385 | $url |
| 386 | ); |
| 387 | } |
| 388 | } |
| 389 |