<?php
/**
 * app/plugins/portfolio/controllers/admin/portfolio.php
 *
 * This is the Portfolio admin controller.
 *
 * @package TP Portfolio
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers\Admin;

use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Classes\Forms;
use TheTempusProject\Models\Links;

class Portfolio extends AdminController {
    public static $links;

    public function __construct() {
        parent::__construct();
        self::$links = new Links;
        self::$title = 'Admin - Portfolio';
        $view = Navigation::activePageSelect( 'nav.admin', '/admin/portfolio' );
        Components::set( 'ADMINNAV', $view );
    }

    public function index( $data = null ) {
        Views::view( 'portfolio.admin.list', self::$links->listPaginated() );
    }

    public function create( $data = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'portfolio.admin.create' );
        }
        if ( !Forms::check( 'newLink' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return $this->index();
        }
        $result = self::$links->create( Input::post( 'section' ), Input::post( 'title' ), Input::post( 'image' ), Input::post( 'url' ), Input::post( 'description' ) );
        if ( $result ) {
            Issues::add( 'success', 'Your link has been created.' );
            return $this->index();
        } else {
            Issues::add( 'error', [ 'There was an unknown error submitting your data.' => Check::userErrors() ] );
            return $this->index();
        }
    }

    public function edit( $data = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'portfolio.admin.edit', self::$links->findById( $data ) );
        }
        if ( !Forms::check( 'editLink' ) ) {
            Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
            return $this->index();
        }
        $fields = [
            'section' => Input::post( 'section' ),
            'title' => Input::post( 'title' ),
            'image' => Input::post( 'image' ),
            'url' => Input::post( 'url' ),
            'description' => Input::post( 'description' ),
        ];
        if ( self::$links->update( $data, $fields ) ) {
            Issues::add( 'success', 'Link Updated.' );
            return $this->index();
        }
        Issues::add( 'error', 'There was an error with your request.' );
        $this->index();
    }

    public function view( $data = null ) {
        $linkData = self::$links->findById( $data );
        if ( $linkData !== false ) {
            return Views::view( 'portfolio.admin.view', $linkData );
        }
        Issues::add( 'error', 'Link not found.' );
        $this->index();
    }

    public function delete( $data = null ) {
        if ( $data == null ) {
            if ( Input::exists( 'P_' ) ) {
                $data = Input::post( 'P_' );
            }
        }
        if ( !self::$links->delete( (array) $data ) ) {
            Issues::add( 'error', 'There was an error with your request.' );
        } else {
            Issues::add( 'success', 'Link has been deleted' );
        }
        $this->index();
    }
}
