<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('DbIndeedAbstract')) require_once ULP_PATH . 'classes/Abstracts/DbIndeedAbstract.class.php';
if (class_exists('DbUserEntitiesRelationMetas')) return;
class DbUserEntitiesRelationMetas extends DbIndeedAbstract{
	/**
	 * @var string
	 */
	protected $table = '';
	/**
	 * @param none
	 * @reutrn none
	 */
	public function __construct(){
		global $wpdb;
		$this->table = $wpdb->prefix . 'ulp_user_entities_relations_metas';
	}
	/**
	 * @param int
	 * @param string
	 * @return mixed
	 */
	public function getMeta($user_entity_relation_id=0, $meta_key=''){
		$user_entity_relation_id = esc_sql($user_entity_relation_id);
		$meta_key = esc_sql($meta_key);
		if (empty($user_entity_relation_id)) return FALSE;
		$data = $this->getVar('meta_value', " user_entity_relation_id=$user_entity_relation_id AND meta_key='$meta_key' ");
		return $data;
	}
	/**
	 * @param int
	 * @param string
	 * @param string
	 * @return bool
	 */
	public function saveMeta($user_entity_relation_id=0, $meta_key='', $meta_value=null){
		global $wpdb;
		$user_entity_relation_id = esc_sql($user_entity_relation_id);
		$meta_key = esc_sql($meta_key);
		$meta_value = esc_sql($meta_value);
		if ($this->getMeta($user_entity_relation_id, $meta_key)!==null){
			/// update
			return $this->update("meta_value='$meta_value'", "user_entity_relation_id=$user_entity_relation_id AND meta_key='$meta_key'");
		} else {
			/// create
			return $this->insert("null, $user_entity_relation_id, '$meta_key', '$meta_value'");
		}
	}
	/**
	 * @param int (module id)
	 * @param int (item id)
	 * @return bool
	 */
	public function deleteItem($user_entity_relation_id=0, $meta_key=''){
		$user_entity_relation_id = esc_sql($user_entity_relation_id);
		$meta_key = esc_sql($meta_key);
		return $this->delete("user_entity_relation_id=$user_entity_relation_id AND meta_key='$meta_key'");
	}
	
	public function deleteAllRelation($user_entity_relation_id=0){
		$user_entity_relation_id = esc_sql($user_entity_relation_id);
		return $this->delete("user_entity_relation_id=$user_entity_relation_id");
	}
}
