protected static function sqlRestriction($id_shop_group, $id_shop)
 {
     ConfigurationKPI::setKpiDefinition();
     $r = parent::sqlRestriction($id_shop_group, $id_shop);
     ConfigurationKPI::unsetKpiDefinition();
     return $r;
 }
Example #2
0
    /**
     * Update configuration key and value into database (automatically insert if key does not exist)
     *
     * @param string $key Key
     * @param mixed $values $values is an array if the configuration is multilingual, a single string else.
     * @param boolean $html Specify if html is authorized in value
     * @param int $id_shop_group
     * @param int $id_shop
     * @return boolean Update result
     */
    public static function updateValue($key, $values, $html = false, $id_shop_group = null, $id_shop = null)
    {
        if (!Validate::isConfigName($key)) {
            die(Tools::displayError());
        }
        if ($id_shop === null) {
            $id_shop = Shop::getContextShopID(true);
        }
        if ($id_shop_group === null) {
            $id_shop_group = Shop::getContextShopGroupID(true);
        }
        if (!is_array($values)) {
            $is_i18n = false;
            $values = array($values);
        } else {
            $is_i18n = true;
        }
        $result = true;
        foreach ($values as $lang => $value) {
            $stored_value = Configuration::get($key, $lang, $id_shop_group, $id_shop);
            // if there isn't a $stored_value, we must insert $value
            if (!is_numeric($value) && $value === $stored_value || is_numeric($value) && $value == $stored_value && Configuration::hasKey($key, $lang)) {
                continue;
            }
            // If key already exists, update value
            if (Configuration::hasKey($key, $lang, $id_shop_group, $id_shop)) {
                if (!$lang) {
                    // Update config not linked to lang
                    $result &= Db::getInstance()->update(self::$definition['table'], array('value' => pSQL($value, $html), 'date_upd' => date('Y-m-d H:i:s')), '`name` = \'' . pSQL($key) . '\'' . Configuration::sqlRestriction($id_shop_group, $id_shop), 1, true);
                } else {
                    // Update multi lang
                    $sql = 'UPDATE `' . _DB_PREFIX_ . bqSQL(self::$definition['table']) . '_lang` cl
							SET cl.value = \'' . pSQL($value, $html) . '\',
								cl.date_upd = NOW()
							WHERE cl.id_lang = ' . (int) $lang . '
								AND cl.`' . bqSQL(self::$definition['primary']) . '` = (
									SELECT c.`' . bqSQL(self::$definition['primary']) . '`
									FROM `' . _DB_PREFIX_ . bqSQL(self::$definition['table']) . '` c
									WHERE c.name = \'' . pSQL($key) . '\'' . Configuration::sqlRestriction($id_shop_group, $id_shop) . ')';
                    $result &= Db::getInstance()->execute($sql);
                }
            } else {
                if (!($configID = Configuration::getIdByName($key, $id_shop_group, $id_shop))) {
                    $newConfig = new Configuration();
                    $newConfig->name = $key;
                    if ($id_shop) {
                        $newConfig->id_shop = (int) $id_shop;
                    }
                    if ($id_shop_group) {
                        $newConfig->id_shop_group = (int) $id_shop_group;
                    }
                    if (!$lang) {
                        $newConfig->value = $value;
                    }
                    $result &= $newConfig->add(true, true);
                    $configID = $newConfig->id;
                }
                if ($lang) {
                    $result &= Db::getInstance()->insert(self::$definition['table'] . '_lang', array(self::$definition['primary'] => $configID, 'id_lang' => $lang, 'value' => pSQL($value, $html), 'date_upd' => date('Y-m-d H:i:s')));
                }
            }
        }
        Configuration::set($key, $values, $id_shop_group, $id_shop);
        return $result;
    }
Example #3
0
    /**
     * Update configuration key and value into database (automatically insert if key does not exist)
     *
     * @param string $key Key
     * @param mixed $values $values is an array if the configuration is multilingual, a single string else.
     * @param boolean $html Specify if html is authorized in value
     * @param int $id_shop_group
     * @param int $id_shop
     * @return boolean Update result
     */
    public static function updateValue($key, $values, $html = false, $id_shop_group = null, $id_shop = null)
    {
        if (!Validate::isConfigName($key)) {
            die(Tools::displayError());
        }
        if ($id_shop === null) {
            $id_shop = Shop::getContextShopID(true);
        }
        if ($id_shop_group === null) {
            $id_shop_group = Shop::getContextShopGroupID(true);
        }
        if (!is_array($values)) {
            $is_i18n = false;
            $values = array($values);
        } else {
            $is_i18n = true;
        }
        $result = true;
        foreach ($values as $lang => $value) {
            if ($value === Configuration::get($key, $lang, $id_shop_group, $id_shop)) {
                continue;
            }
            // If key already exists, update value
            if (Configuration::hasKey($key, $lang, $id_shop_group, $id_shop)) {
                if (!$lang) {
                    // Update config not linked to lang
                    $result &= Db::getInstance()->update('configuration', array('value' => pSQL($value, $html), 'date_upd' => date('Y-m-d H:i:s')), '`name` = \'' . pSQL($key) . '\'' . Configuration::sqlRestriction($id_shop_group, $id_shop), true, true);
                } else {
                    // Update multi lang
                    $sql = 'UPDATE ' . _DB_PREFIX_ . 'configuration_lang cl
							SET cl.value = \'' . pSQL($value, $html) . '\',
								cl.date_upd = NOW()
							WHERE cl.id_lang = ' . (int) $lang . '
								AND cl.id_configuration = (
									SELECT c.id_configuration
									FROM ' . _DB_PREFIX_ . 'configuration c
									WHERE c.name = \'' . pSQL($key) . '\'' . Configuration::sqlRestriction($id_shop_group, $id_shop) . ')';
                    $result &= Db::getInstance()->execute($sql);
                }
            } else {
                if (!($configID = Configuration::getIdByName($key, $id_shop_group, $id_shop))) {
                    $newConfig = new Configuration();
                    $newConfig->name = $key;
                    if ($id_shop) {
                        $newConfig->id_shop = (int) $id_shop;
                    }
                    if ($id_shop_group) {
                        $newConfig->id_shop_group = (int) $id_shop_group;
                    }
                    if (!$lang) {
                        $newConfig->value = $value;
                    }
                    $result &= $newConfig->add(true, true);
                    $configID = $newConfig->id;
                }
                if ($lang) {
                    $result &= Db::getInstance()->insert('configuration_lang', array('id_configuration' => $configID, 'id_lang' => $lang, 'value' => pSQL($value, $html), 'date_upd' => date('Y-m-d H:i:s')));
                }
            }
            if (!$is_i18n) {
                Configuration::set($key, $value, $id_shop_group, $id_shop);
            }
        }
        if ($is_i18n) {
            Configuration::set($key, $values, $id_shop_group, $id_shop);
        }
        return $result;
    }
Example #4
0
 /**
  * Update configuration key and value into database (automatically insert if key does not exist)
  *
  * Values are inserted/updated directly using SQL, because using (Configuration) ObjectModel
  * may not insert values correctly (for example, HTML is escaped, when it should not be).
  * @TODO Fix saving HTML values in Configuration model
  *
  * @param string $key Key
  * @param mixed $values $values is an array if the configuration is multilingual, a single string else.
  * @param bool $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, $values, $html = false, $id_shop_group = null, $id_shop = null)
 {
     if (!Validate::isConfigName($key)) {
         die(sprintf(Tools::displayError('[%s] is not a valid configuration key'), Tools::htmlentitiesUTF8($key)));
     }
     if ($id_shop === null || !Shop::isFeatureActive()) {
         $id_shop = Shop::getContextShopID(true);
     }
     if ($id_shop_group === null || !Shop::isFeatureActive()) {
         $id_shop_group = Shop::getContextShopGroupID(true);
     }
     if (!is_array($values)) {
         $values = array($values);
     }
     if ($html) {
         foreach ($values as &$value) {
             $value = Tools::purifyHTML($value);
         }
         unset($value);
     }
     $result = true;
     foreach ($values as $lang => $value) {
         $stored_value = Configuration::get($key, $lang, $id_shop_group, $id_shop);
         // if there isn't a $stored_value, we must insert $value
         if (!is_numeric($value) && $value === $stored_value || is_numeric($value) && $value == $stored_value && Configuration::hasKey($key, $lang)) {
             continue;
         }
         // If key already exists, update value
         if (Configuration::hasKey($key, $lang, $id_shop_group, $id_shop)) {
             if (!$lang) {
                 // Update config not linked to lang
                 $result &= Db::getInstance()->update(self::$definition['table'], array('value' => pSQL($value, $html), 'date_upd' => date('Y-m-d H:i:s')), '`name` = \'' . pSQL($key) . '\'' . Configuration::sqlRestriction($id_shop_group, $id_shop), 1, true);
             } else {
                 // Update multi lang
                 $sql = 'UPDATE `' . _DB_PREFIX_ . bqSQL(self::$definition['table']) . '_lang` cl
                         SET cl.value = \'' . pSQL($value, $html) . '\',
                             cl.date_upd = NOW()
                         WHERE cl.id_lang = ' . (int) $lang . '
                             AND cl.`' . bqSQL(self::$definition['primary']) . '` = (
                                 SELECT c.`' . bqSQL(self::$definition['primary']) . '`
                                 FROM `' . _DB_PREFIX_ . bqSQL(self::$definition['table']) . '` c
                                 WHERE c.name = \'' . pSQL($key) . '\'' . Configuration::sqlRestriction($id_shop_group, $id_shop) . ')';
                 $result &= Db::getInstance()->execute($sql);
             }
         } else {
             if (!($configID = Configuration::getIdByName($key, $id_shop_group, $id_shop))) {
                 $now = date('Y-m-d H:i:s');
                 $data = array('id_shop_group' => $id_shop_group ? (int) $id_shop_group : null, 'id_shop' => $id_shop ? (int) $id_shop : null, 'name' => pSQL($key), 'value' => $lang ? null : pSQL($value, $html), 'date_add' => $now, 'date_upd' => $now);
                 $result &= Db::getInstance()->insert(self::$definition['table'], $data, true);
                 $configID = Db::getInstance()->Insert_ID();
             }
             if ($lang) {
                 $result &= Db::getInstance()->insert(self::$definition['table'] . '_lang', array(self::$definition['primary'] => $configID, 'id_lang' => (int) $lang, 'value' => pSQL($value, $html), 'date_upd' => date('Y-m-d H:i:s')));
             }
         }
     }
     Configuration::set($key, $values, $id_shop_group, $id_shop);
     return $result;
 }