<?php
include_once( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
include_once( PLOG_CLASS_PATH."class/view/admin/adminerrorview.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" );
include_once( PLOG_CLASS_PATH."plugins/shoutbox/class/dao/shoutboxInstaller.class.php" );

/**
* creates or upgrades the shoutbox
*/
class PluginShoutboxSetupAction extends AdminAction
{

  var $_pluginEnabled;
  
  function PluginShoutboxSetupAction($actionInfo, $request) {
      $this->AdminAction( $actionInfo, $request );
  }
  
  function perform() {
      $blogSettings = $this->_blogInfo->getSettings();
      
      $dbOk = $blogSettings->getValue("plugin_shoutbox_dbok");
      $version = $blogSettings->getValue( "plugin_shoutbox_version");
  
      if($dbOk && $version == '0.2')
          return true;
      else if($dbOk && $version < 0.2) {
          $this->confirmSetup();
          $this->finalSaveSettings();
          $this->_view = new AdminMessageView( $this->_blogInfo );
          $this->_view->setMessage("Shoutbox successfully upgraded from previous version");
          $this->setCommonData();
      }
      else if($dbOk && $version > 0.2) {
          $this->_view = new AdminErrorView( $this->_blogInfo );
          $this->_view->setValue( "message", "It seems that you are downgrading the shoutbox from version $version. Operation not supported. Maybe you have hit a bug?");
          $this->setCommonData();
      }
      else {
          $s = new Shoutbox($this->_blogInfo->getId(), $blogSettings);
      
          $tableExists = $s->checkTableExists();
          if(!$tableExists)
              $s->createTable();
          $this->setDefaults();
          $this->confirmSetup();
          $this->finalSaveSettings();
  
          if($tableExists) {
              $this->_view = new AdminErrorView( $this->_blogInfo );
              $this->_view->setValue( "message", "You have hit a (known) bug. Your shouts are intact, but the configuration options are gone. You will have to set them again :( Sorry for the inconvenience");
              $this->setCommonData();
          }
          else {
              $this->_view = new AdminMessageView( $this->_blogInfo );
              $this->_view->setMessage("Shoutbox options successfully updated");
              $this->setCommonData();
          }
      }
  
      return true;
  }
  
  
  function setDefaults() {
      $blogSettings = $this->_blogInfo->getSettings();
      
      $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_nickname", "Your nickname");
      $blogSettings->setValue( "plugin_shoutbox_default_shout", "Your shout here");
  
      $this->_blogInfo->setSettings( $blogSettings );
  }
  
  function confirmSetup() {
      $blogSettings = $this->_blogInfo->getSettings();
      
      $blogSettings->setValue( "plugin_shoutbox_dbok", true);
      $blogSettings->setValue( "plugin_shoutbox_version", '0.12');
  
      $this->_blogInfo->setSettings( $blogSettings );
  }
  
  function finalSaveSettings() {
      $blogSettings = $this->_blogInfo->getSettings();
  
      // save the blogs settings
      $blogs = new Blogs();
      include(PLOG_CLASS_PATH ."/version.php");
      if(!preg_match('/lifetype-1.1/', $version))
        $updateResult = $blogs->updateBlog( $this->_blogInfo->getId(), $this->_blogInfo );
      else
        $updateResult = $blogs->updateBlog($this->_blogInfo);
      
      if( !$updateResult) {
          $this->_view = new AdminErrorView( $this->_blogInfo );
          $this->_view->setValue( "message", "An error occurred while saving the shoutbox options");
          $this->setCommonData();
  
          return false;
      }
      
      // if everything went ok...
      $this->_blogInfo->setSettings( $blogSettings );
      $this->_session->setValue( "blogInfo", $this->_blogInfo );
      $this->saveSession();
  }
    
}
?>