diff --git a/app/Providers/FipamoServiceProvider.php b/app/Providers/FipamoServiceProvider.php index 50c019b..82af3a8 100644 --- a/app/Providers/FipamoServiceProvider.php +++ b/app/Providers/FipamoServiceProvider.php @@ -11,7 +11,10 @@ use App\Services\ContentService; use App\Services\PaginateService; use App\Services\ThemeService; use App\Services\DocService; +use App\Services\StringService; use App\Services\FileUploadService; +use App\Services\RenderService; +use App\Services\SortingService; class FipamoServiceProvider extends ServiceProvider { @@ -52,6 +55,18 @@ class FipamoServiceProvider extends ServiceProvider $this->app->bind(FileUploadService::class, function ($app) { return new FileUploadService(); }); + + $this->app->bind(RenderService::class, function ($app) { + return new RenderService(); + }); + + $this->app->bind(SortingService::class, function ($app) { + return new SortingService( + new SettingsService(new DocService()), + new ContentService(), + new StringService(), + ); + }); } /** diff --git a/app/Repositories/PageRepository.php b/app/Repositories/PageRepository.php index 9ab1b89..dd819a1 100644 --- a/app/Repositories/PageRepository.php +++ b/app/Repositories/PageRepository.php @@ -8,6 +8,7 @@ use App\Services\ContentService; use App\Services\PaginateService; use App\Services\StringService; use App\Services\DocService; +use App\Services\SortingService; use Carbon\Carbon; class PageRepository implements PageRepositoryInterface @@ -18,6 +19,7 @@ class PageRepository implements PageRepositoryInterface protected $pages; protected $strings; protected $docs; + protected $sort; public function __construct( ContentService $contentService, @@ -25,12 +27,14 @@ class PageRepository implements PageRepositoryInterface PaginateService $paginateService, StringService $stringService, DocService $docService, + SortingService $sortingService, ) { $this->content = $contentService; $this->settings = $settingsService; $this->paginate = $paginateService; $this->strings = $stringService; $this->docs = $docService; + $this->sort = $sortingService; $this->pages = $this->content->loadAllPages(); } diff --git a/app/Services/RenderService.php b/app/Services/RenderService.php new file mode 100644 index 0000000..cb6c9ab --- /dev/null +++ b/app/Services/RenderService.php @@ -0,0 +1,10 @@ +settings['global']; } + public function getMenu() + { + $this->settings = $this->loadSettings(); + return $this->settings['menu']; + } + public function getFolks() { return $this->folks; diff --git a/app/Services/SortingService.php b/app/Services/SortingService.php new file mode 100644 index 0000000..3239ac5 --- /dev/null +++ b/app/Services/SortingService.php @@ -0,0 +1,229 @@ +settings = $settingsService; + $this->contents = $contentService; + $this->strings = $stringService; + } + + public function tags() + { + $pages = $this->contents->loadAllPages(); + foreach ($pages as $page) { + $temp = []; + if (isset($page['tags'])) { + $temp = explode(',', $page['tags']); + foreach ($temp as $tag) { + $label = trim($tag); + if (!find($this->p_tags, ['tag_name' => $label])) { + array_push($this->p_tags, [ + 'tag_name' => $label, + 'slug' => $this->strings::safeString($label), + 'pages' => $this->tagPages($label, $pages), + ]); + } + } + } + } + return $this->p_tags; + } + + private function tagPages($tag, $pages) + { + $tagged = []; + foreach ($pages as $page) { + if (isset($page['tags'])) { + if (strpos($page['tags'], $tag) !== false) { + array_push($tagged, [ + 'title' => $page['title'], + 'slug' => $page['slug'], + 'path' => $page['path'], + 'feature' => $page['feature'], + ]); + } + } + } + + return $tagged; + } + + public function archive() + { + $pages = $this->contents->loadAllPages(); + $years = []; + $archive = []; + foreach ($pages as $page) { + // $year = date("Y", date($page["rawCreated"])); + $date = explode('/', $page['path']); + // echo $page["title"] . " : " . $year . "\n"; + if (!find($years, ['year' => trim($date[0])])) { + $findPages = filter($pages, ['createdYear' => trim($date[0])]); + // var_dump($findPages); + array_push( + $years, + [ + 'year' => trim($date[0]), + 'count' => count($findPages), + ] + ); + } + } + foreach ($years as $year) { + $sorted = []; + $filtered = filter($pages, ['createdYear' => $year['year']]); + + foreach ($filtered as $obj) { + $month = date('m', date($obj['rawCreated'])); + if (!find($sorted, ['month' => $month])) { + $perMonth = filter( + $pages, + [ + 'path' => $year['year'] . '/' . $month, + 'deleted' => false, + 'published' => true, + 'layout' => 'page', + ] + ); + array_push($sorted, [ + 'month' => $month, + 'full_month' => date('F', date($obj['rawCreated'])), + 'count' => count($perMonth), + 'pages' => $perMonth, + ]); + } + } + array_push($this->p_archive, [ + 'year' => $year['year'], + 'year_data' => $sorted, + ]); + } + return $this->p_archive; + } + + public function page($page) + { + $global = $this->settings->getGlobal(); + $pageOptions = []; + + $pageInfo = [ + 'keywords' => isset($global['keywords']) + ? $global['keywords'] + : 'fipamo, blog, jamstack, php, markdown, js', + 'description' => $global['descriptions'], + 'image' => $global['base_url'] . $global['background'], + 'baseURL' => $global['base_url'], + ]; + $tags = []; + if (isset($page['tags'])) { + $taglist = explode(',', $page['tags']); + foreach ($taglist as $tag) { + $label = trim($tag); + array_push($tags, [ + 'label' => $label . ' ', + 'slug' => $this->strings->safeString($label), + ]); + } + } + + $meta = [ + 'who' => $page['author'], + 'when' => $page['created'], + 'tags' => $tags, + ]; + + // if page feature isn't empty, find image from list and set it as background image + // if it is empty, just use global background + if ($page['feature'] != '' || $page['feature'] != null) { + $media = explode(',', $page['feature']); + $set = false; + foreach ($media as $file) { + $item = trim($file); + $ext = pathinfo($item, PATHINFO_EXTENSION); + + if ($ext != 'mp4' && !$set) { + $pageInfo['image'] = $pageInfo['baseURL'] . $item; + $set = true; + } + } + } + + if ($page['layout'] == 'index') { + $recent = []; + $featured = []; + $limit = 4; + $pages = $this->contents->loadAllPages(); + foreach ($pages as $item) { + if ( + !$item['deleted'] && + $item['published'] && + $item['menu'] != 'true' + ) { + if (count($recent) < $limit) { + array_push($recent, [ + 'path' => $item['path'], + 'slug' => $item['slug'], + 'title' => $item['title'], + 'feature' => $item['feature'], + ]); + } + + if ($item['featured'] == true) { + if (count($featured) < $limit) { + array_push($featured, [ + 'path' => $item['path'], + 'slug' => $item['slug'], + 'title' => $item['title'], + 'feature' => $item['feature'], + ]); + } + } + } + } + + $pageOptions = [ + 'title' => $page['title'], + 'background' => $page['feature'], + 'content' => $page['html'], // $cleaned, + 'meta' => $meta, + 'recent' => $recent, + 'featured' => $featured, + 'info' => $pageInfo, + 'menu' => $this->settings->getMenu(), + 'dynamicRender' => $global['dynamicRender'], + 'media' => $page['media'], + 'files' => $page['docs'], + ]; + } else { + $pageOptions = [ + 'title' => $page['title'], + 'background' => $page['feature'], + 'content' => $page['html'], // $cleaned, + 'meta' => $meta, + 'info' => $pageInfo, + 'menu' => $this->settings->getMenu(), + 'dynamicRender' => $global['dynamicRender'], + 'media' => $page['media'], + 'files' => $page['docs'], + ]; + } + var_dump($pageOptions); + //return $pageOptions; + } +} diff --git a/app/Services/StringService.php b/app/Services/StringService.php index dfe13b8..23a97bb 100644 --- a/app/Services/StringService.php +++ b/app/Services/StringService.php @@ -26,4 +26,26 @@ class StringService mt_rand(0, 65535) ); } + + public static function safeString($string) + { + return strtolower( + trim( + preg_replace( + '~[^0-9a-z]+~i', + '_', + html_entity_decode( + preg_replace( + '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', + '$1', + htmlentities($string, ENT_QUOTES, 'UTF-8') + ), + ENT_QUOTES, + 'UTF-8' + ) + ), + '-' + ) + ); + } }