/** * Load ObjectModel * @param $id * @param $id_lang * @param $entity ObjectModel * @param $entity_defs * @param $id_shop * @param $should_cache_objects * @throws PrestaShopDatabaseException */ public function load($id, $id_lang, $entity, $entity_defs, $id_shop, $should_cache_objects) { // Load object from database if object id is present $cache_id = 'objectmodel_' . $entity_defs['classname'] . '_' . (int) $id . '_' . (int) $id_shop . '_' . (int) $id_lang; if (!$should_cache_objects || !Cache::isStored($cache_id)) { $sql = new DbQuery(); $sql->from($entity_defs['table'], 'a'); $sql->where('a.`' . bqSQL($entity_defs['primary']) . '` = ' . (int) $id); // Get lang informations if ($id_lang && isset($entity_defs['multilang']) && $entity_defs['multilang']) { $sql->leftJoin($entity_defs['table'] . '_lang', 'b', 'a.`' . bqSQL($entity_defs['primary']) . '` = b.`' . bqSQL($entity_defs['primary']) . '` AND b.`id_lang` = ' . (int) $id_lang); if ($id_shop && !empty($entity_defs['multilang_shop'])) { $sql->where('b.`id_shop` = ' . (int) $id_shop); } } // Get shop informations if (Shop::isTableAssociated($entity_defs['table'])) { $sql->leftJoin($entity_defs['table'] . '_shop', 'c', 'a.`' . bqSQL($entity_defs['primary']) . '` = c.`' . bqSQL($entity_defs['primary']) . '` AND c.`id_shop` = ' . (int) $id_shop); } if ($object_datas = Db::getInstance()->getRow($sql)) { if (!$id_lang && isset($entity_defs['multilang']) && $entity_defs['multilang']) { $sql = 'SELECT * FROM `' . bqSQL(_DB_PREFIX_ . $entity_defs['table']) . '_lang` WHERE `' . bqSQL($entity_defs['primary']) . '` = ' . (int) $id . ($id_shop && $entity->isLangMultishop() ? ' AND `id_shop` = ' . (int) $id_shop : ''); if ($object_datas_lang = Db::getInstance()->executeS($sql)) { foreach ($object_datas_lang as $row) { foreach ($row as $key => $value) { if ($key != $entity_defs['primary'] && array_key_exists($key, $entity)) { if (!isset($object_datas[$key]) || !is_array($object_datas[$key])) { $object_datas[$key] = array(); } $object_datas[$key][$row['id_lang']] = $value; } } } } } $entity->id = (int) $id; foreach ($object_datas as $key => $value) { if (array_key_exists($key, $entity)) { $entity->{$key} = $value; } else { unset($object_datas[$key]); } } if ($should_cache_objects) { Cache::store($cache_id, $object_datas); } } } else { $object_datas = Cache::retrieve($cache_id); if ($object_datas) { $entity->id = (int) $id; foreach ($object_datas as $key => $value) { $entity->{$key} = $value; } } } }
/** * @param string $entity * @param int $id_shop * @return array|bool */ public static function getEntityIds($entity, $id_shop, $active = false, $delete = false) { if (!Shop::isTableAssociated($entity)) { return false; } return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT entity.`id_' . pSQL($entity) . '` FROM `' . _DB_PREFIX_ . pSQL($entity) . '_shop`es LEFT JOIN ' . _DB_PREFIX_ . pSQL($entity) . ' entity ON (entity.`id_' . pSQL($entity) . '` = es.`id_' . pSQL($entity) . '`) WHERE es.`id_shop` = ' . (int) $id_shop . ($active ? ' AND entity.`active` = 1' : '') . ($delete ? ' AND entity.deleted = 0' : '')); }
/** * Update the associations of shops * * @param int $id_object */ protected function updateAssoShop($id_object) { if (!Shop::isFeatureActive()) { return; } if (!Shop::isTableAssociated($this->table)) { return; } $assos_data = $this->getSelectedAssoShop($this->table); // Get list of shop id we want to exclude from asso deletion $exclude_ids = $assos_data; foreach (Db::getInstance()->executeS('SELECT id_shop FROM ' . _DB_PREFIX_ . 'shop') as $row) { if (!$this->context->employee->hasAuthOnShop($row['id_shop'])) { $exclude_ids[] = $row['id_shop']; } } Db::getInstance()->delete($this->table . '_shop', '`' . bqSQL($this->identifier) . '` = ' . (int) $id_object . ($exclude_ids ? ' AND id_shop NOT IN (' . implode(', ', array_map('intval', $exclude_ids)) . ')' : '')); $insert = array(); foreach ($assos_data as $id_shop) { $insert[] = array($this->identifier => (int) $id_object, 'id_shop' => (int) $id_shop); } return Db::getInstance()->insert($this->table . '_shop', $insert, false, true, Db::INSERT_IGNORE); }
protected static function getAssoShop($table, $id_object = false) { if (Shop::isTableAssociated($table)) { $type = 'shop'; } else { return; } $assos = array(); foreach ($_POST as $k => $row) { if (!preg_match('/^checkBox' . Tools::toCamelCase($type, true) . 'Asso_' . $table . '_([0-9]+)?_([0-9]+)$/Ui', $k, $res)) { continue; } $id_asso_object = !empty($res[1]) ? $res[1] : $id_object; $assos[] = array('id_object' => (int) $id_asso_object, 'id_' . $type => (int) $res[2]); } return array($assos, $type); }
/** * Checks if object is multi-shop object. * * @return bool */ public function isMultishop() { return Shop::isTableAssociated($this->def['table']) || !empty($this->def['multilang_shop']); }
/** * Check if there is more than one entries in associated shop table for current entity * * @since 1.5.0 * @return bool */ public function hasMultishopEntries() { if (!Shop::isTableAssociated($this->def['table']) || !Shop::isFeatureActive()) { return false; } return (bool) Db::getInstance()->getValue('SELECT COUNT(*) FROM `' . _DB_PREFIX_ . $this->def['table'] . '_shop` WHERE `' . $this->def['primary'] . '` = ' . (int) $this->id); }