Add 'plugi-core/' from commit '6c478c54d048ab2fd5af4e8e79074153fb594c16'

git-subtree-dir: plugi-core
git-subtree-mainline: d4c734cdbd
git-subtree-split: 6c478c54d0
This commit is contained in:
Paul Spenke
2023-06-13 07:27:11 +08:00
151 changed files with 46689 additions and 0 deletions
@@ -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;
}
}