From 4a21135545187bd1a383a1cb818c9e7146826059 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 16 Aug 2006 03:54:26 +0000 Subject: [PATCH] Adding the singleton portion of the functions class --- Functions2.php | 130 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/Functions2.php b/Functions2.php index 9130ca5..259f256 100644 --- a/Functions2.php +++ b/Functions2.php @@ -39,6 +39,136 @@ */ class BSFunctions { + /** + * Singleton instance + * @var object + */ + private static $instance; + + /** + * Cookie path + * @var string + */ + private $cookiePath = '/'; + + /** + * Cookie domain setting + * @var string + */ + private $cookieDomain = ''; + + /** + * Cookie expiration time + * @var integer + */ + private $cookieTimeout = 900; + + /** + * Current swapped CSS class + * @var string + */ + public static $cssClass = ''; + + // ################################################################### + /** + * Constructor + */ + private function __construct() {} + + // ################################################################### + /** + * Returns the shared instance for singleton + * + * @return object Shared instance + */ + private function SharedInstance() + { + if (!self::$instance) + { + self::$instance = new BSFunctions; + } + return self::$instance; + } + + // ################################################################### + /** + * Sets the cookie path + * + * @param string New path + */ + public static function SetCookiePath($path) + { + self::SharedInstance()->cookiePath = $path; + } + + // ################################################################### + /** + * Sets the cookie domain setting + * + * @param string Cookie domain + */ + public static function SetCookieDomain($domain) + { + self::SharedInstance()->cookieDomain = $domain; + } + + // ################################################################### + /** + * Sets the cookie timeout + * + * @param integer Cookie timeout + */ + public static function SetCookieTimeout($timeout) + { + self::SharedInstance()->cookieTimeout = intval($timeout); + } + + // ################################################################### + /** + * Sets a cookie in the user's computer/browing session + * + * @param string Name of the cookie + * @param string Value of the cookie, FALSE to clear + * @param bool Is the cookie permanent? + */ + public static function Cookie($name, $value, $sticky = true) + { + // expire the cookie + if ($value === false) + { + setcookie($name, $value, time() - (2 * self::SharedInstance()->cookieTimeout), self::SharedInstance()->cookiePath, self::SharedInstance()->cookieDomain); + } + // set the cookie + else + { + if ($sticky) + { + $expire = time() + 60 * 60 * 24 * 365; + } + else + { + $expire = time() + self::SharedInstance()->cookieTimeout; + } + + setcookie($name, $value, $expire, self::SharedInstance()->cookiePath, self::SharedInstance()->cookieDomain); + } + } + + // ################################################################### + /** + * Alternate between two CSS classes + * + * @param string First CSS class name + * @param string Second CSS class name + */ + public function SwapCssClasses($class1 = 'alt1', $class2 = 'alt2') + { + static $count; + + self::SharedInstance()->cssClass = ($count % 2) ? $class1 : $class2; + $count++; + } + // ################################################################### /** * Returns the 'source path' version of a file path. It adds a -- 2.43.5