app_view.php 2.8 KB

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