<?php
/**
 * app/plugins/subscribe/controllers/subscribe.php
 *
 * This is the home controller for the subscribe plugin.
 *
 * @package TP Subscribe
 * @version 5.0.1
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers;

use TheTempusProject\Classes\Controller;
use TheTempusProject\Classes\Forms;
use TheTempusProject\Hermes\Functions\Redirect;
use TheTempusProject\Bedrock\Functions\Session;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Classes\Email;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Models\Subscribe as SubscribeModel;

class Subscribe extends Controller {
    private static $subscribe;

    public function __construct() {
        parent::__construct();
        self::$subscribe = new SubscribeModel;
    }

    public function index() {
        self::$title = 'Subscribe - {SITENAME}';
        self::$pageDescription = 'We are always publishing great content and keeping our members up to date. If you would like to join our list, you can subscribe here.';
        if ( !Input::exists( 'email' ) ) {
            return Views::view( 'subscribe.subscribe' );
        }
        if ( !Forms::check( 'subscribe' ) ) {
            Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
            return Views::view( 'subscribe.subscribe' );
        }
        if ( !self::$subscribe->add( Input::post( 'email' ) ) ) {
            Issues::add( 'error', 'There was an error with your request, please try again.' );
            return Views::view( 'subscribe.subscribe' );
        }
        $data = self::$subscribe->get( Input::post( 'email' ) );
        Email::send( Input::post( 'email' ), 'subscribe', $data->confirmationCode, ['template' => true] );
        Issues::add( 'success', 'You have successfully been subscribed to our mailing list.' );
    }

    public function unsubscribe( $email = null, $code = null ) {
        self::$title = '{SITENAME}';
        self::$pageDescription = '';
        if ( !empty( $email ) && !empty( $code ) ) {
            if ( self::$subscribe->unsubscribe( $email, $code ) ) {
                return Issues::add( 'success', 'You have been successfully unsubscribed from receiving further mailings.' );
            }
            Issues::add( 'error', 'There was an error with your request.' );
            return Views::view( 'subscribe.unsubscribe' );
        }
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'subscribe.unsubscribe' );
        }
        if ( !Forms::check( 'unsubscribe' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return Views::view( 'subscribe.unsubscribe' );
        }
        $data = self::$subscribe->get( Input::post( 'email' ) );
        if ( empty( $data ) ) {
            Issues::add( 'notice', 'There was an error with your request.' );
            return Views::view( 'subscribe.unsubscribe' );
        }
        Email::send( Input::post( 'email' ), 'unsubInstructions', $data->confirmationCode, ['template' => true] );
        Session::flash( 'success', 'An email with instructions on how to unsubscribe has been sent to your email.' );
        Redirect::to( 'home/index' );
    }
}
