GenericPage.inc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. class GenericPage implements IPage {
  3. public static function getInstance() {
  4. return new GenericPage(new Rest(), MySqliWrapper::getInstance(), new TwigWrapper(), BusinessLogicProcessor::getInstance());
  5. }
  6. private $rest;
  7. private $mysqli;
  8. private $twig;
  9. private $businessLogicProcessor;
  10. private function __construct(Rest $rest, MySqliWrapper $mysqli, TwigWrapper $twig, BusinessLogicProcessor $businessLogicProcessor) {
  11. $this->rest = $rest;
  12. $this->mysqli = $mysqli;
  13. $this->twig = $twig;
  14. $this->businessLogicProcessor = $businessLogicProcessor;
  15. }
  16. public function process() {
  17. $id = $this->rest->getDefaulted('id', 1);
  18. $sql = sprintf("SELECT *
  19. FROM page_settings
  20. WHERE page_id = %s", $id);
  21. $pageSettings = $this->mysqli->get($sql)[0];
  22. if(!$pageSettings) {
  23. throw new PageDataNotValidException("Page data invalid");
  24. }
  25. $path = $pageSettings['template_path'];
  26. $filename = $pageSettings['template_filename'];
  27. $options = $this->businessLogicProcessor->processLoad($id);
  28. $options['error'] = isset($_SESSION['error']) ? $_SESSION['error'] : "";
  29. unset($_SESSION['error']);
  30. $options['session'] = $_SESSION;
  31. $options['id'] = $id;
  32. $options['image_domain'] = "//" . IMAGE_DOMAIN;
  33. $options['image_path'] = $options['image_domain'] . "/" . $path;
  34. return $this->twig->render($path . "/" . $filename, $options);
  35. }
  36. }
  37. class PageDataNotValidException extends Exception {
  38. }