<?php
    include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
    
    /**
     * Sends notifications to administrators every time certain configurable events
     * occur in the system, such as new blogs created, new users created, etc.
     */
    class PluginAdminNotifier extends PluginBase
    {
    
        function PluginAdminNotifier()
        {
            $this->PluginBase();
            
            $this->id = "adminnotifier";
            $this->desc = "Sends email notifications to administrations every time certain events occur";
            $this->author = "The LifeType Project";
            
            // register our actions
            $this->registerAdminAction( "eventList", "AdminNotifierShowEventListAction" );
            $this->registerAdminAction( "updateEvents", "AdminNotifierUpdateEventsAction" );
            
            // register our menu options
            $this->addMenuEntry( "/menu/adminSettings", "adminNotifierGroup", "", "", false, true );
            $this->addMenuEntry( "/menu/adminSettings/adminNotifierGroup", "adminNotifier", "?op=eventList", "", false, true );
            
            // register our notifications
            $this->registerEvents();
        }
        
        function registerEvents()
        {
            // check our configuration and register as many event listeners as necessary
            include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
            $config =& Config::getConfig();
            $events = $config->getValue( "plugin_adminnotifier_events" );
            
            if( !is_array( $events ))
                $events = Array();
            
            foreach( $events as $eventId => $value ) {
                // register those that are configured
                $this->registerNotification( $eventId );
            }
        }

        /**
         * This method processes our event notifications
         */        
        function process( $eventType, $params )
        {        
            include_once( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
        
            // given the event id, let's get its string
            $events = PluginManager::getDefinedEvents();
            $eventList = array_flip( $events );
            $eventName = $eventList[ $eventType ];
            
            // build the message
			if( $this->blogInfo === null ) {
				$blogName = "(undefined)";
				$blogLink = "(undefined)";
			}
			else {
	            $rg = $this->blogInfo->getBlogRequestGenerator();
	            $blogLink = $rg->blogLink();
	            $blogName = $this->blogInfo->getBlog();
			}
            $t = new Timestamp();
            $timestamp = $t->getTimestamp();
            $message = "Event: {$eventName}\n
                        \n
                        Generated by blog: {$blogName}\n  
                        Link: {$blogLink}\n
                        \n
                        Timestamp: {$timestamp}";
            
            // send the message to all administrators
            $this->notifyAdmins( $message );
        }
        
        /**
         * loads all the email addresses of all administrator users
         * @private
         * @return An array with email addresses
         */
        function getAdminEmailAddresses()
        {
            include_once( PLOG_CLASS_PATH."class/database/db.class.php" );
            include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
            $prefix = Db::getPrefix();
            $query = "SELECT DISTINCT u.id AS id, u.user AS user, u.email AS email
                      FROM {$prefix}users u, {$prefix}users_permissions p
                      WHERE p.permission_id = 1 AND u.id = p.user_id AND u.status = ".USER_STATUS_ACTIVE;
                      
            $db =& Db::getDb();
            $result = $db->Execute( $query );
            if( !$result )
                return Array();
                
            $admins = Array();
            while( $row = $result->FetchRow()) {
                $admins[] = $row["email"];
            }
            
            return( $admins );
        }
        
        /**
         * Sends the notification email to administrators
         * @param message The message to be sent to administrator users
         * @private
         */
        function notifyAdmins( $message )
        {
            include_once( PLOG_CLASS_PATH."class/mail/emailservice.class.php" );
            include_once( PLOG_CLASS_PATH."class/mail/emailmessage.class.php" );
            include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
            include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
            
            // build the message
            $message = new EmailMessage();
            $config =& Config::getConfig();
            $message->setFrom( $config->getValue( "post_notification_source_address" ));
            $message->setSubject( "LifeType AdminNotifier Notification" );
            $message->setBody( $message );
            
            // load all the administrator users
            foreach( $this->getAdminEmailAddresses() as $emailAddress ) {
                $message->addTo( $emailAddress );
            }
            
            // once the message is ready, send it out
            $service = new EmailService();
            return( $service->sendMessage( $message ));
        }
    }
?>