/**
* Functions
*
- * This is a bunch of static functions. This class is singleton so it
- * can store data while remaining static.
+ * This is a bunch of static functions wrapped in a class for modularity and
+ * namespace collisions.
*
* @author Blue Static
* @copyright Copyright (c)2005 - 2009, Blue Static
class BSFunctions
{
/**
- * Singleton instance
- * @var object
+ * Make class static
*/
- 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 _instance()
- {
- if (!self::$instance)
- {
- self::$instance = new BSFunctions();
- }
- return self::$instance;
- }
-
- /**
- * Sets the cookie path
- *
- * @param string New path
- */
- public static function set_cookie_path($path)
- {
- self::_instance()->cookiePath = $path;
- }
-
- /**
- * Sets the cookie domain setting
- *
- * @param string Cookie domain
- */
- public static function set_cookie_domain($domain)
- {
- self::_instance()->cookieDomain = $domain;
- }
-
- /**
- * Sets the cookie timeout
- *
- * @param integer Cookie timeout
- */
- public static function set_cookie_timeout($timeout)
- {
- self::_instance()->cookieTimeout = intval($timeout);
- }
+ private function __construct()
+ {}
/**
* 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?
+ * @param string Value of the cookie
+ * @param integer Timeout in seconds for non-sticky cookies. Can also be ::COOKIE_EXPIRE or ::COOKIE_STICKY
+ * @param string Cookie path
+ * @param string Domain
*/
- public static function cookie($name, $value, $sticky = true)
+ const COOKIE_EXPIRE = -1;
+ const COOKIE_STICKY = -2;
+
+ public static function cookie($name, $value, $timeout = 900, $path = '/', $domain = '')
{
// expire the cookie
- if ($value === false)
+ if ($timeout == self::COOKIE_EXPIRE)
{
- setcookie($name, $value, time() - (2 * self::_instance()->cookieTimeout), self::_instance()->cookiePath, self::_instance()->cookieDomain);
+ setcookie($name, $value, time() - 1800, $path, $domain);
}
// set the cookie
else
{
- if ($sticky)
+ // it's sticky so keep it around for a while
+ if ($timeout == self::COOKIE_STICKY)
{
$expire = time() + 60 * 60 * 24 * 365;
}
else
{
- $expire = time() + self::_instance()->cookieTimeout;
+ $expire = time() + $timeout;
}
- setcookie($name, $value, $expire, self::_instance()->cookiePath, self::_instance()->cookieDomain);
+ setcookie($name, $value, $expire, $path, $domain);
}
}
public static function swap_css_classes($class1 = 'alt1', $class2 = 'alt2')
{
static $count;
-
- self::$cssClass = ($count % 2) ? $class1 : $class2;
- $count++;
+ return ($count++ % 2) ? $class1 : $class2;
}
/**
*/
public static function is_valid_email($email)
{
- if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
- {
- return true;
- }
- else
- {
- return false;
- }
+ return (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email));
}
/**
{
return substr($string, $start, $end - $start);
}
+
+ /**
+ * Converts a boolean value into the string 'Yes' or 'No'
+ *
+ * @param boolean
+ * @return string
+ */
+ public static function bool_to_string($bool)
+ {
+ return ($bool ? _('Yes') : _('No'));
+ }
}
?>
\ No newline at end of file