/**
* Creates a new local user in the application data base.
*
* @param WebSoccer $websoccer Application context.
* @param DbConnection $db DB Connection.
* @param string $nick User name of new user. Optional if e-mail address is provided. Must be unique in local data base. Case sensitive.
* @param string $email E-mail address of new user. Optional if nick is provided. Must be unique in local data base. Case insensitive (will be stored with lower letters).
* @throws Exception if both nick and e-mail are blank, or if nick name or e-mail address is already in use. Messages are not internationalized. Method assumes appropriate checks before calling it.
* @return int ID of newly created user.
*/
public static function createLocalUser(WebSoccer $websoccer, DbConnection $db, $nick = null, $email = null)
{
$username = trim($nick);
$emailAddress = strtolower(trim($email));
// check if either nick or e-mail is provided. If not, it most probably is a wrong API call,
// hence message is not required to be translated.
if (!strlen($username) && !strlen($emailAddress)) {
throw new Exception("UsersDataService::createBlankUser(): Either user name or e-mail must be provided in order to create a new internal user.");
}
// verify that there is not already such a user. If so, the calling function is wrongly implemented, hence
// no translation of message.
if (strlen($username) && self::getUserIdByNick($websoccer, $db, $username) > 0) {
throw new Exception("Nick name is already in use.");
}
if (strlen($emailAddress) && self::getUserIdByEmail($websoccer, $db, $emailAddress) > 0) {
throw new Exception("E-Mail address is already in use.");
}
// creates user.
$i18n = I18n::getInstance($websoccer->getConfig("supported_languages"));
$columns = array("nick" => $username, "email" => $emailAddress, "status" => "1", "datum_anmeldung" => $websoccer->getNowAsTimestamp(), "lang" => $i18n->getCurrentLanguage());
if ($websoccer->getConfig("premium_initial_credit")) {
$columns["premium_balance"] = $websoccer->getConfig("premium_initial_credit");
}
$db->queryInsert($columns, $websoccer->getConfig("db_prefix") . "_user");
// provide ID of created user.
if (strlen($username)) {
$userId = self::getUserIdByNick($websoccer, $db, $username);
} else {
$userId = self::getUserIdByEmail($websoccer, $db, $emailAddress);
}
// trigger plug-ins
$event = new UserRegisteredEvent($websoccer, $db, I18n::getInstance($websoccer->getConfig("supported_languages")), $userId, $username, $emailAddress);
PluginMediator::dispatchEvent($event);
return $userId;
}