News but everything is a function.

This commit is contained in:
flash 2018-10-10 11:21:57 +02:00
parent 76cc71b0b8
commit 6d39c6e3c9
6 changed files with 263 additions and 150 deletions

View file

@ -10,25 +10,7 @@ if (config_get_default(false, 'Site', 'embed_linked_data')) {
]);
}
$news = db_query('
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`,
u.`user_id`, u.`username`,
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`,
(
SELECT COUNT(`comment_id`)
FROM `msz_comments_posts`
WHERE `category_id` = `comment_section_id`
) as `post_comments`
FROM `msz_news_posts` as p
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE p.`is_featured` = true
ORDER BY p.`created_at` DESC
LIMIT 5
')->fetchAll(PDO::FETCH_ASSOC);
$news = news_posts_get(0, 5, null, true);
$statistics = cache_get('index:stats:v1', function () {
return [

29
public/manage/news.php Normal file
View file

@ -0,0 +1,29 @@
<?php
require_once '../../misuzu.php';
$newsPerms = perms_get_user(MSZ_PERMS_NEWS, user_session_current('user_id', 0));
switch ($_GET['v'] ?? null) {
case 'posts':
if (!perms_check($newsPerms, MSZ_PERM_NEWS_MANAGE_POSTS)) {
echo render_error(403);
break;
}
echo 'posts';
break;
case 'categories':
if (!perms_check($newsPerms, MSZ_PERM_NEWS_MANAGE_CATEGORIES)) {
echo render_error(403);
break;
}
$catTake = 15;
$catOffset = (int)($_GET['o'] ?? 0);
$cats = news_categories_get($catOffset, $catTake);
echo 'cats';
var_dump($cats);
break;
}

View file

@ -12,25 +12,9 @@ tpl_vars([
]);
if ($postId !== null) {
$getPost = db_prepare('
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, p.`comment_section_id`,
c.`category_id`, c.`category_name`,
u.`user_id`, u.`username`,
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE `post_id` = :post_id
');
$getPost->bindValue(':post_id', $postId, PDO::PARAM_INT);
$post = $getPost->execute() ? $getPost->fetch() : false;
$post = news_post_get($postId);
if ($post === false) {
if (!$post) {
echo render_error(404);
return;
}
@ -40,14 +24,10 @@ if ($postId !== null) {
if ($commentsInfo) {
$post['comment_section_id'] = $commentsInfo['category_id'];
db_prepare('
UPDATE `msz_news_posts`
SET `comment_section_id` = :comment_section_id
WHERE `post_id` = :post_id
')->execute([
'comment_section_id' => $post['comment_section_id'],
'post_id' => $post['post_id'],
]);
news_post_comments_set(
$post['post_id'],
$post['comment_section_id'] = $commentsInfo['category_id']
);
}
} else {
$commentsInfo = comments_category_info($post['comment_section_id']);
@ -63,87 +43,27 @@ if ($postId !== null) {
}
if ($categoryId !== null) {
$getCategory = db_prepare('
SELECT
c.`category_id`, c.`category_name`, c.`category_description`,
COUNT(p.`post_id`) AS `posts_count`
FROM `msz_news_categories` as c
LEFT JOIN `msz_news_posts` as p
ON c.`category_id` = p.`category_id`
WHERE c.`category_id` = :category_id
GROUP BY c.`category_id`
');
$getCategory->bindValue(':category_id', $categoryId, PDO::PARAM_INT);
$category = $getCategory->execute() ? $getCategory->fetch() : false;
$category = news_categories_single($categoryId, true);
if ($category === false || $postsOffset < 0 || $postsOffset >= $category['posts_count']) {
if (!$category || $postsOffset < 0 || $postsOffset >= $category['posts_count']) {
echo render_error(404);
return;
}
$getPosts = db_prepare('
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`,
c.`category_id`, c.`category_name`,
u.`user_id`, u.`username`,
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`,
(
SELECT COUNT(`comment_id`)
FROM `msz_comments_posts`
WHERE `category_id` = `comment_section_id`
) as `post_comments`
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE p.`category_id` = :category_id
ORDER BY `created_at` DESC
LIMIT :offset, :take
');
$getPosts->bindValue('offset', $postsOffset);
$getPosts->bindValue('take', $postsTake);
$getPosts->bindValue('category_id', $category['category_id'], PDO::PARAM_INT);
$posts = $getPosts->execute() ? $getPosts->fetchAll() : false;
$posts = news_posts_get(
$postsOffset,
$postsTake,
$category['category_id']
);
$getFeatured = db_prepare('
SELECT `post_id`, `post_title`
FROM `msz_news_posts`
WHERE `category_id` = :category_id
AND `is_featured` = true
ORDER BY `created_at` DESC
LIMIT 10
');
$getFeatured->bindValue('category_id', $category['category_id'], PDO::PARAM_INT);
$featured = $getFeatured->execute() ? $getFeatured->fetchAll() : [];
$featured = news_posts_get(0, 10, $category['category_id'], true);
echo tpl_render('news.category', compact('category', 'posts', 'featured'));
return;
}
$getCategories = db_prepare('
SELECT
c.`category_id`, c.`category_name`,
COUNT(p.`post_id`) AS count
FROM `msz_news_categories` as c
LEFT JOIN `msz_news_posts` as p
ON c.`category_id` = p.`category_id`
WHERE `is_hidden` = false
GROUP BY c.`category_id`
HAVING count > 0
');
$categories = $getCategories->execute() ? $getCategories->fetchAll() : [];
$postsCount = (int)db_query('
SELECT COUNT(p.`post_id`) as `posts_count`
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
WHERE p.`is_featured` = true
AND c.`is_hidden` = false
')->fetchColumn();
$categories = news_categories_get(0, 0, true);
$postsCount = news_posts_count(null, true);
tpl_var('posts_count', $postsCount);
@ -152,32 +72,12 @@ if ($postsOffset < 0 || $postsOffset >= $postsCount) {
return;
}
$getPosts = db_prepare('
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`,
c.`category_id`, c.`category_name`,
u.`user_id`, u.`username`,
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`,
(
SELECT COUNT(`comment_id`)
FROM `msz_comments_posts`
WHERE `category_id` = `comment_section_id`
) as `post_comments`
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE p.`is_featured` = true
AND c.`is_hidden` = false
ORDER BY p.`created_at` DESC
LIMIT :offset, :take
');
$getPosts->bindValue('offset', $postsOffset);
$getPosts->bindValue('take', $postsTake);
$posts = $getPosts->execute() ? $getPosts->fetchAll() : [];
$posts = news_posts_get(
$postsOffset,
$postsTake,
null,
true
);
if (!$posts) {
echo render_error(404);

View file

@ -24,14 +24,7 @@ function geoip_cache(string $section, string $ipAddress, callable $value)
function geoip_country(string $ipAddress)
{
return geoip_cache('country', $ipAddress, function () {
return geoip_cache('country', $ipAddress, function () use ($ipAddress) {
return $GLOBALS[MSZ_GEOIP_INSTANCE_STORE]->country($ipAddress);
});
}
function geoip_anonymous(string $ipAddress)
{
return geoip_cache('anonymous', $ipAddress, function () {
return $GLOBALS[MSZ_GEOIP_INSTANCE_STORE]->anonymousIp($ipAddress);
});
}

View file

@ -1,3 +1,212 @@
<?php
define('MSZ_PERM_NEWS_MANAGE_POSTS', 1);
define('MSZ_PERM_NEWS_MANAGE_CATEGORIES', 1 << 1);
function news_categories_get(
int $offset,
int $take,
bool $includePostCount = false,
bool $featuredOnly = false,
bool $exposeScheduled = false,
bool $excludeDeleted = true
): array {
$getAll = $offset < 0 || $take < 1;
if ($includePostCount) {
$query = sprintf(
'
SELECT
`category_id`, `category_name`, `is_hidden`,
`created_at`, `updated_at`,
(
SELECT COUNT(`post_id`)
FROM `msz_news_posts`
WHERE %2$s %3$s %4$s
) as `posts_count`
FROM `msz_news_categories`
GROUP BY `category_id`
ORDER BY `category_id` DESC
%1$s
',
$getAll ? '' : 'LIMIT :offset, :take',
$featuredOnly ? '`is_featured`' : '1',
$exposeScheduled ? '' : 'AND `scheduled_for` < NOW()',
$excludeDeleted ? 'AND `deleted_at` IS NULL' : ''
);
} else {
$query = sprintf('
SELECT
`category_id`, `category_name`, `is_hidden`,
`created_at`, `updated_at`
FROM `msz_news_categories`
ORDER BY `category_id` DESC
%s
', $getAll ? '' : 'LIMIT :offset, :take');
}
$getCats = db_prepare($query);
if (!$getAll) {
$getCats->bindValue('offset', $offset);
$getCats->bindValue('take', $take);
}
$cats = $getCats->execute() ? $getCats->fetchAll(PDO::FETCH_ASSOC) : false;
return $cats ? $cats : [];
}
function news_categories_single(
int $category,
bool $includePostCount = false,
bool $featuredOnly = false,
bool $exposeScheduled = false,
bool $excludeDeleted = true
): array {
if ($includePostCount) {
$query = sprintf(
'
SELECT
c.`category_id`, c.`category_name`, c.`category_description`,
COUNT(p.`post_id`) AS `posts_count`
FROM `msz_news_categories` as c
LEFT JOIN `msz_news_posts` as p
ON c.`category_id` = p.`category_id`
WHERE c.`category_id` = :category %1$s %2$s %3$s
GROUP BY c.`category_id`
',
$featuredOnly ? 'AND p.`is_featured` = 1' : '',
$exposeScheduled ? '' : 'AND p.`scheduled_for` < NOW()',
$excludeDeleted ? 'AND p.`deleted_at` IS NULL' : ''
);
} else {
$query = '
SELECT
c.`category_id`, c.`category_name`, c.`category_description`,
FROM `msz_news_categories` as c
WHERE c.`category_id` = :category
GROUP BY c.`category_id`
';
}
$getCategory = db_prepare($query);
$getCategory->bindValue('category', $category);
$category = $getCategory->execute() ? $getCategory->fetch(PDO::FETCH_ASSOC) : false;
return $category ? $category : [];
}
function news_posts_count(
?int $category = null,
bool $featuredOnly = false,
bool $exposeScheduled = false,
bool $excludeDeleted = true
): int {
$hasCategory= $category !== null;
$countPosts = db_prepare(sprintf(
'
SELECT COUNT(`post_id`)
FROM `msz_news_posts`
WHERE %1$s %2$s %3$s %4$s
',
$hasCategory ? '`category_id` = :category' : '1',
$featuredOnly ? 'AND `is_featured` = 1' : '',
$exposeScheduled ? '' : 'AND `scheduled_for` < NOW()',
$excludeDeleted ? 'AND `deleted_at` IS NULL' : ''
));
if ($hasCategory) {
$countPosts->bindValue('category', $category);
}
return $countPosts->execute() ? (int)$countPosts->fetchColumn() : 0;
}
function news_posts_get(
int $offset,
int $take,
?int $category = null,
bool $featuredOnly = false,
bool $exposeScheduled = false,
bool $excludeDeleted = true
): array {
$getAll = $offset < 0 || $take < 1;
$hasCategory= $category !== null;
$getPosts = db_prepare(sprintf(
'
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`,
c.`category_id`, c.`category_name`,
u.`user_id`, u.`username`,
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`,
(
SELECT COUNT(`comment_id`)
FROM `msz_comments_posts`
WHERE `category_id` = `comment_section_id`
) as `post_comments`
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE %5$s %2$s %3$s %4$s
ORDER BY p.`created_at` DESC
%1$s
',
$getAll ? '' : 'LIMIT :offset, :take',
$featuredOnly ? 'AND p.`is_featured` = 1' : '',
$exposeScheduled ? '' : 'AND p.`scheduled_for` < NOW()',
$excludeDeleted ? 'AND p.`deleted_at` IS NULL' : '',
$hasCategory ? 'p.`category_id` = :category' : '1'
));
if ($hasCategory) {
$getPosts->bindValue('category', $category);
}
if (!$getAll) {
$getPosts->bindValue('take', $take);
$getPosts->bindValue('offset', $offset);
}
$posts = $getPosts->execute() ? $getPosts->fetchAll(PDO::FETCH_ASSOC) : false;
return $posts ? $posts : [];
}
function news_post_comments_set(int $postId, int $sectionId): void
{
db_prepare('
UPDATE `msz_news_posts`
SET `comment_section_id` = :comment_section_id
WHERE `post_id` = :post_id
')->execute([
'comment_section_id' => $sectionId,
'post_id' => $postId,
]);
}
function news_post_get(int $postId): array
{
$getPost = db_prepare('
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, p.`comment_section_id`,
c.`category_id`, c.`category_name`,
u.`user_id`, u.`username`,
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE `post_id` = :post_id
');
$getPost->bindValue(':post_id', $postId);
$post = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false;
return $post ? $post : [];
}

View file

@ -25,7 +25,7 @@
{{ category.category_name }}
</div>
<div class="news__list__value">
{{ category.count }} post{{ category.count == 1 ? '' : 's' }}
{{ category.posts_count }} post{{ category.posts_count == 1 ? '' : 's' }}
</div>
</a>
{% endfor %}