[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/content/confirmconsent/src/Field/ -> ConsentBoxField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  Content.confirmconsent
   6   *
   7   * @copyright   (C) 2018 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\Plugin\Content\ConfirmConsent\Field;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Field\CheckboxesField;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\Associations;
  17  use Joomla\CMS\Language\Multilanguage;
  18  use Joomla\CMS\Router\Route;
  19  use Joomla\Component\Content\Site\Helper\RouteHelper;
  20  use Joomla\Database\Exception\ExecutionFailureException;
  21  use Joomla\Database\ParameterType;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('JPATH_PLATFORM') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Consentbox Field class for the Confirm Consent Plugin.
  29   *
  30   * @since  3.9.1
  31   */
  32  class ConsentBoxField extends CheckboxesField
  33  {
  34      /**
  35       * The form field type.
  36       *
  37       * @var    string
  38       * @since  3.9.1
  39       */
  40      protected $type = 'ConsentBox';
  41  
  42      /**
  43       * Flag to tell the field to always be in multiple values mode.
  44       *
  45       * @var    boolean
  46       * @since  3.9.1
  47       */
  48      protected $forceMultiple = false;
  49  
  50      /**
  51       * The article ID.
  52       *
  53       * @var    integer
  54       * @since  3.9.1
  55       */
  56      protected $articleid;
  57  
  58      /**
  59       * The menu item ID.
  60       *
  61       * @var    integer
  62       * @since  4.0.0
  63       */
  64      protected $menuItemId;
  65  
  66      /**
  67       * Type of the privacy policy.
  68       *
  69       * @var    string
  70       * @since  4.0.0
  71       */
  72      protected $privacyType;
  73  
  74      /**
  75       * Method to set certain otherwise inaccessible properties of the form field object.
  76       *
  77       * @param   string  $name   The property name for which to set the value.
  78       * @param   mixed   $value  The value of the property.
  79       *
  80       * @return  void
  81       *
  82       * @since   3.9.1
  83       */
  84      public function __set($name, $value)
  85      {
  86          switch ($name) {
  87              case 'articleid':
  88                  $this->articleid = (int) $value;
  89                  break;
  90  
  91              default:
  92                  parent::__set($name, $value);
  93          }
  94      }
  95  
  96      /**
  97       * Method to get certain otherwise inaccessible properties from the form field object.
  98       *
  99       * @param   string  $name  The property name for which to get the value.
 100       *
 101       * @return  mixed  The property value or null.
 102       *
 103       * @since   3.9.1
 104       */
 105      public function __get($name)
 106      {
 107          if ($name == 'articleid') {
 108              return $this->$name;
 109          }
 110  
 111          return parent::__get($name);
 112      }
 113  
 114      /**
 115       * Method to attach a JForm object to the field.
 116       *
 117       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 118       * @param   mixed              $value    The form field value to validate.
 119       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 120       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 121       *                                       full field name would end up being "bar[foo]".
 122       *
 123       * @return  boolean  True on success.
 124       *
 125       * @see     \Joomla\CMS\Form\FormField::setup()
 126       * @since   3.9.1
 127       */
 128      public function setup(\SimpleXMLElement $element, $value, $group = null)
 129      {
 130          $return = parent::setup($element, $value, $group);
 131  
 132          if ($return) {
 133              $this->articleid = (int) $this->element['articleid'];
 134              $this->menuItemId = (int) $this->element['menu_item_id'];
 135              $this->privacyType = (string) $this->element['privacy_type'];
 136          }
 137  
 138          return $return;
 139      }
 140  
 141      /**
 142       * Method to get the field label markup.
 143       *
 144       * @return  string  The field label markup.
 145       *
 146       * @since   3.9.1
 147       */
 148      protected function getLabel()
 149      {
 150          if ($this->hidden) {
 151              return '';
 152          }
 153  
 154          $data = $this->getLayoutData();
 155  
 156          // Forcing the Alias field to display the tip below
 157          $position = $this->element['name'] == 'alias' ? ' data-bs-placement="bottom" ' : '';
 158  
 159          // When we have an article let's add the modal and make the title clickable
 160          $hasLink = ($data['privacyType'] === 'article' && $data['articleid'])
 161              || ($data['privacyType'] === 'menu_item' && $data['menuItemId']);
 162  
 163          if ($hasLink) {
 164              $attribs['data-bs-toggle'] = 'modal';
 165  
 166              $data['label'] = HTMLHelper::_(
 167                  'link',
 168                  '#modal-' . $this->id,
 169                  $data['label'],
 170                  $attribs
 171              );
 172          }
 173  
 174          // Here mainly for B/C with old layouts. This can be done in the layouts directly
 175          $extraData = array(
 176              'text'     => $data['label'],
 177              'for'      => $this->id,
 178              'classes'  => explode(' ', $data['labelclass']),
 179              'position' => $position,
 180          );
 181  
 182          return $this->getRenderer($this->renderLabelLayout)->render(array_merge($data, $extraData));
 183      }
 184  
 185      /**
 186       * Method to get the field input markup.
 187       *
 188       * @return  string  The field input markup.
 189       *
 190       * @since   4.0.0
 191       */
 192      protected function getInput()
 193      {
 194          $modalHtml  = '';
 195          $layoutData = $this->getLayoutData();
 196  
 197          $hasLink = ($this->privacyType === 'article' && $this->articleid)
 198              || ($this->privacyType === 'menu_item' && $this->menuItemId);
 199  
 200          if ($hasLink) {
 201              $modalParams['title']  = $layoutData['label'];
 202              $modalParams['url']    = ($this->privacyType === 'menu_item') ? $this->getAssignedMenuItemUrl() : $this->getAssignedArticleUrl();
 203              $modalParams['height'] = '100%';
 204              $modalParams['width']  = '100%';
 205              $modalParams['bodyHeight'] = 70;
 206              $modalParams['modalWidth'] = 80;
 207              $modalHtml = HTMLHelper::_('bootstrap.renderModal', 'modal-' . $this->id, $modalParams);
 208          }
 209  
 210          return $modalHtml . parent::getInput();
 211      }
 212  
 213      /**
 214       * Method to get the data to be passed to the layout for rendering.
 215       *
 216       * @return  array
 217       *
 218       * @since   3.9.1
 219       */
 220      protected function getLayoutData()
 221      {
 222          $data = parent::getLayoutData();
 223  
 224          $extraData = array(
 225              'articleid' => (int) $this->articleid,
 226              'menuItemId' => (int) $this->menuItemId,
 227              'privacyType' => (string) $this->privacyType,
 228          );
 229  
 230          return array_merge($data, $extraData);
 231      }
 232  
 233      /**
 234       * Return the url of the assigned article based on the current user language
 235       *
 236       * @return  string  Returns the link to the article
 237       *
 238       * @since   3.9.1
 239       */
 240      private function getAssignedArticleUrl()
 241      {
 242          $db = $this->getDatabase();
 243  
 244          // Get the info from the article
 245          $query = $db->getQuery(true)
 246              ->select($db->quoteName(array('id', 'catid', 'language')))
 247              ->from($db->quoteName('#__content'))
 248              ->where($db->quoteName('id') . ' = ' . (int) $this->articleid);
 249          $db->setQuery($query);
 250  
 251          try {
 252              $article = $db->loadObject();
 253          } catch (ExecutionFailureException $e) {
 254              // Something at the database layer went wrong
 255              return Route::_(
 256                  'index.php?option=com_content&view=article&id='
 257                  . $this->articleid . '&tmpl=component'
 258              );
 259          }
 260  
 261          if (!\is_object($article)) {
 262              // We have not found the article object lets show a 404 to the user
 263              return Route::_(
 264                  'index.php?option=com_content&view=article&id='
 265                  . $this->articleid . '&tmpl=component'
 266              );
 267          }
 268  
 269          if (!Associations::isEnabled()) {
 270              return Route::_(
 271                  RouteHelper::getArticleRoute(
 272                      $article->id,
 273                      $article->catid,
 274                      $article->language
 275                  ) . '&tmpl=component'
 276              );
 277          }
 278  
 279          $associatedArticles = Associations::getAssociations('com_content', '#__content', 'com_content.item', $article->id);
 280          $currentLang        = Factory::getLanguage()->getTag();
 281  
 282          if (isset($associatedArticles) && $currentLang !== $article->language && \array_key_exists($currentLang, $associatedArticles)) {
 283              return Route::_(
 284                  RouteHelper::getArticleRoute(
 285                      $associatedArticles[$currentLang]->id,
 286                      $associatedArticles[$currentLang]->catid,
 287                      $associatedArticles[$currentLang]->language
 288                  ) . '&tmpl=component'
 289              );
 290          }
 291  
 292          // Association is enabled but this article is not associated
 293          return Route::_(
 294              'index.php?option=com_content&view=article&id='
 295                  . $article->id . '&catid=' . $article->catid
 296                  . '&tmpl=component&lang=' . $article->language
 297          );
 298      }
 299  
 300      /**
 301       * Get privacy menu item URL. If the site is a multilingual website and there is associated menu item for the
 302       * current language, the URL of the associated menu item will be returned.
 303       *
 304       * @return  string
 305       *
 306       * @since   4.0.0
 307       */
 308      private function getAssignedMenuItemUrl()
 309      {
 310          $itemId = $this->menuItemId;
 311          $languageSuffix = '';
 312  
 313          if ($itemId > 0 && Associations::isEnabled()) {
 314              $privacyAssociated = Associations::getAssociations('com_menus', '#__menu', 'com_menus.item', $itemId, 'id', '', '');
 315              $currentLang = Factory::getLanguage()->getTag();
 316  
 317              if (isset($privacyAssociated[$currentLang])) {
 318                  $itemId = $privacyAssociated[$currentLang]->id;
 319              }
 320  
 321              if (Multilanguage::isEnabled()) {
 322                  $db    = $this->getDatabase();
 323                  $query = $db->getQuery(true)
 324                      ->select($db->quoteName(['id', 'language']))
 325                      ->from($db->quoteName('#__menu'))
 326                      ->where($db->quoteName('id') . ' = :id')
 327                      ->bind(':id', $itemId, ParameterType::INTEGER);
 328                  $db->setQuery($query);
 329                  $menuItem = $db->loadObject();
 330  
 331                  $languageSuffix = '&lang=' . $menuItem->language;
 332              }
 333          }
 334  
 335          return Route::_(
 336              'index.php?Itemid=' . (int) $itemId . '&tmpl=component' . $languageSuffix
 337          );
 338      }
 339  }


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