[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_content/src/Field/Modal/ -> ArticleField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_content
   6   *
   7   * @copyright   (C) 2009 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\Content\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 article picker.
  27   *
  28   * @since  1.6
  29   */
  30  class ArticleField extends FormField
  31  {
  32      /**
  33       * The form field type.
  34       *
  35       * @var    string
  36       * @since  1.6
  37       */
  38      protected $type = 'Modal_Article';
  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_content', JPATH_ADMINISTRATOR);
  59  
  60          // The active article id field.
  61          $value = (int) $this->value ?: '';
  62  
  63          // Create the modal id.
  64          $modalId = 'Article_' . $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.jSelectArticle_" . $this->id . " = function (id, title, catid, object, url, language) {
  84                      window.processModalSelect('Article', '" . $this->id . "', id, title, catid, object, url, language);
  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          $linkArticles = 'index.php?option=com_content&amp;view=articles&amp;layout=modal&amp;tmpl=component&amp;' . Session::getFormToken() . '=1';
  98          $linkArticle  = 'index.php?option=com_content&amp;view=article&amp;layout=modal&amp;tmpl=component&amp;' . Session::getFormToken() . '=1';
  99  
 100          if (isset($this->element['language'])) {
 101              $linkArticles .= '&amp;forcedLanguage=' . $this->element['language'];
 102              $linkArticle  .= '&amp;forcedLanguage=' . $this->element['language'];
 103              $modalTitle    = Text::_('COM_CONTENT_SELECT_AN_ARTICLE') . ' &#8212; ' . $this->element['label'];
 104          } else {
 105              $modalTitle    = Text::_('COM_CONTENT_SELECT_AN_ARTICLE');
 106          }
 107  
 108          $urlSelect = $linkArticles . '&amp;function=jSelectArticle_' . $this->id;
 109          $urlEdit   = $linkArticle . '&amp;task=article.edit&amp;id=\' + document.getElementById(&quot;' . $this->id . '_id&quot;).value + \'';
 110          $urlNew    = $linkArticle . '&amp;task=article.add';
 111  
 112          if ($value) {
 113              $db    = $this->getDatabase();
 114              $query = $db->getQuery(true)
 115                  ->select($db->quoteName('title'))
 116                  ->from($db->quoteName('#__content'))
 117                  ->where($db->quoteName('id') . ' = :value')
 118                  ->bind(':value', $value, 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_CONTENT_SELECT_AN_ARTICLE') : htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
 129  
 130          // The current article 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 article 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 article 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 article 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 article 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 article 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("jSelectArticle_" . $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 article 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 article modal
 225          if ($allowNew) {
 226              $html .= HTMLHelper::_(
 227                  'bootstrap.renderModal',
 228                  'ModalNew' . $modalId,
 229                  array(
 230                      'title'       => Text::_('COM_CONTENT_NEW_ARTICLE'),
 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, \'' . $this->id . '\', \'add\', \'article\', \'cancel\', \'item-form\'); return false;">'
 241                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 242                              . '<button type="button" class="btn btn-primary"'
 243                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'article\', \'save\', \'item-form\'); return false;">'
 244                              . Text::_('JSAVE') . '</button>'
 245                              . '<button type="button" class="btn btn-success"'
 246                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'article\', \'apply\', \'item-form\'); return false;">'
 247                              . Text::_('JAPPLY') . '</button>',
 248                  )
 249              );
 250          }
 251  
 252          // Edit article modal
 253          if ($allowEdit) {
 254              $html .= HTMLHelper::_(
 255                  'bootstrap.renderModal',
 256                  'ModalEdit' . $modalId,
 257                  array(
 258                      'title'       => Text::_('COM_CONTENT_EDIT_ARTICLE'),
 259                      'backdrop'    => 'static',
 260                      'keyboard'    => false,
 261                      'closeButton' => false,
 262                      'url'         => $urlEdit,
 263                      'height'      => '400px',
 264                      'width'       => '800px',
 265                      'bodyHeight'  => 70,
 266                      'modalWidth'  => 80,
 267                      'footer'      => '<button type="button" class="btn btn-secondary"'
 268                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'article\', \'cancel\', \'item-form\'); return false;">'
 269                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 270                              . '<button type="button" class="btn btn-primary"'
 271                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'article\', \'save\', \'item-form\'); return false;">'
 272                              . Text::_('JSAVE') . '</button>'
 273                              . '<button type="button" class="btn btn-success"'
 274                              . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'article\', \'apply\', \'item-form\'); return false;">'
 275                              . Text::_('JAPPLY') . '</button>',
 276                  )
 277              );
 278          }
 279  
 280          // Note: class='required' for client side validation.
 281          $class = $this->required ? ' class="required modal-value"' : '';
 282  
 283          $html .= '<input type="hidden" id="' . $this->id . '_id" ' . $class . ' data-required="' . (int) $this->required . '" name="' . $this->name
 284              . '" data-text="' . htmlspecialchars(Text::_('COM_CONTENT_SELECT_AN_ARTICLE'), ENT_COMPAT, 'UTF-8') . '" value="' . $value . '">';
 285  
 286          return $html;
 287      }
 288  
 289      /**
 290       * Method to get the field label markup.
 291       *
 292       * @return  string  The field label markup.
 293       *
 294       * @since   3.4
 295       */
 296      protected function getLabel()
 297      {
 298          return str_replace($this->id, $this->id . '_name', parent::getLabel());
 299      }
 300  }


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