Changeset 28485
- Timestamp:
- 03/11/10 16:59:26 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfUserOnlinePlugin/lib/sfUserOnline.class.php
r28482 r28485 12 12 */ 13 13 14 class sfUserOnline 14 class sfUserOnline extends myUser 15 15 { 16 /** 17 * Static private variable to store memcache connection 18 * 19 * @var object Memcache connection resource 20 */ 21 private static $mem = null; 16 protected $statusHolder = null; 17 protected $userUniqueId = null; 22 18 23 p rivate static function connect()19 public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array()) 24 20 { 25 if (!class_exists('Memcache')) { 26 throw new Exception("No memcache extension loaded"); 27 } 28 if (is_null(self::$mem)) { 29 $memcache = new Memcache; 30 $result = $memcache->connect(sfConfig::get('app_memcache_host', '127.0.0.1'), sfConfig::get('app_memcache_port', '11211')); 31 if(!$result) { 32 throw new Exception("Can't connect to memcache server"); 21 // initialize parent 22 parent::initialize($dispatcher, $storage, $options); 23 24 $options = array_merge(array( 25 'online_status_class' => 'onlineMemcacheStorage', 26 'user_unique_method' => 'getId' 27 ), $options); 28 29 $this->userUniqueId = call_user_func(array($this, $options['user_unique_method'])); 30 $this->statusHolder = new $options['online_status_class']; 31 $this->statusHolder->initialize($dispatcher, $storage, $options); 32 $this->refreshStatus(); 33 34 } 35 36 37 public function refreshStatus() 38 { 39 if($status = $this->getStatus()) { 40 if (!$this->statusHolder->replace($this->userUniqueId, $status)) { 41 $this->statusHolder->set($this->userUniqueId, $status); 33 42 } 34 self::$mem = $memcache; 35 } 36 return self::$mem; 37 } 38 private static function getIdWithPrefix($user_id) 39 { 40 return sfConfig::get('app_memcache_online_users_prefix' . $user_id, $user_id); 41 } 42 public static function refreshStatus($user_id) 43 { 44 $user_id = self::getIdWithPrefix($user_id); 45 $memcache = self::connect(); 46 if ($memcache) { 47 if (!$memcache->replace($user_id, 1, false, sfConfig::get('app_memcache_online_time', '600'))) { 48 $memcache->set($user_id, 1, false, sfConfig::get('app_memcache_online_time', '600')); 49 } 43 $this->dispatcher->notify(new sfEvent($this, 'user.change_status', array('status' => $status))); 44 } else { 45 return false; 50 46 } 51 47 } 52 public static function setOffline($user_id)48 public function setOffline() 53 49 { 54 $user_id = self::getIdWithPrefix($user_id); 55 $memcache = self::connect(); 56 if ($memcache) { 57 $memcache->delete($user_id); 58 } 50 return $this->statusHolder->delete($this->userUniqueId); 59 51 } 60 public static function isOnline($user_id)52 public function isOnline() 61 53 { 62 $user_id = self::getIdWithPrefix($user_id); 63 $memcache = self::connect(); 64 if ($memcache) { 65 return (bool)$memcache->get($user_id); 66 } 67 return false; 54 return (bool)self::getStatus(); 68 55 } 69 public static function getPlainStatus($user_id) 56 57 public function setStatus($status) 70 58 { 71 $user_id = self::getIdWithPrefix($user_id); 72 sfLoader::loadHelpers(array('I18N')); 73 $memcache = self::connect(); 74 if ($memcache) { 75 return self::isOnline($user_id) ? __('Online') : __('Offline'); 76 } else { 77 return ''; 78 } 59 return $this->statusHolder->set($this->userUniqueId, $status); 79 60 } 80 public static function getStatus($user_id) 61 62 public function getStatus() 81 63 { 82 $user_id = self::getIdWithPrefix($user_id); 83 sfLoader::loadHelpers(array('I18N')); 84 $memcache = self::connect(); 85 if ($memcache) { 86 return self::isOnline($user_id) ? '<span class="user_status status_online">' . __('Online') . '</span>' : '<span class="user_status status_offline">' . __('Offline') . '</span>'; 87 } else { 88 return ''; 89 } 64 return $this->statusHolder->get($this->userUniqueId); 65 } 66 67 public function getId() 68 { 69 throw new Exception("Replace this method to get user unique id"); 90 70 } 91 71 }