[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Toolbar/Button/ -> PopupButton.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2006 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\Button;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\HTML\HTMLHelper;
  14  use Joomla\CMS\Toolbar\ToolbarButton;
  15  use Joomla\CMS\Uri\Uri;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Renders a modal window button
  23   *
  24   * @method self    url(string $value)
  25   * @method self    icon(string $value)
  26   * @method self    iframeWidth(int $value)
  27   * @method self    iframeHeight(int $value)
  28   * @method self    bodyHeight(int $value)
  29   * @method self    modalWidth(int $value)
  30   * @method self    onclose(string $value)
  31   * @method self    title(string $value)
  32   * @method self    footer(string $value)
  33   * @method self    selector(string $value)
  34   * @method self    listCheck(bool $value)
  35   * @method string  getUrl()
  36   * @method int     getIframeWidth()
  37   * @method int     getIframeHeight()
  38   * @method int     getBodyHeight()
  39   * @method int     getModalWidth()
  40   * @method string  getOnclose()
  41   * @method string  getTitle()
  42   * @method string  getFooter()
  43   * @method string  getSelector()
  44   * @method bool    getListCheck()
  45   *
  46   * @since  3.0
  47   */
  48  class PopupButton extends ToolbarButton
  49  {
  50      /**
  51       * Property layout.
  52       *
  53       * @var  string
  54       *
  55       * @since  4.0.0
  56       */
  57      protected $layout = 'joomla.toolbar.popup';
  58  
  59      /**
  60       * Prepare options for this button.
  61       *
  62       * @param   array  $options  The options about this button.
  63       *
  64       * @return  void
  65       *
  66       * @since   4.0.0
  67       */
  68      protected function prepareOptions(array &$options)
  69      {
  70          $options['icon'] = $options['icon'] ?? 'icon-square';
  71  
  72          parent::prepareOptions($options);
  73  
  74          $options['doTask'] = $this->_getCommand($this->getUrl());
  75  
  76          $options['selector'] = $options['selector'] ?? 'modal-' . $this->getName();
  77      }
  78  
  79      /**
  80       * Fetch the HTML for the button
  81       *
  82       * @param   string   $type          Unused string, formerly button type.
  83       * @param   string   $name          Modal name, used to generate element ID
  84       * @param   string   $text          The link text
  85       * @param   string   $url           URL for popup
  86       * @param   integer  $iframeWidth   Width of popup
  87       * @param   integer  $iframeHeight  Height of popup
  88       * @param   integer  $bodyHeight    Optional height of the modal body in viewport units (vh)
  89       * @param   integer  $modalWidth    Optional width of the modal in viewport units (vh)
  90       * @param   string   $onClose       JavaScript for the onClose event.
  91       * @param   string   $title         The title text
  92       * @param   string   $footer        The footer html
  93       *
  94       * @return  string  HTML string for the button
  95       *
  96       * @since   3.0
  97       */
  98      public function fetchButton(
  99          $type = 'Modal',
 100          $name = '',
 101          $text = '',
 102          $url = '',
 103          $iframeWidth = 640,
 104          $iframeHeight = 480,
 105          $bodyHeight = null,
 106          $modalWidth = null,
 107          $onClose = '',
 108          $title = '',
 109          $footer = null
 110      ) {
 111          $this->name($name)
 112              ->text($text)
 113              ->task($this->_getCommand($url))
 114              ->url($url)
 115              ->icon('icon-' . $name)
 116              ->iframeWidth($iframeWidth)
 117              ->iframeHeight($iframeHeight)
 118              ->bodyHeight($bodyHeight)
 119              ->modalWidth($modalWidth)
 120              ->onclose($onClose)
 121              ->title($title)
 122              ->footer($footer);
 123  
 124          return $this->renderButton($this->options);
 125      }
 126  
 127      /**
 128       * Render button HTML.
 129       *
 130       * @param   array  $options  The button options.
 131       *
 132       * @return  string  The button HTML.
 133       *
 134       * @since   4.0.0
 135       */
 136      protected function renderButton(array &$options): string
 137      {
 138          $html = [];
 139  
 140          $html[] = parent::renderButton($options);
 141  
 142          if ((string) $this->getUrl() !== '') {
 143              // Build the options array for the modal
 144              $params = array();
 145              $params['title']      = $options['title'] ?? $options['text'];
 146              $params['url']        = $this->getUrl();
 147              $params['height']     = $options['iframeHeight'] ?? 480;
 148              $params['width']      = $options['iframeWidth'] ?? 640;
 149              $params['bodyHeight'] = $options['bodyHeight'] ?? null;
 150              $params['modalWidth'] = $options['modalWidth'] ?? null;
 151  
 152              // Place modal div and scripts in a new div
 153              $html[] = '<div class="btn-group" style="width: 0; margin: 0; padding: 0;">';
 154  
 155              $selector = $options['selector'];
 156  
 157              $footer = $this->getFooter();
 158  
 159              if ($footer !== null) {
 160                  $params['footer'] = $footer;
 161              }
 162  
 163              $html[] = HTMLHelper::_('bootstrap.renderModal', $selector, $params);
 164  
 165              $html[] = '</div>';
 166  
 167              // We have to move the modal, otherwise we get problems with the backdrop
 168              // @todo: There should be a better workaround than this
 169              Factory::getDocument()->addScriptDeclaration(
 170                  <<<JS
 171  document.addEventListener('DOMContentLoaded', function() {
 172    var modal =document.getElementById('{$options['selector']}');
 173    document.body.appendChild(modal);
 174    if (Joomla && Joomla.Bootstrap && Joomla.Bootstrap.Methods && Joomla.Bootstrap.Methods.Modal) {
 175      Joomla.Bootstrap.Methods.Initialise.Modal(modal);
 176    }
 177  });
 178  JS
 179              );
 180          }
 181  
 182          // If an $onClose event is passed, add it to the modal JS object
 183          if ((string) $this->getOnclose() !== '') {
 184              Factory::getDocument()->addScriptDeclaration(
 185                  <<<JS
 186  document.addEventListener('DOMContentLoaded', function() {
 187      document.querySelector('#{$options['selector']}').addEventListener('hide.bs.modal', function() {
 188          {$options['onclose']}
 189      });
 190  });
 191  JS
 192              );
 193          }
 194  
 195          return implode("\n", $html);
 196      }
 197  
 198      /**
 199       * Get the JavaScript command for the button
 200       *
 201       * @param   string  $url  URL for popup
 202       *
 203       * @return  string  JavaScript command string
 204       *
 205       * @since   3.0
 206       */
 207      private function _getCommand($url)
 208      {
 209          $url = $url ?? '';
 210  
 211          if (strpos($url, 'http') !== 0) {
 212              $url = Uri::base() . $url;
 213          }
 214  
 215          return $url;
 216      }
 217  
 218      /**
 219       * Method to configure available option accessors.
 220       *
 221       * @return  array
 222       *
 223       * @since   4.0.0
 224       */
 225      protected static function getAccessors(): array
 226      {
 227          return array_merge(
 228              parent::getAccessors(),
 229              [
 230                  'url',
 231                  'iframeWidth',
 232                  'iframeHeight',
 233                  'bodyHeight',
 234                  'modalWidth',
 235                  'onclose',
 236                  'title',
 237                  'footer',
 238                  'selector',
 239                  'listCheck',
 240              ]
 241          );
 242      }
 243  }


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