<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * MiscController
 * 
 * Handles the actions for ip location services related tasks
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.3.3
 */
 
class MiscController extends Controller
{
    
    public function init()
    {
        $this->getData('pageScripts')->add(array('src' => AssetsUrl::js('misc.js')));
        parent::init();
    }
    
    public function actionIndex()
    {
        $this->redirect(array('misc/application_log'));
    }
    
    public function actionEmergency_actions()
    {
        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | ' . Yii::t('app', 'Emergency actions'), 
            'pageHeading'       => Yii::t('app', 'Emergency actions'),
            'pageBreadcrumbs'   => array(
                Yii::t('app', 'Emergency actions'),
            ),
        ));
        
        $this->render('emergency-actions');
    }
    
    public function actionRemove_sending_pid()
    {
        if (!Yii::app()->request->isAjaxRequest) {
            $this->redirect(array('misc/emergency_actions'));
        }
        Yii::app()->options->remove('system.cron.send_campaigns.lock');
        Yii::app()->options->set('system.cron.send_campaigns.campaigns_offset', 0);
        return $this->renderJson();
    }
    
    public function actionRemove_bounce_pid()
    {
        if (!Yii::app()->request->isAjaxRequest) {
            $this->redirect(array('misc/emergency_actions'));
        }
        Yii::app()->options->remove('system.cron.process_bounce_servers.pid');
        return $this->renderJson();
    }
    
    public function actionRemove_fbl_pid()
    {
        if (!Yii::app()->request->isAjaxRequest) {
            $this->redirect(array('misc/emergency_actions'));
        }
        Yii::app()->options->remove('system.cron.process_feedback_loop_servers.pid');
        return $this->renderJson();
    }
    
    public function actionReset_campaigns()
    {
        if (!Yii::app()->request->isAjaxRequest) {
            $this->redirect(array('misc/emergency_actions'));
        }
        Campaign::model()->updateAll(array('status' => Campaign::STATUS_SENDING), 'status = :status', array(':status' => Campaign::STATUS_PROCESSING));
        return $this->renderJson();
    }
    
    public function actionReset_bounce_servers()
    {
        if (!Yii::app()->request->isAjaxRequest) {
            $this->redirect(array('misc/emergency_actions'));
        }
        BounceServer::model()->updateAll(array('status' => BounceServer::STATUS_ACTIVE), 'status = :status', array(':status' => BounceServer::STATUS_CRON_RUNNING));
        return $this->renderJson();
    }
    
    public function actionReset_fbl_servers()
    {
        if (!Yii::app()->request->isAjaxRequest) {
            $this->redirect(array('misc/emergency_actions'));
        }
        FeedbackLoopServer::model()->updateAll(array('status' => FeedbackLoopServer::STATUS_ACTIVE), 'status = :status', array(':status' => FeedbackLoopServer::STATUS_CRON_RUNNING));
        return $this->renderJson();
    }
    
    public function actionApplication_log()
    {
        $request = Yii::app()->request;
        
        if ($request->isPostRequest && $request->getPost('delete') == 1) {
            if (is_file($file = Yii::app()->runtimePath . '/application.log')) {
                @unlink($file);
                Yii::app()->notify->addSuccess(Yii::t('app', 'The application log file has been successfully deleted!'));
            }
        }
        
        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | ' . Yii::t('app', 'Application log'), 
            'pageHeading'       => Yii::t('app', 'Application log'),
            'pageBreadcrumbs'   => array(
                Yii::t('app', 'Application log'),
            ),
        ));
        
        $applicationLog = FileSystemHelper::getFileContents(Yii::app()->runtimePath . '/application.log');
        $this->render('application-log', compact('applicationLog'));
    }
    
    public function actionCampaigns_delivery_logs($archive = null)
    {
        $request   = Yii::app()->request;
        $className = $archive ? 'CampaignDeliveryLogArchive' : 'CampaignDeliveryLog';
        $log       = new $className('search');
        $log->unsetAttributes();
        
        $log->attributes = (array)$request->getQuery($log->modelName, array());

        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | '. Yii::t('misc', 'View campaigns delivery logs'),
            'pageHeading'       => Yii::t('misc', 'View campaigns delivery logs'),
            'pageBreadcrumbs'   => array(
                Yii::t('misc', 'Campaigns delivery logs'),
            )
        ));
        
        $this->render('campaigns-delivery-logs', compact('log', 'archive'));
    }
    
    public function actionCampaigns_bounce_logs()
    {
        $request = Yii::app()->request;
        $log     = new CampaignBounceLog('search');
        $log->unsetAttributes();
        
        $log->attributes = (array)$request->getQuery($log->modelName, array());

        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | '. Yii::t('misc', 'View campaigns bounce logs'),
            'pageHeading'       => Yii::t('misc', 'View campaigns bounce logs'),
            'pageBreadcrumbs'   => array(
                Yii::t('misc', 'Campaigns bounce logs'),
            )
        ));
        
        $this->render('campaigns-bounce-logs', compact('log'));
    }

}