Author: rodchyn
Date: 2010-03-11 13:06:56 +0100 (Thu, 11 Mar 2010)
New Revision: 28482
Added:
plugins/sfUserOnlinePlugin/lib/
plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php
Log:
Add base class
Added: plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php
===================================================================
--- plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php
(rev 0)
+++ plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php 2010-03-11
12:06:56 UTC (rev 28482)
@@ -0,0 +1,92 @@
+<?php
+
+/**
+ * Class that use memcache to store user status.
+ * Is user online or offline or other.
+ *
+ * @author Yura Rodchyn <[email protected]>
+ * @package rodchyn
+ * @example sfUserOnline::isOnline('user_id') return boolean user status
+ * @see http://go.rodchyn.com/sfUserOnlinePlugin
+ *
+ */
+
+class sfUserOnline
+{
+ /**
+ * Static private variable to store memcache connection
+ *
+ * @var object Memcache connection resource
+ */
+ private static $mem = null;
+
+ private static function connect()
+ {
+ if (!class_exists('Memcache')) {
+ throw new Exception("No memcache extension loaded");
+ }
+ if (is_null(self::$mem)) {
+ $memcache = new Memcache;
+ $result = $memcache->connect(sfConfig::get('app_memcache_host',
'127.0.0.1'), sfConfig::get('app_memcache_port', '11211'));
+ if(!$result) {
+ throw new Exception("Can't connect to memcache server");
+ }
+ self::$mem = $memcache;
+ }
+ return self::$mem;
+ }
+ private static function getIdWithPrefix($user_id)
+ {
+ return sfConfig::get('app_memcache_online_users_prefix' . $user_id,
$user_id);
+ }
+ public static function refreshStatus($user_id)
+ {
+ $user_id = self::getIdWithPrefix($user_id);
+ $memcache = self::connect();
+ if ($memcache) {
+ if (!$memcache->replace($user_id, 1, false,
sfConfig::get('app_memcache_online_time', '600'))) {
+ $memcache->set($user_id, 1, false,
sfConfig::get('app_memcache_online_time', '600'));
+ }
+ }
+ }
+ public static function setOffline($user_id)
+ {
+ $user_id = self::getIdWithPrefix($user_id);
+ $memcache = self::connect();
+ if ($memcache) {
+ $memcache->delete($user_id);
+ }
+ }
+ public static function isOnline($user_id)
+ {
+ $user_id = self::getIdWithPrefix($user_id);
+ $memcache = self::connect();
+ if ($memcache) {
+ return (bool)$memcache->get($user_id);
+ }
+ return false;
+ }
+ public static function getPlainStatus($user_id)
+ {
+ $user_id = self::getIdWithPrefix($user_id);
+ sfLoader::loadHelpers(array('I18N'));
+ $memcache = self::connect();
+ if ($memcache) {
+ return self::isOnline($user_id) ? __('Online') : __('Offline');
+ } else {
+ return '';
+ }
+ }
+ public static function getStatus($user_id)
+ {
+ $user_id = self::getIdWithPrefix($user_id);
+ sfLoader::loadHelpers(array('I18N'));
+ $memcache = self::connect();
+ if ($memcache) {
+ return self::isOnline($user_id) ? '<span class="user_status
status_online">' . __('Online') . '</span>' : '<span class="user_status
status_offline">' . __('Offline') . '</span>';
+ } else {
+ return '';
+ }
+ }
+}
+?>
\ No newline at end of file
--
You received this message because you are subscribed to the Google Groups
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-svn?hl=en.