})->setName('login'); // Logout (fake) $app->map(['POST', 'GET'], '/logout', function (Request $request, Response $response) { $_SESSION['user'] = '******'; /** @var RouterInterface $router */ $router = $this->get('router'); return $response->withStatus(302)->withAddedHeader("Location", $router->pathFor('home')); })->setName('logout'); // Index (redirect/login) $app->get('/', function (Request $request, Response $response) { $isAdmin = Utils::isAdmin(); if ($isAdmin) { /** @var RouterInterface $router */ $router = $this->get('router'); return $response->withStatus(302)->withAddedHeader("Location", $router->pathFor('productList')); } return \name\giacomofurlan\TwigUtil::render($this, $response, "home.twig"); })->setName('home'); // 404, silent redirect (home / products list) $app->getContainer()['notFoundHandler'] = function (Container $container) { return function (Request $request, Response $response) use($container) { $isAdmin = Utils::isAdmin(); /** @var RouterInterface $router */ $router = $container->get('router'); $response = $response->withStatus(302); if ($isAdmin) { return $response->withAddedHeader("Location", $router->pathFor('productList')); } return $response->withAddedHeader("Location", $router->pathFor('home')); }; };
/** * Handle the edit requests * GET: the edit form * POST: the result view * * @param Request $request * @param Response $response * @param integer $productId * @return Response */ protected function handleEdit(Request $request, Response $response, $productId) { $logger = LoggerSingleton::getInstance()->getLogger(); switch ($request->getMethod()) { case 'GET': $model = new ProductCreationModel(); $product = null; try { $product = $this->service->read($productId); $model->setProduct($product); } catch (NotFoundException $ex) { $logger->notice(__METHOD__ . " (" . __LINE__ . "): " . $ex->getMessage()); return $response->withStatus(302)->withAddedHeader("Location", $this->container->get('router')->pathFor('productList')); } return TwigUtil::render($this->container, $response, 'product/edit_form.twig', $model->jsonSerialize()); case 'POST': $product = $this->productFromCreateEditForm($request); $files = $request->getUploadedFiles(); /** @var UploadedFile $file */ $file = null; if (array_key_exists('image', $files)) { $file = $files['image']; } $model = new ProductCreationModel(); $this->validateProduct($product, $file, $model); if (!$model->isSuccess()) { $model->setProduct($product); return TwigUtil::render($this->container, $response, 'product/edit_form.twig', $model->jsonSerialize()); } $model->setIsSuccess($this->service->update($product)); // default: success $template = 'product/edit_success.twig'; if ($model->isSuccess()) { try { // delete previous image if ($file->getSize()) { foreach (glob(getenv('PRODUCT_IMAGES_DIRECTORY') . '*') as $entry) { if (preg_match("/.*\\/{$productId}\\.[a-z]+\$/", $entry)) { unlink($entry); } } } Utils::writeUploadedFileToPath($file, getenv(PRODUCT_IMAGES_DIRECTORY), $product->getId()); } catch (\RuntimeException $ex) { $model->setIsSuccess(false); $message = "Can't write image file to the upload directory"; $model->appendMessage("{$message}. Please retry again later."); $logger->error(__FILE__ . "(" . __LINE__ . "): {$message} :: " . $ex->getMessage()); $template = 'product/edit_form.twig'; } } else { // error: back to the form $template = 'product/edit_form.twig'; } return TwigUtil::render($this->container, $response, $template, $model->jsonSerialize()); $model->setIsSuccess($this->service->create($product)); return $response->withStatus(501); // Not implemented yet // Not implemented yet default: return $response->withStatus(404); // Should never happen } }
public static function render(Container $container, ResponseInterface $response, $template, array $context = []) { $defaultCommonContext = ['user' => Utils::isAdmin() ? 'admin' : 'n00b']; $context = array_merge($defaultCommonContext, $context); return $container['view']->render($response, $template, $context); }