<?php

class CTLPPU_Models_User extends CTLPPU_Models_Abstract_Models
{
    protected $_options;

    function __construct()
    {
        parent::__construct();
        $this->_options = json_decode(json_encode(CTLPPU_Classes_Tools::getOptions()));
    }

    public function syncUser()
    {

        $sync = CTLPPU_Classes_Tools::ctlppu_remote_post(
            _CTLPPU_API_PATH_ . 'website/add'
            , CTLPPU_Classes_ObjController::getClass('CTLPPU_Models_Api')->getSyncArgs());

        $sync = json_decode($sync);

	    if (isset($sync->website_id) && $sync->website_id > 0) {
		    $this->_saveUserOptions($sync);
		    CTLPPU_Classes_Tools::dump('user synced');
	    }

        return false;


    }

    private function _saveUserOptions($sync)
    {
        CTLPPU_Classes_Tools::saveOptions("website_id", $sync->website_id);

        $default_fields = CTLPPU_Classes_ObjController::getClass('CTLPPU_Models_Settings')->getDefaultFields();
        CTLPPU_Classes_Tools::saveOptions("ctlppu_display_fields", $default_fields);
        CTLPPU_Classes_Tools::saveOptions("fields_hash", md5(serialize($default_fields)));
        CTLPPU_Classes_Tools::saveOptions("synced_date", strtotime(gmdate('Y-m-d H:i:s')));
        CTLPPU_Classes_Tools::saveOptions("synced", 1);
    }

    public function isSynced()
    {
        if (!isset($this->_options))
            $this->_options = json_decode(json_encode(CTLPPU_Classes_Tools::getOptions()));

        if ($this->_options->synced == 1 && $this->_options->website_id > 0) {
            return true;
        }

        return false;
    }

}
