From e7d0b9fc5bd891329ce214dd57121205febdf9c6 Mon Sep 17 00:00:00 2001 From: habarnam Date: Tue, 19 Aug 2008 16:54:47 +0300 Subject: [PATCH] * modifications for supporting persistent URL params --- _res/_libs/tscontroller.class.php | 78 +++++++++++++++++++++------------ _res/_libs/tscontrollerhtml.class.php | 82 ++++++++++++++++++++--------------- _res/_libs/tspaginator.class.php | 48 ++++++++++---------- 3 files changed, 120 insertions(+), 88 deletions(-) diff --git a/_res/_libs/tscontroller.class.php b/_res/_libs/tscontroller.class.php index 1ac8333..bb0eb99 100644 --- a/_res/_libs/tscontroller.class.php +++ b/_res/_libs/tscontroller.class.php @@ -1,10 +1,11 @@ 0,'end' => 0); - - static private $instance = null; + + static private $instance = null; static protected function memcacheEnabled () { /// this sucks on so many ways @@ -17,31 +18,31 @@ abstract class tsController { } return false; } - -/** + + /** * function to redirect the client * * @param string $url * @param bool $die * @param bool $onlyJScript */ - static public function redirect($url, $die = true, $onlyJScript = false) { + static public function redirect ($url, $die = true, $onlyJScript = false) { if ($onlyJScript == true || headers_sent()) { printf ('Redirect

REDIRECTING - PLEASE CLICK HERE !

', $url, $url, $url); - } else { + } else { ob_end_clean(); header ('Location: '.str_replace ('&', '&', $url)); } - if ($die) + if ($die) die(); } - + // FIXME: I should stop using REQUEST for getting get and post variables !!! static public function getRequest($varName = null) { return tsUrl::getRequest($varName); } - + /** * function to generate URLs compatible with tsController * @@ -50,19 +51,40 @@ abstract class tsController { * @param string $method [get|TODO post] * @return string */ - - static public function setRequest ($whereTo = null, $varVal = null){ + + public function setRequest ($whereTo = null, $varVal = null){ + if (!empty ($this->params)) { + if (!empty ($whereTo)) + array_shift ($this->params); + array_merge ($this->params, $varVal); + } + return tsUrl::setRequest ($whereTo, $varVal); } - + + /** + * function to set the persistent URL parameters + * @param array $incArray + */ + public function setParams ($incArray = array ()) { + if (!empty ($incArray)) + $this->params = array_merge ($this->params, $incArray); + + return false; + } + + public function getParams () { + return $this->params; + } + public function store () { - - } - + + } + /** * static method to return the tsController singleton instance * - * @param string $to + * @param string $to * @return tsController */ static function getInstance ($to) { @@ -76,27 +98,27 @@ abstract class tsController { if ( $memcache) { tsController::$instance = $memcache->get ( tsController::getHash ()); } - + if (!(tsController::$instance instanceof tsController)) { tsController::$instance = new $to (); } } return tsController::$instance; } - + static public function getHash () { return md5($_SERVER['HTTP_HOST'].$_SERVER['QUERY_STRING']); } - + public function getTheme(){ // this should most probably be changed to tsController::getRequest ('theme') - if (!empty($_GET['theme']) && isDebug()) { + if (!empty($_GET['theme']) && isDebug()) { $this->theme = $_GET['theme']; - } elseif (!empty($_POST['theme'])) { + } elseif (!empty($_POST['theme'])) { $this->theme = $_POST['theme']; } elseif (!empty($_COOKIE['theme'])) { $this->theme = $_COOKIE['theme']; - } + } if (empty($this->theme) || !is_dir(THEME_PATH.$this->theme) || !is_dir(THEME_PATH.$this->theme.DIRECTORY_SEPARATOR."_css") || @@ -104,13 +126,13 @@ abstract class tsController { ) { $this->theme = DEFAULT_THEME; } - + return $this->theme; } - + public function __construct() {} - + public function __destruct () {} - + abstract public function dispatch (); } diff --git a/_res/_libs/tscontrollerhtml.class.php b/_res/_libs/tscontrollerhtml.class.php index 7e696f6..f2d312f 100644 --- a/_res/_libs/tscontrollerhtml.class.php +++ b/_res/_libs/tscontrollerhtml.class.php @@ -4,7 +4,7 @@ * @package html Output class * @desc the God class. It does everything, from database initialization * to error reporting and page buffering. <- yeah it might be stupid - * + * * @public the smarty template class instance and the theme path * @private the page identification */ @@ -14,9 +14,9 @@ class tsControllerHtml extends tsController { $varArray = array(), $themePath, $db; - + protected $content; - + private $to, $output, $errors; @@ -27,41 +27,51 @@ class tsControllerHtml extends tsController { * */ public function __construct(){ - $prevOutput = ob_get_clean(); // errors previous to the tsController init - + $prevOutput = ob_get_clean(); // errors previous to the tsController init + // $this->output = $this->setOutput(); // Override standard string functions if (extension_loaded('mbstring')) { ini_set('mbstring.func_overload', 7); } $this->smarty = new Smarty(); - + $this->smarty->compile_check = true; // $this->smarty->debugging = C_SYSTEM_DEBUG; $this->smarty->compile_dir = S_C_TEMPL_DIR; -// set_error_handler(array($this, 'triggerError')); -// commented in the light of the xdebug profiling - +// set_error_handler(array($this, 'triggerError')); +// commented in the light of the xdebug profiling - // dropped loading times by .01 seconds if (!empty($prevOutput)) { $this->errors = "\n".''."\n".$prevOutput."\n"; - } else { + } else { $this->errors = ''; } ob_start(); $this->setTheme(); + // get the persistent url params (NAV_VAR and ACT_VAR) + $to = tsController::getRequest(NAV_VAR); + if (!empty($to)) + $persistent[NAV_VAR] = $to; + + $do = tsController::getRequest(ACT_VAR); + if (!empty($do)) + $persistent[ACT_VAR] = $do; + + $this->setParams($persistent); session_start(); $this->connectDb(); } - + // function __destruct(){ // $this->db->close(); // } - + protected function connectDb () { $this->db = sqlFactory::connect (DB_TYPE); - + if (defined ('DB_NAME') && DB_NAME && !empty($this->db->link)) { $this->db->selectDatabase(DB_NAME); return true; @@ -72,32 +82,32 @@ class tsControllerHtml extends tsController { public function setTheme (){ $this->getTheme(); - + $this->themePath = THEME_PATH . $this->theme; if (!is_dir($this->themePath)) { $this->theme = DEFAULT_THEME; $this->themePath = THEME_PATH . $this->theme; } - + $this->setThemeCookie(); } - + private function setThemeCookie () { if (empty ($_COOKIE['theme']) || $_COOKIE['theme'] != $this->theme) { $test = setcookie ('theme', $this->theme, time()+1296000, '/'); } } - + private function reportErrors (){ if (!stristr (C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR'])) { return false; } if (!empty ($this->errors)) { - $errors = $this->errors; + $errors = $this->errors; $this->errors = ''."\n".$errors; } - - if (defined ('ERROR_LEVEL') && ERROR_LEVEL == 9 ) + + if (defined ('ERROR_LEVEL') && ERROR_LEVEL == 9 ) $this->errors .= "\n".var_export (debug_backtrace(), true); switch (C_SYSTEM_DEBUG_METHOD){ case 0: // output @@ -112,7 +122,7 @@ class tsControllerHtml extends tsController { } return false; } - + public function triggerError($errNo=0, $errStr='', $errFile='', $errLine=''){ if (empty($this->errors)){ $this->errors = "\n"; @@ -152,7 +162,7 @@ class tsControllerHtml extends tsController { $errNo = $errNo / 128; $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))); } - + $this->errors .= ''; $this->errors .= ''.$errType.' '.$errStr; if (false || tsController::getRequest('bt') == 'full') { @@ -160,18 +170,18 @@ class tsControllerHtml extends tsController { $t = debug_backtrace(); // $this->errors .= '
'.var_export($t,true).'
'; } - + $this->errors .= '

'."\n"; - - return; + + return; } public function dispatch (){ $this->db->close(); $this->setTheme(); - + $this->smarty->assign('header', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'header.tpl'); $this->smarty->assign('footer', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'footer.tpl'); - + $this->smarty->assign('url', URL); $this->smarty->assign('style', ''); $this->smarty->assign('contact', tsController::setRequest('contact')); @@ -184,7 +194,7 @@ class tsControllerHtml extends tsController { } else { $this->smarty->assign ('contentFile', $this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'tpl404.tpl'); } - + /* Loading of any varArray template variables we have in the derived classes --------------------------------------------------------------------- */ if (is_array($this->varArray)) { @@ -194,16 +204,16 @@ class tsControllerHtml extends tsController { // if(!empty($GLOBALS['errors'])) { // $GLOBALS['errors'] = '.'; // } - + if (!C_SYSTEM_DEBUG) { ob_end_clean(); ob_start(); } else { $this->errors .= ob_get_clean(); ob_start(); // clean output - heheh, yeah right :P - $this->reportErrors (); + $this->reportErrors (); } - + // $this->smarty->assign ('time', true); $this->content = $this->smarty->fetch ($this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'main.tpl'); @@ -212,10 +222,10 @@ class tsControllerHtml extends tsController { $memcache->connect('localhost', 11211); $memcache->add (tsController::getHash () , $this, MEMCACHE_COMPRESSED); } - + $this->postDispatch ($this->content); } - + private function postDispatch ($incString){ // later on we will do some string manipulation to have a bit _more_ control over the smarty output @@ -225,18 +235,18 @@ class tsControllerHtml extends tsController { if (get_class ($this) == 'tsControllerHtml') {// yes 404 header ("HTTP/1.0 404 Not Found"); } - + if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG) { header('Content-type: application/xhtml+xml'); } else { header('Content-type: text/html'); } - + echo str_replace ( - array ('%TIME%','%QUERIES%', '%MEMUSED%'), + array ('%TIME%','%QUERIES%', '%MEMUSED%'), array ( number_format(microtime (true) - $GLOBALS['st'] , 5, ',', '.'), - $GLOBALS['qCnt'], + $GLOBALS['qCnt'], number_format(memory_get_usage()/1024, 3, ',', '.') ), $incString diff --git a/_res/_libs/tspaginator.class.php b/_res/_libs/tspaginator.class.php index 3e7e7c1..e49e229 100644 --- a/_res/_libs/tspaginator.class.php +++ b/_res/_libs/tspaginator.class.php @@ -1,5 +1,5 @@ getPaginationVariables (); } public function __destruct () {} - + public function setCount ($int) { $this->count = $int; $this->start = $this->getStart(); } - + public function outputOrderHeaders () { - return; + return; } - + public function outputPagination () { $retVal = $left = $right = ''; // this should happen after we made all our stuff with the tdo (joins, wheres, orders..bla) @@ -40,33 +40,33 @@ class tsPaginator { $r = tsController::setRequest($here, array(PAG_VAR => $i)); if ($i != $this->page) $retVal .= ''.$i.' '; - else + else $retVal .= $i.' '; } - + if ($this->page > 1) $left = '« '; - else + else $left = '« '; if ($this->page < $cnt) $right = '»'; - else + else $right = '»'; return $left . $retVal . $right; } - + public function addObject (&$object) { if (!($object instanceof tdoAbstract)) return false; $this->tdo = &$object; - + if (!empty ($this->order)) $this->addOrder (); // if (!empty ($this->start)) $this->addLimit (); } - + protected function getTotal () { // getting the number of elements if (empty ($this->total) && ($this->tdo instanceof tdoAbstract)) { @@ -81,40 +81,40 @@ class tsPaginator { ) { $this->page = 1; } -// $this->start = +// $this->start = // calculate the limit based on $total count of items and the pagination return $this->count * ($this->page-1) /*/+ 1/**/;//$this->start; } - + protected function getPaginationVariables () { // get the page variables: URL?p=? - if (empty($this->count)) + if (empty($this->count)) $this->count = PAG_COUNT; - + $this->page = (int)tsController::getRequest (PAG_VAR); - + $this->start = $this->getStart (); - + // get the ORDER BY variables: URL?o=column_name:(bool)ASC $o = tsController::getRequest (PAG_ORDER); - + if (!empty ($o)) $this->order = explode (':', $o); - + } - + protected function addOrder () { // this is dumb as it requires a column name to comre from user input if ($this->tdo instanceof tdoAbstract && !empty ($this->order)) { $fieldName = $this->order[0]; $order = (bool)$this->order[1]; // if we have no value we treat it as DESC - + // $this->tdo->addOrder ($fieldName, $order); } } - + protected function addLimit () { -// var_dump($this->start, $this->count); +// var_dump($this->start, $this->count); if ($this->tdo instanceof tdoAbstract) { $this->tdo->addLimit ($this->start, $this->count); } -- 2.11.4.GIT