85 lines
3 KiB
PHP
85 lines
3 KiB
PHP
<?php
|
|
namespace Makai\Projects;
|
|
|
|
use Index\Colour\{Colour,ColourRgb};
|
|
use Index\Db\{DbConnection,DbStatementCache};
|
|
|
|
class Projects {
|
|
private DbStatementCache $cache;
|
|
|
|
public function __construct(DbConnection $dbConn) {
|
|
$this->cache = new DbStatementCache($dbConn);
|
|
}
|
|
|
|
public function getProjects(
|
|
bool $featuredOnly = false,
|
|
?bool $deleted = null,
|
|
?int $take = null,
|
|
bool $random = false
|
|
): array {
|
|
$hasDeleted = $deleted !== null;
|
|
$hasTake = $take !== null;
|
|
|
|
$args = 0;
|
|
$query = 'SELECT project_id, project_name, COALESCE(project_name_clean, REPLACE(LOWER(project_name), \' \', \'-\')), project_summary, project_description, project_order, project_type, project_featured, project_colour, project_homepage, project_repository, project_forum, UNIX_TIMESTAMP(project_created), UNIX_TIMESTAMP(project_deleted), UNIX_TIMESTAMP(project_archived) FROM fm_projects';
|
|
if($featuredOnly) {
|
|
++$args;
|
|
$query .= ' WHERE project_featured <> 0';
|
|
}
|
|
if($hasDeleted)
|
|
$query .= sprintf(' %s project_deleted %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $deleted ? 'IS NOT' : 'IS');
|
|
$query .= sprintf(' ORDER BY %s', $random ? 'RAND()' : 'project_order DESC');
|
|
if($hasTake)
|
|
$query .= ' LIMIT ?';
|
|
|
|
$stmt = $this->cache->get($query);
|
|
if($hasTake)
|
|
$stmt->addParameter(1, $take);
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->getResult();
|
|
$projects = [];
|
|
|
|
while($result->next())
|
|
$projects[] = new ProjectInfo($result);
|
|
|
|
return $projects;
|
|
}
|
|
|
|
public function getLanguages(
|
|
ProjectInfo|string|null $projectInfo = null
|
|
): array {
|
|
$hasProjectInfo = $projectInfo !== null;
|
|
|
|
$query = 'SELECT l.language_id, l.language_name, l.language_colour FROM fm_proglangs AS l';
|
|
if($hasProjectInfo)
|
|
$query .= ' LEFT JOIN fm_projects_proglangs AS pl ON pl.language_id = l.language_id WHERE pl.project_id = ? ORDER BY pl.priority ASC';
|
|
|
|
$stmt = $this->cache->get($query);
|
|
if($hasProjectInfo)
|
|
$stmt->addParameter(1, $projectInfo instanceof ProjectInfo ? $projectInfo->getId() : $projectInfo);
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->getResult();
|
|
$langs = [];
|
|
|
|
while($result->next())
|
|
$langs[] = new LanguageInfo($result);
|
|
|
|
return $langs;
|
|
}
|
|
|
|
public function getProjectColour(ProjectInfo|string $projectInfo): Colour {
|
|
$query = 'SELECT language_colour FROM fm_proglangs WHERE language_id = (SELECT language_id FROM fm_projects_proglangs WHERE project_id = ? ORDER BY priority ASC)';
|
|
|
|
$stmt = $this->cache->get($query);
|
|
$stmt->addParameter(1, $projectInfo instanceof ProjectInfo ? $projectInfo->getId() : $projectInfo);
|
|
$stmt->execute();
|
|
$result = $stmt->getResult();
|
|
|
|
if(!$result->next() || $result->isNull(0))
|
|
return Colour::none();
|
|
|
|
return ColourRgb::fromRawRGB($result->getInteger(0));
|
|
}
|
|
}
|