DevtoolPages.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. class DevtoolPages
  3. {
  4. public static function getInstance()
  5. {
  6. return new DevtoolPages();
  7. }
  8. private $twig;
  9. private $mysqli;
  10. private function __construct()
  11. {
  12. $this->twig = new TwigWrapper();
  13. $this->mysqli = MySqliWrapper::getInstance();
  14. }
  15. public function process()
  16. {
  17. $command = isset($_GET['command']) ? $_GET['command'] : null;
  18. switch ($command)
  19. {
  20. case "save":
  21. $this->save();
  22. case "edit":
  23. return $this->edit();
  24. case "add":
  25. $this->add();
  26. case "delete":
  27. $this->delete();
  28. default:
  29. return $this->displayList();
  30. }
  31. }
  32. private function displayList()
  33. {
  34. $sql = "SELECT *
  35. FROM page_settings ORDER BY page_id ASC";
  36. $pageInfo = $this->mysqli->get($sql);
  37. $options = array("pages" => $pageInfo, "title" => "Pages");
  38. return $this->twig->render("devtool_pages.html", $options);
  39. }
  40. private function add()
  41. {
  42. $templatePath = $_POST['template_path'];
  43. $templateFilename = $_POST['template_filename'];
  44. $sql = sprintf("INSERT INTO page_settings
  45. (`template_path`, `template_filename`) VALUES
  46. ('%s', '%s')", $templatePath, $templateFilename, $pageId);
  47. $pageInfo = $this->mysqli->set($sql, null);
  48. header("Location: /editor.php?a=pages");
  49. }
  50. private function delete()
  51. {
  52. $pageId = isset($_GET['id']) ? $_GET['id'] : 0;
  53. $sql = sprintf("DELETE
  54. FROM page_settings
  55. WHERE page_id = %s", $pageId);
  56. $pageInfo = $this->mysqli->delete($sql, null);
  57. header("Location: /editor.php?a=pages");
  58. }
  59. private function save()
  60. {
  61. $pageId = isset($_GET['id']) ? $_GET['id'] : 0;
  62. $templatePath = $_POST['template_path'];
  63. $templateFilename = $_POST['template_filename'];
  64. $sql = sprintf("UPDATE page_settings
  65. SET template_path='%s',
  66. template_filename='%s'
  67. WHERE page_id = %s", $templatePath, $templateFilename, $pageId);
  68. $pageInfo = $this->mysqli->set($sql, null);
  69. echo "saved";
  70. }
  71. private function edit()
  72. {
  73. $pageId = isset($_GET['id']) ? $_GET['id'] : 0;
  74. $sql = sprintf("SELECT *
  75. FROM page_settings
  76. WHERE page_id = %s", $pageId);
  77. $pageInfo = $this->mysqli->get($sql);
  78. $options = array("pageData" => $pageInfo[0], "title" => "Devtool Page Editor #" . $pageId);
  79. return $this->twig->render("devtool_pageedit.html", $options);
  80. }
  81. }