示例#1
0
 public static function addShopAssociation($table, $alias, $context = null)
 {
     if (is_null($context)) {
         $context = Context::getContext();
     }
     $table_alias = $table . '_shop';
     if (strpos($table, '.') !== false) {
         list($table_alias, $table) = explode('.', $table);
     }
     if (!array_key_exists($table, self::$blog_shop_tables)) {
         return;
     }
     $sql = ' INNER JOIN ' . _DB_PREFIX_ . $table . '_shop ' . $table_alias . ' ON (' . $table_alias . '.id_' . $table . ' = ' . $alias . '.id_' . $table;
     if (isset($context->shop->id)) {
         $sql .= ' AND ' . $table_alias . '.id_shop = ' . (int) $context->shop->id;
     } elseif (Shop::checkIdShopDefault($table)) {
         $sql .= ' AND ' . $table_alias . '.id_shop = ' . $alias . '.id_shop_default';
     } else {
         $sql .= ' AND ' . $table_alias . '.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ')';
     }
     $sql .= ')';
     return $sql;
 }
示例#2
0
    /**
     * Add an SQL JOIN in query between a table and its associated table in multishop
     *
     * @param string $table Table name (E.g. product, module, etc.)
     * @param string $alias Alias of table
     * @param bool $inner_join Use or not INNER JOIN
     * @param string $on
     * @return string
     */
    public static function addSqlAssociation($table, $alias, $inner_join = true, $on = null, $force_not_default = false)
    {
        $table_alias = $table . '_shop';
        if (strpos($table, '.') !== false) {
            list($table_alias, $table) = explode('.', $table);
        }
        $asso_table = Shop::getAssoTable($table);
        if ($asso_table === false || $asso_table['type'] != 'shop') {
            return;
        }
        $sql = ($inner_join ? ' INNER' : ' LEFT') . ' JOIN ' . _DB_PREFIX_ . $table . '_shop ' . $table_alias . '
		ON (' . $table_alias . '.id_' . $table . ' = ' . $alias . '.id_' . $table;
        if ((int) self::$context_id_shop) {
            $sql .= ' AND ' . $table_alias . '.id_shop = ' . (int) self::$context_id_shop;
        } elseif (Shop::checkIdShopDefault($table) && !$force_not_default) {
            $sql .= ' AND ' . $table_alias . '.id_shop = ' . $alias . '.id_shop_default';
        } else {
            $sql .= ' AND ' . $table_alias . '.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ')';
        }
        $sql .= ($on ? ' AND ' . $on : '') . ')';
        return $sql;
    }
示例#3
0
 /**
  * Updates the current object in the database
  *
  * @param bool $null_values
  *
  * @return bool
  * @throws PrestaShopDatabaseException
  * @throws PrestaShopException
  */
 public function update($null_values = false)
 {
     // @hook actionObject*UpdateBefore
     Hook::exec('actionObjectUpdateBefore', array('object' => $this));
     Hook::exec('actionObject' . get_class($this) . 'UpdateBefore', array('object' => $this));
     $this->clearCache();
     // Automatically fill dates
     if (array_key_exists('date_upd', $this)) {
         $this->date_upd = date('Y-m-d H:i:s');
         if (isset($this->update_fields) && is_array($this->update_fields) && count($this->update_fields)) {
             $this->update_fields['date_upd'] = true;
         }
     }
     // Automatically fill dates
     if (array_key_exists('date_add', $this) && $this->date_add == null) {
         $this->date_add = date('Y-m-d H:i:s');
         if (isset($this->update_fields) && is_array($this->update_fields) && count($this->update_fields)) {
             $this->update_fields['date_add'] = true;
         }
     }
     $id_shop_list = Shop::getContextListShopID();
     if (count($this->id_shop_list) > 0) {
         $id_shop_list = $this->id_shop_list;
     }
     if (Shop::checkIdShopDefault($this->def['table']) && !$this->id_shop_default) {
         $this->id_shop_default = in_array(Configuration::get('PS_SHOP_DEFAULT'), $id_shop_list) == true ? Configuration::get('PS_SHOP_DEFAULT') : min($id_shop_list);
     }
     // Database update
     if (!($result = Db::getInstance()->update($this->def['table'], $this->getFields(), '`' . pSQL($this->def['primary']) . '` = ' . (int) $this->id, 0, $null_values))) {
         return false;
     }
     // Database insertion for multishop fields related to the object
     if (Shop::isTableAssociated($this->def['table'])) {
         $fields = $this->getFieldsShop();
         $fields[$this->def['primary']] = (int) $this->id;
         if (is_array($this->update_fields)) {
             $update_fields = $this->update_fields;
             $this->update_fields = null;
             $all_fields = $this->getFieldsShop();
             $all_fields[$this->def['primary']] = (int) $this->id;
             $this->update_fields = $update_fields;
         } else {
             $all_fields = $fields;
         }
         foreach ($id_shop_list as $id_shop) {
             $fields['id_shop'] = (int) $id_shop;
             $all_fields['id_shop'] = (int) $id_shop;
             $where = $this->def['primary'] . ' = ' . (int) $this->id . ' AND id_shop = ' . (int) $id_shop;
             // A little explanation of what we do here : we want to create multishop entry when update is called, but
             // only if we are in a shop context (if we are in all context, we just want to update entries that alread exists)
             $shop_exists = Db::getInstance()->getValue('SELECT ' . $this->def['primary'] . ' FROM ' . _DB_PREFIX_ . $this->def['table'] . '_shop WHERE ' . $where);
             if ($shop_exists) {
                 $result &= Db::getInstance()->update($this->def['table'] . '_shop', $fields, $where, 0, $null_values);
             } elseif (Shop::getContext() == Shop::CONTEXT_SHOP) {
                 $result &= Db::getInstance()->insert($this->def['table'] . '_shop', $all_fields, $null_values);
             }
         }
     }
     // Database update for multilingual fields related to the object
     if (isset($this->def['multilang']) && $this->def['multilang']) {
         $fields = $this->getFieldsLang();
         if (is_array($fields)) {
             foreach ($fields as $field) {
                 foreach (array_keys($field) as $key) {
                     if (!Validate::isTableOrIdentifier($key)) {
                         throw new PrestaShopException('key ' . $key . ' is not a valid table or identifier');
                     }
                 }
                 // If this table is linked to multishop system, update / insert for all shops from context
                 if ($this->isLangMultishop()) {
                     $id_shop_list = Shop::getContextListShopID();
                     if (count($this->id_shop_list) > 0) {
                         $id_shop_list = $this->id_shop_list;
                     }
                     foreach ($id_shop_list as $id_shop) {
                         $field['id_shop'] = (int) $id_shop;
                         $where = pSQL($this->def['primary']) . ' = ' . (int) $this->id . ' AND id_lang = ' . (int) $field['id_lang'] . ' AND id_shop = ' . (int) $id_shop;
                         if (Db::getInstance()->getValue('SELECT COUNT(*) FROM ' . pSQL(_DB_PREFIX_ . $this->def['table']) . '_lang WHERE ' . $where)) {
                             $result &= Db::getInstance()->update($this->def['table'] . '_lang', $field, $where);
                         } else {
                             $result &= Db::getInstance()->insert($this->def['table'] . '_lang', $field);
                         }
                     }
                 } else {
                     $where = pSQL($this->def['primary']) . ' = ' . (int) $this->id . ' AND id_lang = ' . (int) $field['id_lang'];
                     if (Db::getInstance()->getValue('SELECT COUNT(*) FROM ' . pSQL(_DB_PREFIX_ . $this->def['table']) . '_lang WHERE ' . $where)) {
                         $result &= Db::getInstance()->update($this->def['table'] . '_lang', $field, $where);
                     } else {
                         $result &= Db::getInstance()->insert($this->def['table'] . '_lang', $field, $null_values);
                     }
                 }
             }
         }
     }
     // @hook actionObject*UpdateAfter
     Hook::exec('actionObjectUpdateAfter', array('object' => $this));
     Hook::exec('actionObject' . get_class($this) . 'UpdateAfter', array('object' => $this));
     return $result;
 }