Back to AddUserCommand class

Method getUserGroups

protected array
getUserGroups
()
Method to get a value from option
Returns
  • array
Since
  • 4.0.0

Method getUserGroups - Source code

/**
 * Method to get a value from option
 *
 * @return  array
 *
 * @since   4.0.0
 */
protected function getUserGroups() : array
{
    $groups = $this->getApplication()->getConsoleInput()->getOption('usergroup');
    $db = Factory::getDbo();
    $groupList = [];
    // Group names have been supplied as input arguments
    if (!\is_null($groups) && $groups[0]) {
        $groups = explode(',', $groups);
        foreach ($groups as $group) {
            $groupId = $this->getGroupId($group);
            if (empty($groupId)) {
                $this->ioStyle->error("Invalid group name '" . $group . "'");
                throw new InvalidOptionException("Invalid group name " . $group);
            }
            $groupList[] = $this->getGroupId($group);
        }
        return $groupList;
    }
    // Generate select list for user
    $query = $db->getQuery(true)->select($db->quoteName('title'))->from($db->quoteName('#__usergroups'))->order($db->quoteName('id') . 'ASC');
    $db->setQuery($query);
    $list = $db->loadColumn();
    $choice = new ChoiceQuestion('Please select a usergroup (separate multiple groups with a comma)', $list);
    $choice->setMultiselect(true);
    $answer = (array) $this->ioStyle->askQuestion($choice);
    foreach ($answer as $group) {
        $groupList[] = $this->getGroupId($group);
    }
    return $groupList;
}