<?php
namespace App\Controller\ING;
use App\Controller\BaseController;
use Empire\Content\Content;
use Empire\Content\Image;
use Empire\Content\Part;
use Empire\Core\Core;
use Empire\Core\User;
use Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class NewsGridController
* @package App\Controller
* @Route("/ing")
*/
class ContentController extends BaseController
{
/**
* @Route("/{category}/{page}", methods={"GET"})
* @param $category
* @param $page
* @param Request $request
* @return Response
* @throws
*/
function displayPage($category, $page, Request $request) {
$content = Content::getByPage($category, $page);
if(null === $content || $content == false) {
$response = new Response();
$response->setStatusCode(404);
return $this->render('404.twig', ['gejs' => ['ajax']], $response);
}
return $this->render('ing/main.twig', [
'title' => "Imperial News Grid",
'content' => $content,
'editable' => Core::user() instanceof User && $content->canEdit(Core::user()),
'content_id' => $content->getID(),
'gejs' => ['ajax'],
]);
}
/**
* @Route("/{category}/{page}", methods={"POST"}, name="ing")
* @param $category
* @param $page
* @param Request $request
* @return Response
* @throws
*/
function editPage($category, $page, Request $request) {
$content = Content::getByPage($category, $page);
$changes = $request->request->all();
$title = $content->title;
$touch = false;
foreach($changes as $key => $change) {
switch($key) {
case 'images': break; //We're dealing with images later.
case 'title':
$title = strip_tags($change);
$touch = true;
break;
default:
Part::save($key, $change);
$touch = true;
break;
}
}
if($touch) {
$content->savePage($title, $content->content, Core::user());
}
//Dealing with the images.
$images = json_decode($request->request->get("images"), true);
$ids = [];
foreach($images as $url => $size) {
try {
$image = Image::find($url);
} catch (Exception $e) {
continue;
}
$ids[] = $image->getId();
$image->thumbnail($size);
}
Image::cleanup($ids, $content);
return $this->json(["status" => true]);
}
}