<?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_get(
            _CTLPPU_API_PATH_ . 'sync'
            , CTLPPU_Classes_ObjController::getClass('CTLPPU_Models_Api')->getSyncArgs());

        $sync = json_decode($sync);

        if (isset($sync->blog_id) && $sync->blog_id > 0 && $sync->token != "") {

            $this->_saveUserOptions($sync);
             CTLPPU_Classes_Tools::dump('user synced');
        }

        return false;


    }

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

        $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->blog_id > 0) {
            return true;
        }

        return false;
    }

}
