* some long waiting minor stuff and improving count on many2many table joins - still...
[vsc.git] / _res / _libs / tscontrollerhtml.class.php
blob7e696f60b8df41b64078014d664d9b51b33d9303
1 <?php
2 /**
3 * @name tsController
4 * @package html Output class
5 * @desc the God class. It does everything, from database initialization
6 * to error reporting and page buffering. <- yeah it might be stupid
7 *
8 * @public the smarty template class instance and the theme path
9 * @private the page identification
12 class tsControllerHtml extends tsController {
13 public $smarty,
14 $varArray = array(),
15 $themePath,
16 $db;
18 protected $content;
20 private $to,
21 $output,
22 $errors;
24 /**
25 * the constructor that does it all.
26 * starts output buffering, and initializes different things
29 public function __construct(){
30 $prevOutput = ob_get_clean(); // errors previous to the tsController init
32 // $this->output = $this->setOutput();
33 // Override standard string functions
34 if (extension_loaded('mbstring')) {
35 ini_set('mbstring.func_overload', 7);
37 $this->smarty = new Smarty();
39 $this->smarty->compile_check = true;
40 // $this->smarty->debugging = C_SYSTEM_DEBUG;
41 $this->smarty->compile_dir = S_C_TEMPL_DIR;
42 // set_error_handler(array($this, 'triggerError'));
43 // commented in the light of the xdebug profiling -
44 // dropped loading times by .01 seconds
45 if (!empty($prevOutput)) {
46 $this->errors = "\n".'<!-- output previous init at '.date('G:i:s').' -->'."\n".$prevOutput."\n";
47 } else {
48 $this->errors = '';
50 ob_start();
52 $this->setTheme();
54 session_start();
55 $this->connectDb();
58 // function __destruct(){
59 // $this->db->close();
60 // }
62 protected function connectDb () {
63 $this->db = sqlFactory::connect (DB_TYPE);
65 if (defined ('DB_NAME') && DB_NAME && !empty($this->db->link)) {
66 $this->db->selectDatabase(DB_NAME);
67 return true;
70 return false;
73 public function setTheme (){
74 $this->getTheme();
76 $this->themePath = THEME_PATH . $this->theme;
77 if (!is_dir($this->themePath)) {
78 $this->theme = DEFAULT_THEME;
79 $this->themePath = THEME_PATH . $this->theme;
82 $this->setThemeCookie();
85 private function setThemeCookie () {
86 if (empty ($_COOKIE['theme']) || $_COOKIE['theme'] != $this->theme) {
87 $test = setcookie ('theme', $this->theme, time()+1296000, '/');
91 private function reportErrors (){
92 if (!stristr (C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR'])) {
93 return false;
95 if (!empty ($this->errors)) {
96 $errors = $this->errors;
97 $this->errors = '<!-- ERRORS: '.get_class($this).' at '.date('G:i:s').'-->'."\n".$errors;
100 if (defined ('ERROR_LEVEL') && ERROR_LEVEL == 9 )
101 $this->errors .= "\n".var_export (debug_backtrace(), true);
102 switch (C_SYSTEM_DEBUG_METHOD){
103 case 0: // output
104 $this->smarty->assign('errors', $this->errors);
105 return true;
106 break;
107 case 1: // email
108 $mailMsg = $this->errors;
109 // mail (DEBUG_MAIL,'DEBUG:',$mailMsg);
110 return true;
111 // break;
113 return false;
116 public function triggerError($errNo=0, $errStr='', $errFile='', $errLine=''){
117 if (empty($this->errors)){
118 $this->errors = "\n";
119 // if (is_a($this->smarty,'Smarty'))
120 // $this->smarty->assign('errors', var_export($this->errors));
122 switch ($errNo){
123 case (2):
124 $errType = 'WARNING';
125 break;
126 case (8):
127 $errType = 'NOTICE';
128 break;
129 case (256): // errNo = 4
130 $errType = 'USER_ERROR';
131 break;
132 case (512): // errNo = 8
133 $errType = 'USER_WARNING';
134 break;
135 case (1024): // errNo = 16
136 $errType = 'USER_NOTICE';
137 break;
138 case (2048): // errNo = 16
139 $errType = 'USER_ERROR';
140 break;
141 // case (4096): // errNo = 16
142 // $errType = '?';
143 // break;
144 default:
145 var_dump($errNo);
147 if ($errNo < 255) {
148 $baseColor = array('R'=>100,'G'=>16,'B'=>16);
149 $color = (string)dechex($baseColor['R']+40 * (log($errNo, 2))).(string)dechex($baseColor['G']+(20 - 4*log($errNo, 2))*(log($errNo, 2))).(string)dechex($baseColor['B']+(20 - 4*log($errNo, 2))*(log($errNo, 2)));
150 } else {
151 $baseColor = array('R'=>16,'G'=>16,'B'=>100);
152 $errNo = $errNo / 128;
153 $color = (string)dechex($baseColor['R']+(20 - 4*log($errNo, 2)) * (log($errNo, 2))).(string)dechex($baseColor['G']+(20 - 4*log($errNo, 2))*(log($errNo, 2))).(string)dechex($baseColor['B']+40*(log($errNo, 2)));
156 $this->errors .= '<span style="color:#'.strtoupper($color).'">';
157 $this->errors .= '<b>'.$errType.'</b> '.$errStr;
158 if (false || tsController::getRequest('bt') == 'full') {
159 $this->errors .= ' at line '.$errLine.' in file '.$errFile;
160 $t = debug_backtrace();
161 // $this->errors .= '<pre>'.var_export($t,true).'</pre>';
164 $this->errors .= ' </span><br/>'."\n";
166 return;
168 public function dispatch (){
169 $this->db->close();
170 $this->setTheme();
172 $this->smarty->assign('header', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'header.tpl');
173 $this->smarty->assign('footer', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'footer.tpl');
175 $this->smarty->assign('url', URL);
176 $this->smarty->assign('style', '');
177 $this->smarty->assign('contact', tsController::setRequest('contact'));
179 $className = get_class ($this);
180 $methArr = get_class_methods ($this);
181 // echo PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl';
182 if (is_file (PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl')) {
183 $this->smarty->assign ('contentFile', PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl');
184 } else {
185 $this->smarty->assign ('contentFile', $this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'tpl404.tpl');
188 /* Loading of any varArray template variables we have in the derived classes
189 --------------------------------------------------------------------- */
190 if (is_array($this->varArray)) {
191 $this->smarty->assign ($this->varArray);
194 // if(!empty($GLOBALS['errors'])) {
195 // $GLOBALS['errors'] = '.';
196 // }
198 if (!C_SYSTEM_DEBUG) {
199 ob_end_clean();
200 ob_start();
201 } else {
202 $this->errors .= ob_get_clean();
203 ob_start(); // clean output - heheh, yeah right :P
204 $this->reportErrors ();
207 // $this->smarty->assign ('time', true);
208 $this->content = $this->smarty->fetch ($this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'main.tpl');
210 $memcache = tsController::memcacheEnabled ();
211 if ($memcache instanceof Memcache) {
212 $memcache->connect('localhost', 11211);
213 $memcache->add (tsController::getHash () , $this, MEMCACHE_COMPRESSED);
216 $this->postDispatch ($this->content);
220 private function postDispatch ($incString){
221 // later on we will do some string manipulation to have a bit _more_ control over the smarty output
222 // (for example smarty->fetch gives a lot of notices - bad templates) - maybe we want to catch them
223 // TODO LOSE SMARTY !! - php is enough for templating ... or so I heard ;)
224 // check to see if we're a 404 error
225 if (get_class ($this) == 'tsControllerHtml') {// yes 404
226 header ("HTTP/1.0 404 Not Found");
229 if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG) {
230 header('Content-type: application/xhtml+xml');
231 } else {
232 header('Content-type: text/html');
235 echo str_replace (
236 array ('%TIME%','%QUERIES%', '%MEMUSED%'),
237 array (
238 number_format(microtime (true) - $GLOBALS['st'] , 5, ',', '.'),
239 $GLOBALS['qCnt'],
240 number_format(memory_get_usage()/1024, 3, ',', '.')
242 $incString