Добавлен класс Posts
This commit is contained in:
1 parent
20d24e23af
commit
d52dd3d0c4
4 files changed
+253
-1
No files matched your search
@@ -155,6 +155,7 @@
|
||||
* Получение файла по идентификатору
|
||||
*
|
||||
* @return array|null
|
||||
* @throws CoreException
|
||||
*/
|
||||
public function getAllProps(): ?array
|
||||
{
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2022 Roman Grinko <rsgrinko@gmail.com>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Класс для работы с пользователями
|
||||
*/
|
||||
|
||||
namespace Core\Models;
|
||||
|
||||
use Core\CoreException;
|
||||
use Core\Helpers\Sanitize;
|
||||
use Core\Models\DB;
|
||||
|
||||
class Posts
|
||||
{
|
||||
private const TABLE = 'posts';
|
||||
|
||||
private const TABLE_SECTIONS = 'sections';
|
||||
|
||||
private $id = null;
|
||||
private $sectionId = null;
|
||||
|
||||
public function __construct(?int $id = null)
|
||||
{
|
||||
if (!empty($id)) {
|
||||
$this->id = $id;
|
||||
}
|
||||
}
|
||||
|
||||
public function setSection(int $sectionId): self
|
||||
{
|
||||
$this->sectionId = Sanitize::sanitizeNumber($sectionId);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSection(): ?int
|
||||
{
|
||||
return $this->sectionId;
|
||||
}
|
||||
|
||||
public function update(array $fields): bool
|
||||
{
|
||||
if(empty($this->id)){
|
||||
throw new CoreException('ID записи не задан');
|
||||
}
|
||||
foreach($fields as $key => $value) {
|
||||
$fields[$key] = Sanitize::sanitizeString($value);
|
||||
}
|
||||
/** @var $DB DB */
|
||||
$DB = DB::getInstance();
|
||||
|
||||
return $DB->update(self::TABLE, ['id' => $this->id], $fields);
|
||||
}
|
||||
|
||||
public function getPosts(string $limit = '10', string $sort = 'DESC'): array
|
||||
{
|
||||
/** @var $DB DB */
|
||||
$DB = DB::getInstance();
|
||||
|
||||
$sql = 'SELECT * FROM ' . self::TABLE;
|
||||
if (!empty($this->sectionId)) {
|
||||
$sql .= ' WHERE section_id="' . $this->sectionId . '"';
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY id ' . $sort . ' LIMIT ' . $limit;
|
||||
return $DB->query($sql) ?: [];
|
||||
}
|
||||
|
||||
public function getAllPostData(): ?array
|
||||
{
|
||||
$result = (DB::getInstance())->getItem(self::TABLE, ['id' => $this->id]);
|
||||
|
||||
if (!empty($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getImage(): ?array
|
||||
{
|
||||
$result = null;
|
||||
$imageId = $this->getAllPostData()['image_id'];
|
||||
|
||||
if (!empty($imageId)) {
|
||||
try {
|
||||
$result = (new File($imageId))->getAllProps();
|
||||
} catch (CoreException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAllCount(): int
|
||||
{
|
||||
/** @var $DB DB */
|
||||
$DB = DB::getInstance();
|
||||
|
||||
$sql = 'SELECT count(*) as count FROM ' . self::TABLE;
|
||||
if (!empty($this->sectionId)) {
|
||||
$sql .= ' WHERE section_id="' . $this->sectionId . '"';
|
||||
}
|
||||
$res = $DB->query($sql);
|
||||
return $res ? (int)$res[0]['count'] : 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -63,7 +63,7 @@
|
||||
/**
|
||||
* Таблица с пользователями
|
||||
*/
|
||||
const TABLE = 'users';
|
||||
private const TABLE = 'users';
|
||||
|
||||
/**
|
||||
* @var string $cryptoSalt Соль для шифрования
|
||||
@@ -312,6 +312,34 @@
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить объект пользователя по ID при его существовании
|
||||
*
|
||||
* @throws CoreException
|
||||
*/
|
||||
public static function getById(int $id): ?self
|
||||
{
|
||||
if(self::isUserExistsByParams(['id' => $id])) {
|
||||
return new self($id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получение строки с данными о пользователе по ID
|
||||
*
|
||||
* @return string [123] (admin) Роман
|
||||
* @throws CoreException
|
||||
*/
|
||||
public static function getInfoById(int $id): string
|
||||
{
|
||||
if (self::isUserExistsByParams(['id' => $id])) {
|
||||
$objectUser = new self($id);
|
||||
return '[' . $id . '] (' . $objectUser->getLogin() . ') ' . $objectUser->getName();
|
||||
}
|
||||
return 'Неизвестно';
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверка пользователя на онлайн
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user