Output.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. class Output {
  3. private static $_instance;
  4. private $_doctype = "<!DOCTYPE html>";
  5. private $_head_items = Array();
  6. private $_body = "";
  7. private $_content = "";
  8. private $_foot_items = Array();
  9. private $view = false;
  10. //SINGLETON==============================================
  11. private function __construct() {
  12. }
  13. private static function newObj() {
  14. if (!isset(self::$_instance)) {
  15. self::$_instance = new Output();
  16. }
  17. return self::$_instance;
  18. }
  19. public function getInstance() {
  20. if (!isset(self::$_instance)) {
  21. return self::newObj();
  22. }
  23. return self::$_instance;
  24. }
  25. public static function load_view($name, $values = null, $location = null) {
  26. if (!$location) {
  27. $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
  28. array_pop($segments);
  29. $location = implode('/', $segments) . '/views/';
  30. }
  31. if (is_array($values) && count($values) > 0) {
  32. extract($values, EXTR_PREFIX_SAME, 'data');
  33. }
  34. $location .= $name;
  35. if (strpos('.php', $location) == 0) {
  36. $location .= '.php';
  37. }
  38. if (!file_exists($location)) {
  39. //return require_once GENERAL_VIEW_FOLDER . 'errors/404.php';
  40. }
  41. return require_once( $location );
  42. }
  43. private function render_view() {
  44. if (is_array($this->view['values']) && count($this->view['values']) > 0) {
  45. extract($this->view['values'], EXTR_PREFIX_SAME, 'data');
  46. }
  47. return require_once( $this->view['location'] );
  48. }
  49. public function set_view($name, $values = null, $location = null) {
  50. if (!$location) {
  51. $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
  52. array_pop($segments);
  53. $location = implode('/', $segments) . '/views/';
  54. }
  55. $location .= $name;
  56. if (strpos('.php', $location) == 0) {
  57. $location .= '.php';
  58. }
  59. $this->view = Array();
  60. $this->view['location'] = $location;
  61. $this->view['values'] = $values;
  62. }
  63. public function render() {
  64. if (is_ajax_request()) {
  65. echo $this->_content;
  66. if ($this->view) {
  67. $this->render_view();
  68. }
  69. return;
  70. }
  71. global $DROPDOWN, $SIDEBAR;
  72. $this->_head_items[] = '<script src="/plugins/jquery/jquery.min.js"></script>';
  73. $this->_head_items[] = '<script src="/plugins/mousetrap/mousetrap.min.js"></script>';
  74. $this->_head_items[] = '<script src="/plugins/jquery-mask-plugin/jquery.mask.min.js"></script>';
  75. $this->_head_items[] = '<link href="/plugins/bootstrap/bootstrap.min.css" rel="stylesheet" />';
  76. $this->_head_items[] = '<script src="/plugins/bootstrap/bootstrap.min.js"></script>';
  77. $this->_head_items[] = '<link href="/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />';
  78. $this->_head_items[] = '<link href="/plugins/normalize-css/normalize.css" rel="stylesheet">';
  79. $this->_head_items[] = '<link href="/dist/style.css" rel="stylesheet">';
  80. $this->_head_items[] = '<link href="/dist/theme/black_and_white.css" rel="stylesheet">';
  81. $this->_head_items[] = '<link href="/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">';
  82. $this->_head_items[] = '<script src="/plugins/metisMenu/metisMenu.min.js"></script>';
  83. $this->_head_items[] = '<link href="/plugins/select2/select2.min.css" rel="stylesheet">';
  84. $this->_head_items[] = '<script src="/plugins/select2/select2.min.js"></script>';
  85. $this->_head_items[] = '<link rel="stylesheet" href="/plugins/datatables/dataTables.css">';
  86. $this->_head_items[] = '<script src="/plugins/datatables/dataTables.js"></script>';
  87. $this->_foot_items[] = '<script src="/dist/script.js"></script>';
  88. $head = "<head>";
  89. foreach ($this->_head_items as $head_item) {
  90. $head .= $head_item . "\n";
  91. }
  92. $head .= "</head>";
  93. $foot = "";
  94. foreach ($this->_foot_items as $foot_item) {
  95. $foot .= $foot_item;
  96. }
  97. $header = str_replace("{{usermenu}}", "", file_get_contents(__DIR__ . '/views/header.html'));
  98. $sidebar = str_replace("{{elements}}", $SIDEBAR->render(), file_get_contents(__DIR__ . '/views/sidebar.html'));
  99. $body_first = file_get_contents(__DIR__ . "/views/body.html");
  100. $body_first = str_replace("{{header}}", $header, $body_first);
  101. $body_first = str_replace("{{sidebar}}", $sidebar, $body_first);
  102. $body_first .= $this->_content;
  103. $foot .= '<div id="modal" class="modal fade" role="dialog"> </div>';
  104. $body_end = file_get_contents(__DIR__ . "/views/body_section.html");
  105. $body_end = str_replace("{{footer}}", $foot, $body_end);
  106. echo $this->_doctype . "\n";
  107. echo "<html> \n";
  108. echo $head;
  109. echo $body_first;
  110. if ($this->view) {
  111. $this->render_view();
  112. }
  113. echo $body_end;
  114. echo "</html>";
  115. }
  116. public function set_content($content) {
  117. $this->_content = $content;
  118. }
  119. }