<?php
/**
 * app/plugins/portfolio/models/links.php
 *
 * This class is used for the manipulation of the links database table.
 *
 * @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\Models;

use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\Plugins\Portfolio as Plugin;

class Links extends DatabaseModel {
    public $tableName = 'portfolio_links';
    public $databaseMatrix = [
        [ 'section', 'varchar', '32' ],
        [ 'title', 'varchar', '128' ],
        [ 'image', 'varchar', '256' ],
        [ 'url', 'varchar', '256' ],
        [ 'description', 'text', '' ],
    ];
    public $plugin;

    /**
     * The model constructor.
     */
    public function __construct() {
        parent::__construct();
        $this->plugin = new Plugin;
    }

    public function create( $section, $title, $image, $url, $description ) {
        if ( !$this->plugin->checkEnabled() ) {
            Debug::info( 'Portfolio is disabled in the config.' );
            return false;
        }
        $fields = [
            'section' => $section,
            'title' => $title,
            'image' => $image,
            'url' => $url,
            'description' => $description,
        ];
        if ( !self::$db->insert( $this->tableName, $fields ) ) {
            Debug::info( 'Links::create - failed to insert to db' );
            return false;
        }
        return self::$db->lastId();
    }
    
    public function update( $id, $fields ) {
        if ( !Check::id( $id ) ) {
            Debug::info( 'modelBlog: illegal ID.' );
            return false;
        }
        if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
            // new CustomException( 'linkUpdate' );
            Debug::error( "Links:update: $id not updated: $fields" );
            return false;
        }
        return true;
    }
}
