* @copyright 2007-2017 PrestaShop SA * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class JQZoomConfig extends ObjectModel { public $id; /** @var string Key */ public $name; public $id_shop_group; public $id_shop; /** @var string Value */ public $value; /** @var string Object creation date */ public $date_add; /** @var string Object last modification date */ public $date_upd; /** * @see ObjectModel::$definition */ public static $definition = array( 'table' => 'jqzoomconfig', 'primary' => 'id_jqzoomconfig', 'fields' => array( 'name' => array( 'type' => self::TYPE_STRING, 'validate' => 'isConfigName', 'required' => true, 'size' => 32 ), 'id_shop_group' => array('type' => self::TYPE_NOTHING, 'validate' => 'isUnsignedId'), 'id_shop' => array('type' => self::TYPE_NOTHING, 'validate' => 'isUnsignedId'), 'value' => array('type' => self::TYPE_STRING), 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), ), ); /** @var array Configuration cache */ protected static $_JQZOOMCONF = null; /** @var array Vars types */ protected static $types = array(); /** * Return ID a configuration key * * @param string $key * @param int $id_shop_group * @param int $id_shop * @return int */ public static function getIdByName($key, $id_shop_group = null, $id_shop = null) { if ($id_shop === null) { $id_shop = Shop::getContextShopID(); } if ($id_shop_group === null) { $id_shop_group = Shop::getContextShopGroupID(); } $sql = 'SELECT id_jqzoomconfig FROM ' . _DB_PREFIX_ . 'jqzoomconfig WHERE name = \'' . pSQL($key) . '\'' . JQZoomConfig::sqlRestriction($id_shop_group, $id_shop); return (int)Db::getInstance()->getValue($sql); } /** * Load all configuration data */ public static function loadConfiguration() { self::$_JQZOOMCONF = array(); $sql = 'SELECT `name`, id_shop_group, id_shop, `value` FROM `' . _DB_PREFIX_ . 'jqzoomconfig`'; if (!$results = Db::getInstance()->executeS($sql)) { return; } foreach ($results as $row) { if ($row['id_shop']) { self::$_JQZOOMCONF['shop'][$row['id_shop']][$row['name']] = $row['value']; } else { if ($row['id_shop_group']) { self::$_JQZOOMCONF['group'][$row['id_shop_group']][$row['name']] = $row['value']; } else { self::$_JQZOOMCONF['global'][$row['name']] = $row['value']; } } } } /** * Get a single configuration value (in one language only) * * @param string $key Key wanted * @param null $id_shop_group * @param null $id_shop * @return string Value */ public static function get($key, $id_shop_group = null, $id_shop = null) { // If conf if not initialized, try manual query if (!self::$_JQZOOMCONF) { JQZoomConfig::loadConfiguration(); if (!self::$_JQZOOMCONF) { return Db::getInstance()->getValue('SELECT `value` FROM `' . _DB_PREFIX_ . 'jqzoomconfig` WHERE `name` = "' . pSQL($key) . '"'); } } if ($id_shop === null) { $id_shop = Shop::getContextShopID(); } if ($id_shop_group === null) { $id_shop_group = Shop::getContextShopGroupID(); } if ($id_shop && JQZoomConfig::hasKey($key, null, $id_shop)) { return self::$_JQZOOMCONF['shop'][$id_shop][$key]; } else { if ($id_shop_group && JQZoomConfig::hasKey($key, $id_shop_group)) { return self::$_JQZOOMCONF['group'][$id_shop_group][$key]; } else { if (JQZoomConfig::hasKey($key)) { return self::$_JQZOOMCONF['global'][$key]; } } } return false; } /** * Get a single configuration value (in one language only) * * @param null $id_shop_group * @param null $id_shop * @return string Value */ public static function getAll($id_shop_group = null, $id_shop = null) { // If conf if not initialized, try manual query if (!self::$_JQZOOMCONF) { JQZoomConfig::loadConfiguration(); } if ($id_shop === null) { $id_shop = Shop::getContextShopID(); } if ($id_shop_group === null) { $id_shop_group = Shop::getContextShopGroupID(); } if ($id_shop) { return self::$_JQZOOMCONF['shop'][$id_shop]; } else { if ($id_shop_group) { return self::$_JQZOOMCONF['group'][$id_shop_group]; } else { return self::$_JQZOOMCONF['global']; } } } /** * Check if key exists in configuration * * @param string $key * @param int $id_shop_group * @param int $id_shop * @return bool */ public static function hasKey($key, $id_shop_group = null, $id_shop = null) { if ($id_shop) { return isset(self::$_JQZOOMCONF['shop'][$id_shop]) && array_key_exists($key, self::$_JQZOOMCONF['shop'][$id_shop]); } else { if ($id_shop_group) { return isset(self::$_JQZOOMCONF['group'][$id_shop_group]) && array_key_exists($key, self::$_JQZOOMCONF['group'][$id_shop_group]); } } return isset(self::$_JQZOOMCONF['global']) && array_key_exists($key, self::$_JQZOOMCONF['global']); } /** * Set TEMPORARY a single configuration value (in one language only) * * @param string $key Key wanted * @param $value * @param int $id_shop_group * @param int $id_shop */ public static function set($key, $value, $id_shop_group = null, $id_shop = null) { if (!Validate::isConfigName($key)) { die(Tools::displayError()); } if ($id_shop === null) { $id_shop = Shop::getContextShopID(); } if ($id_shop_group === null) { $id_shop_group = Shop::getContextShopGroupID(); } if ($id_shop) { self::$_JQZOOMCONF['shop'][$id_shop][$key] = $value; } else { if ($id_shop_group) { self::$_JQZOOMCONF['group'][$id_shop_group][$key] = $value; } else { self::$_JQZOOMCONF['global'][$key] = $value; } } } /** * Update configuration key and value into database (automatically insert if key does not exist) * * @param string $key Key * @param $value * @param boolean $html Specify if html is authorized in value * @param int $id_shop_group * @param int $id_shop * @return bool Update result */ public static function updateValue($key, $value, $html = false, $id_shop_group = null, $id_shop = null) { if (!Validate::isConfigName($key)) { die(Tools::displayError()); } if ($id_shop === null) { $id_shop = Shop::getContextShopID(); } if ($id_shop_group === null) { $id_shop_group = Shop::getContextShopGroupID(); } $result = true; if ((string)$value === JQZoomConfig::get($key, $id_shop_group, $id_shop)) { return; } // If key already exists, update value if (JQZoomConfig::hasKey($key, $id_shop_group, $id_shop)) { // Update config not linked to lang $result &= Db::getInstance()->update('jqzoomconfig', array( 'value' => pSQL($value, $html), 'date_upd' => date('Y-m-d H:i:s'), ), '`name` = \'' . pSQL($key) . '\'' . JQZoomConfig::sqlRestriction($id_shop_group, $id_shop), true, true); } // If key does not exists, create it else { if (!$configID = JQZoomConfig::getIdByName($key, $id_shop_group, $id_shop)) { $newConfig = new JQZoomConfig(); $newConfig->name = $key; if ($id_shop) { $newConfig->id_shop = (int)$id_shop; } if ($id_shop_group) { $newConfig->id_shop_group = (int)$id_shop_group; } $newConfig->value = $value; $result &= $newConfig->add(true, true); $configID = $newConfig->id; } } JQZoomConfig::set($key, $value, $id_shop_group, $id_shop); return $result; } /** * Add SQL restriction on shops for configuration table * * @param int $id_shop_group * @param int $id_shop * @return string */ protected static function sqlRestriction($id_shop_group, $id_shop) { if ($id_shop) { return ' AND id_shop = ' . $id_shop; } else { if ($id_shop_group) { return ' AND id_shop_group = ' . $id_shop_group . ' AND id_shop IS NULL'; } else { return ' AND id_shop_group IS NULL AND id_shop IS NULL'; } } } }