vendor/pimcore/pimcore/models/DataObject/Classificationstore/Dao.php line 79

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Model\DataObject\Classificationstore;
  15. use Pimcore\Element\MarshallerService;
  16. use Pimcore\Logger;
  17. use Pimcore\Model;
  18. use Pimcore\Model\DataObject;
  19. use Pimcore\Normalizer\NormalizerInterface;
  20. /**
  21. * @internal
  22. *
  23. * @property \Pimcore\Model\DataObject\Classificationstore $model
  24. */
  25. class Dao extends Model\Dao\AbstractDao
  26. {
  27. use DataObject\ClassDefinition\Helper\Dao;
  28. /**
  29. * @var array|null
  30. */
  31. protected $tableDefinitions = null;
  32. /**
  33. * @return string
  34. */
  35. public function getDataTableName()
  36. {
  37. return 'object_classificationstore_data_' . $this->model->getClass()->getId();
  38. }
  39. /**
  40. * @return string
  41. */
  42. public function getGroupsTableName()
  43. {
  44. return 'object_classificationstore_groups_' . $this->model->getClass()->getId();
  45. }
  46. /**
  47. * @throws \Exception
  48. */
  49. public function save()
  50. {
  51. if (!DataObject::isDirtyDetectionDisabled() && !$this->model->hasDirtyFields()) {
  52. return;
  53. }
  54. $object = $this->model->getObject();
  55. $objectId = $object->getId();
  56. $dataTable = $this->getDataTableName();
  57. $fieldname = $this->model->getFieldname();
  58. $this->db->delete($dataTable, ['o_id' => $objectId, 'fieldname' => $fieldname]);
  59. $items = $this->model->getItems();
  60. $activeGroups = $this->model->getActiveGroups();
  61. $collectionMapping = $this->model->getGroupCollectionMappings();
  62. foreach ($items as $groupId => $group) {
  63. foreach ($group as $keyId => $keyData) {
  64. if (!isset($activeGroups[$groupId])) {
  65. continue;
  66. }
  67. $keyConfig = DefinitionCache::get($keyId);
  68. $fd = Service::getFieldDefinitionFromKeyConfig($keyConfig);
  69. foreach ($keyData as $language => $value) {
  70. $collectionId = $collectionMapping[$groupId] ?? null;
  71. $data = [
  72. 'o_id' => $objectId,
  73. 'collectionId' => $collectionId,
  74. 'groupId' => $groupId,
  75. 'keyId' => $keyId,
  76. 'fieldname' => $fieldname,
  77. 'language' => $language,
  78. 'type' => $keyConfig->getType(),
  79. ];
  80. $encodedData = [];
  81. if ($fd instanceof NormalizerInterface) {
  82. $normalizedData = $fd->normalize($value, [
  83. 'object' => $object,
  84. 'fieldDefinition' => $fd,
  85. ]);
  86. /** @var MarshallerService $marshallerService */
  87. $marshallerService = \Pimcore::getContainer()->get(MarshallerService::class);
  88. if ($marshallerService->supportsFielddefinition('classificationstore', $fd->getFieldtype())) {
  89. $marshaller = $marshallerService->buildFieldefinitionMarshaller('classificationstore', $fd->getFieldtype());
  90. // TODO format only passed in for BC reasons (localizedfields). remove it as soon as marshal is gone
  91. $encodedData = $marshaller->marshal($normalizedData, ['object' => $object, 'fieldDefinition' => $fd, 'format' => 'classificationstore']);
  92. } else {
  93. $encodedData['value'] = $normalizedData;
  94. }
  95. }
  96. $data['value'] = $encodedData['value'] ?? null;
  97. $data['value2'] = $encodedData['value2'] ?? null;
  98. $this->db->insertOrUpdate($dataTable, $data);
  99. }
  100. }
  101. }
  102. $groupsTable = $this->getGroupsTableName();
  103. $this->db->delete($groupsTable, ['o_id' => $objectId, 'fieldname' => $fieldname]);
  104. if (is_array($activeGroups)) {
  105. foreach ($activeGroups as $activeGroupId => $enabled) {
  106. if ($enabled) {
  107. $data = [
  108. 'o_id' => $objectId,
  109. 'groupId' => $activeGroupId,
  110. 'fieldname' => $fieldname,
  111. ];
  112. $this->db->insertOrUpdate($groupsTable, $data);
  113. }
  114. }
  115. }
  116. }
  117. public function delete()
  118. {
  119. $object = $this->model->getObject();
  120. $objectId = $object->getId();
  121. $dataTable = $this->getDataTableName();
  122. $groupsTable = $this->getGroupsTableName();
  123. // remove relations
  124. $this->db->delete($dataTable, ['o_id' => $objectId]);
  125. $this->db->delete($groupsTable, ['o_id' => $objectId]);
  126. }
  127. /**
  128. * @throws \Exception
  129. */
  130. public function load()
  131. {
  132. $classificationStore = $this->model;
  133. $object = $this->model->getObject();
  134. $dataTableName = $this->getDataTableName();
  135. $objectId = $object->getId();
  136. $fieldname = $this->model->getFieldname();
  137. $groupsTableName = $this->getGroupsTableName();
  138. $query = 'SELECT * FROM ' . $groupsTableName . ' WHERE o_id = ' . $this->db->quote($objectId) . ' AND fieldname = ' . $this->db->quote($fieldname);
  139. $data = $this->db->fetchAll($query);
  140. $list = [];
  141. foreach ($data as $item) {
  142. $list[$item['groupId']] = true;
  143. }
  144. $query = 'SELECT * FROM ' . $dataTableName . ' WHERE o_id = ' . $this->db->quote($objectId) . ' AND fieldname = ' . $this->db->quote($fieldname);
  145. $data = $this->db->fetchAll($query);
  146. $groupCollectionMapping = [];
  147. foreach ($data as $item) {
  148. if (!isset($list[$item['groupId']])) {
  149. continue;
  150. }
  151. $groupId = $item['groupId'];
  152. $keyId = $item['keyId'];
  153. $collectionId = $item['collectionId'];
  154. $groupCollectionMapping[$groupId] = $collectionId;
  155. $value = [
  156. 'value' => $item['value'],
  157. 'value2' => $item['value2'],
  158. ];
  159. $keyConfig = DefinitionCache::get($keyId);
  160. if (!$keyConfig) {
  161. Logger::error('Could not resolve key with ID: ' . $keyId);
  162. continue;
  163. }
  164. $fd = Service::getFieldDefinitionFromKeyConfig($keyConfig);
  165. if ($fd instanceof NormalizerInterface) {
  166. /** @var MarshallerService $marshallerService */
  167. $marshallerService = \Pimcore::getContainer()->get(MarshallerService::class);
  168. if ($marshallerService->supportsFielddefinition('classificationstore', $fd->getFieldtype())) {
  169. $unmarshaller = $marshallerService->buildFieldefinitionMarshaller('classificationstore', $fd->getFieldtype());
  170. // TODO format only passed in for BC reasons (localizedfields). remove it as soon as marshal is gone
  171. $value = $unmarshaller->unmarshal($value, ['object' => $object, 'fieldDefinition' => $fd, 'format' => 'classificationstore']);
  172. } else {
  173. $value = $value['value'];
  174. }
  175. $value = $fd->denormalize($value, [
  176. 'object' => $object,
  177. 'fieldDefinition' => $fd,
  178. ]);
  179. }
  180. $language = $item['language'];
  181. $classificationStore->setLocalizedKeyValue($groupId, $keyId, $value, $language);
  182. }
  183. $classificationStore->setActiveGroups($list);
  184. $classificationStore->setGroupCollectionMappings($groupCollectionMapping);
  185. $classificationStore->resetDirtyMap();
  186. }
  187. public function createUpdateTable()
  188. {
  189. $groupsTable = $this->getGroupsTableName();
  190. $dataTable = $this->getDataTableName();
  191. $this->db->query('CREATE TABLE IF NOT EXISTS `' . $groupsTable . '` (
  192. `o_id` BIGINT(20) NOT NULL,
  193. `groupId` BIGINT(20) NOT NULL,
  194. `fieldname` VARCHAR(70) NOT NULL,
  195. PRIMARY KEY (`o_id`, `fieldname`, `groupId`)
  196. ) DEFAULT CHARSET=utf8mb4;');
  197. $this->db->query('CREATE TABLE IF NOT EXISTS `' . $dataTable . '` (
  198. `o_id` BIGINT(20) NOT NULL,
  199. `collectionId` BIGINT(20) NULL,
  200. `groupId` BIGINT(20) NOT NULL,
  201. `keyId` BIGINT(20) NOT NULL,
  202. `value` LONGTEXT NULL,
  203. `value2` LONGTEXT NULL,
  204. `fieldname` VARCHAR(70) NOT NULL,
  205. `language` VARCHAR(10) NOT NULL,
  206. `type` VARCHAR(50) NULL,
  207. PRIMARY KEY (`o_id`, `fieldname`, `groupId`, `keyId`, `language`),
  208. INDEX `keyId` (`keyId`),
  209. INDEX `language` (`language`)
  210. ) DEFAULT CHARSET=utf8mb4;');
  211. $this->tableDefinitions = null;
  212. $this->handleEncryption($this->model->getClass(), [$groupsTable, $dataTable]);
  213. }
  214. }