<?php

class ClassFeature extends ClassConfig {
    public function __construct(){}
    public function __destruct(){}
    
	public function getFeatures($lang_code='source'){
		$this->_langCode = $lang_code;
		$oPDOLink = ClassConfig::databaseConnect();
		$sql = "
		SELECT cf.*,
		(SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=cf.url_feature_translation_id) AS url,
		(SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=cf.label_feature_translation_id) AS menu_label
		FROM core_feature cf
		WHERE cf.is_available_for_guest=TRUE
			AND cf.is_active=TRUE
		ORDER BY cf.priority ASC;
		";
		$execSQL = $oPDOLink->prepare($sql);
		$execSQL->execute(array());
		$rows = $execSQL->fetchAll(PDO::FETCH_OBJ);
		return $rows;
	}
  
  public function getSubFeatures($parent_id, $lang_code='source'){
    $this->_parentId = $parent_id;
		$this->_langCode = $lang_code;
		$oPDOLink = ClassConfig::databaseConnect();
    $sql = "
		SELECT cf.*,
		(SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=cf.url_feature_translation_id) AS url,
		(SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=cf.label_feature_translation_id) AS menu_label
		FROM core_feature cf
		WHERE cf.parent_id=:parent_id
      AND cf.is_available_for_guest=TRUE
  		AND cf.is_active=TRUE
		ORDER BY cf.priority ASC;
    ";
		$execSQL = $oPDOLink->prepare($sql);
		$execSQL->execute(array(':parent_id'=>$this->_parentId));
		$rows = $execSQL->fetchAll(PDO::FETCH_OBJ);
		return $rows;
  }
  
  public function getSubFeaturesForUser($parent_id, $lang_code='source'){
    $this->_parentId = $parent_id;
		$this->_langCode = $lang_code;
		$oPDOLink = ClassConfig::databaseConnect();
    $sql = "
		SELECT cf.*,
		(SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=cf.url_feature_translation_id) AS url,
		(SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=cf.label_feature_translation_id) AS menu_label
		FROM core_feature cf
		WHERE cf.parent_id=:parent_id
      AND cf.is_available_for_guest=FALSE
  		AND cf.is_active=TRUE
		ORDER BY cf.priority ASC;
    ";
		$execSQL = $oPDOLink->prepare($sql);
		$execSQL->execute(array(':parent_id'=>$this->_parentId));
		$rows = $execSQL->fetchAll(PDO::FETCH_OBJ);
		return $rows;
  }
  
  public function getFeaturesOfUser($user_id, $lang_code='source'){
    $this->_userId = $user_id;
    $this->_langCode = $lang_code;
    $oPDOLink = ClassConfig::databaseConnect();
    $sql = "
    SELECT f.*,
		(SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=f.url_feature_translation_id) AS url,
		(SELECT ".$this->_langCode." FROM core_feature_translation WHERE id=f.label_feature_translation_id) AS menu_label
    FROM useruser_corefeature_rel r
      INNER JOIN core_feature f
        ON r.core_feature_id=f.id
    WHERE r.user_id=:user_id
      AND f.is_active=TRUE
    ORDER BY f.priority ASC;
    ";
    $execSQL = $oPDOLink->prepare($sql);
    $execSQL->execute(array(':user_id'=>$this->_userId));
    $rows = $execSQL->fetchAll(PDO::FETCH_OBJ);
    return $rows;
  }
    
	public function checkAccess($user_id, $menu_code){
        $this->_userId = $user_id;
        $this->_menuCode = $menu_code;
        $oPDOLink = ClassConfig::databaseConnect();
		
		$sql="
		SELECT COUNT(core_feature_id) AS autorization
		FROM useruser_corefeature_rel
		WHERE user_id=:user_id
			AND core_feature_id=(
				SELECT id
				FROM core_feature
				WHERE code=:menu_code
				LIMIT 1
			);
        ";
		
        $execSQL = $oPDOLink->prepare($sql);
        $execSQL->execute(array(
			':user_id'=>$this->_userId,
			':menu_code'=>$this->_menuCode
		));
        $row = $execSQL->fetch(PDO::FETCH_ASSOC);
        return $row;
	}
	
	public function listFeatures(){
        $oPDOLink = ClassConfig::databaseConnect();
		
        $sql = "
        SELECT f.*
        FROM core_feature f
        ORDER BY is_menu_backend ASC, priority ASC;
        ";
		
        $execSQL = $oPDOLink->prepare($sql);
        $execSQL->execute(array());
        $rows = $execSQL->fetchAll(PDO::FETCH_ASSOC);
        return $rows;
	}
	
	public function setActivesFeatures($data){
		$this->_data = $data;
		$is_first_insertion = true;
		$args_number = "?";
		$args_list = array();
		$message = array();
        $oPDOLink = ClassConfig::databaseConnect();
		
		$sql = "
		UPDATE core_feature
		SET is_active=FALSE;
		";
		
        $execSQL = $oPDOLink->prepare($sql);
        if(!$execSQL->execute(array())){
			return array('state'=>'failed');
		}
		
		foreach($this->_data as $k=>$v){
			if($k != 'submit_active_features'){
				if(!$is_first_insertion){
					$args_number .= ', ?';
				} else{
					$is_first_insertion = false;
				}
				$args_list[] = $k;
			}
		}
		
		$sql = "
		UPDATE core_feature
		SET is_active=TRUE
		WHERE code IN (".$args_number.");
		";
		
		$execSQL = $oPDOLink->prepare($sql);
        if($execSQL->execute($args_list)){
			$message['state'] = 'success';
		} else{
			$message['state'] = 'failed';
		}
		return $message;
	}
	
	public function setUserFeatures($user_id, $data){
		$this->_data = $data;
		$this->_userId = $user_id;
		$nb_passages = 0;
		$args = "";
		$args_list = array();
		$message = array();
		$list_of_features_ids = array();
		$list_of_features = ClassFeatures::listFeatures();
        $oPDOLink = ClassConfig::databaseConnect();
		
		$sql = "DELETE FROM useruser_corefeature_rel WHERE user_id=:user_id;";
		$execSQL = $oPDOLink->prepare($sql);
        if(!$execSQL->execute(array(':user_id'=>$this->_userId))){
			return array('state'=>'failed');
		}
		
		foreach($list_of_features as $k=>$v){
			$list_of_features_ids[$list_of_features[$k]['code']] = $list_of_features[$k]['id'];
		}
		
		foreach($this->_data as $k=>$v){
			if(array_key_exists($k, $list_of_features_ids)){
				if($nb_passages > 0){
					$args .= ',';
				}
				$args .= '(:user_id'.$nb_passages.', :core_feature_id'.$nb_passages.')';
				$args_list[':user_id'.$nb_passages] = $this->_userId;
				$args_list[':core_feature_id'.$nb_passages] = $list_of_features_ids[$k];
				$nb_passages++;
			}
		}
		
		if(!empty($args_list)){
			$sql = "
			INSERT INTO useruser_corefeature_rel(user_id, core_feature_id)
			VALUES".$args.";
			";
			$execSQL = $oPDOLink->prepare($sql);
			if($execSQL->execute($args_list)){
				$message['state'] = 'success';
			} else{
				$message['state'] = 'failed';
			}
		} else {
			$message['state'] = 'no_data_to_set';
		}
		return $message;
	}
	
}