admin.aprsto/htdocs/includes/repositories/announcementrepository.class.php
Steve White ae9ece5266 *** Initial Commit of Files for APRS.TO Backend Administration Panel ***
This code is non-functional at this point.
2025-02-02 15:53:34 -05:00

111 lines
2.5 KiB
PHP

<?php
class AnnouncementRepository extends ModelRepository
{
private static $_singletonInstance = null;
public function __construct()
{
parent::__construct('Announcement');
}
/**
* Returns an initiated AnnouncementRepository
*
* @return AnnouncementRepository
*/
public static function getInstance()
{
if (self::$_singletonInstance === null)
{
self::$_singletonInstance = new AnnouncementRepository();
}
return self::$_singletonInstance;
}
/**
* Get Announcement object by id
*
* @param int $id
* @return Announcement
*/
public function getObjectById($id)
{
if (!isInt($id))
{
return new Announcement(0);
}
static $cache = array();
$key = $id;
if (!isset($cache[$key]))
{
$cache[$key] = $this->getObjectFromSql('select * from announcements where id = ? LIMIT 1', [$id]);
}
return $cache[$key];
}
/**
* Get the most recent x Announcements
*
* @param int $limit (default 1)
* @return Announcement
*/
public function getObjectListByLatestDate($limit = 1)
{
if (!isInt($limit))
{
return [new Announcement(0)];
}
$announcements = $this->getObjectListFromSql('select * from announcements where start_date < NOW() and (end_date is null OR end_date > NOW()) order by id desc limit ' . $limit, []);
return count($announcements) > 0 ? $announcements : [new Announcement(0)];
}
/**
* Returns an object based on the provided sql
*
* @param string $sql
* @param array $arg
* @return StandardItem
*/
public function getObjectFromSql($sql, $arg)
{
$pdo = PDOMysqlConnection::getInstance();
$stmt = $pdo->prepareAndExec($sql, $arg);
if ($record = $stmt->fetch(PDO::FETCH_ASSOC))
{
return $this->_getObjectFromRecord($record);
}
// No object found, return empty object
return new Announcement(0);
}
/**
* Returns an array of object based on the provided sql
*
* @param string $sql
* @param array $arg
* @return array
*/
public function getObjectListFromSql($sql, $arg, $index = null)
{
$pdo = PDOMysqlConnection::getInstance();
$stmt = $pdo->prepareAndExec($sql, $arg);
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (is_array($records) && !empty($records)) {
return $this->_getObjectListFromRecords($records, $index);
}
// No object found, return empty array
return [];
}
}