<?php
defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright 2019 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Vladislav Prikhodko (vlados.vp1@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/

class os_cckCoupons extends JTable
{
    var $coup_id = null;
    var $name = null;
    var $type = null;
    var $creation_date = null;
    var $date_start = null;
    var $date_finish = null;
    var $value = null;
    var $used_number = null;
    var $max_uses = null;
    var $entities = null;
    var $user_group_ids = null;
    var $category_ids = null;
    var $publish = null;
    
    /**
     * @param database - A database connector object
     */
    function __construct(&$db){
      parent::__construct('#__os_cck_coupons', 'coup_id', $db);
    }
    
    function store($updateNulls = false){
        if(!parent::store()){
            //echo $this->getError(); exit;
            return false;
        }else{
            return true;
        }
    }
    
    function loadByOrderId($orderId){

        $selectString = "SELECT  row_data FROM #__os_cck_orders_details AS od "
                      . " WHERE od.fk_order_id = " . intval($orderId) . " ORDER BY id ";
        $this->_db->setQuery($selectString);
        $couponData = $this->_db->loadObjectList();

        if( isset($couponData) && count($couponData) > 0 ){

            $string = urldecode( $couponData[0]->row_data );

            //20.05.20026 need later recheck 
            //$string = unserialize( $string, ['allowed_classes' =>  ['stdClass'] ] ) ; - some time error, not clear as reproduce
            //search coupon. full object not unserialized :(  - (some time we have error in serelized objects)
            //errors: why DataBase remove some symbols at save, so: strlen(Value) !=== s:Numer - what we before have in Value 
            //why Value go from User Input for request form
            //replace "row_data" from TEXT to BLOB - possible this help

            $matches = array(); 
            $string = preg_match_all( '/s:6:\"coupon\";(.*;\})s:8/i',$string,  $matches);
            $string = $matches[1][0] ;

            $coupon_tmp = unserialize( $string, ['allowed_classes' =>  ['stdClass'] ] ) ;
            $this->coup_id = $coupon_tmp->coup_id;
            $this->name = $coupon_tmp->name;
            $this->type = $coupon_tmp->type;
            $this->creation_date = null;
            $this->date_start = null;
            $this->date_finish = null;
            $this->value = $coupon_tmp->value;
            $this->used_number = null;
            $this->max_uses = null;
            if( isset($coupon_tmp->entities) ) $this->entities = null;
            if( isset($coupon_tmp->user_group_ids) ) $this->user_group_ids = null;
            if( isset($coupon_tmp->category_ids) ) $this->category_ids = null;
            $this->publish = null;

            return $this;
        }else{
            return null;
        }
    }
    

    
}
