<?php
class  SettingsRepository extends EntityRepository {

    private $table = 'settings';

    public function save(array $data, $table = null) {
        return parent::save($data, $this->table);
    }
    
    public function delete($id, $table = null) {
        return parent::delete($id, $this->table);
    }

    public function update($id, $data, $table = null) {
    }

    public function getById($id, $table = null,$selectAux = null) {
        return parent::getById($id, $this->table,$selectAux);
    }
    
    public function _setSettings($setting,$value){
        $query = "UPDATE $this->table SET value = '$value' WHERE `key` = '$setting' ";
        $this->query($query);
    }
    
    public function setLastUserLogued($user_id){
        $this->_setSettings('last_user_logued',$user_id);
    }
    
    public function getListSettings(){
        if(isset($_SESSION['usuario']['settings']) && !is_null($_SESSION['usuario']['settings']) && !empty($_SESSION['usuario']['settings'])){
            return $_SESSION['usuario']['settings'];
        }
        
        $query = "SELECT * FROM settings";
        $result = $this->query($query);
        
        if($result->num_rows > 0){
            $result = $this->resultToArray($result);
            $settings = array();
            foreach($result as $row){
                $settings[$row['key']] = $row['value'];
            }
            
            $_SESSION['usuario']['settings'] = $settings;
            
            return $settings;
        }
    }
}