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,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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user