<?php

	include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
	include_once( PLOG_CLASS_PATH."class/logger/ploglogger.class.php" );
	
	/**
	 * sample plugin that uses the event-based system to log all the events that are thrown 
	 *
	 * @see PluginManager
	 * @see PluginBase
	 */
	class PluginLogger extends PluginBase
	{
	
		function PluginLogger()
		{
			$this->PluginBase();
			
			// set the plugin information
			$this->id = "logger";
			$this->desc = "A logging plugin";
			$this->author = "The LifeType Project";
			$this->locales = Array();
			
			// register all the events available, but we can use the PluginManager
			// to give us the whole list of events that can be registered...
			$pm =& PluginManager::getPluginManager();
			$events = $pm->getDefinedEvents();
			foreach( $events as $event => $value) {
				$this->registerNotification( $value );
			}
			
			// configure the logger
			$config =& Config::getConfig();
			PlogLogger::setLogFilePath( $config->getValue( "temp_folder" ));
		}
		
		/**
		 * Logs the events. In some cases we elaborate and throw a nicer message or else
		 * simply log the event number and that's it
		 *
		 * @param eventType
		 * @param params
		 * @return Always true :)
		 */
		function process( $eventType, $params )
		{
			// get the event name
			$pm =& PluginManager::getPluginManager();
			$eventList = $pm->getDefinedEvents();
			$eventNames = array_flip( $eventList );
			$eventName = $eventNames[$eventType];
			$from = $params["from"];
			
			// this string is always logged
			if( $this->blogInfo != null ) 
				PlogLogger::log( "Processing event = $eventName($eventType) - from = $from - blog ".$this->blogInfo->getBlog());			
			else
				PlogLogger::log( "Processing event = $eventName($eventType) - from = $from" );
			
			// but then we show a nicer log for some other events
			if( $eventType == EVENT_POST_LOGOUT ) {
				PlogLogger::log( "  User ".$this->userInfo->getUsername()." logged out from blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_LOGIN_SUCCESS ) {
				PlogLogger::log( "  User ".$this->userInfo->getUsername()." logged in blog ".$this->blogInfo->getBlog());			
			}
			elseif( $eventType == EVENT_LOGIN_FAILURE ) {
				PlogLogger::log( "  Error authenticating user ".$params["user"] );
			}
			elseif( $eventType == EVENT_POST_COMMENT_ADD ) {
				$comment = $params["comment"];
				PlogLogger::log( "  Comment with topic \"".$comment->getTopic()."\" added to post ".$comment->getArticleId());
			}
			elseif( $eventType == EVENT_POST_POST_UPDATE ) {
				$post = $params["article"];
				PlogLogger::log( "  Post with topic \"".$post->getTopic()."\" (".$post->getId().") updated in blog ".$this->blogInfo->getBlog());
			}			
			elseif( $eventType == EVENT_POST_POST_ADD ) {
				$post = $params["article"];
				PlogLogger::log( "  Post with topic \"".$post->getTopic()."\" (".$post->getId().") added to blog ".$this->blogInfo->getBlog());
			}			
			elseif( $eventType == EVENT_POST_POST_DELETE ) {
				$post = $params["article"];
				PlogLogger::log( "  Post with topic \"".$post->getTopic()."\" (".$post->getId().") deleted from blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_CATEGORY_ADD ) {
				$category = $params["category"];
				PlogLogger::log( "  Category with title \"".$category->getName()."\" (".$category->getId().") added to blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_CATEGORY_UPDATE ) {
				$category = $params["category"];
				PlogLogger::log( "  Category with title \"".$category->getName()."\" (".$category->getId().") updated in blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_CATEGORY_DELETE ) {
				$category = $params["category"];
				PlogLogger::log( "  Category with title \"".$category->getName()."\" (".$category->getId().") deleted from blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_COMMENT_DELETE ) {
				$comment = $params["comment"];
				PlogLogger::log( "  Comment with topic \"".$comment->getTopic()."\" (".$comment->getId().") deleted from article ".$comment->getArticleId()." and blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_MARK_NO_SPAM_COMMENT ) {
				$commentId = $params["commentId"];
				PlogLogger::log( "  Comment id \"".$commentId."\" marked as no-spam in blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_MARK_SPAM_COMMENT ) {
				$commentId = $params["commentId"];
				PlogLogger::log( "  Comment id \"".$commentId."\" marked as spam in blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_CUSTOM_FIELD_ADD ) {
				$field = $params["field"];
				PlogLogger::log( "  Custom field named \"".$field->getName()."\" of type ".$field->getType()." added to blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_CUSTOM_FIELD_DELETE ) {
				$field = $params["field"];
				PlogLogger::log( "  Custom field named \"".$field->getName()."\" of type ".$field->getType()." deleted from blog ".$this->blogInfo->getBlog());
			}
			elseif( $eventType == EVENT_POST_CUSTOM_FIELD_UPDATE ) {
				$field = $params["field"];
				PlogLogger::log( "  Custom field named \"".$field->getName()."\" of type ".$field->getType()." updated in blog ".$this->blogInfo->getBlog());
			}
			
			
			return true;
		}
	}
?>