Skip to content

Instantly share code, notes, and snippets.

@KristianH
Created January 13, 2015 13:00
Show Gist options
  • Save KristianH/205d0140896e4ad0391a to your computer and use it in GitHub Desktop.
Save KristianH/205d0140896e4ad0391a to your computer and use it in GitHub Desktop.
setDefaultUserGroups.php set usergroups 'oxidforeigncustomer' and 'oxidnewcustomer' based on the country(oxuser)
<?php
//WARNING: this file change DB data! be careful!
// Use this in a oxid e-sales shop 4.7.0 or higher
//if you are aware of the risks, remove or comment the next line:
die('Please read the comments!');
define('OX_IS_ADMIN', true);
include('bootstrap.php');
$sGetRestUserQuery = 'SELECT `oxobjectid` AS `oxobjectid` FROM `oxobject2group` `o2g`
LEFT JOIN `oxuser` ON `oxuser`.`oxid` = `o2g`.`oxobjectid`
WHERE `oxgroupsid` IN (\'oxidforeigncustomer\', \'oxidnewcustomer\')
AND `oxuser`.`oxid` IS NOT NULL
AND `oxuser`.`oxactive` = 1
ORDER BY `oxuser`.`oxid`';
$oDB = oxDb::getDb(oxDB::FETCH_MODE_ASSOC);
$aExcludeUsers = $oDB->getArray($sGetRestUserQuery);
$sExcludeUserQuery = "oxid NOT IN (";
foreach ($aExcludeUsers as $aUserId) {
$sExcludeUserQuery .= "'" . $aUserId['oxobjectid'] . "', ";
}
$sExcludeUserQuery = trim($sExcludeUserQuery, ", ") . ')';
/** @var oxuser $oUser */
$oUser = oxNew('oxuser');
$sGetRestUserQuery = 'SELECT * FROM `oxuser` WHERE ' . $oUser->getSqlActiveSnippet(
) . ' AND `oxcountryid` != \'\' AND ' . $sExcludeUserQuery . ' LIMIT 0, 500 ';
$oDB = oxDb::getDb(oxDB::FETCH_MODE_ASSOC);
$aRestUser = $oDB->getArray($sGetRestUserQuery);
$iKrauts = 0;
$iNotKrauts = 0;
$aHomeCountries = oxRegistry::getConfig()->getConfigParam('aHomeCountry');
foreach ($aRestUser as $aUser) {
/** @var oxuser $oUser */
$oUser = oxNew('oxuser');
if (false == $oUser->load($aUser['OXID'])) {
continue;
}
if ($oUser->inGroup('oxidforeigncustomer') || $oUser->inGroup('oxidnewcustomer')) {
continue;
}
if (in_array($oUser->getFieldData('oxcountryid'), $aHomeCountries)) {
$iKrauts++;
$oUser->addToGroup('oxidnewcustomer');
continue;
}
$iNotKrauts++;
$oUser->addToGroup('oxidforeigncustomer');
}
echo <<<HTML
<html>
<head>
HTML;
$sGetRestUserQuery = 'SELECT count(*) FROM `oxuser` WHERE ' . $oUser->getSqlActiveSnippet(
) . ' AND `oxcountryid` != \'\' AND ' . $sExcludeUserQuery;
$oDB = oxDb::getDb(oxDB::FETCH_MODE_ASSOC);
$aRestUser = $oDB->getOne($sGetRestUserQuery);
if ($aRestUser > 0) {
echo '<meta http-equiv="refresh" content="0">';
}
echo "</head><body>";
echo "not krauts: $iNotKrauts, krauts: $iKrauts, rest: $aRestUser";
echo <<<HTML
</body>
</html>
HTML;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment