Some restructuring.
This commit is contained in:
parent
f9b73c6067
commit
2050ba294b
24 changed files with 611 additions and 411 deletions
src/Projects
89
src/Projects/Projects.php
Normal file
89
src/Projects/Projects.php
Normal file
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
namespace Makai\Projects;
|
||||
|
||||
use Index\Colour\Colour;
|
||||
use Index\Colour\ColourRGB;
|
||||
use Index\Data\DbStatementCache;
|
||||
use Index\Data\IDbConnection;
|
||||
|
||||
class Projects {
|
||||
private IDbConnection $dbConn;
|
||||
private DbStatementCache $cache;
|
||||
|
||||
public function __construct(IDbConnection $dbConn) {
|
||||
$this->dbConn = $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));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue