Output.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Core\Template;
  3. use Mustache_Engine as Mustache_Engine;
  4. use Mustache_Loader_FilesystemLoader as Mustache_Loader_FilesystemLoader;
  5. class Output
  6. {
  7. private static $_instance;
  8. private $_template = '';
  9. private $_extension = '';
  10. private $_engine;
  11. private function __construct()
  12. {
  13. $this->_template = (defined('TEMPLATE_DEFAULT')) ? TEMPLATE_DEFAULT : 'DefaultTemplate';
  14. $this->_extension = (defined('TEMPLATE_FILETYPE')) ? TEMPLATE_FILETYPE : '.mustache';
  15. $this->_engine = new Mustache_Engine(array(
  16. 'entity_flags' => ENT_QUOTES,
  17. 'partials_loader' => new Mustache_Loader_FilesystemLoader(DIR_APP),
  18. 'pragmas' => [Mustache_Engine::PRAGMA_FILTERS],
  19. ));
  20. // ========================
  21. // Helpers
  22. //
  23. // Format {{#icons}}xx{{/icons}} as a font awesome icon
  24. $this->_engine->addHelper('icons', function ($icon) {
  25. return "<i class='fa fa-" . trim($icon) . "' aria-hidden='true'></i>";
  26. });
  27. // Change the {{#strings}}string,module{{/strings}} for the requested localized string
  28. $this->_engine->addHelper('string', function ($string) {
  29. return \Lang::getString(...explode(',', trim($string)));
  30. });
  31. // ========================
  32. // Filters
  33. //
  34. $this->_engine->addHelper('case', [
  35. 'lower' => function ($value) {
  36. return strtolower((string) $value);
  37. },
  38. 'upper' => function ($value) {
  39. return strtoupper((string) $value);
  40. }
  41. ]);
  42. }
  43. private static function newObj()
  44. {
  45. if (!isset(self::$_instance)) {
  46. self::$_instance = new Output();
  47. }
  48. return self::$_instance;
  49. }
  50. public static function getInstance()
  51. {
  52. if (!isset(self::$_instance)) {
  53. return self::newObj();
  54. }
  55. return self::$_instance;
  56. }
  57. //
  58. // ----------------------------------------
  59. // Done with singletone and template boot
  60. //
  61. //
  62. public static function setTemplate($template = 'DefaultTemplate')
  63. {
  64. self::getInstance()->_template = $template;
  65. return self::getInstance();
  66. }
  67. public static function render($view = '', $values = array())
  68. {
  69. $instance = self::getInstance();
  70. $template = '';
  71. if ($view != '') {
  72. $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
  73. array_pop($segments);
  74. $location = implode('/', $segments) . '/views/';
  75. if (is_file($location . $view . $instance->_extension)) {
  76. $template = file_get_contents($location . $view . $instance->_extension);
  77. }
  78. }
  79. if (is_ajax_request()) {
  80. echo $instance->_engine->render($template, $values);
  81. return;
  82. }
  83. if (!is_file(__DIR__ . "/classes/" . strtolower($instance->_template) . "/" . $instance->_template . '.php')) {
  84. $instance->_template = 'DefaultTemplate';
  85. }
  86. include_once __DIR__ . "/classes/" . strtolower($instance->_template) . "/" . $instance->_template . '.php';
  87. $wrapper = new $instance->_template;
  88. $wrapper = $wrapper->render($template);
  89. echo $instance->_engine->render($wrapper, $values);
  90. }
  91. //
  92. // ----------------------------------------
  93. // Menu Stuff Here
  94. //
  95. //
  96. public static function addMenu($route, $name, $icon = '', $attributes = [], $weight = 0){
  97. global $SIDEBAR;
  98. $SIDEBAR->add($route, $name, $icon, $attributes, $weight);
  99. }
  100. public static function addSubmenu($key, $name, $icon = '', $attributes = [], $weight = 0){
  101. global $SIDEBAR;
  102. $SIDEBAR->create_submenu($key, $name, $icon, $attributes , $weight);
  103. }
  104. public static function addOnSubmenu($key, $route, $name, $icon = '', $attributes = [], $weight = 0){
  105. global $SIDEBAR;
  106. $SIDEBAR->add_on_new($key, $route, $name, $icon, $attributes, $weight);
  107. }
  108. public static function headAdd($route, $name){
  109. global $DROPDOWN;
  110. $DROPDOWN[] = ['route' => $route, 'name' => $name];
  111. }
  112. public static function addSubHeader($key, $name, $icon = '', $attributes = [], $weight = 0){
  113. global $DROPDOWN;
  114. $DROPDOWN->create_submenu($key, $name, $icon, $attributes , $weight);
  115. }
  116. public static function addOnSubHeader($key, $route, $name, $icon = '', $attributes = [], $weight = 0){
  117. global $DROPDOWN;
  118. $DROPDOWN->add_on_new($key, $route, $name, $icon, $attributes, $weight);
  119. }
  120. }