<?php
/**
* 2024 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2024 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

if (!defined('_PS_VERSION_')) {
    exit;
}

class anStatisticsSizeGuide extends ObjectModel
{
    public $id_views;

    public $id;

    public $id_widget;
    public $id_product;
	public $views;
	public $date_add;



    public static $definition = [
        'table' => 'an_size_guide_widgets_views',
        'primary' => 'id_views',
        'multilang' => false,
        'fields' => [
            'id_widget' => ['type' =>self::TYPE_INT],
            'id_product' => ['type' =>self::TYPE_INT],

            'views' => ['type' =>self::TYPE_INT],

            'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
        ],
    ];

    /**
     * Formula constructor.
     *
     * @param null $id
     */
    public function __construct($id = null)
    {
        parent::__construct($id);
    }

    public static function saveRecordingStatistics($idWidget)
    {
        $sql = '
            SELECT `id_views` FROM  `' . _DB_PREFIX_ . 'an_size_guide_widgets_views` 
            WHERE `id_widget` = '.(int) $idWidget .' AND `date_add` = "'.date('Y-m-d').'"
        ';	

        $idViews = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);

        $obj = new anStatisticsSizeGuide($idViews);
        $obj->id_widget = $idWidget;
        $obj->views++;

        $obj->save();
    }
}
