id = filterId($id); $this->loginName = $loginName; $this->name = $name; $this->firstName = $firstName; $this->dateOfBirth = ($dateOfBirth != null) ? DateTime::createFromFormat('Y-m-d', $dateOfBirth) : null; $this->eMail = ($eMail != null) ? filter_var($eMail, FILTER_VALIDATE_EMAIL, ['options' => ['default' => null]]) : null; $this->config = $config; $this->pwHash = $pwHash; } /** return users year of birth as int * * @retval int>=0 on success, * @retval null failure */ public function yearOfBirth() { if (isset($this->dateOfBirth)) { return filterPosInt($this->dateOfBirth->format('Y')); } return null; } //// // dbInterface //// public function addToDb() { // if the user has an Id set it has to come from the Db. Hence don't add an User that is already added. if (isset($this->id) || !participo::isUserAdmin()) { return; } $this->id = self::dbInsert( $this->loginName, $this->name, $this->firstName, (isset($this->dateOfBirth)) ? ($this->dateOfBirth->format('Y-m-d')) : null, $this->eMail, $this->config, $this->pwHash ); return $this->id; } private static function dbInsert($loginName, $name, $firstName, $dateOfBirth = null, $eMail = null, $config = null, $pwHash = null) { $query = 'INSERT INTO `' . self::$tableName . '` ' . '(loginName, name, vorname, gebDatum, eMail, config, pwHash) ' . ' VALUES (:loginName, :name, :vorname, :gebDatum, :eMail, :config, :pwHash);'; $params = [ ':loginName' => ['value' => $loginName, 'data_type' => self::$dbColumns['loginName']], ':name' => ['value' => $name, 'data_type' => self::$dbColumns['name']], ':vorname' => ['value' => $firstName, 'data_type' => self::$dbColumns['vorname']], ':gebDatum' => ['value' => $dateOfBirth, 'data_type' => self::$dbColumns['gebDatum']], ':eMail' => ['value' => $eMail, 'data_type' => self::$dbColumns['eMail']], ':config' => ['value' => $config, 'data_type' => self::$dbColumns['config']], ':pwHash' => ['value' => $pwHash, 'data_type' => self::$dbColumns['pwHash']], ]; $response = dbConnector::query($query, $params); return dbConnector::getLastInsertId(); } public static function dbSelectWithAttribute(int $attributeId) { $query = "SELECT DISTINCT" . " `wkParticipo_Users`.* " . " FROM `wkParticipo_Users`" . " JOIN `wkParticipo_user<=>userAttributes`" . " ON `wkParticipo_user<=>userAttributes`.`userId` = `wkParticipo_Users`.`id`" . " WHERE `wkParticipo_user<=>userAttributes`.`attributeId` = :attributeId". " ORDER BY `wkParticipo_Users`.`id` ASC;"; $params = [ ':attributeId' => ['value' => $attributeId, 'data_type' => PDO::PARAM_INT] ]; $response = dbConnector::query($query, $params); return $response; } /** Name of the table with all the Users * * @var string */ private static $tableName = 'wkParticipo_Users'; /** columns in the User table (in the database) with their type * * @var array */ private static $dbColumns = [ 'id' => PDO::PARAM_INT, 'loginName' => PDO::PARAM_STR, 'name' => PDO::PARAM_STR, 'vorname' => PDO::PARAM_STR, 'gebDatum' => PDO::PARAM_STR, 'eMail' => PDO::PARAM_STR, 'config' => PDO::PARAM_STR, 'pwHash' => PDO::PARAM_STR ]; //// // html interface //// public static function getHtmlFormAddUser($options = []) { $returnToUrl = $options['returnToUrl'] ?? urlencode(getCurPagesUrl()); $formClass = isset($options['formClass']) ? 'class="' . $options['formClass'] . '"' : ''; $form = '
' . '' . '
' . '
' . '
' . '
' . '
' . '' . '
'; return $form; } public static function htmlFormAddUser($options = []) { echo(self::getHtmlFormAddUser($options)); } // member variables private $id; private $loginName; private $name; private $firstName; private $dateOfBirth; private $eMail; private $config; private $pwHash; // database member data /** List of ids of the users kids */ private $kidIds = null; /** List of users kids */ private $kids = null; // public function kidIds(bool $forceLoading = false) // { // if (is_null($this->kidIds) || $forceLoading) { // $this->kidIds = self::getKidIds($id); // } // return self::$kidIds; // } public function kids(bool $forceLoading = false) { if (is_null($this->kids) || $forceLoading) { $this->kids = participo::getKids($this->id); } return $this->kids; } // private static function getKidIds(int $id) // { // $response = dbConnector::query( // 'SELECT * FROM `wkParticipo_Users` WHERE `' . $name . '` = :' . $name, // [$name => ['value' => $value, 'data_type' => self::$dbColumns[$name]]] // ); // $query = << ['value' => $userId, 'data_type' => PDO::PARAM_INT] // ]; // $response = dbConnector::query($query, $params); // return $response; // } /** Export the User data into an associative array * * @return array associative array representing the user */ public function toAssoc() { return [ 'id' => $this->id, 'loginName' => $this->loginName, 'name' => $this->name, 'vorname' => $this->firstName, 'gebDatum' => $this->dateOfBirth, 'eMail' => $this->eMail, 'config' => $this->config, 'pwHash' => $this->pwHash ]; } /** verify the users password * * @param [string] $password the password to verify * @return true if password is verified, false otherwise */ public function verifyPassword($password) { return password_verify($password, $this->pwHash); } // getter functions public function getId() { return $this->id; } public function getLoginName() { return $this->loginName; } public function getName() { return $this->name; } public function getFirstName() { return $this->firstName; } public function getConfig() { return $this->config; } public function getDateOfBirth(){ return $this->dateOfBirth; } public function getStrBirthday(){ return $this->dateOfBirth->format("Y-m-d"); } // static functions /** Create a User from an assoziative array like it is returned from db requests * * @param array $member associative array with the UserData from the dbRequest * @param $columnMappings renaming of columnNames, e.g., if the id isn't under 'id' in the array but under 'userID', add 'id'=>'userId' to the mappings * @return User initialized user */ public static function fromDbArray($member, $columnMappings = []) { // if it isn't remapped, take default column name foreach (self::$dbColumns as $columnName => $columnDataType) { if (!array_key_exists($columnName, $columnMappings)) { $columnMappings[$columnName] = $columnName; } } return new User( $member[ $columnMappings['id'] ] ?? null, $member[$columnMappings['loginName']] ?? null, $member[$columnMappings['name']] ?? null, $member[$columnMappings['vorname']] ?? null, $member[$columnMappings['gebDatum']] ?? null, array_key_exists($columnMappings['eMail'], $member) ? explode(',', $member['eMail']) : null, array_key_exists($columnMappings['config'], $member) ? json_decode($member['config']) : null, array_key_exists($columnMappings['pwHash'], $member) ? $member['pwHash'] : null ); } /** Load an User from the db via an id * * @param int $userId * @return loaded user or null (if sth. wrong) */ public static function loadFromDb($userId) { return self::loadFromDbBy('id', $userId); } /** Load an User from the db via the loginName * * @param int $login * @return loaded user or null (if sth. wrong) */ public static function loadFromDbByLoginName($login) { return self::loadFromDbBy('loginName', $login); } /** Load a user from the db by a column * * @param [string] $name name of the column * @param [mixed] $value value to look for * @param [bool] $unique if the value is unique (true->return single value) or not (false->return array) * @return loaded user or null (if sth. wrong) */ public static function loadFromDbBy($name, $value) { if (!array_key_exists($name, self::$dbColumns)) { return null; } $response = dbConnector::query( 'SELECT * FROM `wkParticipo_Users` WHERE `' . $name . '` = :' . $name, [$name => ['value' => $value, 'data_type' => self::$dbColumns[$name]]] ); if (count($response) != 1) { return null; } return User::fromDbArray($response[0]); } public static function loadFromDbByAttribute(int $attributeId){ $query = "SELECT DISTINCT" . " `wkParticipo_Users`.* " . " FROM `wkParticipo_Users`" . " JOIN `wkParticipo_user<=>userAttributes`" . " ON `wkParticipo_user<=>userAttributes`.`userId` = `wkParticipo_Users`.`id`" . " WHERE `wkParticipo_user<=>userAttributes`.`attributeId` = :attributeId". " ORDER BY `wkParticipo_Users`.`id` ASC;"; $response = dbConnector::query( $query , [ 'attributeId'=>[ 'value'=>filterId($attributeId) , 'data_type'=>PDO::PARAM_INT] ] ); // Postprocessing // - convert the comma separated list into an array foreach ($response as &$user) { $user["eMail"] = explode(",", $user["eMail"]); foreach ($user["eMail"] as &$email) { $email = trim($email); } } return $response; } }