app_view.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class View.
  5. *
  6. * @deprecated use Template class
  7. */
  8. class View
  9. {
  10. private $data;
  11. private $template;
  12. private $layout;
  13. private $tool_path;
  14. /**
  15. * Constructor, init tool path for rendering.
  16. *
  17. * @deprecated
  18. *
  19. * @param string $toolname tool name (optional)
  20. * @param string $template_path
  21. */
  22. public function __construct($toolname = '', $template_path = null)
  23. {
  24. if (!empty($toolname)) {
  25. if (isset($template_path)) {
  26. $path = $template_path.$toolname.'/';
  27. } else {
  28. $path = api_get_path(SYS_CODE_PATH).$toolname.'/';
  29. }
  30. if (is_dir($path)) {
  31. $this->tool_path = $path;
  32. } else {
  33. throw new Exception('View::__construct() $path directory does not exist '.$path);
  34. }
  35. }
  36. }
  37. /**
  38. * Set data sent from a controller.
  39. *
  40. * @param array data
  41. */
  42. public function set_data($data)
  43. {
  44. if (!is_array($data)) {
  45. throw new Exception('View::set_data() $data must to be an array, you have sent a'.gettype($data));
  46. }
  47. $this->data = $data;
  48. }
  49. /**
  50. * Set layout view sent from a controller.
  51. *
  52. * @param string $layout view
  53. */
  54. public function set_layout($layout)
  55. {
  56. $this->layout = $layout;
  57. }
  58. /**
  59. * Set template view sent from a controller.
  60. *
  61. * @param string $template view
  62. */
  63. public function set_template($template)
  64. {
  65. $this->template = $template;
  66. }
  67. /**
  68. * Render data to the template and layout views.
  69. */
  70. public function render()
  71. {
  72. $content = $this->render_template();
  73. $target = $this->tool_path.$this->layout.'.php';
  74. if (file_exists($target)) {
  75. require_once $target;
  76. } else {
  77. throw new Exception('View::render() invalid file path '.$target);
  78. }
  79. }
  80. /**
  81. * It's used into render method for rendering data in the template and layout views.
  82. *
  83. * @return string Rendered template (as HTML, most of the time)
  84. */
  85. private function render_template()
  86. {
  87. $target = $this->tool_path.$this->template.'.php';
  88. if (file_exists($target)) {
  89. ob_start();
  90. @extract($this->data, EXTR_OVERWRITE); //pass the $this->data array into local scope
  91. require_once $target;
  92. $content = ob_get_clean();
  93. return $content;
  94. } else {
  95. throw new Exception('View::render_template() invalid file path '.$target);
  96. }
  97. }
  98. }