metadata = $metadata; $this->hookManager = $hookManager; $this->applicationState = $applicationState; $hookMediator = null; if (!$this->hooksDisabled) { $hookMediator = new HookMediator($hookManager); } parent::__construct($entityType, $entityManager, $entityFactory, $hookMediator); } /** * @deprecated Use `$this->metadata`. */ protected function getMetadata() { return $this->metadata; } /** * @deprecated Will be removed. */ public function handleSelectParams(&$params) { } public function save(Entity $entity, array $options = []): void { if ( $entity->isNew() && !$entity->has('id') && !$entity->getAttributeParam('id', 'autoincrement') ) { $entity->set('id', Util::generateId()); } if (empty($options['skipAll'])) { $this->processCreatedAndModifiedFieldsSave($entity, $options); } $this->restoreData = []; parent::save($entity, $options); } protected function beforeRemove(Entity $entity, array $options = []) { parent::beforeRemove($entity, $options); if (!$this->hooksDisabled && empty($options['skipHooks'])) { $this->hookManager->process($this->entityType, 'beforeRemove', $entity, $options); } $nowString = DateTimeUtil::getSystemNowString(); if ($entity->hasAttribute('modifiedAt')) { $entity->set('modifiedAt', $nowString); } if ($entity->hasAttribute('modifiedById')) { if ($this->applicationState->hasUser()) { $entity->set('modifiedById', $this->applicationState->getUser()->id); } } } protected function afterRemove(Entity $entity, array $options = []) { parent::afterRemove($entity, $options); if (!$this->hooksDisabled && empty($options['skipHooks'])) { $this->hookManager->process($this->entityType, 'afterRemove', $entity, $options); } } protected function afterMassRelate(Entity $entity, $relationName, array $params = [], array $options = []) { if (!$this->hooksDisabled && empty($options['skipHooks'])) { $hookData = [ 'relationName' => $relationName, 'relationParams' => $params, ]; $this->hookManager->process( $this->entityType, 'afterMassRelate', $entity, $options, $hookData ); } } protected function afterRelate(Entity $entity, $relationName, $foreign, $data = null, array $options = []) { parent::afterRelate($entity, $relationName, $foreign, $data, $options); if (!$this->hooksDisabled && empty($options['skipHooks'])) { if (is_string($foreign)) { $foreignId = $foreign; $foreignEntityType = $entity->getRelationParam($relationName, 'entity'); if ($foreignEntityType) { $foreign = $this->getEntityManager()->getEntity($foreignEntityType); $foreign->set('id', $foreignId); $foreign->setAsFetched(); } } if ($foreign instanceof Entity) { if (is_object($data)) { $data = (array) $data; } $this->hookMediator->afterRelate($entity, $relationName, $foreign, $data, $options); } } } protected function afterUnrelate(Entity $entity, $relationName, $foreign, array $options = []) { parent::afterUnrelate($entity, $relationName, $foreign, $options); if (!$this->hooksDisabled && empty($options['skipHooks'])) { if (is_string($foreign)) { $foreignId = $foreign; $foreignEntityType = $entity->getRelationParam($relationName, 'entity'); if ($foreignEntityType) { $foreign = $this->getEntityManager()->getEntity($foreignEntityType); $foreign->id = $foreignId; $foreign->setAsFetched(); } } if ($foreign instanceof Entity) { $this->hookMediator->afterUnrelate($entity, $relationName, $foreign, $options); } } } protected function beforeSave(Entity $entity, array $options = []) { parent::beforeSave($entity, $options); if (!$this->hooksDisabled && empty($options['skipHooks'])) { $this->hookManager->process($this->entityType, 'beforeSave', $entity, $options); } } protected function afterSave(Entity $entity, array $options = []) { if (!empty($this->restoreData)) { $entity->set($this->restoreData); $this->restoreData = null; } parent::afterSave($entity, $options); if (!$this->processFieldsAfterSaveDisabled) { $this->processFileFieldsSave($entity); $this->processArrayFieldsSave($entity); $this->processWysiwygFieldsSave($entity); } if (!$this->hooksDisabled && empty($options['skipHooks'])) { $this->hookManager->process($this->entityType, 'afterSave', $entity, $options); } } private function processCreatedAndModifiedFieldsSave(Entity $entity, array $options): void { if ($entity->isNew()) { $this->processCreatedAndModifiedFieldsSaveNew($entity, $options); return; } $nowString = DateTimeUtil::getSystemNowString(); if (!empty($options['silent']) || !empty($options['skipModifiedBy'])) { return; } if ($entity->hasAttribute('modifiedAt')) { $entity->set('modifiedAt', $nowString); } if ($entity->hasAttribute('modifiedById')) { if (!empty($options['modifiedById'])) { $entity->set('modifiedById', $options['modifiedById']); } else if ($this->applicationState->hasUser()) { $entity->set('modifiedById', $this->applicationState->getUser()->getId()); $entity->set('modifiedByName', $this->applicationState->getUser()->get('name')); } } } private function processCreatedAndModifiedFieldsSaveNew(Entity $entity, array $options): void { $nowString = DateTimeUtil::getSystemNowString(); if ( $entity->hasAttribute('createdAt') && (empty($options['import']) || !$entity->has('createdAt')) ) { $entity->set('createdAt', $nowString); } if ($entity->hasAttribute('modifiedAt')) { $entity->set('modifiedAt', $nowString); } if ($entity->hasAttribute('createdById')) { if (!empty($options['createdById'])) { $entity->set('createdById', $options['createdById']); } else if ( empty($options['skipCreatedBy']) && (empty($options['import']) || !$entity->has('createdById')) && $this->applicationState->hasUser() ) { $entity->set('createdById', $this->applicationState->getUser()->getId()); } } } protected function processFileFieldsSave(Entity $entity) { $entityDefs = $this->entityManager ->getDefs() ->getEntity($entity->getEntityType()); foreach ($entity->getRelationList() as $name) { $defs = $entityDefs->getRelation($name); $type = $defs->getType(); if (!$defs->hasForeignEntityType()) { continue; } $foreignEntityType = $defs->getForeignEntityType(); if (!($type === $entity::BELONGS_TO && $foreignEntityType === 'Attachment')) { continue; } $attribute = $name . 'Id'; if (!$entity->hasAttribute($attribute)) { continue; } if (!$entity->get($attribute)) { continue; } if (!$entity->isAttributeChanged($attribute)) { continue; } $attachment = $this->getEntityManager()->getEntity('Attachment', $entity->get($attribute)); if (!$attachment) { continue; } $attachment->set([ 'relatedId' => $entity->id, 'relatedType' => $entity->getEntityType(), ]); $this->getEntityManager()->saveEntity($attachment); } if ($entity->isNew()) { return; } foreach ($this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'fields']) as $name => $defs) { if (!empty($defs['type']) && in_array($defs['type'], ['file', 'image'])) { $attribute = $name . 'Id'; if ($entity->isAttributeChanged($attribute)) { $previousAttachmentId = $entity->getFetched($attribute); if ($previousAttachmentId) { $attachment = $this->getEntityManager() ->getEntity('Attachment', $previousAttachmentId); if ($attachment) { $this->getEntityManager()->removeEntity($attachment); } } } } } } protected function processArrayFieldsSave(Entity $entity) { foreach ($entity->getAttributeList() as $attribute) { $type = $entity->getAttributeType($attribute); if ($type !== Entity::JSON_ARRAY) { continue; } if (!$entity->has($attribute)) { continue; } if (!$entity->isAttributeChanged($attribute)) { continue; } if (!$entity->getAttributeParam($attribute, 'storeArrayValues')) { continue; } if ($entity->getAttributeParam($attribute, 'notStorable')) { continue; } $this->getEntityManager() ->getRepository('ArrayValue') ->storeEntityAttribute($entity, $attribute); } } protected function processWysiwygFieldsSave(Entity $entity) { if (!$entity->isNew()) { return; } $fieldsDefs = $this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'fields'], []); foreach ($fieldsDefs as $field => $defs) { if (!empty($defs['type']) && $defs['type'] === 'wysiwyg') { $content = $entity->get($field); if (!$content) { continue; } if (preg_match_all("/\?entryPoint=attachment&id=([^&=\"']+)/", $content, $matches)) { if (!empty($matches[1]) && is_array($matches[1])) { foreach ($matches[1] as $id) { $attachment = $this->getEntityManager()->getEntity('Attachment', $id); if ($attachment) { if (!$attachment->get('relatedId') && !$attachment->get('sourceId')) { $attachment->set([ 'relatedId' => $entity->id, 'relatedType' => $entity->getEntityType() ]); $this->getEntityManager()->saveEntity($attachment); } } } } } } } } }