[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_newsfeeds/src/Field/Modal/ -> NewsfeedField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_newsfeeds
   6   *
   7   * @copyright   (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Newsfeeds\Administrator\Field\Modal;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\FormField;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\LanguageHelper;
  17  use Joomla\CMS\Language\Text;
  18  use Joomla\CMS\Session\Session;
  19  use Joomla\Database\ParameterType;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Supports a modal newsfeeds picker.
  27   *
  28   * @since  1.6
  29   */
  30  class NewsfeedField extends FormField
  31  {
  32      /**
  33       * The form field type.
  34       *
  35       * @var     string
  36       * @since   1.6
  37       */
  38      protected $type = 'Modal_Newsfeed';
  39  
  40      /**
  41       * Method to get the field input markup.
  42       *
  43       * @return  string  The field input markup.
  44       *
  45       * @since   1.6
  46       */
  47      protected function getInput()
  48      {
  49          $allowNew       = ((string) $this->element['new'] == 'true');
  50          $allowEdit      = ((string) $this->element['edit'] == 'true');
  51          $allowClear     = ((string) $this->element['clear'] != 'false');
  52          $allowSelect    = ((string) $this->element['select'] != 'false');
  53          $allowPropagate = ((string) $this->element['propagate'] == 'true');
  54  
  55          $languages = LanguageHelper::getContentLanguages(array(0, 1), false);
  56  
  57          // Load language
  58          Factory::getLanguage()->load('com_newsfeeds', JPATH_ADMINISTRATOR);
  59  
  60          // The active newsfeed id field.
  61          $value = (int) $this->value ?: '';
  62  
  63          // Create the modal id.
  64          $modalId = 'Newsfeed_' . $this->id;
  65  
  66          /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
  67          $wa = Factory::getApplication()->getDocument()->getWebAssetManager();
  68  
  69          // Add the modal field script to the document head.
  70          $wa->useScript('field.modal-fields');
  71  
  72          // Script to proxy the select modal function to the modal-fields.js file.
  73          if ($allowSelect) {
  74              static $scriptSelect = null;
  75  
  76              if (is_null($scriptSelect)) {
  77                  $scriptSelect = array();
  78              }
  79  
  80              if (!isset($scriptSelect[$this->id])) {
  81                  $wa->addInlineScript(
  82                      "
  83                  window.jSelectNewsfeed_" . $this->id . " = function (id, title, object) {
  84                      window.processModalSelect('Newsfeed', '" . $this->id . "', id, title, '', object);
  85                  }",
  86                      [],
  87                      ['type' => 'module']
  88                  );
  89  
  90                  Text::script('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED');
  91  
  92                  $scriptSelect[$this->id] = true;
  93              }
  94          }
  95  
  96          // Setup variables for display.
  97          $linkNewsfeeds = 'index.php?option=com_newsfeeds&amp;view=newsfeeds&amp;layout=modal&amp;tmpl=component&amp;' . Session::getFormToken() . '=1';
  98          $linkNewsfeed  = 'index.php?option=com_newsfeeds&amp;view=newsfeed&amp;layout=modal&amp;tmpl=component&amp;' . Session::getFormToken() . '=1';
  99          $modalTitle    = Text::_('COM_NEWSFEEDS_SELECT_A_FEED');
 100  
 101          if (isset($this->element['language'])) {
 102              $linkNewsfeeds .= '&amp;forcedLanguage=' . $this->element['language'];
 103              $linkNewsfeed  .= '&amp;forcedLanguage=' . $this->element['language'];
 104              $modalTitle    .= ' &#8212; ' . $this->element['label'];
 105          }
 106  
 107          $urlSelect = $linkNewsfeeds . '&amp;function=jSelectNewsfeed_' . $this->id;
 108          $urlEdit   = $linkNewsfeed . '&amp;task=newsfeed.edit&amp;id=\' + document.getElementById("' . $this->id . '_id").value + \'';
 109          $urlNew    = $linkNewsfeed . '&amp;task=newsfeed.add';
 110  
 111          if ($value) {
 112              $id    = (int) $value;
 113              $db    = $this->getDatabase();
 114              $query = $db->getQuery(true)
 115                  ->select($db->quoteName('name'))
 116                  ->from($db->quoteName('#__newsfeeds'))
 117                  ->where($db->quoteName('id') . ' = :id')
 118                  ->bind(':id', $id, ParameterType::INTEGER);
 119              $db->setQuery($query);
 120  
 121              try {
 122                  $title = $db->loadResult();
 123              } catch (\RuntimeException $e) {
 124                  Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
 125              }
 126          }
 127  
 128          $title = empty($title) ? Text::_('COM_NEWSFEEDS_SELECT_A_FEED') : htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
 129  
 130          // The current newsfeed display field.
 131          $html  = '';
 132  
 133          if ($allowSelect || $allowNew || $allowEdit || $allowClear) {
 134              $html .= '<span class="input-group">';
 135          }
 136  
 137          $html .= '<input class="form-control" id="' . $this->id . '_name" type="text" value="' . $title . '" readonly size="35">';
 138  
 139          // Select newsfeed button
 140          if ($allowSelect) {
 141              $html .= '<button'
 142                  . ' class="btn btn-primary' . ($value ? ' hidden' : '') . '"'
 143                  . ' id="' . $this->id . '_select"'
 144                  . ' data-bs-toggle="modal"'
 145                  . ' type="button"'
 146                  . ' data-bs-target="#ModalSelect' . $modalId . '">'
 147                  . '<span class="icon-file" aria-hidden="true"></span> ' . Text::_('JSELECT')
 148                  . '</button>';
 149          }
 150  
 151          // New newsfeed button
 152          if ($allowNew) {
 153              $html .= '<button'
 154                  . ' class="btn btn-secondary' . ($value ? ' hidden' : '') . '"'
 155                  . ' id="' . $this->id . '_new"'
 156                  . ' data-bs-toggle="modal"'
 157                  . ' type="button"'
 158                  . ' data-bs-target="#ModalNew' . $modalId . '">'
 159                  . '<span class="icon-plus" aria-hidden="true"></span> ' . Text::_('JACTION_CREATE')
 160                  . '</button>';
 161          }
 162  
 163          // Edit newsfeed button
 164          if ($allowEdit) {
 165              $html .= '<button'
 166                  . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
 167                  . ' id="' . $this->id . '_edit"'
 168                  . ' data-bs-toggle="modal"'
 169                  . ' type="button"'
 170                  . ' data-bs-target="#ModalEdit' . $modalId . '">'
 171                  . '<span class="icon-pen-square" aria-hidden="true"></span> ' . Text::_('JACTION_EDIT')
 172                  . '</button>';
 173          }
 174  
 175          // Clear newsfeed button
 176          if ($allowClear) {
 177              $html .= '<button'
 178                  . ' class="btn btn-secondary' . ($value ? '' : ' hidden') . '"'
 179                  . ' id="' . $this->id . '_clear"'
 180                  . ' type="button"'
 181                  . ' onclick="window.processModalParent(\'' . $this->id . '\'); return false;">'
 182                  . '<span class="icon-times" aria-hidden="true"></span> ' . Text::_('JCLEAR')
 183                  . '</button>';
 184          }
 185  
 186          // Propagate newsfeed button
 187          if ($allowPropagate && count($languages) > 2) {
 188              // Strip off language tag at the end
 189              $tagLength = (int) strlen($this->element['language']);
 190              $callbackFunctionStem = substr("jSelectNewsfeed_" . $this->id, 0, -$tagLength);
 191  
 192              $html .= '<button'
 193              . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
 194              . ' type="button"'
 195              . ' id="' . $this->id . '_propagate"'
 196              . ' title="' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_TIP') . '"'
 197              . ' onclick="Joomla.propagateAssociation(\'' . $this->id . '\', \'' . $callbackFunctionStem . '\');">'
 198              . '<span class="icon-sync" aria-hidden="true"></span> ' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_BUTTON')
 199              . '</button>';
 200          }
 201  
 202          if ($allowSelect || $allowNew || $allowEdit || $allowClear) {
 203              $html .= '</span>';
 204          }
 205  
 206          // Select newsfeed modal
 207          if ($allowSelect) {
 208              $html .= HTMLHelper::_(
 209                  'bootstrap.renderModal',
 210                  'ModalSelect' . $modalId,
 211                  array(
 212                      'title'       => $modalTitle,
 213                      'url'         => $urlSelect,
 214                      'height'      => '400px',
 215                      'width'       => '800px',
 216                      'bodyHeight'  => 70,
 217                      'modalWidth'  => 80,
 218                      'footer'      => '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">'
 219                                          . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>',
 220                  )
 221              );
 222          }
 223  
 224          // New newsfeed modal
 225          if ($allowNew) {
 226              $html .= HTMLHelper::_(
 227                  'bootstrap.renderModal',
 228                  'ModalNew' . $modalId,
 229                  array(
 230                      'title'       => Text::_('COM_NEWSFEEDS_NEW_NEWSFEED'),
 231                      'backdrop'    => 'static',
 232                      'keyboard'    => false,
 233                      'closeButton' => false,
 234                      'url'         => $urlNew,
 235                      'height'      => '400px',
 236                      'width'       => '800px',
 237                      'bodyHeight'  => 70,
 238                      'modalWidth'  => 80,
 239                      'footer'      => '<button type="button" class="btn btn-secondary"'
 240                              . ' onclick="window.processModalEdit(this, \''
 241                              . $this->id . '\', \'add\', \'newsfeed\', \'cancel\', \'newsfeed-form\', \'jform_id\', \'jform_name\'); return false;">'
 242                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 243                              . '<button type="button" class="btn btn-primary"'
 244                              . ' onclick="window.processModalEdit(this, \''
 245                              . $this->id . '\', \'add\', \'newsfeed\', \'save\', \'newsfeed-form\', \'jform_id\', \'jform_name\'); return false;">'
 246                              . Text::_('JSAVE') . '</button>'
 247                              . '<button type="button" class="btn btn-success"'
 248                              . ' onclick="window.processModalEdit(this, \''
 249                              . $this->id . '\', \'add\', \'newsfeed\', \'apply\', \'newsfeed-form\', \'jform_id\', \'jform_name\'); return false;">'
 250                              . Text::_('JAPPLY') . '</button>',
 251                  )
 252              );
 253          }
 254  
 255          // Edit newsfeed modal.
 256          if ($allowEdit) {
 257              $html .= HTMLHelper::_(
 258                  'bootstrap.renderModal',
 259                  'ModalEdit' . $modalId,
 260                  array(
 261                      'title'       => Text::_('COM_NEWSFEEDS_EDIT_NEWSFEED'),
 262                      'backdrop'    => 'static',
 263                      'keyboard'    => false,
 264                      'closeButton' => false,
 265                      'url'         => $urlEdit,
 266                      'height'      => '400px',
 267                      'width'       => '800px',
 268                      'bodyHeight'  => 70,
 269                      'modalWidth'  => 80,
 270                      'footer'      => '<button type="button" class="btn btn-secondary"'
 271                              . ' onclick="window.processModalEdit(this, \'' . $this->id
 272                              . '\', \'edit\', \'newsfeed\', \'cancel\', \'newsfeed-form\', \'jform_id\', \'jform_name\'); return false;">'
 273                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 274                              . '<button type="button" class="btn btn-primary"'
 275                              . ' onclick="window.processModalEdit(this, \''
 276                              . $this->id . '\', \'edit\', \'newsfeed\', \'save\', \'newsfeed-form\', \'jform_id\', \'jform_name\'); return false;">'
 277                              . Text::_('JSAVE') . '</button>'
 278                              . '<button type="button" class="btn btn-success"'
 279                              . ' onclick="window.processModalEdit(this, \''
 280                              . $this->id . '\', \'edit\', \'newsfeed\', \'apply\', \'newsfeed-form\', \'jform_id\', \'jform_name\'); return false;">'
 281                              . Text::_('JAPPLY') . '</button>',
 282                  )
 283              );
 284          }
 285  
 286          // Add class='required' for client side validation
 287          $class = $this->required ? ' class="required modal-value"' : '';
 288  
 289          $html .= '<input type="hidden" id="' . $this->id . '_id"' . $class . ' data-required="' . (int) $this->required . '" name="' . $this->name
 290              . '" data-text="' . htmlspecialchars(Text::_('COM_NEWSFEEDS_SELECT_A_FEED', true), ENT_COMPAT, 'UTF-8') . '" value="' . $value . '">';
 291  
 292          return $html;
 293      }
 294  
 295      /**
 296       * Method to get the field label markup.
 297       *
 298       * @return  string  The field label markup.
 299       *
 300       * @since   3.4
 301       */
 302      protected function getLabel()
 303      {
 304          return str_replace($this->id, $this->id . '_name', parent::getLabel());
 305      }
 306  }


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