<?php
/**
 * 2021 Anvanto
 *
 * NOTICE OF LICENSE
 *
 * This file is not open source! Each license that you purchased is only available for 1 wesite only.
 * If you want to use this file on more websites (or projects), you need to purchase additional licenses. 
 * You are not allowed to redistribute, resell, lease, license, sub-license or offer our resources to any third party.
 *
 *  @author Anvanto <anvantoco@gmail.com>
 *  @copyright  2021 Anvanto
 *  @license    Valid for 1 website (or project) for each purchase of license
 *  International Registered Trademark & Property of Anvanto
 */

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();
    }
}
