File: /var/www/vhosts/creativefellows.nl/inekehamoen.creativefellows.nl/src/Hamoen/Controller.php
<?php
namespace Hamoen;
use Cassette\BaseController;
use Cassette\Controller as CassetteController;
class Controller extends CassetteController{
public $default_section = "portfolio";
function __construct($container)
{
$this->db = $container->get('db');
$this->router = $container->get('router');
$this->view = $container->get('renderer');
$this->settings = $container->get('settings');
$this->client_settings = $this->getClientSettings();
$this->config = new BaseController($this->settings,$this->client_settings);
}
private function defaultSection(){
return $this->default_section;
}
private function setCurrentSection($section)
{
$this->section_name = $section;
}
private function setCurrentProject($category_name)
{
$this->category_name = $category_name;
}
private function setCurrentImage($image)
{
$this->image = $image;
}
private function getCurrentSection()
{
if(!$this->section_name) $this->section_name = $this->defaultSection();
return $this->section_name;
}
private function getCurrentProject()
{
return $this->category_name;
}
private function getCurrentImage()
{
return $this->image;
}
public function viewHome($request, $response, $args)
{
// get url parameters
$this->section_name = isset($args["name"]) ? $args["name"] : null;
$this->category_name = isset($args["category"]) ? $args["category"] : null;
$this->entry_name = isset($args["entry"]) ? $args["entry"] : null;
$this->image = isset($args["image"]) ? $args["image"] : null;
$current_pagination = isset($args["pagination"]) ? $args["pagination"] : 1;
$this->setCurrentSection($this->section_name);
$this->setCurrentProject($this->category_name);
$this->setCurrentImage($this->category_name);
// get topbar navigation
$navigation = $this->getMainNavigation(true,$this->section_name);
// get page contents
$page = $this->getPage($this->section_name,$this->category_name,$this->entry_name,$current_pagination,true,0,true);
// get work
$work = $this->getWork();
$view_template = empty($work) ? "view.default.php" : "view.home.php";
// return the page
return $this->view->render($response, $view_template, [
"cassette" => $this,
"config" => $this->config,
"router" => $this->router,
"page" => $page,
"work" => $work,
"navigation" => $navigation,
"categories" => $categories,
"section_name" => $this->section_name,
"category_name" => $this->category_name,
"meta_title" => ""
]);
}
public function viewAtWork($request, $response, $args)
{
// get url parameters
$this->section_name = "at-work";
$this->category_name = "at-work";
$this->entry_name = null;
$this->image = null;
$current_pagination = isset($args["pagination"]) ? $args["pagination"] : 1;
$this->setCurrentSection($this->section_name);
$this->setCurrentProject($this->category_name);
//$this->setCurrentImage($this->category_name);
// get topbar navigation
$navigation = $this->getMainNavigation(true,$this->section_name);
// get page contents
$page = $this->getPage($this->section_name,$this->category_name,$this->entry_name,$this->current_pagination,true,0,true);
// get work
$work = $this->getWork();
// return the page
return $this->view->render($response, "view.project.php", [
"cassette" => $this,
"config" => $this->config,
"router" => $this->router,
"page" => $page,
"work" => $work,
"navigation" => $navigation,
"categories" => $categories,
"section_name" => $this->section_name,
"category_name" => $this->category_name,
"meta_title" => ""
]);
}
public function viewProject($request, $response, $args)
{
// get url parameters
$this->section_name = isset($args["name"]) ? $args["name"] : 0;
$this->category_name = isset($args["category"]) ? $args["category"] : 0;
$this->entry_name = isset($args["entry"]) ? $args["entry"] : 0;
$this->image = isset($args["image"]) ? $args["image"] : null;
$this->current_pagination = isset($args["pagination"]) ? $args["pagination"] : 1;
// set arguments
$this->setCurrentSection($this->section_name);
$this->setCurrentProject($this->category_name);
$this->setCurrentImage($this->image);
// get topbar navigation
$navigation = $this->getMainNavigation(true,$this->section_name);
// get page contents
$page = $this->getPage($this->section_name,$this->category_name,$this->entry_name,$this->current_pagination,true,0,true);
// get work
$work = $this->getWork();
// return the page
return $this->view->render($response, "view.project.php", [
"cassette" => $this,
"config" => $this->config,
"router" => $this->router,
"page" => $page,
"work" => $work,
"navigation" => $navigation,
"categories" => $categories,
"section_name" => $this->section_name,
"category_name" => $this->category_name,
"meta_title" => ""
]);
}
public function viewImage($request, $response, $args)
{
// get url parameters
$this->section_name = isset($args["name"]) ? $args["name"] : 0;
$this->category_name = isset($args["category"]) ? $args["category"] : 0;
$this->entry_name = isset($args["entry"]) ? $args["entry"] : 0;
$this->image = isset($args["image"]) ? $args["image"] : null;
$this->current_pagination = isset($args["pagination"]) ? $args["pagination"] : 1;
// set arguments
$this->setCurrentSection($this->section_name);
$this->setCurrentProject($this->category_name);
$this->setCurrentImage($this->image);
// get topbar navigation
$navigation = $this->getMainNavigation(true,$this->section_name);
// get page contents
$page = $this->getPage($this->section_name,$this->category_name,$this->entry_name,$this->current_pagination,true,0,true);
// get work
$work = $this->getWork();
// return the page
return $this->view->render($response, "view.image.php", array(
"cassette" => $this,
"config" => $this->config,
"router" => $this->router,
"page" => $page,
"work" => $work,
"navigation" => $navigation,
"section_name" => $this->section_name,
"category_name" => $this->category_name,
"meta_title" => "",//$page[0]->getMetaTitle(),
"pagination" => [
"pagination_rows_total" => $this->pagination_rows_total,
"pagination_count" => $this->pagination_count,
"current_page" => $current_pagination,
"next_page" => $current_pagination + 1 ,
"previous_page" => $current_pagination - 1
]
));
}
public function orderWork($request, $response, $args)
{
// get url parameters
$this->section_name = isset($args["name"]) ? $args["name"] : 0;
$this->category_name = isset($args["category"]) ? $args["category"] : 0;
$this->entry_name = isset($args["entry"]) ? $args["entry"] : 0;
$current_pagination = isset($args["pagination"]) ? $args["pagination"] : 1;
// get topbar navigation
$navigation = $this->getMainNavigation(true,$this->section_name);
// get page contents
$page = $this->getPage($this->section_name,$this->category_name,$this->entry_name,$current_pagination,true,0,true);
// get work
//$work = $this->getWork(2,$this->category_name);
// return the page
return $this->view->render($response, "view.default.php", [
"cassette" => $this,
"config" => $this->config,
"router" => $this->router,
"page" => $page,
"work" => $work,
"navigation" => $navigation,
"categories" => $categories,
"section_name" => $this->section_name,
"category_name" => $this->category_name,
"meta_title" => "",//$page[0]->getMetaTitle(),
"pagination" => [
"pagination_rows_total" => $this->pagination_rows_total,
"pagination_count" => $this->pagination_count,
"current_page" => $current_pagination,
"next_page" => $current_pagination + 1 ,
"previous_page" => $current_pagination - 1
]
]);
}
private function projectUrl($uid)
{
$category = $this->entryCategory($uid);
$project = $this->workProject( 2,$category->name() );
$section = $this->projectSection($category->sectie_id,"url");
return $section.$project->url()."/";
}
private function getWork()
{
// view image
if( $this->getCurrentImage() ){
$project = $this->workProject();
$project->pushEntity( $this->projectCover( $project->id() ) );
return $project;
}
// view project
elseif( $this->getCurrentProject() ){
$project = $this->workProject();
$sql = "SELECT p.naam, p.project, p.images, p.omschrijving, u.url, p.bestellen, p.video_embed FROM unique_ids u
LEFT JOIN entry_categories e
ON e.unique_id = u.unique_id
LEFT JOIN td_project p
ON u.unique_id = p.unique_id
WHERE e.category_id = :category_id
AND u.status = 2
ORDER BY u.position";
$sth = $this->db->prepare($sql . $limit_query_items);
$sth->execute([
"category_id" => $project->id()
]);
while($entry = $sth->fetch())
{
$entity = new HamoenEntity($entry);
$entity->setBasePath( $project->path() );
$project->pushEntity($entity);
}
return $project;
}
// list all projects
else
{
return $this->listProjects();
}
}
/*
* get project based on section and category
*/
private function workProject()
{
$sql = "SELECT c.category_id, c.naam, c.url, c.position FROM categories c LEFT JOIN secties s on c.sectie_id = s.sectie_id WHERE s.url = :section_name AND (c.naam LIKE :project_name OR c.url LIKE :project_name) AND c.active = 1";
$sth = $this->db->prepare($sql);
$sth->execute([
"project_name" => $this->getCurrentProject(),
"section_name" => $this->getCurrentSection()
]);
$project = new Project( $sth->fetch() );
$project->setSection( $this->getCurrentSection() );
return $project;
}
/*
* List all projects
*/
private function listProjects()
{
// section id
$section = $this->projectSection( $this->getCurrentSection() );
$sql = "SELECT category_id, naam, url, position FROM categories WHERE sectie_id = :section_id AND active = 1 ORDER BY position ASC";
$sth = $this->db->prepare($sql);
$sth->execute([
"section_id" => $section->id()
]);
$projects = [];
foreach($sth->fetchAll() as $i => $row)
{
$project = new Project($row);
$project->setSection( $section->url() );
$project->pushEntity( $this->projectCover( $project->id() ) );
$project->setBaseUrl( $this->projectSection( $section_id,"url") );
$projects[] = $project;
}
return $projects;
}
/*
* Information on section
*/
private function projectSection($id,$return=null)
{
$section = null;
$sql = "SELECT * FROM `secties` WHERE secties.active = '1' AND (secties.sectie_id = :id OR secties.url = :id)";
$sth = $this->db->prepare($sql);
$sth->execute([
"id" => $id
]);
if($sth->rowCount() > 0){
$section = new HamoenSection( $sth->fetch() );
}
return $section;
}
/*
* Set image url as project cover
*/
private function projectCover($id)
{
if( $this->getCurrentImage() )
{
$sql = "SELECT p.naam, p.project, p.images, p.omschrijving FROM unique_ids u
LEFT JOIN td_project p
ON u.unique_id = p.unique_id
WHERE u.url = :url
AND u.status = 2
ORDER BY u.position ASC
LIMIT 0,1";
$sth = $this->db->prepare($sql);
$sth->execute([
"url" => $this->getCurrentImage()
]);
}
else{
$sql = "SELECT p.naam, p.project, p.images, p.omschrijving FROM unique_ids u
LEFT JOIN entry_categories e
ON e.unique_id = u.unique_id
LEFT JOIN td_project p
ON u.unique_id = p.unique_id
WHERE e.category_id = :category_id
AND u.status = 2
ORDER BY u.position ASC
LIMIT 0,1";
$sth = $this->db->prepare($sql);
$sth->execute([
"category_id" => $id
]);
}
return new HamoenEntity( $sth->fetch() );
}
/*
private function projectImage($id)
{
$sql = "SELECT p.naam, p.project, p.images, p.omschrijving, u.url, u.unique_id FROM unique_ids u
LEFT JOIN td_project p
ON u.unique_id = p.unique_id
WHERE u.unique_id = :unique_id
AND u.status = 2";
$sth = $this->db->prepare($sql);
$sth->execute([
"unique_id" => $id
]);
return new HamoenEntity( $sth->fetch() );
}
private function relatedImages(array $tags)
{
$sql = "SELECT
e.unique_id,
count(*) as filter_count,
u.url,
u.unique_id AS uid
FROM tags t
LEFT JOIN entry_tags e
ON t.tag_id = e.tag_id
LEFT JOIN unique_ids u
ON u.unique_id = e.unique_id
WHERE t.name IN ('". implode("','",$tags)."')
AND u.status = 2
GROUP BY e.unique_id ORDER BY filter_count DESC LIMIT 0,6 ";
$sth = $this->db->prepare($sql);
$sth->execute();
$related = [];
while($row = $sth->fetch())
{
$image = $this->projectImage($row["unique_id"]);
$image->setBasePath( $this->projectUrl($image->id() ) );
$related[] = $image;
}
return $related;
}
*/
}
?>