<?php
require_once 'Zend/Service/Exception.php';
require_once 'Zend/Http/Client.php';
require_once 'Zend/XmlRpc/Client.php';

class Zend_Service_Fotolia_Exception extends Zend_Service_Exception
{
}

class Zend_Service_Fotolia
{
/**
* Fotolia xml-rpc uri
*/
const FOTOLIA_URI = 'http://www.fotolia.com/Xmlrpc/rpc';

/**
* API key
*
* @var $_key string
*/
private $_key;

/**
* XML-RPC client
*
* @var $_client Zend_XmlRpc_Client
*/
private $_client;

/**
* Proxy instances array
*
* @var $_proxies array
*/
private $_proxies;

/**
* Current session id
*
* @var $_session_id string
*/
private $_session_id;

/**
* Constructor
*
* @param string $apiKey
*/
public function __construct($apiKey)
{
$this->_key = $apiKey;
$this->_session_id = NULL;
$http = new Zend_Http_Client();
$http->setCookieJar();
$this->_client = new Zend_XmlRpc_Client(self::FOTOLIA_URI, $http);
}

/**
* Returns current api key
*
* return string
*/
public function getApiKey()
{
return $this->_key;
}

/**
* This method makes possible to search media in fotolia image bank.
* Full search capabilities are available through the API
*
* @param array $searchParams
* @return array
*/
public function getSearchResults($searchParams)
{
return $this->__call('getSearchResults',
array($this->_key,
$searchParams));
}

/**
* This method returns childs of a parent category in fotolia representative category system.
* This method could be used to display a part of the category system or the all tree.
* Fotolia categories system counts three levels.
*
* @param int $language_id
* @param int $id
* @param int $category_type_id
* @return array
*/
public function getCategories1($language_id = 2, $id = 0, $category_type_id = 1)
{
return $this->__call('getCategories1',
array($this->_key,
$language_id,
$id,
$category_type_id));
}

/**
* This method returns childs of a parent category in fotolia conceptual category system.
* This method could be used to display a part of the category system or the all tree.
* Fotolia categories system counts three levels.
*
* @param int $language_id
* @param int $id
* @return array
*/
public function getCategories2($language_id = 2, $id = 0)
{
return $this->__call('getCategories2',
array($this->_key, $language_id, $id));
}

/**
* This method returns most searched tag and most used tag on fotolia website.
* This method may help you to create a tags cloud.
*
* @param int $language_id
* @param string $type
* @return array
*/
public function getTags($language_id = 2, $type = 'Used')
{
return $this->_call('getTags',
array($this->_key, $language_id, $type));
}

/**
* This method returns public galleries for a defined language
*
* @param int $language_id
* @return array
*/
public function getGalleries($language_id = 2)
{
return $this->__call('getGalleries',
array($this->_key, $language_id));
}

/**
* This method returns childs of a parent colors in the Fotolia color scheme.
* If no parent is provided first level colors are returned.
* This method can be used to display color and subcolors for color search queries.
*
* @param int $id
* @return array
*/
public function getColors($id = 0)
{
return $this->__call('getColors', array($this->_key, $id));
}

/**
* This method returns Fotolia list of countries.
*
* @param int $language_id
* @return array
*/
public function getCountries($language_id = 2)
{
return $this->__call('getCountries',
array($this->_key, $language_id));
}

/**
* This method returns fotolia data
*
* @return array
*/
public function getData()
{
return $this->__call('getData', array($this->_key));
}

/**
* This method is a test method which returns success if connexion is valid
*
* @return array
*/
public function test()
{
return $this->__call('test', array($this->_key));
}

/**
* This method return all information about a media
*
* @param int $id
* @param int $thumbnail_size
* @param int $language_id
* @return array
*/
public function getMediaData($id, $thumbnail_size = 110, $language_id = 2)
{
return $this->__call('getMediaData',
array($this->_key,
$id,
$thumbnail_size,
$language_id));
}

/**
* This method return private galleries for logged user
*
* @param int $id
* @param int $language_id
* @param int $thumbnail_size
* @return array
*/
public function getMediaGalleries($id, $language_id = 2, $thumbnail_size = 110)
{
return $this->__call('getMediaData',
array($this->_key,
$id,
$language_id,
$thumbnail_size));
}

/**
* This method allows to purchase a media and returns url to the purchased file
*
* @param int $id
* @param string $license_name
* @return array
*/
public function getMedia($id, $license_name)
{
return $this->__call('getMedia',
array($this->_key,
$this->_getSessionId(),
$id,
$license_name));
}

/**
* This method returns comp images. Comp images can ONLY be used to evaluate the image
* as to suitability for a project, obtain client or internal company approvals,
* or experiment with layout alternatives.
*
* @param int $id
* @return array
*/
public function getMediaComp($id)
{
return $this->__call('getMediaComp',
array($this->_key, $id));
}

/**
* Authenticate an user
*
* @param string $login User login
* @param string $pass User password
*/
public function loginUser($login, $pass)
{
$res = $this->__call('loginUser',
array($this->_key,
$login,
$pass));
$this->_session_id = $res['session_id'];
}

/**
* Log out an user
*/
public function logoutUser()
{
$this->__call('logoutUser',
array($this->key,
$this->_getSessionId()));
$this->_session_id = NULL;
}

/**
* This method returns data for logged user.
*
* @return array
*/
public function getUserData()
{
return $this->__call('getUserData',
array($this->_key,
$this->_getSessionId()));
}

/**
* This method allows you to get sales/views/income statistics from your account.
*
* @param string $type
* @param string $time_range
* @param string $easy_date_period
* @param string $start_date
* @param string $end_date
* @return array
*/
public function getUserAdvancedStats($type,
$time_range,
$easy_date_period=NULL,
$start_date=NULL,
$end_date=NULL)
{
return $this->__call('getUserAdvancedStats',
array($this->_key,
$this->_getSessionId(),
$type,
$time_range,
$easy_date_period,
$start_date,
$end_date));
}

/**
* This methods returns statistics for logged user
*
* @return array
*/
public function getUserStats()
{
return $this->__call('getUserStats',
array($this->_key,
$this->_getSessionId()));
}

/**
* Delete a user's gallery
*
* @param string $id
*/
public function deleteUserGallery($id)
{
$this->__call('deleteUserGallery',
array($this->_key,
$this->_getSessionId(),
$id));
}

/**
* This method allows you to create a new gallery in your account.
*
* @param string $name
* @return array
*/
public function createUserGallery($name)
{
return $this->__call('createUserGallery',
array($this->_key,
$this->_getSessionId(),
$name));
}

/**
* This method allows you to add a content to your default lightbox or any of your existing galleries
*
* @param int $content_id
* @param string $id
* @return array
*/
public function addToUserGallery($content_id, $id = '')
{
return $this->__call('addToUserGallery',
array($this->_key,
$this->_getSessionId(),
$content_id,
$id));
}

/**
* This method allows you to remove a content from your default lightbox or any of your existing galleries
*
* @param int $content_id
* @param string $id
* @return array
*/
public function removeFromUserGallery($content_id, $id = '')
{
return $this->__call('removeFromUserGallery',
array($this->_key,
$this->_getSessionId(),
$content_id,
$id));
}

/**
* This method allows to search media in logged user galleries or lightbox.
*
* @param int $page
* @param int $per_page
* @param int $thumbnail_size
* @param string $id
* @return array
*/
public function getUserGalleryMedias($page = 0,
$per_page = 32,
$thumbnail_size = 110,
$id = '')
{
return $this->__call('getUserGalleryMedias',
array($this->_key,
$this->_getSessionId(),
$page,
$per_page,
$thumbnail_size,
$id));
}

/**
* This method returns private galleries for logged user.
*
* @return array
*/
public function getUserGalleries()
{
return $this->__call('getUserGalleries',
array($this->_key,
$this->_getSessionId()));
}

/**
* Magic method used to call fotolia xml-rpc functions
*
* @param string $method
* @param array $args
* @return array
* @throws Zend_Service_Fotolia_Exception
*/
public function __call($method, $args)
{
$obj = $this->_getProxy($this->_getNamespace($method));
try {
return $obj->__call($method, $args);
} catch (Zend_XmlRpc_Client_Exception $e) {
throw new Zend_Service_Fotolia_Exception($e->getMessage(),
$e->getCode());
}
}

/**
* Returns xml-rpc client
*
* @return Zend_XmlRpc_Client
*/
public function getClient()
{
return $this->_client;
}

/**
* Returns namespace associated to given method name
*
* @param string $method
* @return string
*/
protected function _getNamespace($method)
{
switch ($method) {
case 'getSearchResults':
case 'getCategories1':
case 'getCategories2':
case 'getTags':
case 'getGalleries':
case 'getColors':
case 'getCountries':
return 'search';
case 'getMediaData':
case 'getMediaGalleries':
case 'getMedia':
case 'getMediaComp':
return 'media';
case 'loginUSer':
case 'logoutUser':
case 'getUserData':
case 'getUserGalleries':
case 'getUserGalleryMedias':
case 'deleteUserGallery':
case 'createUserGallery':
case 'addToUserGallery':
case 'removeFromUserGallery':
case 'getUSerAdvancedStats':
return 'user';
case 'getData':
case 'test':
return 'main';
default:
return 'xmlrpc';
}
}

/**
* Returns a xml-rpc proxy instance from a namespace
*
* @param string $namespace
* @return Zend_xmlRpc_Client_ServerProxy
* @throws Zend_Service_Fotolia_Exception
*/
protected function _getProxy($namespace)
{
if (!isset($this->_proxies[$namespace])) {
$valid = array('xmlrpc', 'search', 'media', 'user', 'main');
if (!in_array($namespace, $valid)) {
throw new Zend_Service_Fotolia_Exception('invalid namespace');
}

$this->_proxies[$namespace] = $this->_client->getProxy($namespace);
}

return $this->_proxies[$namespace];
}

/**
* Returns current session id
*
* @return string
* @throws Zend_Service_Fotolia_Exception
*/
protected function _getSessionId()
{
if ($this->_session_id === NULL) {
throw new Zend_Service_Fotolia_Exception('session id not set, please use loginUser method before');
}

return $this->_session_id;
}
}