DEV: SLUG поста (false/true)

This commit is contained in:
Evg 2023-01-02 16:38:36 +03:00
parent 8bebef006b
commit 858b620542
34 changed files with 63 additions and 49 deletions

View file

@ -29,17 +29,17 @@ class ActionController extends Controller
switch ($type) {
case 'post':
$url = url('post', ['id' => $info_type['post_id'], 'slug' => $info_type['post_slug']]);
$url = post_slug($info_type['post_id'], $info_type['post_slug']);
$action_type = 'post';
break;
case 'comment':
$post = PostModel::getPost($info_type['comment_post_id'], 'id', $this->user);
$url = url('post', ['id' => $info_type['comment_post_id'], 'slug' => $post['post_slug']]) . '#comment_' . $info_type['comment_id'];
$url = post_slug($info_type['comment_post_id'], $post['post_slug']) . '#comment_' . $info_type['comment_id'];
$action_type = 'comment';
break;
case 'answer':
$post = PostModel::getPost($info_type['answer_post_id'], 'id', $this->user);
$url = url('post', ['id' => $info_type['answer_post_id'], 'slug' => $post['post_slug']]) . '#answer_' . $info_type['answer_id'];
$url = post_slug($info_type['answer_post_id'], $post['post_slug']) . '#answer_' . $info_type['answer_id'];
$action_type = 'answer';
break;
case 'reply':

View file

@ -14,7 +14,7 @@ class AddAnswerController extends Controller
{
$post = PostPresence::index(Request::getPostInt('post_id'), 'id');
$url_post = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]);
$url_post = post_slug($post['post_id'], $post['post_slug']);
Validator::Length($content = $_POST['content'], 6, 5000, 'content', $url_post);

View file

@ -56,7 +56,7 @@ class EditAnswerController extends Controller
$post = PostPresence::index($answer['answer_post_id'], 'id');
$url_post = url('post', ['id' => $answer['answer_post_id'], 'slug' => $post['post_slug']]);
$url_post = post_slug($answer['answer_post_id'], $post['post_slug']);
Validator::Length($content, 6, 5000, 'content', url('content.edit', ['type' => 'answer', 'id' => $answer['answer_id']]));

View file

@ -36,7 +36,7 @@ class AddCommentController extends Controller
$post = PostPresence::index($answer['answer_post_id'], 'id');
$url_post = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]);
$url_post = post_slug($post['post_id'], $post['post_slug']);
Validator::Length($content = $_POST['comment'], 6, 2024, 'content', $url_post);

View file

@ -44,8 +44,7 @@ class EditCommentController extends Controller
$post = PostPresence::index($comment['comment_post_id'], 'id');
$slug = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]);
$redirect = $slug . '#comment_' . $comment['comment_id'];
$redirect = post_slug($post['post_id'], $post['post_slug']) . '#comment_' . $comment['comment_id'];
$content = $_POST['comment']; // для Markdown
Validator::length($content, 3, 5500, 'content', $redirect);

View file

@ -128,7 +128,7 @@ class AddPostController extends Controller
(new \App\Services\Audit())->create('post', $last_id, url('admin.audits'));
}
$url_content = url('post', ['id' => $last_id, 'slug' => $slug]);
$url_content = post_slug($last_id, $slug);
if ($type == 'page') {
$url_content = url('info.page', ['slug' => $slug]);
}

View file

@ -124,7 +124,7 @@ class EditPostController extends Controller
]
);
is_return(__('msg.change_saved'), 'success', '/post/' . $post['post_id']);
is_return(__('msg.change_saved'), 'success', url('post_id', ['id' => $post['post_id']]));
}
// Add fastes (blogs, topics) to the post

View file

@ -88,7 +88,7 @@ class PostController extends Controller
'type' => 'article',
'og' => true,
'imgurl' => $content_img,
'url' => url('post', ['id' => $content['post_id'], 'slug' => $content['post_slug']]),
'url' => post_slug($content['post_id'], $content['post_slug']),
];
// Sending Last-Modified and handling HTTP_IF_MODIFIED_SINCE
@ -169,14 +169,16 @@ class PostController extends Controller
// If the post slug is different from the data in the database
// Если slug поста отличается от данных в базе
if ($slug != $content['post_slug']) {
redirect(url('post', ['id' => $content['post_id'], 'slug' => $content['post_slug']]));
if (config('meta.slug_post') == true) {
if ($slug != $content['post_slug']) {
redirect(post_slug($content['post_id'], $content['post_slug']));
}
}
// Redirect when merging a post
// Редирект при слиянии поста
if ($content['post_merged_id'] > 0 && !UserData::checkAdmin()) {
redirect('/post/' . $content['post_merged_id']);
redirect(url('post_id', ['id' => $content['post_merged_id']]));
}
return $content;
@ -189,7 +191,7 @@ class PostController extends Controller
// Размещение своего поста у себя в профиле
public function postProfile()
{
$post = PostPresence::index($post_id = Request::getPostInt('post_id'), 'id');
$post = PostPresence::index($post_id = Request::getPostInt('post_id'), 'id');
// Access check
// Проверка доступа

View file

@ -34,7 +34,7 @@ class Audit extends Base
if (!in_array($content_type, ['post', 'answer', 'comment'])) return false;
$type_id = $content_type == 'answer' ? 'answer_' . $content_id : 'comment_' . $content_id;
$url = '/post/' . $post['post_id'] . '/' . $post['post_slug'] . '#' . $type_id;
$url = post_slug($post['post_id'], $post['post_slug']) . '#' . $type_id;
$this->create($content_type, $content_id, $url, 'report');

View file

@ -29,6 +29,15 @@ function url(string $key = null, array $params = [])
return hleb_get_by_name($key, $params);
}
function post_slug(int $id, string $slug = '')
{
if (config('meta.slug_post') == false) {
return hleb_get_by_name('post_id', ['id' => $id]);
}
return hleb_get_by_name('post', ['id' => $id, 'slug' => $slug]);
}
function config(string $key = null)
{
if ($key === null) {

View file

@ -39,4 +39,10 @@ return [
// For site directory
// Для каталог сайтов
'img_path_web' => '/assets/images/libarea-web.png',
// If false, then the URL of the posts will be: /post/id
// otherwise: /post/id/slug
// Если false, то URL постов будет: /post/id
// в противном случае: /post/id/slug
'slug_post' => true,
];

View file

@ -52,7 +52,7 @@ return [
'addition' => 'https://vk.com/',
'title' => 'vk',
'lang' => 'Vk',
'help' => 'https://vk.com/<b>NICK / id</b>',
'help' => 'https://vk.com/<b>NICK</b>',
'name' => 'vk'
],
];

View file

@ -65,7 +65,7 @@
<?php if (!empty($audit['post'])) : ?>
<?php if ($audit['post']['post_slug']) : ?>
<a class="block" href="<?= url('post', ['id' => $audit['post']['post_id'], 'slug' => $audit['post']['post_slug']]); ?>">
<a class="block" href="<?= post_slug($audit['post']['post_id'], $audit['post']['post_slug']); ?>">
<?= $audit['post']['post_title']; ?>
</a>
<?php endif; ?>

View file

@ -20,7 +20,7 @@
</a>
<span class="lowercase"><?= Html::langDate($answer['answer_date']); ?></span>
</div>
<a class="last-content_telo" href="<?= url('post', ['id' => $answer['post_id'], 'slug' => $answer['post_slug']]); ?>#answer_<?= $answer['answer_id']; ?>">
<a class="last-content_telo" href="<?= post_slug($answer['post_id'], $answer['post_slug']); ?>#answer_<?= $answer['answer_id']; ?>">
<?php if (mb_strlen($fragment = fragment($answer['answer_content'], 98), 'utf-8') < 5) : ?>
<span class="lowercase">+ <?= __('app.comment'); ?>...</span>
<?php else : ?>

View file

@ -7,7 +7,7 @@
<?php if ($related['post_content_img']) : ?>
<?= Img::image($related['post_content_img'], $related['value'], 'w60 mr20', 'post', 'cover'); ?>
<?php endif; ?>
<a href="<?= url('post', ['id' => $related['id'], 'slug' => $related['post_slug']]); ?>">
<a href="<?= post_slug($related['id'], $related['post_slug']); ?>">
<?= $related['value']; ?>
</a>
</li>

View file

@ -1,15 +1,13 @@
<?= insert('/_block/add-js-css');
$post = $data['post'];
$url = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?>
<?= insert('/_block/add-js-css'); ?>
<main>
<a href="/"><?= __('app.home'); ?></a> / <span class="gray-600"><?= __('app.edit_answer'); ?>:</span>
<a class="mb5 block" href="<?= $url; ?>"><?= $post['post_title']; ?></a>
<a class="mb5 block" href="<?= post_slug($data['post']['post_id'], $data['post']['post_slug']); ?>"><?= $data['post']['post_title']; ?></a>
<form class="max-w780" action="<?= url('content.change', ['type' => 'answer']); ?>" accept-charset="UTF-8" method="post">
<?= csrf_field() ?>
<?= insert('/_block/form/editor', ['height' => '300px', 'content' => $data['answer']['answer_content'], 'type' => 'answer', 'id' => $post['post_id']]); ?>
<?= insert('/_block/form/editor', ['height' => '300px', 'content' => $data['answer']['answer_content'], 'type' => 'answer', 'id' => $data['post']['post_id']]); ?>
<?php if (UserData::checkAdmin()) : ?>
<?= insert('/_block/form/select/user', ['user' => $data['user']]); ?>
@ -17,6 +15,6 @@ $url = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?>
<input type="hidden" name="answer_id" value="<?= $data['answer']['answer_id']; ?>">
<?= Html::sumbit(__('app.edit')); ?>
<a href="<?= $url; ?>#answer_<?= $data['answer']['answer_id']; ?>" class="text-sm inline ml15 gray"><?= __('app.cancel'); ?></a>
<a href="<?= post_slug($data['post']['post_id'], $data['post']['post_slug']); ?>#answer_<?= $data['answer']['answer_id']; ?>" class="text-sm inline ml15 gray"><?= __('app.cancel'); ?></a>
</form>
</main>

View file

@ -17,7 +17,7 @@ foreach ($comments as $comment) :
</div>
<div><?= Html::votes($comment, 'answer'); ?></div>
</div>
<a class="block cut-off" href="<?= url('post', ['id' => $comment['post_id'], 'slug' => $comment['post_slug']]); ?>#answer_<?= $comment['answer_id']; ?>">
<a class="block cut-off" href="<?= post_slug($comment['post_id'], $comment['post_slug']); ?>#answer_<?= $comment['answer_id']; ?>">
<?= $comment['post_title']; ?>
</a>
<div class="cut-post black"><?= markdown($comment['answer_content']); ?></div>
@ -36,7 +36,7 @@ foreach ($comments as $comment) :
</div>
<div><?= Html::votes($comment, 'comment'); ?></div>
</div>
<a class="block cut-off" href="<?= url('post', ['id' => $comment['post_id'], 'slug' => $comment['post_slug']]); ?>#comment_<?= $comment['comment_id']; ?>">
<a class="block cut-off" href="<?= post_slug($comment['post_id'], $comment['post_slug']); ?>#comment_<?= $comment['comment_id']; ?>">
<?= $comment['post_title']; ?>
</a>
<div class="cut-post black"><?= markdown($comment['comment_content']); ?></div>

View file

@ -5,7 +5,7 @@
</ul>
</div>
<?php if (!empty($data['comments'])) : ?>
<div class="mb15">
<div class="max-w780">
<?= insert('/content/comment/comment', ['comments' => $data['comments']]); ?>
</div>
<?= Html::pagination($data['pNum'], $data['pagesCount'], false, '/comments'); ?>

View file

@ -104,7 +104,7 @@
<ul>
<?php foreach ($data['related_posts'] as $rp) : ?>
<li class="mb15">
<a href="<?= url('post', ['id' => $rp['id'], 'slug' => $rp['post_slug']]); ?>"><?= $rp['value']; ?></a>
<a href="<?= post_slug($rp['id'], $rp['post_slug']); ?>"><?= $rp['value']; ?></a>
</li>
<?php endforeach; ?>
</ul>

View file

@ -11,7 +11,7 @@
<?php $n = 0;
foreach ($data['answers'] as $answer) :
$n++;
$post_url = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]);
$post_url = post_slug($post['post_id'], $post['post_slug']);
?>
<div class="block-answer mb15">

View file

@ -7,7 +7,7 @@
<?= insert('/_block/no-login-screensaver'); ?>
<?php endif; ?>
<?php $post_url = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?>
<?php $post_url = post_slug($post['post_id'], $post['post_slug']); ?>
<div class="box box-fon article_<?= $post['post_id']; ?>">
<div class="flex items-center gap-min text-sm mb5">
<a class="gray-600 flex gap-min items-center" href="<?= url('profile', ['login' => $post['login']]); ?>">
@ -55,7 +55,7 @@
</div>
<?php if ($arr['button']) : ?>
<a class="btn btn-outline-primary" href="<?= url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?>">
<a class="btn btn-outline-primary" href="<?= post_slug($post['post_id'], $post['post_slug']); ?>">
<?= __('app.read_more'); ?>
</a>
<?php endif; ?>

View file

@ -10,7 +10,7 @@ use Hleb\Constructor\Handlers\Request; ?>
<?= insert('/_block/no-login-screensaver'); ?>
<?php endif; ?>
<?php $post_url = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?>
<?php $post_url = post_slug($post['post_id'], $post['post_slug']); ?>
<div class="box box-fon article_<?= $post['post_id']; ?>">
<div class="flex justify-between">

View file

@ -215,7 +215,7 @@
<h4 class="uppercase-box"><?= __('app.recommended'); ?></h4>
<?php foreach ($data['recommend'] as $rec_post) : ?>
<div class="mb15 hidden flex gap text-sm">
<a class="gray" href="<?= url('post', ['id' => $rec_post['post_id'], 'slug' => $rec_post['post_slug']]); ?>">
<a class="gray" href="<?= post_slug($rec_post['post_id'], $rec_post['post_slug']); ?>">
<?php if ($rec_post['post_answers_count'] > 0) : ?>
<div class="p5-10 bg-green br-rd3 white center">
<?= $rec_post['post_answers_count'] ?>
@ -224,7 +224,7 @@
<div class="p5-10 bg-lightgray br-rd3 gray center">0</div>
<?php endif; ?>
</a>
<a class="black" href="<?= url('post', ['id' => $rec_post['post_id'], 'slug' => $rec_post['post_slug']]); ?>">
<a class="black" href="<?= post_slug($rec_post['post_id'], $rec_post['post_slug']); ?>">
<?= $rec_post['post_title']; ?>
</a>
</div>

View file

@ -11,9 +11,9 @@
<description>
<![CDATA[{{ <?= $post['post_content']; ?> }}]]>
</description>
<link><?= $data['url']; ?><?= url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?></link>
<link><?= $data['url']; ?><?= post_slug($post['post_id'], $post['post_slug']); ?></link>
<pubDate><?= date(DATE_RFC822, strtotime($post['post_date'])); ?></pubDate>
<guid><?= $data['url']; ?><?= url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?></guid>
<guid><?= $data['url']; ?><?= post_slug($post['post_id'], $post['post_slug']); ?></guid>
</item>
<?php endforeach; ?>
</channel>

View file

@ -16,7 +16,7 @@
<?php endforeach; ?>
<?php foreach ($data['posts'] as $post) : ?>
<url>
<loc><?= $data['url']; ?><?= url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?></loc>
<loc><?= $data['url']; ?><?= post_slug($post['post_id'], $post['post_slug']); ?></loc>
<priority>0.5</priority>
<changefreq>daily</changefreq>
</url>

View file

@ -7,7 +7,7 @@
<language>ru</language>
<?php foreach ($data['posts'] as $post) : ?>
<item turbo="true">
<link><?= $data['url']; ?><?= url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?></link>
<link><?= $data['url']; ?><?= post_slug($post['post_id'], $post['post_slug']); ?></link>
<pubDate><?= $post['post_date']; ?></pubDate>
<turbo:content>
<![CDATA[

View file

@ -28,7 +28,7 @@ $sw = $sw ?? '?';
if ($type == 'website') {
$url_content = $result['item_url'];
} elseif ($type == 'answer') {
$url_content = '/post/' . $result['post_id'] . '/' . $result['post_slug'] . '#answer_' . $result['answer_id'];
$url_content = post_slug($result['post_id'], $result['post_slug']) . '#answer_' . $result['answer_id'];
} else {
$url_content = '/post/' . $result['post_id'];
}

View file

@ -7,7 +7,7 @@
<?php if (!empty($data['drafts'])) : ?>
<?php foreach ($data['drafts'] as $draft) : ?>
<div class="box bg-lightgray">
<a href="<?= url('post', ['id' => $draft['post_id'], 'slug' => $draft['post_slug']]); ?>">
<a href="<?= post_slug($draft['post_id'], $draft['post_slug']); ?>">
<h3 class="text-2xl m0"><?= $draft['post_title']; ?></h3>
</a>
<div class="mr5 text-sm gray-600 lowercase">

View file

@ -38,7 +38,7 @@
<?php endif; ?>
<?php if ($fav['action_type'] == 'post') : ?>
<a class="font-normal" href="<?= url('post', ['id' => $fav['post_id'], 'slug' => $fav['post_slug']]); ?>">
<a class="font-normal" href="<?= post_slug($fav['post_id'], $fav['post_slug']); ?>">
<?= $fav['post_title']; ?>
</a>
<?php elseif ($fav['action_type'] == 'website') : ?>
@ -56,7 +56,7 @@
</a>
</span>
<?php else : ?>
<a href="<?= url('post', ['id' => $fav['post']['post_id'], 'slug' => $fav['post']['post_slug']]); ?>#answer_<?= $fav['answer_id']; ?>">
<a href="<?= post_slug($fav['post']['post_id'], $fav['post']['post_slug']); ?>#answer_<?= $fav['answer_id']; ?>">
<?= $fav['post']['post_title']; ?>
</a>
<?php endif; ?>

View file

@ -14,7 +14,7 @@
<?php endif; ?>
</h4>
<div class="mt5">
<a class="text-2xl" href="<?= url('post', ['id' => $data['my_post']['post_id'], 'slug' => $data['my_post']['post_slug']]); ?>">
<a class="text-2xl" href="<?= post_slug($data['my_post']['post_id'], $data['my_post']['post_slug']); ?>">
<?= $data['my_post']['post_title']; ?>
</a>
<div class="text-sm mt5 gray-600 lowercase">

View file

@ -29,7 +29,7 @@ $post = $data['post'] ?? false;
<?php if ($post) : ?>
<div class="d-header-post none">
<span class="v-line mb-none"></span>
<a class="mb-none" href="<?= url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]) ?>">
<a class="mb-none" href="<?= post_slug($post['post_id'], $post['post_slug']) ?>">
<?= $data['post']['post_title'] ?>
</a>
</div>

View file

@ -6,7 +6,7 @@
<?php if (!UserData::checkActiveUser() && $n == 6) : ?>
<?= insert('/_block/no-login-screensaver'); ?>
<?php endif; ?>
<?php $post_url = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?>
<?php $post_url = post_slug($post['post_id'], $post['post_slug']); ?>
<li class="list-post br-top-zebra article_<?= $post['post_id']; ?>">
<div class="w40 no-flex center">
<?= Html::votes($post, 'post', 'arrow-up'); ?>

View file

@ -10,7 +10,7 @@ use Hleb\Constructor\Handlers\Request; ?>
<?php if (UserData::getUserId() == 0 && $n == 6) : ?>
<?= insert('/_block/no-login-screensaver'); ?>
<?php endif; ?>
<?php $post_url = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]); ?>
<?php $post_url = post_slug($post['post_id'], $post['post_slug']); ?>
<li class="list-post br-top-zebra article_<?= $post['post_id']; ?>">
<div class="flex mr15">
<div class="box-answer block bg-lightgray gray mt5 br-rd3 mb-none lowercase mr15">

View file

@ -86,7 +86,7 @@ Route::get('/search')->controller('SearchController', ['post'])->name('search');
Route::get('/search/go')->controller('SearchController@go', ['post'])->name('search.go');
// Other pages without authorization
Route::get('/post/{id}')->controller('Post\PostController', ['post'])->where(['id' => '[0-9]+']);
Route::get('/post/{id}')->controller('Post\PostController', ['post'])->where(['id' => '[0-9]+'])->name('post_id');;
Route::get('/post/{id}/{slug}')->controller('Post\PostController', ['post'])->where(['id' => '[0-9]+', 'slug' => '[A-Za-z0-9-_]+'])->name('post');
// Страницы info