<?php

class Atmail_SpamSettings_Plugin extends Atmail_Controller_Plugin
{
    
    protected $_pluginFullName   = 'SpamSettings';
    protected $_pluginAuthor = 'Brad Kowalczyk <brad@staff.atmail.com>';
    protected $_pluginDescription = '';
    protected $_pluginCopyright = 'Copyright Brad Kowalczyk';
    protected $_pluginUrl = '';
    protected $_pluginNotes = '';
    protected $_pluginVersion = '1.0.0';
    protected $_pluginCompat = '6.1.5';
    protected $_pluginModule = 'mail';

    public function __construct()
    {
        parent::__construct();
        $path = dirname(__FILE__) . "/Api.php";
        $this->_pluginDescription = <<<EOF
Adds Spam Settings functionality to webmail only installs.
There is a class file at $path that you need to modify in order to let the plugin interface with your API so it can get/set settings.
EOF;
        
    }
    
            
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // Intercept any calls to display the Mail Options settings page
        if ($request->getControllerName() == 'settings' && $request->getActionName() == 'spam') {
            $this->_initView();
            $auth = Zend_Auth::getInstance();
            $userData = $auth->getIdentity();
            require_once(dirname(__FILE__) . "/Api.php");
            $api = new Api($userData['Account']);
            $this->view->spamSettings = $api->getSpamSettings();
            echo $this->view->render("spam.phtml");
            exit;
        }
        
        // Intercept any calls to save mail options
        if ($request->getControllerName() == 'settings' && $request->getActionName() == 'spamsave') {
            $this->_initView();
            $auth = Zend_Auth::getInstance();
            $userData = $auth->getIdentity();
            require_once(dirname(__FILE__) . "/Api.php");
            $api = new Api($userData['Account']);
            
            $whitelist = explode("\n", $_POST['whitelist_from']);
            $blacklist = explode("\n", $_POST['blacklist_from']);
            $result = $api->saveSpamSettings($whitelist, $blacklist, $_POST['required_score'], $_POST['spam_treatment'], $_POST['rewrite_header']);
            require_once("jQuery/jQuery.php");
            
            if ($result !== true) {
                jQuery::evalScript("$('#Flash',  '#spam').removeClass('flash_notice')");
                jQuery::evalScript("$('#Flash',  '#spam').addClass('flash_notice_error')");
                jQuery::evalScript("$('#Flash',  '#spam').show();");
                $error = addslashes($this->view->translate($result));
                sleep(2);
                jQuery::evalScript("$('#Flash',  '#spam').html('$error');");
                jQuery::evalScript("$('#primary_content', '#spam').scrollTop(0);");                
            } else {
                jQuery::evalScript("$('#Flash',  '#spam').show();");
                jQuery::evalScript("$('#primary_content', '#spam').scrollTop(0);");
            }
            
            echo $this->view->render("jsonresponse.phtml", null, true);
            exit;
        }
                                                                                        
    }

    public function renderMailSettingsNav()
    {
        $this->_initView();
        $label = $this->view->translate('Anti-Spam Settings');
        $baseUrl = $this->_getModuleBaseUrl();
        $nav = <<<EOF
            <li id="spam_settings">
                <a href="{$baseUrl}/settings/spam" title='spam'>
                    <span class="label">$label</span>
                </a>
            </li>
EOF;

				// Uncomment next line to enable spam settings
        // echo $nav;
				echo "";
    }
    
}
