<?php
/**
 * ThemeREX Framework: theme variables storage
 *
 * @package	seohub
 * @since	seohub 1.0
 */

// Disable direct call
if ( ! defined( 'ABSPATH' ) ) { exit; }

// Get theme variable
if (!function_exists('seohub_storage_get')) {
	function seohub_storage_get($var_name, $default='') {
		global $SEOHUB_GLOBALS;
		return isset($SEOHUB_GLOBALS[$var_name]) ? $SEOHUB_GLOBALS[$var_name] : $default;
	}
}

// Set theme variable
if (!function_exists('seohub_storage_set')) {
	function seohub_storage_set($var_name, $value) {
		global $SEOHUB_GLOBALS;
		$SEOHUB_GLOBALS[$var_name] = $value;
	}
}

// Check if theme variable is empty
if (!function_exists('seohub_storage_empty')) {
	function seohub_storage_empty($var_name, $key='', $key2='') {
		global $SEOHUB_GLOBALS;
		if (!empty($key) && !empty($key2))
			return empty($SEOHUB_GLOBALS[$var_name][$key][$key2]);
		else if (!empty($key))
			return empty($SEOHUB_GLOBALS[$var_name][$key]);
		else
			return empty($SEOHUB_GLOBALS[$var_name]);
	}
}

// Check if theme variable is set
if (!function_exists('seohub_storage_isset')) {
	function seohub_storage_isset($var_name, $key='', $key2='') {
		global $SEOHUB_GLOBALS;
		if (!empty($key) && !empty($key2))
			return isset($SEOHUB_GLOBALS[$var_name][$key][$key2]);
		else if (!empty($key))
			return isset($SEOHUB_GLOBALS[$var_name][$key]);
		else
			return isset($SEOHUB_GLOBALS[$var_name]);
	}
}

// Inc/Dec theme variable with specified value
if (!function_exists('seohub_storage_inc')) {
	function seohub_storage_inc($var_name, $value=1) {
		global $SEOHUB_GLOBALS;
		if (empty($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = 0;
		$SEOHUB_GLOBALS[$var_name] += $value;
	}
}

// Concatenate theme variable with specified value
if (!function_exists('seohub_storage_concat')) {
	function seohub_storage_concat($var_name, $value) {
		global $SEOHUB_GLOBALS;
		if (empty($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = '';
		$SEOHUB_GLOBALS[$var_name] .= $value;
	}
}

// Get array (one or two dim) element
if (!function_exists('seohub_storage_get_array')) {
	function seohub_storage_get_array($var_name, $key, $key2='', $default='') {
		global $SEOHUB_GLOBALS;
		if (empty($key2))
			return !empty($var_name) && !empty($key) && isset($SEOHUB_GLOBALS[$var_name][$key]) ? $SEOHUB_GLOBALS[$var_name][$key] : $default;
		else
			return !empty($var_name) && !empty($key) && isset($SEOHUB_GLOBALS[$var_name][$key][$key2]) ? $SEOHUB_GLOBALS[$var_name][$key][$key2] : $default;
	}
}

// Set array element
if (!function_exists('seohub_storage_set_array')) {
	function seohub_storage_set_array($var_name, $key, $value) {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = array();
		if ($key==='')
			$SEOHUB_GLOBALS[$var_name][] = $value;
		else
			$SEOHUB_GLOBALS[$var_name][$key] = $value;
	}
}

// Set two-dim array element
if (!function_exists('seohub_storage_set_array2')) {
	function seohub_storage_set_array2($var_name, $key, $key2, $value) {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = array();
		if (!isset($SEOHUB_GLOBALS[$var_name][$key])) $SEOHUB_GLOBALS[$var_name][$key] = array();
		if ($key2==='')
			$SEOHUB_GLOBALS[$var_name][$key][] = $value;
		else
			$SEOHUB_GLOBALS[$var_name][$key][$key2] = $value;
	}
}

// Add array element after the key
if (!function_exists('seohub_storage_set_array_after')) {
	function seohub_storage_set_array_after($var_name, $after, $key, $value='') {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = array();
		if (is_array($key))
			seohub_array_insert_after($SEOHUB_GLOBALS[$var_name], $after, $key);
		else
			seohub_array_insert_after($SEOHUB_GLOBALS[$var_name], $after, array($key=>$value));
	}
}

// Add array element before the key
if (!function_exists('seohub_storage_set_array_before')) {
	function seohub_storage_set_array_before($var_name, $before, $key, $value='') {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = array();
		if (is_array($key))
			seohub_array_insert_before($SEOHUB_GLOBALS[$var_name], $before, $key);
		else
			seohub_array_insert_before($SEOHUB_GLOBALS[$var_name], $before, array($key=>$value));
	}
}

// Push element into array
if (!function_exists('seohub_storage_push_array')) {
	function seohub_storage_push_array($var_name, $key, $value) {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = array();
		if ($key==='')
			array_push($SEOHUB_GLOBALS[$var_name], $value);
		else
			array_push($SEOHUB_GLOBALS[$var_name][$key], $value);
	}
}

// Inc/Dec array element with specified value
if (!function_exists('seohub_storage_inc_array')) {
	function seohub_storage_inc_array($var_name, $key, $value=1) {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = array();
		if (empty($SEOHUB_GLOBALS[$var_name][$key])) $SEOHUB_GLOBALS[$var_name][$key] = 0;
		$SEOHUB_GLOBALS[$var_name][$key] += $value;
	}
}

// Concatenate array element with specified value
if (!function_exists('seohub_storage_concat_array')) {
	function seohub_storage_concat_array($var_name, $key, $value) {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS[$var_name])) $SEOHUB_GLOBALS[$var_name] = array();
		if (empty($SEOHUB_GLOBALS[$var_name][$key])) $SEOHUB_GLOBALS[$var_name][$key] = '';
		$SEOHUB_GLOBALS[$var_name][$key] .= $value;
	}
}

// Call object's method
if (!function_exists('seohub_storage_call_obj_method')) {
	function seohub_storage_call_obj_method($var_name, $method, $param=null) {
		global $SEOHUB_GLOBALS;
		if ($param===null)
			return !empty($var_name) && !empty($method) && isset($SEOHUB_GLOBALS[$var_name]) ? $SEOHUB_GLOBALS[$var_name]->$method(): '';
		else
			return !empty($var_name) && !empty($method) && isset($SEOHUB_GLOBALS[$var_name]) ? $SEOHUB_GLOBALS[$var_name]->$method($param): '';
	}
}

// Get object's property
if (!function_exists('seohub_storage_get_obj_property')) {
	function seohub_storage_get_obj_property($var_name, $prop, $default='') {
		global $SEOHUB_GLOBALS;
		return !empty($var_name) && !empty($prop) && isset($SEOHUB_GLOBALS[$var_name]->$prop) ? $SEOHUB_GLOBALS[$var_name]->$prop : $default;
	}
}
?>