<?php
/**
 * @package    Proxim
 * @author     Davison Pro <davis@davisonpro.dev | https://davisonpro.dev>
 * @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\ObjectModel;

/**
 * PaperType
 */
class PaperType extends ObjectModel
{
    const PAPER_TYPE_OTHER = 64;

	/** @var $id PaperType ID */
    public $id;

    /** @var string title */
    public $title;
    
    /** @var string date_upd */
	public $date_upd;
	
	/** @var string date_add */
	public $date_add;
	
	/**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'paper_type',
        'primary' => 'paper_type_id',
        'fields' => array( 
            'title' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true, 'size' => 255),
			'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 paper types by site ID.
     *
     * @return array PaperTypes
     */
    public static function getPaperTypes()
    {
        $sql = new DbQuery();
        $sql->select('pt.*');
        $sql->from('paper_type', 'pt');
        $sql->orderBy('pt.paper_type_id ASC');

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

    public static function getTopPaperTypes( $limit = 5 ) {
        $sql = new DbQuery();
        $sql->select('o.paper_type_id, COUNT(*) AS paper_type_count');
        $sql->from('order', 'o');
        $sql->where('o.paper_type_id != ' . (int) self::PAPER_TYPE_OTHER );
        $sql->groupBy('o.paper_type_id');
        $sql->orderBy('paper_type_count DESC');
        $sql->limit( $limit );

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

        return $result;
    }
}