<?php

	include_once( PLOG_CLASS_PATH."plugins/atom/class/xml/atom/atomabstractservice.class.php" );
	include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
	include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
	
	/**
	 * plog-dependent class that starts messing around with blogs and users. So far the layer
	 * was pretty generic but at some point we have to get into plog... and it is here :)
	 */
	class PlogAtomAbstractService extends AtomAbstractService 
	{
		var $_userInfo;
		var $_blogInfo;
		var $_blogInfos;
	 
		function PlogAtomAbstractService( $request )
		{
			$this-> AtomAbstractService( $request );
			
			// load the information we need from the user and the blog
			$this->_loadInfo();	
		}
		
		/**
		 * @private
		 */
		function _loadInfo()
		{
			// loads the blog information and the user information based on what
			// came from the request
			
			// first, get the username from the authentication layer
			$userName = $this->_request->getUsernameFromAuthentication();
			AtomLogger::Log( "PlogAtomAbstractService:_loadInfo: username = $userName" );

			// load user information for the user
			$users = new Users();
			$this->_userInfo = $users->getUserInfoFromUsername( $userName );
			if( !$this->_userInfo ) {
				AtomLogger::Log( "Error loading the user information" );
				$response = new AtomErrorResponse( "Error loading user information",
                                                                    HTTP_ERROR_500 );
				$this->setResponse( $response );
				$this->_isInitError = true;
				return false;
			}

			$blogs = new Blogs();
			$blogId = $this->_request->getBlogId();
			$userId = $this->_userInfo->getId();
			if( $blogId == ATOM_NO_BLOG) {
				AtomLogger::Log( "Loading all blogs for user ".$this->_userInfo->getUsername());
				$this->_blogInfos = $users->getUsersBlogs( $userId );
			}
			else {
				AtomLogger::Log( "Loading blog with id = ".$blogId );
				$this->_blogInfo = $blogs->getBlogInfo( $blogId );
			}

			return true;
		}
		
		/**
		 * Returns the current BlogInfo object
		 *
		 * @return A BlogInfo object
		 */
		function getBlogInfo()
		{
			return $this->_blogInfo;
		}
		
		/**
		 * Returns the current UserInfo object
		 *
		 * @return A UserInfo object
		 */
		function getUserInfo()
		{
			return $this->_userInfo;
		}	
	}
?>