<?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 AnModelAvatarManager extends KObject
{
protected $_basePath;
protected $_storage;
/**
* CONSTRUCTOR
* @param array of $options
* @return null
*/
public function __construct(array $options=array())
{
$options = $this->_initialize($options);
$this->_basePath = $options['avatar_base_path'];
$this->_storage = $options['storage'];
}
/**
* method to initialize the class instance object
* @param array of options
* @return array of options
*/
protected function _initialize($options)
{
$default = array(
'avatar_base_path' => 'com_socialengine/avatars',
'storage' => KFactory::get('lib.anahita.util.storage')
);
return array_merge($default, $options);
}
/**
* Set the storage adapter
* @param $storage AnUtilStorageAdapterAbstract Object
*/
public function setStorage(AnUtilStorageAdapterAbstract $storage)
{
$this->_storage = $storage;
}
/**
* Get the storage adapter
* @return AnUtilStorageAdapterAbstract Object
*/
public function getStorage()
{
return $this->_storage;
}
/**
* Set the basepath of the avatars
* @param string
*/
public function setBasePath($path='')
{
$this->_basePath = $path;
}
/**
* Get base url of the avatars
* @return
*/
public function getBaseURL()
{
$path = $this->_storage->getUrl($this->_basePath);
return $path;
}
/**
* Remove the physical files associated with an avatar
* @return
* @param $actor AnModelActor Object
*/
public function deleteAvatar(AnModelActor $actor)
{
if ( !$actor->getAcl()->canUpdateAvatar() )
return;
if ( !$actor->avatar || $actor->avatar->isDefault() )
return;
foreach(AnModelAvatar::getSizes() as $size=>$dimension) {
$filename = $size.$actor->avatar->getName();
$path = $this->_basePath.'/'.$filename;
$this->_storage->delete($path);
}
$actor->avatar = AnModelAvatar::getDefaultAvatar();
}
/**
* Resize and store image as an avatar for an actor
* @return
* @param $actor AnModelActor Object
* @param $data AnUtilData Object
*/
public function setAvatar(AnModelActor $actor,AnUtilData $data)
{
jimport('joomla.filesystem.file');
if ( !$actor->getAcl()->canUpdateAvatar() )
return;
$firstime = false;
if ( is_null($actor->avatar) || $actor->avatar->isDefault() ) {
$thumbnail = md5($actor->id).'.jpg';
$firstime = true;
} else
$thumbnail = $actor->avatar->getName();
$format = JFile::getExt($thumbnail);
$image = imagecreatefromstring($data);
foreach(AnModelAvatar::getSizes() as $size=>$dimension) {
$aData = AnUtilImage::resize($image, $dimension, array('format'=>$format));
$filename = $size.$thumbnail;
$path = $this->_basePath.'/'.$filename;
$this->_storage->write($path, $aData);
}
if ( $firstime ) {
$avatar = new AnModelAvatar(array('name' => $thumbnail,'base' => $this->getBaseURL()));
$actor->avatar = $avatar;
}
return true;
}
//end class
}