base.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Template;
  9. class Base {
  10. private $template; // The template
  11. private $vars; // Vars
  12. private $l10n; // The l10n-Object
  13. private $theme; // theme defaults
  14. /**
  15. * @param string $template
  16. * @param \OC_L10N $l10n
  17. * @param \OC_Defaults $theme
  18. */
  19. public function __construct( $template, $requesttoken, $l10n, $theme ) {
  20. $this->vars = array();
  21. $this->vars['requesttoken'] = $requesttoken;
  22. $this->l10n = $l10n;
  23. $this->template = $template;
  24. $this->theme = $theme;
  25. }
  26. /**
  27. * @param string $serverroot
  28. * @param string|false $app_dir
  29. * @param string $theme
  30. * @param string $app
  31. */
  32. protected function getAppTemplateDirs($theme, $app, $serverroot, $app_dir) {
  33. // Check if the app is in the app folder or in the root
  34. if( file_exists($app_dir.'/templates/' )) {
  35. return array(
  36. $serverroot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
  37. $app_dir.'/templates/',
  38. );
  39. }
  40. return array(
  41. $serverroot.'/themes/'.$theme.'/'.$app.'/templates/',
  42. $serverroot.'/'.$app.'/templates/',
  43. );
  44. }
  45. /**
  46. * @param string $serverroot
  47. * @param string $theme
  48. */
  49. protected function getCoreTemplateDirs($theme, $serverroot) {
  50. return array(
  51. $serverroot.'/themes/'.$theme.'/core/templates/',
  52. $serverroot.'/core/templates/',
  53. );
  54. }
  55. /**
  56. * Assign variables
  57. * @param string $key key
  58. * @param array|bool|integer|string $value value
  59. * @return bool
  60. *
  61. * This function assigns a variable. It can be accessed via $_[$key] in
  62. * the template.
  63. *
  64. * If the key existed before, it will be overwritten
  65. */
  66. public function assign( $key, $value) {
  67. $this->vars[$key] = $value;
  68. return true;
  69. }
  70. /**
  71. * Appends a variable
  72. * @param string $key key
  73. * @param mixed $value value
  74. * @return boolean|null
  75. *
  76. * This function assigns a variable in an array context. If the key already
  77. * exists, the value will be appended. It can be accessed via
  78. * $_[$key][$position] in the template.
  79. */
  80. public function append( $key, $value ) {
  81. if( array_key_exists( $key, $this->vars )) {
  82. $this->vars[$key][] = $value;
  83. }
  84. else{
  85. $this->vars[$key] = array( $value );
  86. }
  87. }
  88. /**
  89. * Prints the proceeded template
  90. * @return bool
  91. *
  92. * This function proceeds the template and prints its output.
  93. */
  94. public function printPage() {
  95. $data = $this->fetchPage();
  96. if( $data === false ) {
  97. return false;
  98. }
  99. else{
  100. print $data;
  101. return true;
  102. }
  103. }
  104. /**
  105. * Process the template
  106. * @return string
  107. *
  108. * This function processes the template.
  109. */
  110. public function fetchPage() {
  111. return $this->load($this->template);
  112. }
  113. /**
  114. * doing the actual work
  115. * @param string $file
  116. * @return string content
  117. *
  118. * Includes the template file, fetches its output
  119. */
  120. protected function load( $file, $additionalparams = null ) {
  121. // Register the variables
  122. $_ = $this->vars;
  123. $l = $this->l10n;
  124. $theme = $this->theme;
  125. if( !is_null($additionalparams)) {
  126. $_ = array_merge( $additionalparams, $this->vars );
  127. }
  128. // Include
  129. ob_start();
  130. include $file;
  131. $data = ob_get_contents();
  132. @ob_end_clean();
  133. // Return data
  134. return $data;
  135. }
  136. }