[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/system/fields/ -> fields.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  System.Fields
   6   *
   7   * @copyright   (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9  
  10   * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
  11   */
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Language\Multilanguage;
  16  use Joomla\CMS\Plugin\CMSPlugin;
  17  use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
  18  use Joomla\Registry\Registry;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Fields Plugin
  26   *
  27   * @since  3.7
  28   */
  29  class PlgSystemFields extends CMSPlugin
  30  {
  31      /**
  32       * Load the language file on instantiation.
  33       *
  34       * @var    boolean
  35       * @since  3.7.0
  36       */
  37      protected $autoloadLanguage = true;
  38  
  39      /**
  40       * Normalizes the request data.
  41       *
  42       * @param   string  $context  The context
  43       * @param   object  $data     The object
  44       * @param   Form    $form     The form
  45       *
  46       * @return  void
  47       *
  48       * @since   3.8.7
  49       */
  50      public function onContentNormaliseRequestData($context, $data, Form $form)
  51      {
  52          if (!FieldsHelper::extract($context, $data)) {
  53              return;
  54          }
  55  
  56          // Loop over all fields
  57          foreach ($form->getGroup('com_fields') as $field) {
  58              if ($field->disabled === true) {
  59                  /**
  60                   * Disabled fields should NEVER be added to the request as
  61                   * they should NEVER be added by the browser anyway so nothing to check against
  62                   * as "disabled" means no interaction at all.
  63                   */
  64  
  65                  // Make sure the data object has an entry before delete it
  66                  if (isset($data->com_fields[$field->fieldname])) {
  67                      unset($data->com_fields[$field->fieldname]);
  68                  }
  69  
  70                  continue;
  71              }
  72  
  73              // Make sure the data object has an entry
  74              if (isset($data->com_fields[$field->fieldname])) {
  75                  continue;
  76              }
  77  
  78              // Set a default value for the field
  79              $data->com_fields[$field->fieldname] = false;
  80          }
  81      }
  82  
  83      /**
  84       * The save event.
  85       *
  86       * @param   string                   $context  The context
  87       * @param   \Joomla\CMS\Table\Table  $item     The table
  88       * @param   boolean                  $isNew    Is new item
  89       * @param   array                    $data     The validated data
  90       *
  91       * @return  void
  92       *
  93       * @since   3.7.0
  94       */
  95      public function onContentAfterSave($context, $item, $isNew, $data = []): void
  96      {
  97          // Check if data is an array and the item has an id
  98          if (!is_array($data) || empty($item->id) || empty($data['com_fields'])) {
  99              return;
 100          }
 101  
 102          // Create correct context for category
 103          if ($context === 'com_categories.category') {
 104              $context = $item->extension . '.categories';
 105  
 106              // Set the catid on the category to get only the fields which belong to this category
 107              $item->catid = $item->id;
 108          }
 109  
 110          // Check the context
 111          $parts = FieldsHelper::extract($context, $item);
 112  
 113          if (!$parts) {
 114              return;
 115          }
 116  
 117          // Compile the right context for the fields
 118          $context = $parts[0] . '.' . $parts[1];
 119  
 120          // Loading the fields
 121          $fields = FieldsHelper::getFields($context, $item);
 122  
 123          if (!$fields) {
 124              return;
 125          }
 126  
 127          // Loading the model
 128  
 129          /** @var \Joomla\Component\Fields\Administrator\Model\FieldModel $model */
 130          $model = Factory::getApplication()->bootComponent('com_fields')->getMVCFactory()
 131              ->createModel('Field', 'Administrator', ['ignore_request' => true]);
 132  
 133          // Loop over the fields
 134          foreach ($fields as $field) {
 135              // Determine the value if it is (un)available from the data
 136              if (array_key_exists($field->name, $data['com_fields'])) {
 137                  $value = $data['com_fields'][$field->name] === false ? null : $data['com_fields'][$field->name];
 138              } else {
 139                  // Field not available on form, use stored value
 140                  $value = $field->rawvalue;
 141              }
 142  
 143              // If no value set (empty) remove value from database
 144              if (is_array($value) ? !count($value) : !strlen($value)) {
 145                  $value = null;
 146              }
 147  
 148              // JSON encode value for complex fields
 149              if (is_array($value) && (count($value, COUNT_NORMAL) !== count($value, COUNT_RECURSIVE) || !count(array_filter(array_keys($value), 'is_numeric')))) {
 150                  $value = json_encode($value);
 151              }
 152  
 153              // Setting the value for the field and the item
 154              $model->setFieldValue($field->id, $item->id, $value);
 155          }
 156      }
 157  
 158      /**
 159       * The save event.
 160       *
 161       * @param   array    $userData  The date
 162       * @param   boolean  $isNew     Is new
 163       * @param   boolean  $success   Is success
 164       * @param   string   $msg       The message
 165       *
 166       * @return  void
 167       *
 168       * @since   3.7.0
 169       */
 170      public function onUserAfterSave($userData, $isNew, $success, $msg): void
 171      {
 172          // It is not possible to manipulate the user during save events
 173          // Check if data is valid or we are in a recursion
 174          if (!$userData['id'] || !$success) {
 175              return;
 176          }
 177  
 178          $user = Factory::getUser($userData['id']);
 179  
 180          $task = Factory::getApplication()->input->getCmd('task');
 181  
 182          // Skip fields save when we activate a user, because we will lose the saved data
 183          if (in_array($task, array('activate', 'block', 'unblock'))) {
 184              return;
 185          }
 186  
 187          // Trigger the events with a real user
 188          $this->onContentAfterSave('com_users.user', $user, false, $userData);
 189      }
 190  
 191      /**
 192       * The delete event.
 193       *
 194       * @param   string    $context  The context
 195       * @param   stdClass  $item     The item
 196       *
 197       * @return  void
 198       *
 199       * @since   3.7.0
 200       */
 201      public function onContentAfterDelete($context, $item): void
 202      {
 203          $parts = FieldsHelper::extract($context, $item);
 204  
 205          if (!$parts || empty($item->id)) {
 206              return;
 207          }
 208  
 209          $context = $parts[0] . '.' . $parts[1];
 210  
 211          /** @var \Joomla\Component\Fields\Administrator\Model\FieldModel $model */
 212          $model = Factory::getApplication()->bootComponent('com_fields')->getMVCFactory()
 213              ->createModel('Field', 'Administrator', ['ignore_request' => true]);
 214          $model->cleanupValues($context, $item->id);
 215      }
 216  
 217      /**
 218       * The user delete event.
 219       *
 220       * @param   stdClass  $user    The context
 221       * @param   boolean   $success Is success
 222       * @param   string    $msg     The message
 223       *
 224       * @return  void
 225       *
 226       * @since   3.7.0
 227       */
 228      public function onUserAfterDelete($user, $success, $msg): void
 229      {
 230          $item     = new stdClass();
 231          $item->id = $user['id'];
 232  
 233          $this->onContentAfterDelete('com_users.user', $item);
 234      }
 235  
 236      /**
 237       * The form event.
 238       *
 239       * @param   Form      $form  The form
 240       * @param   stdClass  $data  The data
 241       *
 242       * @return  boolean
 243       *
 244       * @since   3.7.0
 245       */
 246      public function onContentPrepareForm(Form $form, $data)
 247      {
 248          $context = $form->getName();
 249  
 250          // When a category is edited, the context is com_categories.categorycom_content
 251          if (strpos($context, 'com_categories.category') === 0) {
 252              $context = str_replace('com_categories.category', '', $context) . '.categories';
 253              $data    = $data ?: Factory::getApplication()->input->get('jform', [], 'array');
 254  
 255              // Set the catid on the category to get only the fields which belong to this category
 256              if (is_array($data) && array_key_exists('id', $data)) {
 257                  $data['catid'] = $data['id'];
 258              }
 259  
 260              if (is_object($data) && isset($data->id)) {
 261                  $data->catid = $data->id;
 262              }
 263          }
 264  
 265          $parts = FieldsHelper::extract($context, $form);
 266  
 267          if (!$parts) {
 268              return true;
 269          }
 270  
 271          $input = Factory::getApplication()->input;
 272  
 273          // If we are on the save command we need the actual data
 274          $jformData = $input->get('jform', array(), 'array');
 275  
 276          if ($jformData && !$data) {
 277              $data = $jformData;
 278          }
 279  
 280          if (is_array($data)) {
 281              $data = (object) $data;
 282          }
 283  
 284          FieldsHelper::prepareForm($parts[0] . '.' . $parts[1], $form, $data);
 285  
 286          return true;
 287      }
 288  
 289      /**
 290       * The display event.
 291       *
 292       * @param   string    $context     The context
 293       * @param   stdClass  $item        The item
 294       * @param   Registry  $params      The params
 295       * @param   integer   $limitstart  The start
 296       *
 297       * @return  string
 298       *
 299       * @since   3.7.0
 300       */
 301      public function onContentAfterTitle($context, $item, $params, $limitstart = 0)
 302      {
 303          return $this->display($context, $item, $params, 1);
 304      }
 305  
 306      /**
 307       * The display event.
 308       *
 309       * @param   string    $context     The context
 310       * @param   stdClass  $item        The item
 311       * @param   Registry  $params      The params
 312       * @param   integer   $limitstart  The start
 313       *
 314       * @return  string
 315       *
 316       * @since   3.7.0
 317       */
 318      public function onContentBeforeDisplay($context, $item, $params, $limitstart = 0)
 319      {
 320          return $this->display($context, $item, $params, 2);
 321      }
 322  
 323      /**
 324       * The display event.
 325       *
 326       * @param   string    $context     The context
 327       * @param   stdClass  $item        The item
 328       * @param   Registry  $params      The params
 329       * @param   integer   $limitstart  The start
 330       *
 331       * @return  string
 332       *
 333       * @since   3.7.0
 334       */
 335      public function onContentAfterDisplay($context, $item, $params, $limitstart = 0)
 336      {
 337          return $this->display($context, $item, $params, 3);
 338      }
 339  
 340      /**
 341       * Performs the display event.
 342       *
 343       * @param   string    $context      The context
 344       * @param   stdClass  $item         The item
 345       * @param   Registry  $params       The params
 346       * @param   integer   $displayType  The type
 347       *
 348       * @return  string
 349       *
 350       * @since   3.7.0
 351       */
 352      private function display($context, $item, $params, $displayType)
 353      {
 354          $parts = FieldsHelper::extract($context, $item);
 355  
 356          if (!$parts) {
 357              return '';
 358          }
 359  
 360          // If we have a category, set the catid field to fetch only the fields which belong to it
 361          if ($parts[1] === 'categories' && !isset($item->catid)) {
 362              $item->catid = $item->id;
 363          }
 364  
 365          $context = $parts[0] . '.' . $parts[1];
 366  
 367          // Convert tags
 368          if ($context == 'com_tags.tag' && !empty($item->type_alias)) {
 369              // Set the context
 370              $context = $item->type_alias;
 371  
 372              $item = $this->prepareTagItem($item);
 373          }
 374  
 375          if (is_string($params) || !$params) {
 376              $params = new Registry($params);
 377          }
 378  
 379          $fields = FieldsHelper::getFields($context, $item, $displayType);
 380  
 381          if ($fields) {
 382              $app = Factory::getApplication();
 383  
 384              if ($app->isClient('site') && Multilanguage::isEnabled() && isset($item->language) && $item->language === '*') {
 385                  $lang = $app->getLanguage()->getTag();
 386  
 387                  foreach ($fields as $key => $field) {
 388                      if ($field->language === '*' || $field->language == $lang) {
 389                          continue;
 390                      }
 391  
 392                      unset($fields[$key]);
 393                  }
 394              }
 395          }
 396  
 397          if ($fields) {
 398              foreach ($fields as $key => $field) {
 399                  $fieldDisplayType = $field->params->get('display', '2');
 400  
 401                  if ($fieldDisplayType == $displayType) {
 402                      continue;
 403                  }
 404  
 405                  unset($fields[$key]);
 406              }
 407          }
 408  
 409          if ($fields) {
 410              return FieldsHelper::render(
 411                  $context,
 412                  'fields.render',
 413                  array(
 414                      'item'            => $item,
 415                      'context'         => $context,
 416                      'fields'          => $fields,
 417                  )
 418              );
 419          }
 420  
 421          return '';
 422      }
 423  
 424      /**
 425       * Performs the display event.
 426       *
 427       * @param   string    $context  The context
 428       * @param   stdClass  $item     The item
 429       *
 430       * @return  void
 431       *
 432       * @since   3.7.0
 433       */
 434      public function onContentPrepare($context, $item)
 435      {
 436          // Check property exists (avoid costly & useless recreation), if need to recreate them, just unset the property!
 437          if (isset($item->jcfields)) {
 438              return;
 439          }
 440  
 441          $parts = FieldsHelper::extract($context, $item);
 442  
 443          if (!$parts) {
 444              return;
 445          }
 446  
 447          $context = $parts[0] . '.' . $parts[1];
 448  
 449          // Convert tags
 450          if ($context == 'com_tags.tag' && !empty($item->type_alias)) {
 451              // Set the context
 452              $context = $item->type_alias;
 453  
 454              $item = $this->prepareTagItem($item);
 455          }
 456  
 457          // Get item's fields, also preparing their value property for manual display
 458          // (calling plugins events and loading layouts to get their HTML display)
 459          $fields = FieldsHelper::getFields($context, $item, true);
 460  
 461          // Adding the fields to the object
 462          $item->jcfields = array();
 463  
 464          foreach ($fields as $key => $field) {
 465              $item->jcfields[$field->id] = $field;
 466          }
 467      }
 468  
 469      /**
 470       * Prepares a tag item to be ready for com_fields.
 471       *
 472       * @param   stdClass  $item  The item
 473       *
 474       * @return  object
 475       *
 476       * @since   3.8.4
 477       */
 478      private function prepareTagItem($item)
 479      {
 480          // Map core fields
 481          $item->id       = $item->content_item_id;
 482          $item->language = $item->core_language;
 483  
 484          // Also handle the catid
 485          if (!empty($item->core_catid)) {
 486              $item->catid = $item->core_catid;
 487          }
 488  
 489          return $item;
 490      }
 491  }


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