<?php
/**
* @version 1.0.0
* @category Anahita Social Engineâ¢
* @copyright Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link http://www.anahitapolis.com
*/
class AnModelAvatar extends KObject
{
/**
*
* @const string
*/
const SizeSmall = 'small';
/**
*
* @const string
*/
const SizeMedium = 'medium';
/**
*
* @const string
*/
const SizeLarge = 'large';
/**
*
* @const string
*/
const SizeSquare = 'square';
/**
* default avatar of an actor node if there aren't any
*/
static protected $_default_avatar;
/**
* sets the default avatar
* @param $avatar AnModelAvatar
*/
static function setDefaultAvatar(AnModelAvatar $avatar)
{
self::$_default_avatar = $avatar;
}
/**
* returns the default avatar
* @return AnModelAvatar
*/
static function getDefaultAvatar()
{
if ( !self::$_default_avatar ) {
$base_url = AnUikitTemplate::getURL('media/com_socialengine/images/avatar', array('overwrite_path'=>'images/com_socialengine/avatar/'));
self::$_default_avatar = new AnModelAvatar(array('name'=>'_default.jpg', 'base' => $base_url));
}
return self::$_default_avatar;
}
/**
* return an array of avatar sizes with its respective dimension
* @return array
*/
static public function getSizes()
{
return array(self::SizeSmall=>'80xauto', self::SizeMedium => '160xauto', self::SizeLarge => '480xauto', self::SizeSquare => 56);
}
/**
* creates an avatar based the given options. if the avatar name is null, it will give a default avatar
* @return
* @param $options Object
*/
static public function factory($options)
{
$options = self::_initialize($options);
$name = $options['name'];
$manager = $options['avatar_manager'];
if ( is_null($name) || strlen($name) == 0) {
$avatar = self::getDefaultAvatar();
} else {
$avatar = new AnModelAvatar(array('base'=>$manager->getBaseURL(),'name'=>$name));
}
return $avatar;
}
static protected function _initialize($options)
{
$default = array(
'name' => null,
'base' => null,
'avatar_manager' => KFactory::get('lib.anahita.model.avatar.manager')
);
return array_merge($default, $options);
}
protected $_name;
protected $_base;
/**
* CONSTRUCT
* @param STRING $base avatar image file base url
* @param STRING $name avatar file ame
* @return STRING URI path to the avatar image
*/
public function __construct($options=array())
{
$options = self::_initialize($options);
$this->_name = $options['name'];
$this->_base = $options['base'];
}
/**
* return a boolean whether this avatar is the default avatar
* @return boolean
*/
public function isDefault()
{
return $this == self::getDefaultAvatar();
}
/**
* gets the name of the avatar
* @return string
*/
public function getName()
{
return $this->_name;
}
/**
* gets the base url of the avatar
* @return
*/
public function getBaseURL()
{
return $this->_base;
}
/**
* Method to obtain the URL path to the avatar image
* @param avatar size constants
* @return STRING
*/
public function getURL($size=AnModelAvatar::SizeSquare)
{
return $this->getBaseURL().'/'.$size.$this->getName();
}
/**
* helper method to get the default url of the avatar
* @return
*/
public function __toString()
{
return $this->getURL();
}
/**
*
* @return
*/
public function serialize()
{
if ( self::getDefaultAvatar() == $this )
return array('name'=> '');
else
return array('name' => (string) $this->getName());
}
//end class
}