From 019af59c8a804960d5b70cd34e8ff156adcfb2f1 Mon Sep 17 00:00:00 2001 From: yuri Date: Fri, 9 Mar 2018 12:01:27 +0200 Subject: [PATCH] default currency if empty --- .../Espo/Core/ORM/Repositories/RDB.php | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/application/Espo/Core/ORM/Repositories/RDB.php b/application/Espo/Core/ORM/Repositories/RDB.php index 149c2f3fcd..007e07e5b4 100644 --- a/application/Espo/Core/ORM/Repositories/RDB.php +++ b/application/Espo/Core/ORM/Repositories/RDB.php @@ -40,7 +40,8 @@ use \Espo\Core\Interfaces\Injectable; class RDB extends \Espo\ORM\Repositories\RDB implements Injectable { protected $dependencies = array( - 'metadata' + 'metadata', + 'config' ); protected $injections = array(); @@ -51,6 +52,10 @@ class RDB extends \Espo\ORM\Repositories\RDB implements Injectable protected $processFieldsAfterSaveDisabled = false; + protected $processFieldsBeforeSaveDisabled = false; + + protected $fieldByTypeListCache = []; + protected function addDependency($name) { $this->dependencies[] = $name; @@ -83,6 +88,11 @@ class RDB extends \Espo\ORM\Repositories\RDB implements Injectable return $this->getInjection('metadata'); } + protected function getConfig() + { + return $this->getInjection('config'); + } + public function __construct($entityType, EntityManager $entityManager, EntityFactory $entityFactory) { parent::__construct($entityType, $entityManager, $entityFactory); @@ -254,6 +264,10 @@ class RDB extends \Espo\ORM\Repositories\RDB implements Injectable if (!$this->hooksDisabled && empty($options['skipHooks'])) { $this->getEntityManager()->getHookManager()->process($this->entityType, 'beforeSave', $entity, $options); } + + if (!$this->processFieldsBeforeSaveDisabled) { + $this->processCurrencyFieldsBeforeSave($entity); + } } protected function afterSave(Entity $entity, array $options = array()) @@ -326,6 +340,39 @@ class RDB extends \Espo\ORM\Repositories\RDB implements Injectable return $result; } + protected function getFieldByTypeList($type) + { + if (!array_key_exists($type, $this->fieldByTypeListCache)) { + $fieldDefs = $this->getMetadata()->get(['entityDefs', $this->entityType, 'field'], []); + $list = []; + foreach ($fieldDefs as $field => $defs) { + if (isset($defs['type']) && $defs['type'] === $type) { + $list[] = $field; + } + } + $this->fieldByTypeListCache[$type] = $list; + } + + return $this->fieldByTypeListCache[$type]; + } + + protected function processCurrencyFieldsBeforeSave(Entity $entity) + { + foreach ($this->getFieldByTypeList('currency') as $field) { + $currencyAttribute = $field . 'Currency'; + $defaultCurrency = $this->getConfig()->get('defaultCurrency'); + if ($entity->isNew()) { + if ($entity->get($field) && !$entity->get($currencyAttribute)) { + $entity->set($currencyAttribute, $defaultCurrency); + } + } else { + if ($entity->isAttributeChanged($field) && $entity->has($currencyAttribute) && !$entity->get($currencyAttribute)) { + $entity->set($currencyAttribute, $defaultCurrency); + } + } + } + } + protected function processFileFieldsSave(Entity $entity) { foreach ($entity->getRelations() as $name => $defs) {