1
<?php
2
/*
3
  Belokan Micro Framework
4
  Licensed under GNU GPL
5
  http://gitorious.org/belokan
6
  ---
7
  Belokan/View.php:
8
  View system
9
*/
10
11
class Belokan_View {
12
  
13
  private
14
    $_plugins,
15
    $_data,
16
    $_logBelokan,
17
    $_log,
18
    $_viewDir,
19
    $_layoutDir,
20
    $_format;
21
  
22
  public function __construct ()
23
  {
24
    $this->_plugins = null;
25
    $this->_data = array();
26
    $config = Belokan_Config::getInstance();
27
    $this->_logBelokan = $config->get('log', 'belokan');
28
    $this->_log = Belokan_Log::getInstance();
29
    $this->_viewsDir = $config->get('dir', 'views');
30
    $this->_layoutDir = $this->_viewsDir.'_layouts/';
31
    $this->_format = Belokan_Request::getInstance()->getFormat();
32
    $this->_intl = $config->get('intl', 'enabled');
33
  }
34
  
35
  public function __get ($var)
36
  {
37
    if (isset($this->_data[$var])) return $this->_data[$var];
38
    return null;
39
  }
40
  
41
  public function __set ($var, $value)
42
  {
43
    return $this->_data[$var] = $value;
44
  }
45
  
46
  public function __isset ($var)
47
  {
48
    return isset($this->_data[$var]);
49
  }
50
  
51
  public function __unset ($var)
52
  {
53
    unset($this->_data[$var]);
54
  }
55
  
56
  public function __call ($plugin_name, $args)
57
  {
58
    if ($this->_plugins == null)
59
      $this->_plugins = new Belokan_Plugins;
60
    if (!$this->_plugins->isLoaded($plugin_name)) {
61
      if ($this->_plugins->load($plugin_name) === false) {
62
        if ($this->_logBelokan)
63
          $this->_log->add('Belokan', 'ERROR: Failed to load plugin ('.$plugin_name.').');
64
        return null;
65
      }
66
    }
67
    $plugin = $this->_plugins->get($plugin_name);
68
    $defaultMethod = $plugin_name;
69
    if (method_exists($plugin, $defaultMethod))
70
      return call_user_func_array(array($plugin, $defaultMethod), $args);
71
    return $plugin;
72
  }
73
74
  public function renderLayoutBefore ($layout)
75
  {
76
    if ($layout == null) return;
77
    $layoutBefore = $this->_layoutDir.$layout.'/before.p'.$this->_format;
78
    $layoutBeforeLocal = $layoutBefore.'.'.Belokan_Config::getInstance()->get('intl', 'lang');
79
    $layoutBeforeDefault = $layoutBefore.'.'.Belokan_Config::getInstance()->get('intl', 'default');
80
    if ($this->_intl && file_exists($layoutBeforeLocal))
81
      include $layoutBeforeLocal;
82
    else if (file_exists($layoutBeforeDefault))
83
      include $layoutBeforeDefault;
84
    else if (file_exists($layoutBefore))
85
      include $layoutBefore;
86
    else if ($this->_logBelokan)
87
      $this->_log->add('Belokan', 'ERROR: Layout before file ('.$layoutBefore.') not found.');
88
  }
89
90
  public function renderView ($view) {
91
    $viewFile = $this->_viewsDir.$view.'.p'.$this->_format;
92
    $viewFileLocal = $viewFile.'.'.Belokan_Config::getInstance()->get('intl', 'lang');
93
    $viewFileDefault = $viewFile.'.'.Belokan_Config::getInstance()->get('intl', 'default');
94
    if ($this->_intl && file_exists($viewFileLocal))
95
      include $viewFileLocal;
96
    else if (file_exists($viewFileDefault))
97
      include $viewFileDefault;
98
    else if (file_exists($viewFile))
99
      include $viewFile;
100
    else if ($this->_logBelokan)
101
      $this->_log->add('Belokan', 'ERROR: View file ('.$viewFile.') not found.');
102
  }
103
104
  public function renderLayoutAfter ($layout) {
105
    if ($layout == null) return;
106
    $layoutAfter = $this->_layoutDir.$layout.'/after.p'.$this->_format;
107
    $layoutAfterLocal = $layoutAfter.'.'.Belokan_Config::getInstance()->get('intl', 'lang');
108
    $layoutAfterDefault = $layoutAfter.'.'.Belokan_Config::getInstance()->get('intl', 'default');
109
    if ($this->_intl && file_exists($layoutAfterLocal))
110
      include $layoutAfterLocal;
111
    else if (file_exists($layoutAfterDefault))
112
      include $layoutAfterDefault;
113
    else if (file_exists($layoutAfter))
114
      include $layoutAfter;
115
    else if ($this->_logBelokan)
116
      $this->_log->add('Belokan', 'ERROR: Layout after file ('.$layoutAfter.') not found.');
117
  }
118
  
119
  public function loadPlugin ($plugin_name, $plugin=null, $plugin_file=null)
120
  {
121
    if ($this->_plugins == null)
122
      $this->_plugins = new Belokan_Plugins;
123
    return $this->_plugins->load($plugin_name, $plugin, $plugin_file);
124
  }
125
  
126
  public function getPlugin ($plugin_name)
127
  {
128
    return $this->_plugins->get($plugin_name);
129
  }
130
  
131
  public function _ ($plugin_name)
132
  {
133
    return $this->_plugins->get($plugin_name);
134
  }
135
136
  public function setFormat ($format)
137
  {
138
    $this->_format = $format;
139
  }
140
  
141
}