<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=users.edit.update.first, users.register.add.first
 * [END_COT_EXT]
 */

/**
 * plugin User Group Selector for Cotonti Siena
 * 
 * @package usergroupselector
 * @version 1.0.0
 * @author CMSWorks Team
 * @copyright Copyright (c) CMSWorks.ru, littledev.ru
 * @license BSD
 *  */
defined('COT_CODE') or die('Wrong URL');

require_once cot_langfile('usergroupselector', 'plug');

if(($cfg['plugin']['usergroupselector']['allowchange'] || $cfg['plugin']['usergroupselector']['required']) 
	&& !empty($cfg['plugin']['usergroupselector']['groups'])
	&& $urr['user_maingrp'] != COT_GROUP_SUPERADMINS 
	&& !in_array($urr['user_maingrp'], $cfg['moder_group_ids']))
{
	$groupstoselect = explode(',', $cfg['plugin']['usergroupselector']['groups']);
	$ruser['user_usergroup'] = (isset($_POST['ruserusergroup']) ? cot_import('ruserusergroup','P','INT') : 7);

  if(isset($_POST['userType']) && in_array(cot_import('userType','P','INT'), array(1, 2))) {
    $ruser['user_usergroup'] = (cot_import('userType','P','INT') == 1 ? 7 : 4);
  }

	if($cfg['plugin']['usergroupselector']['required'])
	{	
		cot_check((empty($ruser['user_usergroup']) || !in_array($ruser['user_usergroup'], $groupstoselect)), $L['usergroupselector_error_emptygroup'], 'rusergroup');
	}
}