Добавлен класс Posts
This commit is contained in:
1 parent
20d24e23af
commit
d52dd3d0c4
4 files changed
+253
-1
No files matched your search
@@ -0,0 +1,96 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
use Core\Helpers\Pagination;
|
||||
use Core\Models\File;
|
||||
use Core\Models\Posts;
|
||||
use Core\Models\User;
|
||||
|
||||
require_once __DIR__ . '/inc/header.php';
|
||||
?>
|
||||
<div class="pageheader">
|
||||
<div class="media">
|
||||
<div class="pageicon pull-left">
|
||||
<i class="fa fa-home"></i>
|
||||
</div>
|
||||
<div class="media-body">
|
||||
<ul class="breadcrumb">
|
||||
<li><a href=""><i class="glyphicon glyphicon-home"></i></a></li>
|
||||
<li>Список записей</li>
|
||||
</ul>
|
||||
<h4>Список записей</h4>
|
||||
</div>
|
||||
</div><!-- media -->
|
||||
</div><!-- pageheader -->
|
||||
|
||||
<div class="contentpanel">
|
||||
<!-- table -->
|
||||
<div class="row">
|
||||
<div class="col-md-12r">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-primary table-hover mb30">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Заголовок</th>
|
||||
<th>Текст</th>
|
||||
<th>Автор</th>
|
||||
<th>Картинка</th>
|
||||
<th>Создан</th>
|
||||
<th>Обновлен</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$objectPosts = new Posts();
|
||||
Pagination::execute($_REQUEST['page'], $objectPosts->getAllCount(), PAGINATION_LIMIT);
|
||||
$limit = Pagination::getLimit();
|
||||
|
||||
$arPosts = $objectPosts->getPosts($limit);
|
||||
foreach ($arPosts as $elPost) {
|
||||
?>
|
||||
<tr>
|
||||
<td><?= $elPost['id'] ?></a></td>
|
||||
<td><?= $elPost['title'] ?></td>
|
||||
<td><?= htmlspecialchars($elPost['text']) ?></td>
|
||||
<td><?= User::getInfoById($elPost['user_id']) ?></td>
|
||||
<td><?php
|
||||
$image = (new File($elPost['image_id']))->getAllProps();
|
||||
if(!empty($image)) {
|
||||
echo '<img src="' . $image['path'] . '" alt="' . $image['name'] . '" width="50px">';
|
||||
}
|
||||
?></td>
|
||||
|
||||
<td><?= $elPost['date_created'] ? date('d.m.Y H:i:s', strtotime($elPost['date_created'])) : '-'; ?></td>
|
||||
<td><?= $elPost['date_updated'] ? date('d.m.Y H:i:s', strtotime($elPost['date_updated'])) : '-'; ?></td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div><!-- table-responsive -->
|
||||
<?php Pagination::show('page') ?>
|
||||
</div>
|
||||
</div>
|
||||
<!-- end table -->
|
||||
</div><!-- contentpanel -->
|
||||
<?php require_once __DIR__ . '/inc/footer.php'; ?>
|
||||
@@ -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