<?php
/**
 * @package    Proxim
 * @author     Dominic Tanui <dominic@codeworldweb.com | https://codeworldweb.com>
 * @copyright  2019 Proxim
 * @version    1.5.0
 * @since      File available since Release 1.0.0
 */

namespace Proxim\Preference;

use Db;
use Proxim\Database\DbQuery;
use Proxim\Cache\Cache;
use Proxim\ObjectModel;
use Proxim\Util\ArrayUtils;
use Proxim\Util\DateUtils;
use Proxim\Validate;
use Proxim\Tools;

/**
 * Deadline
 */
class Deadline extends ObjectModel
{
	/** @var $id Deadline ID */
    public $id;

	/** @var $id Site ID */
    public $site_id;

    /** @var int academic_level_id */
    public $academic_level_id;

    /** @var string name */
    public $name;

    /** @var float price_per_page */
    public $price_per_page;

    /** @var int hrs_customer */
    public $hrs_customer;
    
    /** @var int hrs_writer */
    public $hrs_writer;
    
    /** @var int min_pages */
	public $min_pages = 1;
    
    /** @var string date_upd */
	public $date_upd;
	
	/** @var string date_add */
	public $date_add;
	
	/**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'deadline',
        'primary' => 'deadline_id',
        'fields' => array( 
            'site_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required'=> true), 
            'academic_level_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required'=> true),
            'name' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true, 'size' => 255),
            'price_per_page' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required'=> true),
            'hrs_customer' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required'=> true),
            'hrs_writer' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required'=> true),
            'min_pages' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
			'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
            'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
        ),
    );

    /**
     * constructor.
     *
     * @param null $id
     */
    public function __construct($id = null) {
		parent::__construct($id);
    }
    
    /**
     * Return deadlines by site ID.
     *
     * @return array Deadlines
     */
    public static function getDeadlines( $site_id = null )
    {
        $sql = new DbQuery();
        $sql->select('d.*');
        $sql->from('deadline', 'd');
        if($site_id) {
            $sql->where('d.site_id = ' . (int) $site_id );
        }

        $sql->orderBy('d.hrs_customer ASC');

        $result = Db::getInstance()->executeS($sql);
        
        return $result;
    }
}