File: /var/www/vhosts/creativefellows.nl/skl.creativefellows.nl/src/Domain/Page/Service/PageReader.php
<?php
namespace App\Domain\Page\Service;
use App\Domain\Page\Data\PageData;
use App\Domain\Page\Repository\PageRepository;
use App\Domain\Entry\Data\EntryData;
use App\Domain\Dragdrop\Service\DragdropReader;
use App\Domain\Field\FieldData;
use Psr\Container\ContainerInterface;
use Slim\Interfaces\RouteParserInterface;
use App\Domain\Language\LanguageData;
use App\Domain\Form\Service\FormReader;
/**
* Service.
*/
final class PageReader
{
/**
* @var UserViewerRepository
*/
private $repository;
protected $container;
protected $form_status = [];
/**
* The constructor.
*
* @param UserViewerRepository $repository The repository
*/
public function __construct(
ContainerInterface $container,
PageRepository $repository,
RouteParserInterface $routeParser,
DragdropReader $dragdropReader,
LanguageData $language,
FormReader $form
){
$this->container = $container;
$this->repository = $repository;
$this->router = $routeParser;
$this->dragdrop = $dragdropReader;
$this->language = $language;
$this->form = $form;
}
/**
* Get contents for homepage
*
* @return PageReaderData The Page data
*/
public function getHomeData(): PageData
{
// Fetch primary
$pageRow = $this->repository->getHomepage(0);
$primary = $this->getData($pageRow);
// get page localization
if($this->language->isLocalized() ){
// fetch secondary
$translationRow = $this->repository->getHomepage($this->language->id());
$secondary = $this->getData($translationRow);
foreach($secondary->fields() as $field)
{
if( $field->translate() ) {
$primary->setField($field);
}
}
$primary->setMetaTitle( $secondary->metaTitle() );
$primary->setMetaDescription($secondary->metaDescription());
}
return $primary;
}
public function setFormStatus($status)
{
$this->form_status = $status;
}
public function getPageData($section=null,$page=null,$pagination=null): PageData
{
// set page url
$this->section = $section ?? null;
$this->page = $page ?? null;
$this->pagination = $pagination ?? null;
$this->pageUrl = $page == null ? $section : $page;
// Fetch data from the database
$pageRow = $this->repository->getPageByUrl($this->pageUrl,$this->language->id());
$primary = $this->getData($pageRow);
// return date
return $primary;
}
/*
private function getPageLocalization( PageData $primary,$deeplink=true): PageData
{
$translation = $this->repository->getTranslationById($primary->uniqueId(),$this->language->id());
if( $translation )
{
$secondary = $this->getData($translation,$deeplink);
foreach($secondary->fields() as $field)
{
if( $field->translate() ) {
$primary->setField($field);
}
}
$primary->setUrl( "/". $this->language->current() . $secondary->getUrl() );
$primary->setMetaTitle( $secondary->metaTitle() );
$primary->setMetaDescription($secondary->metaDescription());
$primary->setForm( $secondary->form() );
}
return $primary;
}
*/
public function getPageDataById($unique_id): PageData
{
// Fetch data from the database
$pageRow = $this->repository->getPageByUid($unique_id);
return $this->getData($pageRow);
}
public function getPagesById($section,$category,$entry=null,$deeplink=true)
{
$rows = $this->repository->getPagesById($section,$category,$entry);
$pages = [];
foreach($rows as $row)
{
$pages[] = $this->getData($row,0,$deeplink);
}
return $pages;
}
public function getPagesInSection($section)
{
$data = $this->repository->sectionId($section);
if($data) return $this->getSectionPages($data["id"]);
else return [];
}
public function getSectionPages(int $section)
{
$rows = $this->repository->getPagesInSection($section);
$pages = [];
foreach($rows as $row)
{
$page = $this->getData($row,0,$deeplink);
$pages[] = $page->twigData();
}
return $pages;
}
public function getCategoryPages($category_id,$limit)
{
$rows = $this->repository->getCategoryPages($category_id,$limit);
$pages = [];
foreach($rows as $row)
{
$page = $this->getData($row,0,true);
$pages[] = $page;
}
return $pages;
}
public function getLinks($link)
{
$pages = [];
$links = explode("|*|",$link);
foreach($links as $link)
{
$data = explode("|-|",$link);
if($data[0])
{
$row = $this->repository->getPageByUid($data[0]);
if(!empty($row)) $pages[] = $this->getData($row);
}
}
//d($pages);
return $pages;
}
private function getData($pageRow,$show_fields=0,$deeplink=true): PageData
{
// set page data
$page = new PageData($pageRow);
$page->setHashKey( $this->getHashKey() );
// return if no page present at url
if(!$pageRow) return $page;
// get data
$template_fields = $this->repository->getTemplateFields( $page->templateId() );
$table = $this->repository->getTableName( $page->templateId() );
$entry_data = $this->repository->getEntryData( $table,$page->entryId() );
$template_fields[] = [
"template_field_id" => 0,
"field_naam" => "link",
"form_element" => "link",
"field_id" => 0,
"translate" => 0,
];
// get page grid
if($entry_data["stramien_id"]) $grid = $this->repository->getGridName( $entry_data["stramien_id"] );
else $grid = $this->repository->getGridName( $page->getGrid() );
// set grid
$page->setGrid( $grid["naam"] );
// set meta
$page->setBaseUrl( $this->getBaseUrl() );
$page->setMetaTitle( $entry_data["meta_title"] );
$page->setMetaDescription( $entry_data["meta_desc"] );
// set canonical url
$page->setCanonical( $this->canonicalUrl(["section" => $this->section, "page" => $this->page, "pagination" => $this->pagination]) );
// set url
$page->setUrl( $this->router->urlFor('page.view',["section" => $page->sectionUrl(), "page" => $page->url()]) );
// set the form
$form = $entry_data["form_id"] ? $this->form->getForm( $entry_data["form_id"],$this->form_status) : null;
$page->setForm( $form );
// set fields
foreach($template_fields as $i => $field)
{
// new field
$fieldData = new FieldData($field);
// field is dragdrop
if( $fieldData->isDragDrop() && $deeplink == true ){
$data = $this->dragdrop->getHTML( $this->getPageReader(), $page->uniqueId() );
}
// field in link
elseif( $fieldData->isEntryLinks() && $deeplink == true ) $data = $this->getLinks( $entry_data[ $fieldData->name() ] );
// link
else if( $fieldData->isLink() ) $data = $page->url();
// default
else $data = $entry_data[ $fieldData->name() ];
// Set field data
$fieldData->setValue($data);
// Set field in page
$page->setField($fieldData);
// set first field as name
if($i == 0) $page->setName( $data );
}
return $page;
}
private function canonicalUrl($url_arguments)
{
if(!$this->page && !$this->section) return $this->container->get("settings")["domain_path"];
if(!$this->page) unset($url_arguments["page"]);
if( is_numeric($url_arguments["pagination"]) ) $url_arguments["page"] = $url_arguments["pagination"];
return $this->language->baseUrl() . $this->router->urlFor('page.view',$url_arguments);
}
private function getPageReader()
{
return $this;
}
private function getBaseUrl()
{
return $this->container->get('settings')['domain_name'];
}
private function getHashKey()
{
return $this->container->get('settings')['hash_key'];
}
public function getTemplateName($section)
{
}
}