add userGroup when installing component

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
mixahlos
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 132
Joined: Sat Jul 22, 2006 9:52 pm
Contact:

add userGroup when installing component

Post by mixahlos » Sat Jul 14, 2018 10:34 am

Hi,

In my custom component I want to add a custom user group when installing and delete it when uninstalling. I have altered my script.php file to add those operations but is not working.

So in my script.php I have the following code :

Code: Select all

class com_ContractInstallerScript {

  public function install($parent) {
    $parent->getParent()->setRedirectURL('index.php?option=com_contract');
    $group = array('id' => 0, 'title' => 'Contracts', 'parent_id' => 1);
    JLoader::import('joomla.application.component.model');
    JLoader::import('group', JPATH_ADMINISTRATOR . '/components/com_users/models');
    $groupModel = JModelLegacy::getInstance('Group', 'UsersModel');

    if (!$groupModel->save($group)) {
      JFactory::getApplication()->enqueueMessage($groupModel->getError());
    }
  }

  public function uninstall($parent) {
    $db = JFactory::getDBO();
    $db->setQuery($db->getQuery(true)
                    ->select('id')
                    ->from("#__usergroups")
                    ->where($db->quoteName('title') . ' = ' . $db->quote('Contracts'))
    );
    $group = $db->loadObject();
    if (!$groupModel->delete($group->id)) {
      JFactory::getApplication()->enqueueMessage($groupModel->getError());
      return false;
    } else {
      echo '<p>' . JText::_('COM_CONTRACT_UNINSTALL_REMOVE_GROUP_TEXT') . '</p>';
    }

    echo '<p>' . JText::_('COM_CONTRACT_UNINSTALL_TEXT') . '</p>';
  }
}
Thank you for your help
Mike

Locked

Return to “Joomla! 3.x Coding”