2023-08-28 01:17:34 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu\Forum;
|
|
|
|
|
2024-08-04 21:37:12 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2023-08-28 01:17:34 +00:00
|
|
|
use Index\Colour\Colour;
|
2024-10-05 02:40:29 +00:00
|
|
|
use Index\Db\DbResult;
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
class ForumCategoryInfo {
|
|
|
|
// should the types just be replaced with flags indicating what's allowed?
|
|
|
|
public const TYPE_DISCUSSION = 0;
|
|
|
|
public const TYPE_LISTING = 1;
|
|
|
|
public const TYPE_LINK = 2;
|
|
|
|
|
|
|
|
public const TYPE_ALIASES = [
|
|
|
|
'discussion' => self::TYPE_DISCUSSION,
|
|
|
|
'listing' => self::TYPE_LISTING,
|
|
|
|
'link' => self::TYPE_LINK,
|
|
|
|
];
|
|
|
|
|
|
|
|
public const MAY_HAVE_CHILDREN = [
|
|
|
|
self::TYPE_DISCUSSION,
|
|
|
|
self::TYPE_LISTING,
|
|
|
|
];
|
|
|
|
|
|
|
|
public const MAY_HAVE_TOPICS = [
|
|
|
|
self::TYPE_DISCUSSION,
|
|
|
|
];
|
|
|
|
|
2024-02-07 00:04:45 +00:00
|
|
|
public function __construct(
|
2024-11-30 04:09:29 +00:00
|
|
|
public private(set) string $id,
|
|
|
|
public private(set) int $order,
|
|
|
|
public private(set) ?string $parentId,
|
|
|
|
public private(set) string $name,
|
|
|
|
public private(set) int $type,
|
|
|
|
public private(set) ?string $description,
|
|
|
|
public private(set) ?string $icon,
|
|
|
|
public private(set) ?int $colourRaw,
|
|
|
|
public private(set) ?string $linkTarget,
|
|
|
|
public private(set) ?int $linkClicks,
|
|
|
|
public private(set) int $createdTime,
|
|
|
|
public private(set) bool $archived,
|
|
|
|
public private(set) bool $hidden,
|
|
|
|
public private(set) int $topicsCount,
|
|
|
|
public private(set) int $postsCount,
|
2024-02-07 00:04:45 +00:00
|
|
|
) {}
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2024-10-05 02:40:29 +00:00
|
|
|
public static function fromResult(DbResult $result): ForumCategoryInfo {
|
2024-02-07 00:04:45 +00:00
|
|
|
return new ForumCategoryInfo(
|
|
|
|
id: $result->getString(0),
|
|
|
|
order: $result->getInteger(1),
|
|
|
|
parentId: $result->getStringOrNull(2),
|
|
|
|
name: $result->getString(3),
|
|
|
|
type: $result->getInteger(4),
|
2024-11-30 04:09:29 +00:00
|
|
|
description: $result->getStringOrNull(5),
|
2024-02-07 00:04:45 +00:00
|
|
|
icon: $result->getStringOrNull(6),
|
2024-11-30 04:09:29 +00:00
|
|
|
colourRaw: $result->getIntegerOrNull(7),
|
|
|
|
linkTarget: $result->getStringOrNull(8),
|
|
|
|
linkClicks: $result->getIntegerOrNull(9),
|
|
|
|
createdTime: $result->getInteger(10),
|
2024-02-07 00:04:45 +00:00
|
|
|
archived: $result->getBoolean(11),
|
|
|
|
hidden: $result->getBoolean(12),
|
|
|
|
topicsCount: $result->getInteger(13),
|
|
|
|
postsCount: $result->getInteger(14),
|
|
|
|
);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $hasParent {
|
|
|
|
get => $this->parentId !== null && $this->parentId !== '0';
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isDirectChildOf(ForumCategoryInfo|string $parentInfo): bool {
|
|
|
|
if($parentInfo instanceof ForumCategoryInfo)
|
2024-11-30 04:09:29 +00:00
|
|
|
$parentInfo = $parentInfo->id;
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
return $this->hasParent && $this->parentId === $parentInfo;
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isDiscussion {
|
|
|
|
get => $this->type === self::TYPE_DISCUSSION;
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isListing {
|
|
|
|
get => $this->type === self::TYPE_LISTING;
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $isLink {
|
|
|
|
get => $this->type === self::TYPE_LINK;
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $mayHaveChildren {
|
|
|
|
get => in_array($this->type, self::MAY_HAVE_CHILDREN);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $mayHaveTopics {
|
|
|
|
get => in_array($this->type, self::MAY_HAVE_TOPICS);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public string $iconForDisplay {
|
|
|
|
get {
|
|
|
|
if(!empty($this->icon))
|
|
|
|
return $this->icon;
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
if($this->archived)
|
|
|
|
return 'fas fa-archive fa-fw';
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
return match($this->type) {
|
|
|
|
self::TYPE_LISTING => 'fas fa-folder fa-fw',
|
|
|
|
self::TYPE_LINK => 'fas fa-link fa-fw',
|
|
|
|
default => 'fas fa-comments fa-fw',
|
|
|
|
};
|
|
|
|
}
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public bool $hasColour {
|
|
|
|
get => $this->colourRaw !== null && ($this->colourRaw & 0x40000000) === 0;
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public Colour $colour {
|
|
|
|
get => $this->colourRaw === null ? Colour::none() : Colour::fromMisuzu($this->colourRaw);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
public CarbonImmutable $createdAt {
|
|
|
|
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
}
|