132 lines
3.8 KiB
PHP
132 lines
3.8 KiB
PHP
|
<?php
|
||
|
function category_info(int $category): array {
|
||
|
global $pdo;
|
||
|
static $cats = [];
|
||
|
|
||
|
if($category < 1)
|
||
|
return [];
|
||
|
|
||
|
if(!empty($cats[$category]))
|
||
|
return $cats[$category];
|
||
|
|
||
|
$getCat = $pdo->prepare('SELECT * FROM `fmf_categories` WHERE `cat_id` = :id');
|
||
|
$getCat->bindValue('id', $category);
|
||
|
$categoryInfo = $getCat->execute() ? $getCat->fetch(PDO::FETCH_ASSOC) : false;
|
||
|
|
||
|
return $cats[$category] = ($categoryInfo ? $categoryInfo : []);
|
||
|
}
|
||
|
|
||
|
function category_children(int $parent = 0, int $recurse = 3): array {
|
||
|
global $pdo;
|
||
|
|
||
|
if($parent < 0)
|
||
|
return [];
|
||
|
|
||
|
$getCats = $pdo->prepare('SELECT * FROM `fmf_categories` WHERE `cat_parent` = :parent ORDER BY `cat_order`');
|
||
|
$getCats->bindValue('parent', $parent);
|
||
|
$categories = $getCats->execute() ? $getCats->fetchAll(PDO::FETCH_ASSOC) : false;
|
||
|
$categories = $categories ? $categories : [];
|
||
|
|
||
|
if($recurse > 0) {
|
||
|
$recurse--;
|
||
|
|
||
|
for($i = 0; $i < count($categories); $i++)
|
||
|
$categories[$i]['children'] = category_children($categories[$i]['cat_id'], $recurse);
|
||
|
}
|
||
|
|
||
|
return $categories;
|
||
|
}
|
||
|
|
||
|
function root_category(): array {
|
||
|
$categories = category_children();
|
||
|
$forums = [];
|
||
|
|
||
|
for($i = 0; $i < count($categories); $i++) {
|
||
|
if($categories[$i]['cat_type'] != 1) {
|
||
|
$forums[] = $categories[$i];
|
||
|
unset($categories[$i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(count($forums) > 0)
|
||
|
$categories[] = [
|
||
|
'cat_id' => 0,
|
||
|
'cat_type' => 1,
|
||
|
'cat_name' => 'Categories',
|
||
|
'children' => $forums,
|
||
|
];
|
||
|
|
||
|
return $categories;
|
||
|
}
|
||
|
|
||
|
function category_bump(int $category, ?int $post = null, bool $topics = true, bool $posts = true): void {
|
||
|
global $pdo;
|
||
|
|
||
|
if($category < 1)
|
||
|
return;
|
||
|
|
||
|
$getParentId = $pdo->prepare('SELECT `cat_parent` FROM `fmf_categories` WHERE `cat_id` = :id');
|
||
|
$getParentId->bindValue('id', $category);
|
||
|
$parentId = $getParentId->execute() ? (int)$getParentId->fetchColumn() : 0;
|
||
|
|
||
|
if($parentId > 0)
|
||
|
category_bump($parentId, $post, $topics, $posts);
|
||
|
|
||
|
$bump = $pdo->prepare('UPDATE `fmf_categories` SET `cat_count_topics` = IF(:topics, `cat_count_topics` + 1, `cat_count_topics`), `cat_count_posts` = IF(:posts, `cat_count_posts` + 1, `cat_count_posts`), `cat_last_post_id` = COALESCE(:post, `cat_last_post_id`) WHERE `cat_id` = :id');
|
||
|
$bump->bindValue('topics', $topics ? 1 : 0);
|
||
|
$bump->bindValue('posts', $posts ? 1 : 0);
|
||
|
$bump->bindValue('post', $post);
|
||
|
$bump->bindValue('id', $category);
|
||
|
$bump->execute();
|
||
|
}
|
||
|
|
||
|
function category_breadcrumbs(int $category, bool $excludeSelf): array {
|
||
|
global $pdo;
|
||
|
|
||
|
$breadcrumbs = [];
|
||
|
$getBreadcrumb = $pdo->prepare('
|
||
|
SELECT `cat_id`, `cat_name`, `cat_parent`
|
||
|
FROM `fmf_categories`
|
||
|
WHERE `cat_id` = :category
|
||
|
');
|
||
|
|
||
|
while($category > 0) {
|
||
|
$getBreadcrumb->bindValue('category', $category);
|
||
|
$breadcrumb = $getBreadcrumb->execute() ? $getBreadcrumb->fetch(PDO::FETCH_ASSOC) : [];
|
||
|
|
||
|
if(empty($breadcrumb)) {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
$breadcrumbs[] = $breadcrumb;
|
||
|
$category = $breadcrumb['cat_parent'];
|
||
|
}
|
||
|
|
||
|
if($excludeSelf)
|
||
|
$breadcrumbs = array_slice($breadcrumbs, 1);
|
||
|
|
||
|
return array_reverse($breadcrumbs);
|
||
|
}
|
||
|
|
||
|
function category_child_ids(int $category): array {
|
||
|
global $pdo;
|
||
|
|
||
|
if($category < 1)
|
||
|
return [];
|
||
|
|
||
|
static $cached = [];
|
||
|
|
||
|
if(isset($cached[$category]))
|
||
|
return $cached[$category];
|
||
|
|
||
|
$getChildren = $pdo->prepare('
|
||
|
SELECT `cat_id`
|
||
|
FROM `fmf_categories`
|
||
|
WHERE `cat_parent` = :category
|
||
|
');
|
||
|
$getChildren->bindValue('category', $category);
|
||
|
$children = $getChildren->fetchAll(PDO::FETCH_ASSOC);
|
||
|
|
||
|
return $cached[$category] = array_column($children, 'cat_id');
|
||
|
}
|