|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * 2007-2016 PrestaShop |
| 4 | + * |
| 5 | + * NOTICE OF LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the Open Software License (OSL 3.0) |
| 8 | + * that is bundled with this package in the file LICENSE.txt. |
| 9 | + * It is also available through the world-wide-web at this URL: |
| 10 | + * http://opensource.org/licenses/osl-3.0.php |
| 11 | + * If you did not receive a copy of the license and are unable to |
| 12 | + * obtain it through the world-wide-web, please send an email |
| 13 | + * to license@prestashop.com so we can send you a copy immediately. |
| 14 | + * |
| 15 | + * DISCLAIMER |
| 16 | + * |
| 17 | + * Do not edit or add to this file if you wish to upgrade PrestaShop to newer |
| 18 | + * versions in the future. If you wish to customize PrestaShop for your |
| 19 | + * needs please refer to http://www.prestashop.com for more information. |
| 20 | + * |
| 21 | + * @author PrestaShop SA <contact@prestashop.com> |
| 22 | + * @copyright 2007-2016 PrestaShop SA |
| 23 | + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
| 24 | + * International Registered Trademark & Property of PrestaShop SA |
| 25 | + */ |
| 26 | + |
| 27 | +class Adapter_EntityMapper |
| 28 | +{ |
| 29 | + /** |
| 30 | + * Load ObjectModel |
| 31 | + * @param $id |
| 32 | + * @param $id_lang |
| 33 | + * @param $entity ObjectModel |
| 34 | + * @param $entity_defs |
| 35 | + * @param $id_shop |
| 36 | + * @param $should_cache_objects |
| 37 | + * @throws PrestaShopDatabaseException |
| 38 | + */ |
| 39 | + public function load($id, $id_lang, $entity, $entity_defs, $id_shop, $should_cache_objects) |
| 40 | + { |
| 41 | + // Load object from database if object id is present |
| 42 | + $cache_id = 'objectmodel_' . $entity_defs['classname'] . '_' . (int)$id . '_' . (int)$id_shop . '_' . (int)$id_lang; |
| 43 | + if (!$should_cache_objects || !Cache::isStored($cache_id)) { |
| 44 | + $sql = new DbQuery(); |
| 45 | + $sql->from($entity_defs['table'], 'a'); |
| 46 | + $sql->where('a.`' . bqSQL($entity_defs['primary']) . '` = ' . (int)$id); |
| 47 | + |
| 48 | + // Get lang informations |
| 49 | + if ($id_lang && isset($entity_defs['multilang']) && $entity_defs['multilang']) { |
| 50 | + $sql->leftJoin($entity_defs['table'] . '_lang', 'b', 'a.`' . bqSQL($entity_defs['primary']) . '` = b.`' . bqSQL($entity_defs['primary']) . '` AND b.`id_lang` = ' . (int)$id_lang); |
| 51 | + if ($id_shop && !empty($entity_defs['multilang_shop'])) { |
| 52 | + $sql->where('b.`id_shop` = ' . (int)$id_shop); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Get shop informations |
| 57 | + if (Shop::isTableAssociated($entity_defs['table'])) { |
| 58 | + $sql->leftJoin($entity_defs['table'] . '_shop', 'c', 'a.`' . bqSQL($entity_defs['primary']) . '` = c.`' . bqSQL($entity_defs['primary']) . '` AND c.`id_shop` = ' . (int)$id_shop); |
| 59 | + } |
| 60 | + |
| 61 | + if ($object_datas = Db::getInstance()->getRow($sql)) { |
| 62 | + if (!$id_lang && isset($entity_defs['multilang']) && $entity_defs['multilang']) { |
| 63 | + $sql = 'SELECT * |
| 64 | + FROM `' . bqSQL(_DB_PREFIX_ . $entity_defs['table']) . '_lang` |
| 65 | + WHERE `' . bqSQL($entity_defs['primary']) . '` = ' . (int)$id |
| 66 | + .(($id_shop && $entity->isLangMultishop()) ? ' AND `id_shop` = ' . (int)$id_shop : ''); |
| 67 | + |
| 68 | + if ($object_datas_lang = Db::getInstance()->executeS($sql)) { |
| 69 | + foreach ($object_datas_lang as $row) { |
| 70 | + foreach ($row as $key => $value) { |
| 71 | + if ($key != $entity_defs['primary'] && array_key_exists($key, $entity)) { |
| 72 | + if (!isset($object_datas[$key]) || !is_array($object_datas[$key])) { |
| 73 | + $object_datas[$key] = array(); |
| 74 | + } |
| 75 | + |
| 76 | + $object_datas[$key][$row['id_lang']] = $value; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + $entity->id = (int)$id; |
| 83 | + foreach ($object_datas as $key => $value) { |
| 84 | + if (array_key_exists($key, $entity)) { |
| 85 | + $entity->{$key} = $value; |
| 86 | + } else { |
| 87 | + unset($object_datas[$key]); |
| 88 | + } |
| 89 | + } |
| 90 | + if ($should_cache_objects) { |
| 91 | + Cache::store($cache_id, $object_datas); |
| 92 | + } |
| 93 | + } |
| 94 | + } else { |
| 95 | + $object_datas = Cache::retrieve($cache_id); |
| 96 | + if ($object_datas) { |
| 97 | + $entity->id = (int)$id; |
| 98 | + foreach ($object_datas as $key => $value) { |
| 99 | + $entity->{$key} = $value; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments