1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?php
namespace hji\common\models;
class JsVars
{
static protected $vars = array();
static function loadVars()
{
$vars = apply_filters('hji-js-vars', self::getVars());
$vars = ($vars) ? $vars : new \stdClass;
if ($vars)
{
$script = "hjiSettings = " . json_encode($vars) . ';';
echo "<script type='text/javascript'>\n";
echo "/* <![CDATA[ */\n";
echo "$script\n";
echo "/* ]]> */\n";
echo "</script>\n";
}
}
static public function setVar($container, $key, $value)
{
$result = $value;
if (isset(self::$vars[$container][$key]))
{
if (is_array(self::$vars[$container][$key]) && is_array($value))
{
$result = array_merge(self::$vars[$container][$key], $value);
}
}
self::$vars[$container][$key] = $result;
}
static public function getVars($container = false)
{
if ($container && isset(self::$vars[$container]))
{
return self::$vars[$container];
}
return self::$vars;
}
}
class PluginJsVars
{
private $slug;
function __construct($slug)
{
$this->slug = $slug;
}
function setVar($key, $value)
{
JsVars::setVar($this->slug, $key, $value);
}
function getVars()
{
return JsVars::getVars($this->slug);
}
}