[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Toolbar/ -> CoreButtonsTrait.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Toolbar;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\CMS\Layout\FileLayout;
  15  use Joomla\CMS\Toolbar\Button\ConfirmButton;
  16  use Joomla\CMS\Toolbar\Button\CustomButton;
  17  use Joomla\CMS\Toolbar\Button\HelpButton;
  18  use Joomla\CMS\Toolbar\Button\LinkButton;
  19  use Joomla\CMS\Toolbar\Button\PopupButton;
  20  use Joomla\CMS\Toolbar\Button\SeparatorButton;
  21  use Joomla\CMS\Toolbar\Button\StandardButton;
  22  use Joomla\CMS\Uri\Uri;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Enhance Toolbar class to add more pre-defined methods.
  30   *
  31   * @since  4.0.0
  32   */
  33  trait CoreButtonsTrait
  34  {
  35      /**
  36       * Writes a divider between dropdown menu items.
  37       *
  38       * @param   string  $text  The text of button.
  39       *
  40       * @return  SeparatorButton
  41       *
  42       * @since  4.0.0
  43       */
  44      public function divider(string $text = ''): SeparatorButton
  45      {
  46          return $this->separatorButton('divider', $text);
  47      }
  48  
  49      /**
  50       * Writes a preview button for a given option (opens a popup window).
  51       *
  52       * @param   string  $url        The name of the popup file (excluding the file extension)
  53       * @param   string  $text       The text of button.
  54       * @param   bool    $newWindow  Whether to open the preview in _blank or just a modal
  55       *
  56       * @return  PopupButton|LinkButton
  57       *
  58       * @since   4.0.0
  59       */
  60      public function preview(string $url, string $text = 'JGLOBAL_PREVIEW', $newWindow = false)
  61      {
  62          if ($newWindow === true) {
  63              $button = $this->linkButton('link', $text)
  64                  ->url($url)
  65                  ->attributes(['target' => '_blank'])
  66                  ->icon('icon-eye');
  67          } else {
  68              $button = $this->popupButton('preview', $text)
  69                  ->url($url)
  70                  ->iframeWidth(640)
  71                  ->iframeHeight(480)
  72                  ->icon('icon-eye');
  73          }
  74  
  75          return $button;
  76      }
  77  
  78      /**
  79       * Writes a jooa11y accessibility checker button for a given option (opens a popup window).
  80       *
  81       * @param   string  $url        The url to open
  82       * @param   string  $text       The text of button.
  83       * @param   bool    $newWindow  Whether to open the preview in _blank or just a modal
  84       *
  85       * @return  PopupButton|LinkButton
  86       *
  87       * @since   4.1.0
  88       */
  89      public function jooa11y(string $url, string $text = 'JGLOBAL_JOOA11Y', $newWindow = false)
  90      {
  91          if ($newWindow === true) {
  92              $button = $this->linkButton('jooa11y-link', $text)
  93                  ->url($url)
  94                  ->attributes(['target' => '_blank'])
  95                  ->icon('icon-universal-access');
  96          } else {
  97              $button = $this->popupButton('jooa11y-preview', $text)
  98                  ->url($url)
  99                  ->iframeWidth(640)
 100                  ->iframeHeight(480)
 101                  ->icon('icon-universal-access');
 102          }
 103  
 104          return $button;
 105      }
 106  
 107      /**
 108       * Writes a help button for a given option (opens a popup window).
 109       *
 110       * @param   string  $ref           The name of the popup file (excluding the file extension for an xml file).
 111       * @param   bool    $useComponent  Use the help file in the component directory.
 112       * @param   string  $url           Use this URL instead of any other.
 113       * @param   string  $component     Name of component to get Help (null for current component)
 114       *
 115       * @return  HelpButton
 116       *
 117       * @since   4.0.0
 118       */
 119      public function help($ref, $useComponent = false, $url = null, $component = null): HelpButton
 120      {
 121          return $this->helpButton('help', 'JTOOLBAR_HELP')
 122              ->ref($ref)
 123              ->useComponent($useComponent)
 124              ->url($url)
 125              ->component($component);
 126      }
 127  
 128      /**
 129       * Writes a cancel button that will go back to the previous page without doing
 130       * any other operation.
 131       *
 132       * @param   string  $text  The text of button.
 133       *
 134       * @return  LinkButton
 135       *
 136       * @since   4.0.0
 137       */
 138      public function back(string $text = 'JTOOLBAR_BACK'): LinkButton
 139      {
 140          return $this->link('back', $text)
 141              ->url('javascript:history.back();');
 142      }
 143  
 144      /**
 145       * Creates a button to redirect to a link.
 146       *
 147       * @param   string  $text  Button text.
 148       * @param   string  $url   The link url.
 149       *
 150       * @return  LinkButton
 151       *
 152       * @since   4.0.0
 153       */
 154      public function link(string $text, string $url): LinkButton
 155      {
 156          return $this->linkButton('link', $text)
 157              ->url($url);
 158      }
 159  
 160      /**
 161       * Writes a media_manager button.
 162       *
 163       * @param   string  $directory  The subdirectory to upload the media to.
 164       * @param   string  $text       An override for the alt text.
 165       *
 166       * @return  PopupButton
 167       *
 168       * @since   4.0.0
 169       */
 170      public function mediaManager(string $directory, string $text = 'JTOOLBAR_UPLOAD'): PopupButton
 171      {
 172          return $this->popupButton('upload', $text)
 173              ->iframeWidth(800)
 174              ->iframeHeight(520)
 175              ->url('index.php?option=com_media&tmpl=component&task=popupUpload&folder=' . $directory);
 176      }
 177  
 178      /**
 179       * Writes a common 'default' button for a record.
 180       *
 181       * @param   string  $task  An override for the task.
 182       * @param   string  $text  An override for the alt text.
 183       *
 184       * @return  StandardButton
 185       *
 186       * @since   4.0.0
 187       */
 188      public function makeDefault(string $task, string $text = 'JTOOLBAR_DEFAULT'): StandardButton
 189      {
 190          return $this->standardButton('default', $text)
 191              ->task($task);
 192      }
 193  
 194      /**
 195       * Writes a common 'assign' button for a record.
 196       *
 197       * @param   string  $task  The task name of this button.
 198       * @param   string  $text  The text of this button.
 199       *
 200       * @return  StandardButton
 201       *
 202       * @since   4.0.0
 203       */
 204      public function assign(string $task, string $text = 'JTOOLBAR_ASSIGN'): StandardButton
 205      {
 206          return $this->standardButton('assign', $text)
 207              ->task($task);
 208      }
 209  
 210      /**
 211       * Writes the common 'new' icon for the button bar.
 212       *
 213       * @param   string  $task  The task name of this button.
 214       * @param   string  $text  The text of this button.
 215       *
 216       * @return  StandardButton
 217       *
 218       * @since   4.0.0
 219       */
 220      public function addNew(string $task, string $text = 'JTOOLBAR_NEW'): StandardButton
 221      {
 222          return $this->standardButton('new', $text)
 223              ->task($task);
 224      }
 225  
 226      /**
 227       * Writes a common 'publish' button.
 228       *
 229       * @param   string  $task  The task name of this button.
 230       * @param   string  $text  The text of this button.
 231       *
 232       * @return  StandardButton
 233       *
 234       * @since   4.0.0
 235       */
 236      public function publish(string $task, string $text = 'JTOOLBAR_PUBLISH'): StandardButton
 237      {
 238          return $this->standardButton('publish', $text)
 239              ->task($task);
 240      }
 241  
 242      /**
 243       * Writes a common 'unpublish' button.
 244       *
 245       * @param   string  $task  The task name of this button.
 246       * @param   string  $text  The text of this button.
 247       *
 248       * @return  StandardButton
 249       *
 250       * @since   4.0.0
 251       */
 252      public function unpublish(string $task, string $text = 'JTOOLBAR_UNPUBLISH'): StandardButton
 253      {
 254          return $this->standardButton('unpublish', $text)
 255              ->task($task);
 256      }
 257  
 258      /**
 259       * Writes a common 'archive' button.
 260       *
 261       * @param   string  $task  The task name of this button.
 262       * @param   string  $text  The text of this button.
 263       *
 264       * @return  StandardButton
 265       *
 266       * @since   4.0.0
 267       */
 268      public function archive(string $task, string $text = 'JTOOLBAR_ARCHIVE'): StandardButton
 269      {
 270          return $this->standardButton('archive', $text)
 271              ->task($task);
 272      }
 273  
 274      /**
 275       * Writes a common 'unarchive' button.
 276       *
 277       * @param   string  $task  The task name of this button.
 278       * @param   string  $text  The text of this button.
 279       *
 280       * @return  StandardButton
 281       *
 282       * @since   4.0.0
 283       */
 284      public function unarchive(string $task, string $text = 'JTOOLBAR_UNARCHIVE'): StandardButton
 285      {
 286          return $this->standardButton('unarchive', $text)
 287              ->task($task);
 288      }
 289  
 290      /**
 291       * Writes a common 'edit' button.
 292       *
 293       * @param   string  $task  The task name of this button.
 294       * @param   string  $text  The text of this button.
 295       *
 296       * @return  StandardButton
 297       *
 298       * @since   4.0.0
 299       */
 300      public function edit(string $task, string $text = 'JTOOLBAR_EDIT'): StandardButton
 301      {
 302          return $this->standardButton('edit', $text)
 303              ->task($task);
 304      }
 305  
 306      /**
 307       * Writes a common 'editHtml' button.
 308       *
 309       * @param   string  $task  The task name of this button.
 310       * @param   string  $text  The text of this button.
 311       *
 312       * @return  StandardButton
 313       *
 314       * @since   4.0.0
 315       */
 316      public function editHtml(string $task, string $text = 'JTOOLBAR_EDIT_HTML'): StandardButton
 317      {
 318          return $this->standardButton('edithtml', $text)
 319              ->task($task);
 320      }
 321  
 322      /**
 323       * Writes a common 'editCss' button.
 324       *
 325       * @param   string  $task  The task name of this button.
 326       * @param   string  $text  The text of this button.
 327       *
 328       * @return  StandardButton
 329       *
 330       * @since   4.0.0
 331       */
 332      public function editCss(string $task, string $text = 'JTOOLBAR_EDIT_CSS'): StandardButton
 333      {
 334          return $this->standardButton('editcss', $text)
 335              ->task($task);
 336      }
 337  
 338      /**
 339       * Writes a common 'delete' button.
 340       *
 341       * @param   string  $task  The task name of this button.
 342       * @param   string  $text  The text of this button.
 343       *
 344       * @return  ConfirmButton
 345       *
 346       * @since   4.0.0
 347       */
 348      public function delete(string $task, string $text = 'JTOOLBAR_DELETE'): ConfirmButton
 349      {
 350          return $this->confirmButton('delete', $text)
 351              ->task($task);
 352      }
 353  
 354      /**
 355       * Writes a common 'trash' button.
 356       *
 357       * @param   string  $task  The task name of this button.
 358       * @param   string  $text  The text of this button.
 359       *
 360       * @return  StandardButton
 361       *
 362       * @since   4.0.0
 363       */
 364      public function trash(string $task, string $text = 'JTOOLBAR_TRASH'): StandardButton
 365      {
 366          return $this->standardButton('trash', $text)
 367              ->task($task);
 368      }
 369  
 370      /**
 371       * Writes a save button for a given option.
 372       * Apply operation leads to a save action only (does not leave edit mode).
 373       *
 374       * @param   string  $task  The task name of this button.
 375       * @param   string  $text  The text of this button.
 376       *
 377       * @return  StandardButton
 378       *
 379       * @since   4.0.0
 380       */
 381      public function apply(string $task, string $text = 'JTOOLBAR_APPLY'): StandardButton
 382      {
 383          return $this->standardButton('apply', $text)
 384              ->task($task)
 385              ->formValidation(true);
 386      }
 387  
 388      /**
 389       * Writes a save button for a given option.
 390       * Save operation leads to a save and then close action.
 391       *
 392       * @param   string  $task  The task name of this button.
 393       * @param   string  $text  The text of this button.
 394       *
 395       * @return  StandardButton
 396       *
 397       * @since   4.0.0
 398       */
 399      public function save(string $task, string $text = 'JTOOLBAR_SAVE'): StandardButton
 400      {
 401          return $this->standardButton('save', $text)
 402              ->task($task)
 403              ->formValidation(true);
 404      }
 405  
 406      /**
 407       * Writes a save and create new button for a given option.
 408       * Save and create operation leads to a save and then add action.
 409       *
 410       * @param   string  $task  The task name of this button.
 411       * @param   string  $text  The text of this button.
 412       *
 413       * @return  StandardButton
 414       *
 415       * @since   4.0.0
 416       */
 417      public function save2new(string $task, string $text = 'JTOOLBAR_SAVE_AND_NEW'): StandardButton
 418      {
 419          return $this->standardButton('save-new', $text)
 420              ->task($task)
 421              ->formValidation(true);
 422      }
 423  
 424      /**
 425       * Writes a save as copy button for a given option.
 426       * Save as copy operation leads to a save after clearing the key,
 427       * then returns user to edit mode with new key.
 428       *
 429       * @param   string  $task  The task name of this button.
 430       * @param   string  $text  The text of this button.
 431       *
 432       * @return  StandardButton
 433       *
 434       * @since   4.0.0
 435       */
 436      public function save2copy(string $task, string $text = 'JTOOLBAR_SAVE_AS_COPY'): StandardButton
 437      {
 438          return $this->standardButton('save-copy', $text)
 439              ->task($task)
 440              ->formValidation(true);
 441      }
 442  
 443      /**
 444       * Writes a checkin button for a given option.
 445       *
 446       * @param   string  $task  The task name of this button.
 447       * @param   string  $text  The text of this button.
 448       *
 449       * @return  StandardButton
 450       *
 451       * @since   4.0.0
 452       */
 453      public function checkin(string $task, string $text = 'JTOOLBAR_CHECKIN'): StandardButton
 454      {
 455          return $this->standardButton('checkin', $text)
 456              ->task($task);
 457      }
 458  
 459      /**
 460       * Writes a cancel button and invokes a cancel operation (eg a checkin).
 461       *
 462       * @param   string  $task  The task name of this button.
 463       * @param   string  $text  The text of this button.
 464       *
 465       * @return  StandardButton
 466       *
 467       * @since   4.0.0
 468       */
 469      public function cancel(string $task, string $text = 'JTOOLBAR_CLOSE'): StandardButton
 470      {
 471          return $this->standardButton('cancel', $text)
 472              ->task($task);
 473      }
 474  
 475      /**
 476       * Writes a configuration button and invokes a cancel operation (eg a checkin).
 477       *
 478       * @param   string  $component  The name of the component, eg, com_content.
 479       * @param   string  $text       The text of this button.
 480       * @param   string  $path       An alternative path for the configuration xml relative to JPATH_SITE.
 481       *
 482       * @return  LinkButton
 483       *
 484       * @since   4.0.0
 485       */
 486      public function preferences(string $component, string $text = 'JTOOLBAR_OPTIONS', string $path = ''): LinkButton
 487      {
 488          $component = urlencode($component);
 489          $path = urlencode($path);
 490  
 491          $uri = (string) Uri::getInstance();
 492          $return = urlencode(base64_encode($uri));
 493  
 494          return $this->linkButton('options', $text)
 495              ->url('index.php?option=com_config&amp;view=component&amp;component=' . $component . '&amp;path=' . $path . '&amp;return=' . $return);
 496      }
 497  
 498      /**
 499       * Writes a version history
 500       *
 501       * @param   string   $typeAlias  The component and type, for example 'com_content.article'
 502       * @param   integer  $itemId     The id of the item, for example the article id.
 503       * @param   integer  $height     The height of the popup.
 504       * @param   integer  $width      The width of the popup.
 505       * @param   string   $text       The name of the button.
 506       *
 507       * @return  CustomButton
 508       *
 509       * @since   4.0.0
 510       */
 511      public function versions(
 512          string $typeAlias,
 513          int $itemId,
 514          int $height = 800,
 515          int $width = 500,
 516          string $text = 'JTOOLBAR_VERSIONS'
 517      ): CustomButton {
 518          $lang = Factory::getLanguage();
 519          $lang->load('com_contenthistory', JPATH_ADMINISTRATOR, $lang->getTag(), true);
 520  
 521          // Options array for Layout
 522          $options              = array();
 523          $options['title']     = Text::_($text);
 524          $options['height']    = $height;
 525          $options['width']     = $width;
 526          $options['itemId']    = $typeAlias . '.' . $itemId;
 527  
 528          $layout = new FileLayout('joomla.toolbar.versions');
 529  
 530          return $this->customHtml($layout->render($options), 'version');
 531      }
 532  
 533      /**
 534       * Writes a custom HTML to toolbar.
 535       *
 536       * @param   string  $html  The HTML string to write.
 537       * @param   string  $name  The button name.
 538       *
 539       * @return  CustomButton
 540       *
 541       * @since   4.0.0
 542       */
 543      public function customHtml(string $html, string $name = 'custom'): CustomButton
 544      {
 545          return $this->customButton($name)
 546              ->html($html);
 547      }
 548  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer