[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_contact/src/Field/Modal/ -> ContactField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_contact
   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\Contact\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 contact picker.
  27   *
  28   * @since  1.6
  29   */
  30  class ContactField extends FormField
  31  {
  32      /**
  33       * The form field type.
  34       *
  35       * @var     string
  36       * @since   1.6
  37       */
  38      protected $type = 'Modal_Contact';
  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_contact', JPATH_ADMINISTRATOR);
  59  
  60          // The active contact id field.
  61          $value = (int) $this->value ?: '';
  62  
  63          // Create the modal id.
  64          $modalId = 'Contact_' . $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.jSelectContact_" . $this->id . " = function (id, title, object) {
  84                      window.processModalSelect('Contact', '" . $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          $linkContacts = 'index.php?option=com_contact&amp;view=contacts&amp;layout=modal&amp;tmpl=component&amp;' . Session::getFormToken() . '=1';
  98          $linkContact  = 'index.php?option=com_contact&amp;view=contact&amp;layout=modal&amp;tmpl=component&amp;' . Session::getFormToken() . '=1';
  99          $modalTitle   = Text::_('COM_CONTACT_SELECT_A_CONTACT');
 100  
 101          if (isset($this->element['language'])) {
 102              $linkContacts .= '&amp;forcedLanguage=' . $this->element['language'];
 103              $linkContact   .= '&amp;forcedLanguage=' . $this->element['language'];
 104              $modalTitle     .= ' &#8212; ' . $this->element['label'];
 105          }
 106  
 107          $urlSelect = $linkContacts . '&amp;function=jSelectContact_' . $this->id;
 108          $urlEdit   = $linkContact . '&amp;task=contact.edit&amp;id=\' + document.getElementById("' . $this->id . '_id").value + \'';
 109          $urlNew    = $linkContact . '&amp;task=contact.add';
 110  
 111          if ($value) {
 112              $db    = $this->getDatabase();
 113              $query = $db->getQuery(true)
 114                  ->select($db->quoteName('name'))
 115                  ->from($db->quoteName('#__contact_details'))
 116                  ->where($db->quoteName('id') . ' = :id')
 117                  ->bind(':id', $value, ParameterType::INTEGER);
 118              $db->setQuery($query);
 119  
 120              try {
 121                  $title = $db->loadResult();
 122              } catch (\RuntimeException $e) {
 123                  Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
 124              }
 125          }
 126  
 127          $title = empty($title) ? Text::_('COM_CONTACT_SELECT_A_CONTACT') : htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
 128  
 129          // The current contact display field.
 130          $html  = '';
 131  
 132          if ($allowSelect || $allowNew || $allowEdit || $allowClear) {
 133              $html .= '<span class="input-group">';
 134          }
 135  
 136          $html .= '<input class="form-control" id="' . $this->id . '_name" type="text" value="' . $title . '" readonly size="35">';
 137  
 138          // Select contact button
 139          if ($allowSelect) {
 140              $html .= '<button'
 141                  . ' class="btn btn-primary' . ($value ? ' hidden' : '') . '"'
 142                  . ' id="' . $this->id . '_select"'
 143                  . ' data-bs-toggle="modal"'
 144                  . ' type="button"'
 145                  . ' data-bs-target="#ModalSelect' . $modalId . '">'
 146                  . '<span class="icon-file" aria-hidden="true"></span> ' . Text::_('JSELECT')
 147                  . '</button>';
 148          }
 149  
 150          // New contact button
 151          if ($allowNew) {
 152              $html .= '<button'
 153                  . ' class="btn btn-secondary' . ($value ? ' hidden' : '') . '"'
 154                  . ' id="' . $this->id . '_new"'
 155                  . ' data-bs-toggle="modal"'
 156                  . ' type="button"'
 157                  . ' data-bs-target="#ModalNew' . $modalId . '">'
 158                  . '<span class="icon-plus" aria-hidden="true"></span> ' . Text::_('JACTION_CREATE')
 159                  . '</button>';
 160          }
 161  
 162          // Edit contact button
 163          if ($allowEdit) {
 164              $html .= '<button'
 165                  . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
 166                  . ' id="' . $this->id . '_edit"'
 167                  . ' data-bs-toggle="modal"'
 168                  . ' type="button"'
 169                  . ' data-bs-target="#ModalEdit' . $modalId . '">'
 170                  . '<span class="icon-pen-square" aria-hidden="true"></span> ' . Text::_('JACTION_EDIT')
 171                  . '</button>';
 172          }
 173  
 174          // Clear contact button
 175          if ($allowClear) {
 176              $html .= '<button'
 177                  . ' class="btn btn-secondary' . ($value ? '' : ' hidden') . '"'
 178                  . ' id="' . $this->id . '_clear"'
 179                  . ' type="button"'
 180                  . ' onclick="window.processModalParent(\'' . $this->id . '\'); return false;">'
 181                  . '<span class="icon-times" aria-hidden="true"></span> ' . Text::_('JCLEAR')
 182                  . '</button>';
 183          }
 184  
 185          // Propagate contact button
 186          if ($allowPropagate && count($languages) > 2) {
 187              // Strip off language tag at the end
 188              $tagLength = (int) strlen($this->element['language']);
 189              $callbackFunctionStem = substr("jSelectContact_" . $this->id, 0, -$tagLength);
 190  
 191              $html .= '<button'
 192              . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
 193              . ' type="button"'
 194              . ' id="' . $this->id . '_propagate"'
 195              . ' title="' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_TIP') . '"'
 196              . ' onclick="Joomla.propagateAssociation(\'' . $this->id . '\', \'' . $callbackFunctionStem . '\');">'
 197              . '<span class="icon-sync" aria-hidden="true"></span> ' . Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_BUTTON')
 198              . '</button>';
 199          }
 200  
 201          if ($allowSelect || $allowNew || $allowEdit || $allowClear) {
 202              $html .= '</span>';
 203          }
 204  
 205          // Select contact modal
 206          if ($allowSelect) {
 207              $html .= HTMLHelper::_(
 208                  'bootstrap.renderModal',
 209                  'ModalSelect' . $modalId,
 210                  array(
 211                      'title'       => $modalTitle,
 212                      'url'         => $urlSelect,
 213                      'height'      => '400px',
 214                      'width'       => '800px',
 215                      'bodyHeight'  => 70,
 216                      'modalWidth'  => 80,
 217                      'footer'      => '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">'
 218                                          . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>',
 219                  )
 220              );
 221          }
 222  
 223          // New contact modal
 224          if ($allowNew) {
 225              $html .= HTMLHelper::_(
 226                  'bootstrap.renderModal',
 227                  'ModalNew' . $modalId,
 228                  array(
 229                      'title'       => Text::_('COM_CONTACT_NEW_CONTACT'),
 230                      'backdrop'    => 'static',
 231                      'keyboard'    => false,
 232                      'closeButton' => false,
 233                      'url'         => $urlNew,
 234                      'height'      => '400px',
 235                      'width'       => '800px',
 236                      'bodyHeight'  => 70,
 237                      'modalWidth'  => 80,
 238                      'footer'      => '<button type="button" class="btn btn-secondary"'
 239                              . ' onclick="window.processModalEdit(this, \''
 240                              . $this->id . '\', \'add\', \'contact\', \'cancel\', \'contact-form\', \'jform_id\', \'jform_name\'); return false;">'
 241                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 242                              . '<button type="button" class="btn btn-primary"'
 243                              . ' onclick="window.processModalEdit(this, \''
 244                              . $this->id . '\', \'add\', \'contact\', \'save\', \'contact-form\', \'jform_id\', \'jform_name\'); return false;">'
 245                              . Text::_('JSAVE') . '</button>'
 246                              . '<button type="button" class="btn btn-success"'
 247                              . ' onclick="window.processModalEdit(this, \''
 248                              . $this->id . '\', \'add\', \'contact\', \'apply\', \'contact-form\', \'jform_id\', \'jform_name\'); return false;">'
 249                              . Text::_('JAPPLY') . '</button>',
 250                  )
 251              );
 252          }
 253  
 254          // Edit contact modal.
 255          if ($allowEdit) {
 256              $html .= HTMLHelper::_(
 257                  'bootstrap.renderModal',
 258                  'ModalEdit' . $modalId,
 259                  array(
 260                      'title'       => Text::_('COM_CONTACT_EDIT_CONTACT'),
 261                      'backdrop'    => 'static',
 262                      'keyboard'    => false,
 263                      'closeButton' => false,
 264                      'url'         => $urlEdit,
 265                      'height'      => '400px',
 266                      'width'       => '800px',
 267                      'bodyHeight'  => 70,
 268                      'modalWidth'  => 80,
 269                      'footer'      => '<button type="button" class="btn btn-secondary"'
 270                              . ' onclick="window.processModalEdit(this, \'' . $this->id
 271                              . '\', \'edit\', \'contact\', \'cancel\', \'contact-form\', \'jform_id\', \'jform_name\'); return false;">'
 272                              . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
 273                              . '<button type="button" class="btn btn-primary"'
 274                              . ' onclick="window.processModalEdit(this, \''
 275                              . $this->id . '\', \'edit\', \'contact\', \'save\', \'contact-form\', \'jform_id\', \'jform_name\'); return false;">'
 276                              . Text::_('JSAVE') . '</button>'
 277                              . '<button type="button" class="btn btn-success"'
 278                              . ' onclick="window.processModalEdit(this, \''
 279                              . $this->id . '\', \'edit\', \'contact\', \'apply\', \'contact-form\', \'jform_id\', \'jform_name\'); return false;">'
 280                              . Text::_('JAPPLY') . '</button>',
 281                  )
 282              );
 283          }
 284  
 285          // Note: class='required' for client side validation.
 286          $class = $this->required ? ' class="required modal-value"' : '';
 287  
 288          $html .= '<input type="hidden" id="' . $this->id . '_id"' . $class . ' data-required="' . (int) $this->required . '" name="' . $this->name
 289              . '" data-text="' . htmlspecialchars(Text::_('COM_CONTACT_SELECT_A_CONTACT', true), ENT_COMPAT, 'UTF-8') . '" value="' . $value . '">';
 290  
 291          return $html;
 292      }
 293  
 294      /**
 295       * Method to get the field label markup.
 296       *
 297       * @return  string  The field label markup.
 298       *
 299       * @since   3.4
 300       */
 301      protected function getLabel()
 302      {
 303          return str_replace($this->id, $this->id . '_name', parent::getLabel());
 304      }
 305  }


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