fipamo/app/Repositories/PageRepository.php

152 lines
4.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Repositories;
use App\Interfaces\PageRepositoryInterface;
use App\Services\SettingsService;
use App\Services\ContentService;
use App\Services\PaginateService;
use App\Services\StringService;
use App\Services\DocService;
use Carbon\Carbon;
class PageRepository implements PageRepositoryInterface
{
protected $content;
protected $setttings;
protected $paginate;
protected $pages;
protected $strings;
protected $docs;
public function __construct(
ContentService $contentService,
SettingsService $settingsService,
PaginateService $paginateService,
StringService $stringService,
DocService $docService,
) {
$this->content = $contentService;
$this->settings = $settingsService;
$this->paginate = $paginateService;
$this->strings = $stringService;
$this->docs = $docService;
$this->pages = $this->content->loadAllPages();
}
public function getAll()
{
return $this->pages;
}
public function getById($uuid)
{
return $this->pages->where('uuid', $uuid);
}
public function delete($uuid)
{
}
public function create($page)
{
return $this->editPage($page, null, 'create');
}
public function update($page)
{
return $this->editPage($page, $this->pages->where('uuid', $page->uuid)->first(), 'update');
//hande result of page update
}
public function getGroup($num, $limit, $sort = "all")
{
return $this->paginate->getPage($num, $limit, $sort);
}
private function editPage($body, $page, $task)
{
$path;
$file;
$writePath;
$message;
if ($task != 'create') {
$path = date('Y', date($page['rawCreated'])) .
'/' .
date('m', date($page['rawCreated']));
} else {
$path = date('Y') . '/' . date('m');
}
if ($task == 'delete') {
$deleted = 'true';
$body->menu = 'false';
$body->published = 'false';
$body->featured = 'false';
} else {
$deleted = isset($page['deleted']) ? $page['deleted'] : 'false';
}
$created = $task != 'create' ? new Carbon($page['rawCreated']) : Carbon::now();
$updated = Carbon::now();
// grab current index from settings and update
$id = $task != 'create' ? $body->id : $this->settings->getSettings()['library_stats']['current_index'];
$uuid = $task != 'create' ? $body->uuid : $this->strings::createUUID();
//set variables post body for saving
$body->id = $id;
$body->uuid = $uuid;
$body->path = $path;
$body->author = session('member')['handle'];
$body->created = $created->format("Y-m-d\TH:i:sP");
$body->updated = $updated->format("Y-m-d\TH:i:sP");
$body->deleted = $deleted;
//set path to save file
if ($body->layout == 'index') {
$writePath = '../content/pages/start/index.md';
} else {
$writePath = '../content/pages/' . $path . '/' . $body->slug . '.md';
}
//write file to path
$saved = $this->docs::writePages($task, $path, $writePath, $this->docs::objectToMD($body));
//handle post save result
if ($saved) {
if (
$this->settings->getGlobal()['renderOnSave'] == 'true' &&
$this->settings->getGlobal()['dynamicRender'] == 'false'
) {
//TODO: RENDER ENGINE STUFF
//$render = new Render();
//$render->renderTags();
//$render->renderArchive();
//$render->renderPages();
$message = 'Filed edited and rendered. NOICE.';
} else {
$message = 'File edited. Nice work';
}
//upadte settings if needed
$body->path = $path;
//Settings::updateMenu($body);
//Settings::updateTags();
// if new page added, update current index in Settings file
if ($task == 'create') {
//Settings::updateIndex();
$this->settings->updatePageIndex();
}
return [
'message' => $message,
'type' => $task == 'update' ? 'postUpdated' : 'postAdded',
'id' => $uuid,
];
} else {
return $response = [
'message' => "Uh oh. File save problem. Don't panic",
'type' => 'postError',
'id' => $uuid,
];
}
}
}