AjaxSubmitPage.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. class AjaxSubmitPage implements IPage
  3. {
  4. public static function getInstance()
  5. {
  6. return new AjaxSubmitPage(new Rest(), new Header(), BusinessLogicProcessor::getInstance(), MySqliWrapper::getInstance(), new TwigWrapper());
  7. }
  8. private $rest;
  9. private $header;
  10. private $businessLogicProcessor;
  11. private $mysqli;
  12. private $twig;
  13. private function __construct(Rest $rest, Header $header, BusinessLogicProcessor $businessLogicProcessor, MySqliWrapper $mysqli, TwigWrapper $twig)
  14. {
  15. $this->rest = $rest;
  16. $this->header = $header;
  17. $this->businessLogicProcessor = $businessLogicProcessor;
  18. $this->mysqli = $mysqli;
  19. $this->twig = $twig;
  20. }
  21. public function process()
  22. {
  23. try
  24. {
  25. $id = $this->rest->get('id');
  26. }
  27. catch (RestException $e)
  28. {
  29. return json_encode(array("error" => "Page ID not specified"));
  30. }
  31. $result = $this->businessLogicProcessor->processAjaxSubmit($id);
  32. if(isset($result['error']))
  33. {
  34. $_SESSION['error'] = $result;
  35. }
  36. $returnData = $result;
  37. $returnData['html'] = $this->buildPage($result['next']);
  38. return json_encode($returnData);
  39. }
  40. public function buildPage($id)
  41. {
  42. $sql = sprintf("SELECT *
  43. FROM page_settings
  44. WHERE page_id = %s", $id);
  45. $pageSettings = $this->mysqli->get($sql)[0];
  46. if(!$pageSettings)
  47. {
  48. throw new PageDataNotValidException("Page data invalid");
  49. }
  50. $path = $pageSettings['template_path'];
  51. $filename = $pageSettings['template_filename'];
  52. $options['error'] = isset($_SESSION['error']) ? $_SESSION['error'] : "";
  53. unset($_SESSION['error']);
  54. $options['session'] = $_SESSION;
  55. $options['id'] = $id;
  56. $options['image_domain'] = "//" . IMAGE_DOMAIN;
  57. $options['image_path'] = $options['image_domain'] . "/" . $path;
  58. $this->header->jsonContent();
  59. return $this->twig->render($path . "/" . $filename, $options);
  60. }
  61. }