/** * Load all the user rooms right in the $entityCollection attribute * * @param int $userId The user ID */ public function loadRoomsRight(int $userId) { $sqlMarks = 'SELECT * FROM %s WHERE idUser = %d'; $sql = static::sqlFormat($sqlMarks, $this->entity->getTableName(), $userId); $roomsRight = DB::query($sql)->fetchAll(); foreach ($roomsRight as $roomRightInfo) { $this->entityCollection->add(new RoomRight($roomRightInfo)); } }
/** * Load the banned users for the current room */ public function loadBannedUsers() { $roomBanCollection = new RoomBanCollection(); $sqlMarks = 'SELECT * FROM %s WHERE `idRoom` = %d'; $sql = static::sqlFormat($sqlMarks, $this->entity->getTableName(), $this->entity->idRoom); foreach (DB::query($sql)->fetchAll(\PDO::FETCH_ASSOC) as $row) { $roomBanCollection->add(new RoomBan($row)); } $this->entityCollection = $roomBanCollection; }
/** * Create a new room * * @param int $idUser The user creator id * @param string $roomName The room name * @param int $maxUsers The max room users * @param string $password The room password DEFAULT '' * * @throws Exception If the room name is empty * @throws Exception If the room name already exists * @throws Exception If the max number of users is lower than 2 * * @return bool True if the room was successfully created, false otherwise */ public function createRoom(int $idUser, string $roomName, int $maxUsers, string $password = '') : bool { $roomName = trim($roomName); // Checking error if ($roomName === '') { throw new Exception(_('The room name cannot be empty'), LogLevel::PARAMETER); } if ($maxUsers < 2) { throw new Exception(_('The max number of users must be greater than 1'), LogLevel::PARAMETER); } $sqlMarks = 'SELECT COUNT(id) FROM %s WHERE name = %s'; $sql = static::sqlFormat($sqlMarks, (new Room())->getTableName(), DB::quote($roomName)); if ((int) DB::query($sql)->fetchColumn() > 0) { throw new Exception(_('This room name already exists'), LogLevel::PARAMETER); } // Creation $query = 'SELECT MAX(id) FROM ' . $this->entity->getTableName(); $room = new Room(['id' => (int) DB::query($query)->fetchColumn() + 1, 'name' => $roomName, 'creator' => $idUser, 'password' => $password, 'creationDate' => new \DateTime(), 'maxUsers' => $maxUsers]); return $this->saveEntity($room); }
/** * Check the user security token * * @return bool True if the check is ok else false */ public function checkSecurityToken() : bool { $sqlMarks = 'SELECT securityToken, securityTokenExpires FROM %s WHERE id = %d'; $sql = static::sqlFormat($sqlMarks, (new User())->getTableName(), $this->entity->id); $results = DB::query($sql)->fetch(); return $this->entity->securityToken === $results['securityToken'] && new \DateTime() <= new \DateTime($results['securityTokenExpires']); }
/** * Check if a column value is not already in database if the column has a unique attribute constraint * * @param string $columnName The column name * @param mixed $value The column value * * @return bool True if the value is already in database and the column has a unique attribute constraint * else false * * @todo Move to EntityManager ? */ public function checkUniqueField(string $columnName, $value) : bool { $alreadyInDatabase = false; if (strpos($this->constraints['unique'], $columnName) !== false) { $sqlMarks = 'SELECT count(*) FROM %s WHERE %s = ' . DB::quote($value); $sql = EntityManager::sqlFormat($sqlMarks, $this->tableName, $columnName); $alreadyInDatabase = (int) DB::query($sql)->fetchColumn() > 0; } return $alreadyInDatabase; }
/** * Check if the entity already exists in the database * * @return bool True if the entity exists else false */ private function entityAlreadyExists() : bool { $sqlMarks = " SELECT COUNT(*)\n FROM %s\n WHERE %s"; $sql = static::sqlFormat($sqlMarks, $this->entity->getTableName(), $this->getEntityPrimaryKeysWhereClause()); return (int) DB::query($sql)->fetchColumn() >= 1; }
/** * Create all the entities table */ private function createAllTables() { $entityManager = new EntityManager(); // @todo Handle table creation order based on constraint DB::query('SET FOREIGN_KEY_CHECKS = 0;'); foreach (DB::getAllEntities() as $entityName) { /** * @var Entity $entity An entity */ $entityClassNamespace = Ini::getParam('Entities', 'entitiesClassNamespace') . '\\' . $entityName; $entity = new $entityClassNamespace(); // @see DB::getAllTables() $tableName = strtoupper($entity->getTableName()); if (!in_array($tableName, DB::getAllTables())) { static::out('Create table "' . $tableName . '"' . PHP_EOL); $entityManager->setEntity($entity); $entityManager->createEntityTable(); } else { static::out('Table table "' . $tableName . '" already exists' . PHP_EOL); } } // @todo Handle table creation order based on constraint DB::query('SET FOREIGN_KEY_CHECKS = 1;'); static::ok(static::ACTION_DONE . PHP_EOL); }