<?php

    include_once( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
    include_once( PLOG_CLASS_PATH."class/view/admin/adminplugintemplatedview.class.php" );
    include_once( PLOG_CLASS_PATH."class/view/admin/adminmessageview.class.php" );
    include_once( PLOG_CLASS_PATH."plugins/shoutbox/class/dao/shoutbox.class.php" );

    /**
     * shows a form with the current configuration
    */
    class PluginShoutboxAction extends AdminAction {
        
        function PluginShoutboxAction( $actionInfo, $request ) {
            $this->AdminAction( $actionInfo, $request );
        }
        
        function perform() {
        
            // load some configuration settings
            $blogSettings = $this->_blogInfo->getSettings();

            /*
            if(!$blogSettings->getValue("plugin_shoutbox_dbok") || $blogSettings->getValue( "plugin_shoutbox_version") != '0.12') {
                header('Location: admin.php?op=setupShoutbox');
                exit();
            }
            */

            // Get the shoutbox entries
            $s = new Shoutbox($this->_blogInfo->getId(), $blogSettings);
            
            // Check the setup of the box
            $message = $s->setup();
            if($message !== true) {
              $this->_view = new AdminMessageView($this->_blogInfo);
              $this->_view->setMessage($message);
              $this->setCommonData();
              return true;
            }
            // Check if defaults have ever been set for this blog
            if(!$blogSettings->getValue("plugin_shoutbox_defaultsset"))
              $this->setDefaults();
            
            // create a view and export the settings to the template
            $this->_view = new AdminPluginTemplatedView( $this->_blogInfo, "shoutbox", "shoutbox", true );
            $this->_view->setValue( "pluginEnabled", $pluginEnabled );

            $this->_view->setValue( "shouts", $s->getShoutsArray());
            
            if($s->mail_alert)
                $mail_alert_checked = "checked";
            
            $this->_view->setValue("mail_alert_checked", $mail_alert_checked);
            $this->_view->setValue("max_length", $s->max_length);
            $this->_view->setValue("shouts_per_view", $s->shouts_per_view);
            $this->_view->setValue("default_nick", $s->default_nick);
            $this->_view->setValue("default_shout", $s->default_shout);
            $this->_view->setValue("protected_nicks", $s->protected_nicks);
            $this->_view->setValue("protection_password", $s->protection_password);
            $this->_view->setValue("current_page", $s->current_page);

            $this->setCommonData();
            
            return true;
        }
        
          function setDefaults() {
            $blogSettings = $this->_blogInfo->getSettings();
            
            $blogSettings->setValue( "plugin_shoutbox_defaultsset", 1);
            
            $blogSettings->setValue( "plugin_shoutbox_shout_enabled", 1);
            $blogSettings->setValue( "plugin_shoutbox_mail_alert", 1);
            $blogSettings->setValue( "plugin_shoutbox_quick_delete", 0);
            $blogSettings->setValue( "plugin_shoutbox_max_length", 400);
            $blogSettings->setValue( "plugin_shoutbox_shouts_per_view", 15);
            
            $blogSettings->setValue( "plugin_shoutbox_protected_nicks", "ExampleNick");
            $blogSettings->setValue( "plugin_shoutbox_protection_password", "");
            
            $blogSettings->setValue( "plugin_shoutbox_default_nick", "Your nickname");
            $blogSettings->setValue( "plugin_shoutbox_default_shout", "Your shout here");
            
            $this->_blogInfo->setSettings( $blogSettings );
            $this->_session->setValue( "blogInfo", $this->_blogInfo );
            $this->saveSession();
        }
}
?>