[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_users/src/Field/ -> GroupparentField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_users
   6   *
   7   * @copyright   (C) 2010 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\Users\Administrator\Field;
  12  
  13  use Joomla\CMS\Access\Access;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Form\Field\ListField;
  16  use Joomla\CMS\Helper\UserGroupsHelper;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * User Group Parent field..
  24   *
  25   * @since  1.6
  26   */
  27  class GroupparentField extends ListField
  28  {
  29      /**
  30       * The form field type.
  31       *
  32       * @var        string
  33       * @since   1.6
  34       */
  35      protected $type = 'GroupParent';
  36  
  37      /**
  38       * Method to clean the Usergroup Options from all children starting by a given father
  39       *
  40       * @param   array    $userGroupsOptions  The usergroup options to clean
  41       * @param   integer  $fatherId           The father ID to start with
  42       *
  43       * @return  array  The cleaned field options
  44       *
  45       * @since   3.9.4
  46       */
  47      private function cleanOptionsChildrenByFather($userGroupsOptions, $fatherId)
  48      {
  49          foreach ($userGroupsOptions as $userGroupsOptionsId => $userGroupsOptionsData) {
  50              if ((int) $userGroupsOptionsData->parent_id === (int) $fatherId) {
  51                  unset($userGroupsOptions[$userGroupsOptionsId]);
  52  
  53                  $userGroupsOptions = $this->cleanOptionsChildrenByFather($userGroupsOptions, $userGroupsOptionsId);
  54              }
  55          }
  56  
  57          return $userGroupsOptions;
  58      }
  59  
  60      /**
  61       * Method to get the field options.
  62       *
  63       * @return  array  The field option objects
  64       *
  65       * @since   1.6
  66       */
  67      protected function getOptions()
  68      {
  69          $options        = UserGroupsHelper::getInstance()->getAll();
  70          $currentGroupId = (int) Factory::getApplication()->input->get('id', 0, 'int');
  71  
  72          // Prevent to set yourself as parent
  73          if ($currentGroupId) {
  74              unset($options[$currentGroupId]);
  75          }
  76  
  77          // We should not remove any groups when we are creating a new group
  78          if ($currentGroupId !== 0) {
  79              // Prevent parenting direct children and children of children of this item.
  80              $options = $this->cleanOptionsChildrenByFather($options, $currentGroupId);
  81          }
  82  
  83          $options      = array_values($options);
  84          $isSuperAdmin = Factory::getUser()->authorise('core.admin');
  85  
  86          // Pad the option text with spaces using depth level as a multiplier.
  87          for ($i = 0, $n = count($options); $i < $n; $i++) {
  88              // Show groups only if user is super admin or group is not super admin
  89              if ($isSuperAdmin || !Access::checkGroup($options[$i]->id, 'core.admin')) {
  90                  $options[$i]->value = $options[$i]->id;
  91                  $options[$i]->text = str_repeat('- ', $options[$i]->level) . $options[$i]->title;
  92              } else {
  93                  unset($options[$i]);
  94              }
  95          }
  96  
  97          // Merge any additional options in the XML definition.
  98          return array_merge(parent::getOptions(), $options);
  99      }
 100  }


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