Add 'plugi-core/' from commit '6c478c54d048ab2fd5af4e8e79074153fb594c16'
git-subtree-dir: plugi-core git-subtree-mainline:d4c734cdbdgit-subtree-split:6c478c54d0
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Repositories;
|
||||
|
||||
use Plugi\Core\DB;
|
||||
|
||||
/**
|
||||
* Class BaseRepository
|
||||
*
|
||||
* The base repository passes CRUD calls to DB and initialises class instances for records returned from DB.
|
||||
*
|
||||
* @package Plugi\Repositories
|
||||
*/
|
||||
class BaseRepository
|
||||
{
|
||||
/**
|
||||
* @var DB $db
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* The database table of this repository.
|
||||
* Note: do not replace this value with user input.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* The class that represents each record of this repository's table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $class;
|
||||
|
||||
/**
|
||||
* Repository constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
global $phpb_db;
|
||||
$this->db = $phpb_db;
|
||||
|
||||
// apply the configured prefix to the table set in the superclass and remove non-alphanumeric characters
|
||||
$this->table = phpb_config('storage.database.prefix') . $this->removeNonAlphaNumeric($this->table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance using the given data.
|
||||
*
|
||||
* @param array $data
|
||||
* @return object|null
|
||||
*/
|
||||
protected function create(array $data)
|
||||
{
|
||||
$columns = array_keys($data);
|
||||
foreach ($columns as &$column) {
|
||||
$column = $this->removeNonAlphaNumeric($column);
|
||||
}
|
||||
$columns = implode(', ', $columns);
|
||||
$questionMarks = implode(', ', array_fill(0, sizeof($data), '?'));
|
||||
|
||||
$this->db->query(
|
||||
"INSERT INTO {$this->table} ({$columns}) VALUES ({$questionMarks})",
|
||||
array_values($data)
|
||||
);
|
||||
|
||||
$id = $this->db->lastInsertId();
|
||||
if ($id) {
|
||||
return $this->findWithId($id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the record with the given id with the given updated data.
|
||||
*
|
||||
* @param $instance
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
protected function update($instance, array $data)
|
||||
{
|
||||
$set = '';
|
||||
foreach ($data as $column => $value) {
|
||||
if ($set !== '') {
|
||||
$set .= ', ';
|
||||
}
|
||||
$set .= $this->removeNonAlphaNumeric($column) . '=?';
|
||||
}
|
||||
|
||||
$values = array_values($data);
|
||||
$values[] = $instance->id ?? $instance->getId();
|
||||
|
||||
return $this->db->query(
|
||||
"UPDATE {$this->table} SET {$set} WHERE id=?",
|
||||
$values
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the given instance from the database.
|
||||
*
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
return $this->db->query(
|
||||
"DELETE FROM {$this->table} WHERE id=?",
|
||||
[$id]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all instances from the database that satisfy the given condition.
|
||||
*
|
||||
* @param string $column
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function destroyWhere(string $column, $value)
|
||||
{
|
||||
$column = $this->removeNonAlphaNumeric($column);
|
||||
return $this->db->query(
|
||||
"DELETE FROM {$this->table} WHERE {$column}=?",
|
||||
[$value]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all instances from the database.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroyAll()
|
||||
{
|
||||
return $this->db->query(
|
||||
"DELETE FROM {$this->table}"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of all pages.
|
||||
*
|
||||
* @param array|string $columns
|
||||
* @return array
|
||||
*/
|
||||
public function getAll($columns = '*')
|
||||
{
|
||||
if (is_array($columns)) {
|
||||
foreach ($columns as &$column) {
|
||||
$column = $this->removeNonAlphaNumeric($column);
|
||||
}
|
||||
}
|
||||
return $this->createInstances($this->db->all($this->table, $columns));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the instance with the given id, or null.
|
||||
*
|
||||
* @param string $id
|
||||
* @return object|null
|
||||
*/
|
||||
public function findWithId($id)
|
||||
{
|
||||
return $this->createInstance($this->db->findWithId($this->table, $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the instances for which the given condition holds.
|
||||
*
|
||||
* @param string $column do NOT pass user input here
|
||||
* @param string $value
|
||||
* @return array
|
||||
*/
|
||||
public function findWhere($column, $value)
|
||||
{
|
||||
$column = $this->removeNonAlphaNumeric($column);
|
||||
return $this->createInstances($this->db->select(
|
||||
"SELECT * FROM {$this->table} WHERE {$column} = ?",
|
||||
[$value]
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove any non-alphanumeric characters.
|
||||
*
|
||||
* @param string $string
|
||||
* @return string|null
|
||||
*/
|
||||
protected function removeNonAlphaNumeric(string $string)
|
||||
{
|
||||
return preg_replace('/\W*/', '', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance using the first record.
|
||||
*
|
||||
* @param array $records
|
||||
* @return object|null
|
||||
*/
|
||||
protected function createInstance(array $records)
|
||||
{
|
||||
$instances = $this->createInstances($records);
|
||||
if (empty($instances)) {
|
||||
return null;
|
||||
}
|
||||
return $instances[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* For each record create an instance.
|
||||
*
|
||||
* @param array $records
|
||||
* @return array
|
||||
*/
|
||||
protected function createInstances(array $records)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
if (empty($this->class)) {
|
||||
return $records;
|
||||
}
|
||||
|
||||
foreach ($records as $record) {
|
||||
$instance = new $this->class;
|
||||
if (method_exists($instance, 'setData')) {
|
||||
$data = [];
|
||||
foreach($record as $k => $v) {
|
||||
$data[$k] = $v;
|
||||
}
|
||||
$instance->setData($data);
|
||||
} else {
|
||||
foreach($record as $k => $v) {
|
||||
$instance->$k = $v;
|
||||
}
|
||||
}
|
||||
$result[] = $instance;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Repositories;
|
||||
|
||||
use Plugi\Contracts\MenuRepositoryContract;
|
||||
|
||||
class MenuRepository extends BaseRepository implements MenuRepositoryContract
|
||||
{
|
||||
/**
|
||||
* The pages database table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'menus';
|
||||
|
||||
/**
|
||||
* Replace all website menus by the given data.
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool|object|null
|
||||
*/
|
||||
public function updateMenus(array $data)
|
||||
{
|
||||
$this->destroyAll();
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$this->create([
|
||||
'name' => $key,
|
||||
'pages' => $value,
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get one hydrated menu with the given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return array|null
|
||||
*/
|
||||
public function getHydratedMenu(string $name)
|
||||
{
|
||||
$pageRepository = new PageRepository;
|
||||
$pages = $pageRepository->getAll();
|
||||
$menu = $this->findWhere('name', $name)[0];
|
||||
$menuPages = explode(',',$menu['pages']);
|
||||
$hyMenuPages = [];
|
||||
foreach ($pages as $page) {
|
||||
$key = array_search($page->getId(), $menuPages);
|
||||
if($key === false) continue;
|
||||
$hyMenuPages[$key] = [
|
||||
'id' => $page->getId(),
|
||||
'title' => $page->getName(),
|
||||
'route' => $page->getRoute()
|
||||
];
|
||||
}
|
||||
ksort($hyMenuPages);
|
||||
|
||||
$hyMenu = [
|
||||
'name' => $menu['name'],
|
||||
'pageIds' => $menu['pages'],
|
||||
'pages' => $hyMenuPages,
|
||||
];
|
||||
return $hyMenu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all hydrated menus with the given name.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHydratedMenus()
|
||||
{
|
||||
$menus = [];
|
||||
|
||||
foreach ($this->getAll() as $entry){
|
||||
$menus[] = $this->getHydratedMenu($entry['name']);
|
||||
}
|
||||
|
||||
return $menus;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Repositories;
|
||||
|
||||
use Plugi\Contracts\PageContract;
|
||||
use Plugi\Contracts\PageRepositoryContract;
|
||||
use Exception;
|
||||
|
||||
class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
{
|
||||
/**
|
||||
* The pages database table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* The class that represents each page.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $class;
|
||||
|
||||
/**
|
||||
* PageRepository constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->table = empty(phpb_config('page.table')) ? 'pages' : phpb_config('page.table');
|
||||
parent::__construct();
|
||||
$this->class = phpb_instance('page');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new page.
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool|object|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
foreach (['name', 'layout'] as $field) {
|
||||
if (! isset($data[$field]) || ! is_string($data[$field])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$page = parent::create([
|
||||
'name' => $data['name'],
|
||||
'layout' => $data['layout'],
|
||||
]);
|
||||
if (! ($page instanceof PageContract)) {
|
||||
throw new Exception("Page not of type PageContract");
|
||||
}
|
||||
return $this->replaceTranslations($page, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the given page with the given updated data.
|
||||
*
|
||||
* @param $page
|
||||
* @param array $data
|
||||
* @return bool|object|null
|
||||
*/
|
||||
public function update($page, array $data)
|
||||
{
|
||||
foreach (['name', 'layout'] as $field) {
|
||||
if (! isset($data[$field]) || ! is_string($data[$field])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$this->replaceTranslations($page, $data);
|
||||
|
||||
$updateResult = parent::update($page, [
|
||||
'name' => $data['name'],
|
||||
'layout' => $data['layout'],
|
||||
]);
|
||||
$page->invalidateCache();
|
||||
return $updateResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the translations of the given page by the given data.
|
||||
*
|
||||
* @param PageContract $page
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
protected function replaceTranslations(PageContract $page, array $data)
|
||||
{
|
||||
$activeLanguages = phpb_active_languages();
|
||||
foreach (['title', 'meta_title', 'meta_description', 'route'] as $field) {
|
||||
foreach ($activeLanguages as $languageCode => $languageTranslation) {
|
||||
if (! isset($data[$field][$languageCode])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$pageTranslationRepository = new PageTranslationRepository;
|
||||
$pageTranslationRepository->destroyWhere(phpb_config('page.translation.foreign_key'), $page->getId());
|
||||
foreach ($activeLanguages as $languageCode => $languageTranslation) {
|
||||
$pageTranslationRepository->create([
|
||||
phpb_config('page.translation.foreign_key') => $page->getId(),
|
||||
'locale' => $languageCode,
|
||||
'title' => $data['title'][$languageCode],
|
||||
'meta_title' => $data['meta_title'][$languageCode],
|
||||
'meta_description' => $data['meta_description'][$languageCode],
|
||||
'route' => $data['route'][$languageCode],
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the given page with the given updated page data.
|
||||
*
|
||||
* @param $page
|
||||
* @param array $data
|
||||
* @return bool|object|null
|
||||
*/
|
||||
public function updatePageData($page, array $data)
|
||||
{
|
||||
$updateResult = parent::update($page, [
|
||||
'data' => json_encode($data),
|
||||
]);
|
||||
$page->invalidateCache();
|
||||
return $updateResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the given page from the database.
|
||||
*
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$this->findWithId($id)->invalidateCache();
|
||||
|
||||
return parent::destroy($id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Repositories;
|
||||
|
||||
use Plugi\Contracts\PageTranslationRepositoryContract;
|
||||
|
||||
class PageTranslationRepository extends BaseRepository implements PageTranslationRepositoryContract
|
||||
{
|
||||
/**
|
||||
* The page translations database table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* The class that represents each page translation.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $class;
|
||||
|
||||
/**
|
||||
* PageTranslationRepository constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->table = empty(phpb_config('page.translation.table')) ? 'page_translations' : phpb_config('page.translation.table');
|
||||
parent::__construct();
|
||||
$this->class = phpb_instance('page.translation');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Repositories;
|
||||
|
||||
use Plugi\Contracts\SettingRepositoryContract;
|
||||
|
||||
class SettingRepository extends BaseRepository implements SettingRepositoryContract
|
||||
{
|
||||
/**
|
||||
* The pages database table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'settings';
|
||||
|
||||
/**
|
||||
* Replace all website settings by the given data.
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool|object|null
|
||||
*/
|
||||
public function updateSettings(array $data)
|
||||
{
|
||||
$this->destroyAll();
|
||||
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$isArray = is_array($value) ? 1 : 0;
|
||||
if ($isArray) {
|
||||
$value = implode(',', $value);
|
||||
}
|
||||
$this->create([
|
||||
'setting' => $key,
|
||||
'value' => $value,
|
||||
'is_array' => $isArray,
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Repositories;
|
||||
|
||||
use Plugi\UploadedFile;
|
||||
|
||||
class UploadRepository extends BaseRepository
|
||||
{
|
||||
/**
|
||||
* The uploads database table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'uploads';
|
||||
|
||||
/**
|
||||
* The class that represents each uploaded file.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $class = UploadedFile::class;
|
||||
|
||||
/**
|
||||
* Create a new uploaded file.
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool|object
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$fields = ['public_id', 'original_file', 'mime_type', 'server_file'];
|
||||
foreach ($fields as $field) {
|
||||
if (! isset($data[$field]) || ! is_string($data[$field])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::create([
|
||||
'public_id' => $data['public_id'],
|
||||
'original_file' => $data['original_file'],
|
||||
'mime_type' => $data['mime_type'],
|
||||
'server_file' => $data['server_file'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user