diff --git a/application/Espo/Controllers/Preferences.php b/application/Espo/Controllers/Preferences.php index 459c29fcb0..087bb4ce54 100644 --- a/application/Espo/Controllers/Preferences.php +++ b/application/Espo/Controllers/Preferences.php @@ -17,16 +17,41 @@ class Preferences extends \Espo\Core\Controllers\Base { return $this->getContainer()->get('entityManager'); } - - public function actionRead($params) + + protected function handleUserAccess($userId) { - $userId = $params['id']; if (!$this->getUser()->isAdmin()) { if ($this->getUser()->id != $userId) { throw new Forbidden(); } } - $entity = $this->getEntityManager()->getEntity('Preferences', $userId); + } + + public function actionPatch($params, $data) + { + return $this->actionUpdate($params, $data); + } + + public function actionUpdate($params, $data) + { + $userId = $params['id']; + $this->handleUserAccess($userId); + + $entity = $this->getEntityManager()->getEntity('Preferences', $userId); + if ($entity) { + $entity->set($data); + $this->getEntityManager()->saveEntity($entity); + return $entity->toArray(); + } + throw new Error(); + } + + public function actionRead($params) + { + $userId = $params['id']; + $this->handleUserAccess($userId); + + $entity = $this->getEntityManager()->getEntity('Preferences', $userId); if ($entity) { return $entity->toArray(); } diff --git a/application/Espo/Core/Loaders/EntityManager.php b/application/Espo/Core/Loaders/EntityManager.php index f07df564f5..73d1254722 100644 --- a/application/Espo/Core/Loaders/EntityManager.php +++ b/application/Espo/Core/Loaders/EntityManager.php @@ -35,6 +35,7 @@ class EntityManager $entityManager = new \Espo\Core\ORM\EntityManager($params); $entityManager->setEspoMetadata($this->getContainer()->get('metadata')); $entityManager->setHookManager($this->getContainer()->get('hookManager')); + $entityManager->setContainer($this->getContainer()); return $entityManager; } diff --git a/application/Espo/Core/ORM/EntityManager.php b/application/Espo/Core/ORM/EntityManager.php index 5293b0dba9..2d3822ecda 100644 --- a/application/Espo/Core/ORM/EntityManager.php +++ b/application/Espo/Core/ORM/EntityManager.php @@ -11,6 +11,18 @@ class EntityManager extends \Espo\ORM\EntityManager protected $user; + protected $container; + + public function setContainer(\Espo\Core\Container $container) + { + $this->container = $container; + } + + public function getContainer() + { + return $this->container; + } + public function setUser($user) { $this->user = $user; diff --git a/application/Espo/Core/ORM/Repositories/RDB.php b/application/Espo/Core/ORM/Repositories/RDB.php new file mode 100644 index 0000000000..24a5d38dc5 --- /dev/null +++ b/application/Espo/Core/ORM/Repositories/RDB.php @@ -0,0 +1,245 @@ +injections[$name] = $object; + } + + protected function getInjection($name) + { + return $this->injections[$name]; + } + + public function getDependencyList() + { + return $this->dependencies; + } + + protected function getMetadata() + { + return $this->metadata; + } + + public function setMetadata($metadata) + { + $this->metadata = $metadata; + } + + protected function handleSelectParams(&$params, $entityName = false) + { + $this->handleEmailAddressParams($params, $entityName); + } + + protected function handleEmailAddressParams(&$params, $entityName = false) + { + if (empty($entityName)) { + $entityName = $this->entityName; + } + + $defs = $this->getEntityManager()->getMetadata()->get($entityName); + if (!empty($defs['relations']) && array_key_exists('emailAddresses', $defs['relations'])) { + if (empty($params['leftJoins'])) { + $params['leftJoins'] = array(); + } + if (empty($params['whereClause'])) { + $params['whereClause'] = array(); + } + if (empty($params['joinConditions'])) { + $params['joinConditions'] = array(); + } + $params['distinct'] = true; + $params['leftJoins'] = array('emailAddresses'); + $params['joinConditions'] = array( + 'emailAddresses' => array( + 'primary' => 1 + ) + ); + } + } + + protected function beforeRemove(Entity $entity) + { + parent::beforeRemove($entity); + $this->getEntityManager()->getHookManager()->process($this->entityName, 'beforeRemove', $entity); + } + + protected function afterRemove(Entity $entity) + { + parent::afterRemove($entity); + $this->getEntityManager()->getHookManager()->process($this->entityName, 'afterRemove', $entity); + } + + public function remove(Entity $entity) + { + $this->getEntityManager()->getHookManager()->process($this->entityName, 'beforeRemove', $entity); + + $result = parent::remove($entity); + if ($result) { + $this->getEntityManager()->getHookManager()->process($this->entityName, 'afterRemove', $entity); + } + return $result; + } + + protected function beforeSave(Entity $entity) + { + parent::beforeSave($entity); + $this->getEntityManager()->getHookManager()->process($this->entityName, 'beforeSave', $entity); + } + + protected function afterSave(Entity $entity) + { + parent::afterSave($entity); + $this->getEntityManager()->getHookManager()->process($this->entityName, 'afterSave', $entity); + } + + public function save(Entity $entity) + { + $nowString = date('Y-m-d H:i:s', time()); + $restoreData = array(); + + if ($entity->isNew()) { + if (!$entity->has('id')) { + $entity->set('id', uniqid()); + } + + if ($entity->hasField('createdAt')) { + $entity->set('createdAt', $nowString); + } + if ($entity->hasField('createdById')) { + $entity->set('createdById', $this->entityManager->getUser()->id); + } + + if ($entity->has('modifiedById')) { + $restoreData['modifiedById'] = $entity->get('modifiedById'); + } + if ($entity->has('modifiedAt')) { + $restoreData['modifiedAt'] = $entity->get('modifiedAt'); + } + $entity->clear('modifiedById'); + $entity->clear('modifiedAt'); + } else { + if ($entity->hasField('modifiedAt')) { + $entity->set('modifiedAt', $nowString); + } + if ($entity->hasField('modifiedById')) { + $entity->set('modifiedById', $this->entityManager->getUser()->id); + } + + if ($entity->has('createdById')) { + $restoreData['createdById'] = $entity->get('createdById'); + } + if ($entity->has('createdAt')) { + $restoreData['createdAt'] = $entity->get('createdAt'); + } + $entity->clear('createdById'); + $entity->clear('createdAt'); + } + $result = parent::save($entity); + + $entity->set($restoreData); + + $this->handleEmailAddressSave($entity); + $this->handleSpecifiedRelations($entity); + + return $result; + } + + protected function handleEmailAddressSave(Entity $entity) + { + if ($entity->hasRelation('emailAddresses') && $entity->hasField('emailAddress')) { + $email = $entity->get('emailAddress'); + $pdo = $this->getPDO(); + + $emailAddressRepository = $this->getEntityManager()->getRepository('EmailAddress'); + + if (!empty($email)) { + if ($email != $entity->getFetched('emailAddress')) { + + $emailAddressNew = $emailAddressRepository->where(array('lower' => strtolower($email)))->findOne(); + $isNewEmailAddress = false; + if (!$emailAddressNew) { + $emailAddressNew = $emailAddressRepository->get(); + $emailAddressNew->set('name', $email); + $emailAddressRepository->save($emailAddressNew); + $isNewEmailAddress = true; + } + + $emailOld = $entity->getFetched('emailAddress'); + if (!empty($emailOld)) { + $emailAddressOld = $emailAddressRepository->where(array('lower' => strtolower($emailOld)))->findOne(); + $this->unrelate($entity, 'emailAddresses', $emailAddressOld); + } + $this->relate($entity, 'emailAddresses', $emailAddressNew); + + $query = " + UPDATE entity_email_address + SET `primary` = 1 + WHERE + entity_id = ".$pdo->quote($entity->id)." AND + entity_type = ".$pdo->quote($this->entityName)." AND + email_address_id = ".$pdo->quote($emailAddressNew->id)." + "; + $sth = $pdo->prepare($query); + $sth->execute(); + } + } else { + $emailOld = $entity->getFetched('emailAddress'); + if (!empty($emailOld)) { + $emailAddressOld = $emailAddressRepository->where(array('lower' => strtolower($emailOld)))->findOne(); + $this->unrelate($entity, 'emailAddresses', $emailAddressOld); + } + } + } + } + + protected function handleSpecifiedRelations(Entity $entity) + { + $relationTypes = array($entity::HAS_MANY, $entity::MANY_MANY, $entity::HAS_CHILDREN); + foreach ($entity->getRelations() as $name => $defs) { + if (in_array($defs['type'], $relationTypes)) { + $fieldName = $name . 'Ids'; + if ($entity->has($fieldName)) { + $specifiedIds = $entity->get($fieldName); + if (is_array($specifiedIds)) { + $toRemoveIds = array(); + $existingIds = array(); + foreach ($entity->get($name) as $foreignEntity) { + $existingIds[] = $foreignEntity->id; + } + foreach ($existingIds as $id) { + if (!in_array($id, $specifiedIds)) { + $toRemoveIds[] = $id; + } + } + foreach ($specifiedIds as $id) { + if (!in_array($id, $existingIds)) { + $this->relate($entity, $name, $id); + } + } + foreach ($toRemoveIds as $id) { + $this->unrelate($entity, $name, $id); + } + } + } + } + } + } +} + diff --git a/application/Espo/Core/ORM/Repository.php b/application/Espo/Core/ORM/Repository.php index 494ed418fc..7f9ac67ecf 100644 --- a/application/Espo/Core/ORM/Repository.php +++ b/application/Espo/Core/ORM/Repository.php @@ -2,9 +2,29 @@ namespace Espo\Core\ORM; -class Repository extends \Espo\ORM\Repository -{ +use \Espo\Core\Interfaces\Injectable; +abstract class Repository extends \Espo\ORM\Repository implements Injectable +{ + protected $dependencies = array(); + + protected $injections = array(); + + public function inject($name, $object) + { + $this->injections[$name] = $object; + } + + protected function getInjection($name) + { + return $this->injections[$name]; + } + + public function getDependencyList() + { + return $this->dependencies; + } + protected function getMetadata() { return $this->metadata; @@ -14,205 +34,5 @@ class Repository extends \Espo\ORM\Repository { $this->metadata = $metadata; } - - protected function handleSelectParams(&$params, $entityName = false) - { - $this->handleEmailAddressParams($params, $entityName); - } - - protected function handleEmailAddressParams(&$params, $entityName = false) - { - if (empty($entityName)) { - $entityName = $this->entityName; - } - - $defs = $this->getEntityManager()->getMetadata()->get($entityName); - if (!empty($defs['relations']) && array_key_exists('emailAddresses', $defs['relations'])) { - if (empty($params['leftJoins'])) { - $params['leftJoins'] = array(); - } - if (empty($params['whereClause'])) { - $params['whereClause'] = array(); - } - if (empty($params['joinConditions'])) { - $params['joinConditions'] = array(); - } - $params['distinct'] = true; - $params['leftJoins'] = array('emailAddresses'); - $params['joinConditions'] = array( - 'emailAddresses' => array( - 'primary' => 1 - ) - ); - } - } - - protected function beforeRemove(Entity $entity) - { - parent::beforeRemove($entity); - $this->getEntityManager()->getHookManager()->process($this->entityName, 'beforeRemove', $entity); - } - - protected function afterRemove(Entity $entity) - { - parent::afterRemove($entity); - $this->getEntityManager()->getHookManager()->process($this->entityName, 'afterRemove', $entity); - } - - public function remove(Entity $entity) - { - $this->getEntityManager()->getHookManager()->process($this->entityName, 'beforeRemove', $entity); - - $result = parent::remove($entity); - if ($result) { - $this->getEntityManager()->getHookManager()->process($this->entityName, 'afterRemove', $entity); - } - return $result; - } - - protected function beforeSave(Entity $entity) - { - parent::beforeSave($entity); - $this->getEntityManager()->getHookManager()->process($this->entityName, 'beforeSave', $entity); - } - - protected function afterSave(Entity $entity) - { - parent::afterSave($entity); - $this->getEntityManager()->getHookManager()->process($this->entityName, 'afterSave', $entity); - } - - public function save(Entity $entity) - { - $nowString = date('Y-m-d H:i:s', time()); - $restoreData = array(); - - if ($entity->isNew()) { - if (!$entity->has('id')) { - $entity->set('id', uniqid()); - } - - if ($entity->hasField('createdAt')) { - $entity->set('createdAt', $nowString); - } - if ($entity->hasField('createdById')) { - $entity->set('createdById', $this->entityManager->getUser()->id); - } - - if ($entity->has('modifiedById')) { - $restoreData['modifiedById'] = $entity->get('modifiedById'); - } - if ($entity->has('modifiedAt')) { - $restoreData['modifiedAt'] = $entity->get('modifiedAt'); - } - $entity->clear('modifiedById'); - $entity->clear('modifiedAt'); - } else { - if ($entity->hasField('modifiedAt')) { - $entity->set('modifiedAt', $nowString); - } - if ($entity->hasField('modifiedById')) { - $entity->set('modifiedById', $this->entityManager->getUser()->id); - } - - if ($entity->has('createdById')) { - $restoreData['createdById'] = $entity->get('createdById'); - } - if ($entity->has('createdAt')) { - $restoreData['createdAt'] = $entity->get('createdAt'); - } - $entity->clear('createdById'); - $entity->clear('createdAt'); - } - $result = parent::save($entity); - - $entity->set($restoreData); - - $this->handleEmailAddressSave($entity); - $this->handleSpecifiedRelations($entity); - - return $result; - } - - protected function handleEmailAddressSave(Entity $entity) - { - if ($entity->hasRelation('emailAddresses') && $entity->hasField('emailAddress')) { - $email = $entity->get('emailAddress'); - $pdo = $this->getPDO(); - - $emailAddressRepository = $this->getEntityManager()->getRepository('EmailAddress'); - - if (!empty($email)) { - if ($email != $entity->getFetched('emailAddress')) { - - $emailAddressNew = $emailAddressRepository->where(array('lower' => strtolower($email)))->findOne(); - $isNewEmailAddress = false; - if (!$emailAddressNew) { - $emailAddressNew = $emailAddressRepository->get(); - $emailAddressNew->set('name', $email); - $emailAddressRepository->save($emailAddressNew); - $isNewEmailAddress = true; - } - - $emailOld = $entity->getFetched('emailAddress'); - if (!empty($emailOld)) { - $emailAddressOld = $emailAddressRepository->where(array('lower' => strtolower($emailOld)))->findOne(); - $this->unrelate($entity, 'emailAddresses', $emailAddressOld); - } - $this->relate($entity, 'emailAddresses', $emailAddressNew); - - $query = " - UPDATE entity_email_address - SET `primary` = 1 - WHERE - entity_id = ".$pdo->quote($entity->id)." AND - entity_type = ".$pdo->quote($this->entityName)." AND - email_address_id = ".$pdo->quote($emailAddressNew->id)." - "; - $sth = $pdo->prepare($query); - $sth->execute(); - } - } else { - $emailOld = $entity->getFetched('emailAddress'); - if (!empty($emailOld)) { - $emailAddressOld = $emailAddressRepository->where(array('lower' => strtolower($emailOld)))->findOne(); - $this->unrelate($entity, 'emailAddresses', $emailAddressOld); - } - } - } - } - - protected function handleSpecifiedRelations(Entity $entity) - { - $relationTypes = array($entity::HAS_MANY, $entity::MANY_MANY, $entity::HAS_CHILDREN); - foreach ($entity->getRelations() as $name => $defs) { - if (in_array($defs['type'], $relationTypes)) { - $fieldName = $name . 'Ids'; - if ($entity->has($fieldName)) { - $specifiedIds = $entity->get($fieldName); - if (is_array($specifiedIds)) { - $toRemoveIds = array(); - $existingIds = array(); - foreach ($entity->get($name) as $foreignEntity) { - $existingIds[] = $foreignEntity->id; - } - foreach ($existingIds as $id) { - if (!in_array($id, $specifiedIds)) { - $toRemoveIds[] = $id; - } - } - foreach ($specifiedIds as $id) { - if (!in_array($id, $existingIds)) { - $this->relate($entity, $name, $id); - } - } - foreach ($toRemoveIds as $id) { - $this->unrelate($entity, $name, $id); - } - } - } - } - } - } } diff --git a/application/Espo/Core/ORM/RepositoryFactory.php b/application/Espo/Core/ORM/RepositoryFactory.php index b1d05af3a9..434e350a94 100644 --- a/application/Espo/Core/ORM/RepositoryFactory.php +++ b/application/Espo/Core/ORM/RepositoryFactory.php @@ -4,7 +4,7 @@ namespace Espo\Core\ORM; class RepositoryFactory extends \Espo\ORM\RepositoryFactory { - protected $defaultRepositoryClassName = '\\Espo\\Core\\ORM\\Repository'; + protected $defaultRepositoryClassName = '\\Espo\\Core\\ORM\\Repositories\\RDB'; protected $espoMetadata = false; @@ -16,9 +16,14 @@ class RepositoryFactory extends \Espo\ORM\RepositoryFactory public function create($name) { $repository = parent::create($name); + + $dependencies = $repository->getDependencyList(); + foreach ($dependencies as $name) { + $repository->inject($name, $this->entityManager->getContainer()->get($name)); + } + $repository->setMetadata($this->espoMetadata); return $repository; } - } diff --git a/application/Espo/Core/Utils/File/Manager.php b/application/Espo/Core/Utils/File/Manager.php index 8b55a7a099..24559d82ee 100644 --- a/application/Espo/Core/Utils/File/Manager.php +++ b/application/Espo/Core/Utils/File/Manager.php @@ -121,9 +121,9 @@ class Manager * @param string $folderPath string - Folder path, Ex. myfolder * @param bool $filePath - File path, Ex. file.json * - * @return string | bool + * @return string | bool | array */ - function getContent($folderPath, $filePath='') + function getContent($folderPath, $filePath = '') { $fullPath= Utils\Util::concatPath($folderPath, $filePath); @@ -140,7 +140,7 @@ class Manager * * @return bool */ - function setContent($content, $folderPath, $filePath='') + function setContent($content, $folderPath, $filePath = '') { $fullPath= Utils\Util::concatPath($folderPath, $filePath); @@ -156,7 +156,7 @@ class Manager * * @return bool */ - function setContentPHP($content, $folderPath, $filePath='') + function setContentPHP($content, $folderPath, $filePath = '') { return $this->setContent($this->getPHPFormat($content), $folderPath, $filePath); } @@ -324,8 +324,7 @@ class Manager return false; } - - + /** * Remove all files in defined directory * @@ -335,7 +334,7 @@ class Manager public function removeFiles($filePaths, $dirPath='') { if (!is_array($filePaths)) { - $filePaths= (array) $filePaths; + $filePaths = (array) $filePaths; } $result= true; @@ -345,7 +344,7 @@ class Manager } if (file_exists($filePath) && is_file($filePath)) { - $result&= unlink($filePath); + $result &= unlink($filePath); } } @@ -789,4 +788,3 @@ return '.var_export($content, true).'; } -?> diff --git a/application/Espo/Entities/Preferences.php b/application/Espo/Entities/Preferences.php index d890eb6fbc..6ab834a790 100644 --- a/application/Espo/Entities/Preferences.php +++ b/application/Espo/Entities/Preferences.php @@ -5,4 +5,10 @@ namespace Espo\Entities; class Preferences extends \Espo\Core\ORM\Entity { + public function getSmtpSettings() + { + // TODO + } + } + diff --git a/application/Espo/ORM/EntityFactory.php b/application/Espo/ORM/EntityFactory.php index 8dcb194830..75923d99de 100644 --- a/application/Espo/ORM/EntityFactory.php +++ b/application/Espo/ORM/EntityFactory.php @@ -23,4 +23,3 @@ class EntityFactory } - diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 76c80e2eef..bdec456029 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -16,10 +16,12 @@ class EntityManager protected $metadata; protected $repositoryHash = array(); + + protected $params = array(); public function __construct($params) { - $this->initPDO($params); + $this->params = $params; $this->metadata = new Metadata(); @@ -46,13 +48,14 @@ class EntityManager public function getMapper($className) { if (empty($this->mappers[$className])) { - $this->mappers[$className] = new $className($this->pdo, $this->entityFactory); + $this->mappers[$className] = new $className($this->getPDO(), $this->entityFactory); } return $this->mappers[$className]; } - protected function initPDO($params) + protected function initPDO() { + $params = $this->params; $this->pdo = new \PDO('mysql:host='.$params['host'].';dbname=' . $params['dbname'], $params['user'], $params['password']); } @@ -93,6 +96,9 @@ class EntityManager public function getPDO() { + if (empty($this->pdo)) { + $this->initPDO(); + } return $this->pdo; } diff --git a/application/Espo/ORM/Repositories/RDB.php b/application/Espo/ORM/Repositories/RDB.php new file mode 100644 index 0000000000..e62de7ee47 --- /dev/null +++ b/application/Espo/ORM/Repositories/RDB.php @@ -0,0 +1,324 @@ +entityName = $entityName; + $this->entityFactory = $entityFactory; + $this->seed = $this->entityFactory->create($entityName); + $this->entityClassName = get_class($this->seed); + $this->entityManager = $entityManager; + } + + protected function getMapper() + { + if (empty($this->mapper)) { + $this->mapper = $this->getEntityManager()->getMapper(self::$mapperClassName); + } + return $this->mapper; + } + + protected function handleSelectParams(&$params) + { + } + + protected function getEntityFactory() + { + return $this->entityFactory; + } + + protected function getEntityManager() + { + return $this->entityManager; + } + + public function reset() + { + $this->whereClause = array(); + $this->listParams = array(); + } + + protected function getNewEntity() + { + $entity = $this->entityFactory->create($this->entityName); + $entity->setIsNew(true); + return $entity; + } + + protected function getEntityById($id) + { + $params = array(); + $this->handleSelectParams($params); + + $entity = $this->entityFactory->create($this->entityName); + if ($this->getMapper()->selectById($entity, $id, $params)) { + $entity->setAsFetched(); + return $entity; + } + return null; + } + + public function get($id = null) + { + if (empty($id)) { + return $this->getNewEntity(); + } + return $this->getEntityById($id); + } + + protected function beforeSave(Entity $entity) + { + } + + protected function afterSave(Entity $entity) + { + } + + public function save(Entity $entity) + { + $this->beforeSave($entity); + if ($entity->isNew()) { + $result = $this->getMapper()->insert($entity); + if ($result) { + $entity->setIsNew(false); + } + } else { + $result = $this->getMapper()->update($entity); + } + if ($result) { + $this->afterSave($entity); + } + return $result; + } + + protected function beforeRemove(Entity $entity) + { + } + + protected function afterRemove(Entity $entity) + { + } + + public function remove(Entity $entity) + { + $this->beforeRemove($entity); + $result = $this->getMapper()->delete($entity); + if ($result) { + $this->afterRemove($entity); + } + return $result; + } + + public function find(array $params = array()) + { + $this->handleSelectParams($params); + $params = $this->getSelectParams($params); + + $dataArr = $this->getMapper()->select($this->seed, $params); + + $collection = new EntityCollection($dataArr, $this->entityName, $this->entityFactory); + $this->reset(); + + return $collection; + } + + public function findOne(array $params = array()) + { + $collection = $this->find($params); + if (count($collection)) { + return $collection[0]; + } + return null; + } + + public function findRelated(Entity $entity, $relationName, array $params = array()) + { + $entityName = $entity->relations[$relationName]['entity']; + $this->handleSelectParams($params, $entityName); + + $dataArr = $this->getMapper()->selectRelated($entity, $relationName, $params); + + $collection = new EntityCollection($dataArr, $entityName, $this->entityFactory); + return $collection; + } + + public function countRelated(Entity $entity, $relationName, array $params = array()) + { + $entityName = $entity->relations[$relationName]['entity']; + $this->handleSelectParams($params, $entityName); + + return $this->getMapper()->countRelated($entity, $relationName, $params); + } + + public function relate(Entity $entity, $relationName, $foreign) + { + if ($foreign instanceof Entity) { + return $this->getMapper()->relate($entity, $relationName, $foreign); + } + if (is_string($foreign)) { + return $this->getMapper()->addRelation($entity, $relationName, $foreign); + } + return false; + } + + public function unrelate(Entity $entity, $relationName, $foreign) + { + if ($foreign instanceof Entity) { + return $this->getMapper()->unrelate($entity, $relationName, $foreign); + } + if (is_string($foreign)) { + return $this->getMapper()->removeRelation($entity, $relationName, $foreign); + } + if ($foreign === true) { + return $this->getMapper()->removeAllRelations($entity, $relationName); + } + return false; + } + + public function getAll() + { + $this->reset(); + return $this->find(); + } + + public function count(array $params = array()) + { + $this->handleSelectParams($params); + + $params = $this->getSelectParams($params); + return $this->getMapper()->count($this->seed, $params); + } + + public function max($field) + { + $params = $this->getSelectParams(); + return $this->getMapper()->max($this->seed, $params, $field); + } + + public function min($field) + { + $params = $this->getSelectParams(); + return $this->getMapper()->min($this->seed, $params, $field); + } + + public function sum($field) + { + $params = $this->getSelectParams(); + return $this->getMapper()->sum($this->seed, $params, $field); + } + + // @TODO use abstract class for list params + // @TODO join conditions + public function join() + { + $args = func_get_args(); + + if (empty($this->listParams['joins'])) { + $this->listParams['joins'] = array(); + } + + foreach ($args as &$param) { + if (is_array($param)) { + foreach ($param as $k => $v) { + $this->listParams['joins'][] = array(); + } + } else { + $this->listParams['joins'][] = $param; + } + } + + return $this; + } + + public function distinct() + { + $this->listParams['distinct'] = true; + return $this; + } + + public function where($param1 = array(), $param2 = null) + { + if (is_array($param1)) { + $this->whereClause = $param1 + $this->whereClause; + + } else { + if (!is_null($param2)) { + $this->whereClause[$param1] = $param2; + } + } + + return $this; + } + + public function order($field = 'id', $direction = "ASC") + { + $this->listParams['orderBy'] = $field; + $this->listParams['order'] = $direction; + + return $this; + } + + public function limit($offset, $limit) + { + $this->listParams['offset'] = $offset; + $this->listParams['limit'] = $limit; + + return $this; + } + + public function setListParams(array $params = array()) + { + $this->listParams = $params; + } + + public function getListParams() + { + return $this->listParams; + } + + protected function getSelectParams(array $params = array()) + { + if (isset($params['whereClause'])) { + $params['whereClause'] = $params['whereClause'] + $this->whereClause; + } else { + $params['whereClause'] = $this->whereClause; + } + $params = $params + $this->listParams; + + return $params; + } + + protected function getPDO() + { + return $this->getEntityManager()->getPDO(); + } +} + diff --git a/application/Espo/ORM/Repository.php b/application/Espo/ORM/Repository.php index 15e4ab4f1e..24c0dc219a 100644 --- a/application/Espo/ORM/Repository.php +++ b/application/Espo/ORM/Repository.php @@ -2,12 +2,8 @@ namespace Espo\ORM; -// TODO make it abstract; use Mysql Repostitory -class Repository -{ - - public static $mapperClassName = '\\Espo\\Core\\ORM\\DB\\MysqlMapper'; - +abstract class Repository +{ /** * @var EntityFactory EntityFactory object. */ @@ -18,10 +14,6 @@ class Repository */ protected $entityManager; - /** - * @var Object Mapper. - */ - protected $mapper; /** * @var iModel Seed entity. @@ -36,18 +28,7 @@ class Repository /** * @var string Model Name of aggregate root. */ - protected $entityName; - - /** - * @var array Where clause array. To be used in further find operation. - */ - protected $whereClause = array(); - - /** - * @var array Parameters to be used in further find operations. - */ - protected $listParams = array(); - + protected $entityName; public function __construct($entityName, EntityManager $entityManager, EntityFactory $entityFactory) { @@ -57,19 +38,7 @@ class Repository $this->entityClassName = get_class($this->seed); $this->entityManager = $entityManager; } - - protected function getMapper() - { - if (empty($this->mapper)) { - $this->mapper = $this->getEntityManager()->getMapper(self::$mapperClassName); - } - return $this->mapper; - } - - protected function handleSelectParams(&$params) - { - } - + protected function getEntityFactory() { return $this->entityFactory; @@ -80,265 +49,18 @@ class Repository return $this->entityManager; } - public function reset() - { - $this->whereClause = array(); - $this->listParams = array(); - } + abstract public function get($id = null); - protected function getNewEntity() - { - $entity = $this->entityFactory->create($this->entityName); - $entity->setIsNew(true); - return $entity; - } - - protected function getEntityById($id) - { - $params = array(); - $this->handleSelectParams($params); + abstract public function save(Entity $entity); - $entity = $this->entityFactory->create($this->entityName); - if ($this->getMapper()->selectById($entity, $id, $params)) { - $entity->setAsFetched(); - return $entity; - } - return null; - } - - public function get($id = null) - { - if (empty($id)) { - return $this->getNewEntity(); - } - return $this->getEntityById($id); - } - - protected function beforeSave(Entity $entity) - { - } - - protected function afterSave(Entity $entity) - { - } - - public function save(Entity $entity) - { - $this->beforeSave($entity); - if ($entity->isNew()) { - $result = $this->getMapper()->insert($entity); - if ($result) { - $entity->setIsNew(false); - } - } else { - $result = $this->getMapper()->update($entity); - } - if ($result) { - $this->afterSave($entity); - } - return $result; - } - - protected function beforeRemove(Entity $entity) - { - } - - protected function afterRemove(Entity $entity) - { - } - - public function remove(Entity $entity) - { - $this->beforeRemove($entity); - $result = $this->getMapper()->delete($entity); - if ($result) { - $this->afterRemove($entity); - } - return $result; - } + abstract public function remove(Entity $entity); - public function find(array $params = array()) - { - $this->handleSelectParams($params); - $params = $this->getSelectParams($params); + abstract public function find(array $params); + + abstract public function findOne(array $params); - $dataArr = $this->getMapper()->select($this->seed, $params); - - $collection = new EntityCollection($dataArr, $this->entityName, $this->entityFactory); - $this->reset(); - - return $collection; - } + abstract public function getAll(); - public function findOne(array $params = array()) - { - $collection = $this->find($params); - if (count($collection)) { - return $collection[0]; - } - return null; - } - - public function findRelated(Entity $entity, $relationName, array $params = array()) - { - $entityName = $entity->relations[$relationName]['entity']; - $this->handleSelectParams($params, $entityName); - - $dataArr = $this->getMapper()->selectRelated($entity, $relationName, $params); - - $collection = new EntityCollection($dataArr, $entityName, $this->entityFactory); - return $collection; - } - - public function countRelated(Entity $entity, $relationName, array $params = array()) - { - $entityName = $entity->relations[$relationName]['entity']; - $this->handleSelectParams($params, $entityName); - - return $this->getMapper()->countRelated($entity, $relationName, $params); - } - - public function relate(Entity $entity, $relationName, $foreign) - { - if ($foreign instanceof Entity) { - return $this->getMapper()->relate($entity, $relationName, $foreign); - } - if (is_string($foreign)) { - return $this->getMapper()->addRelation($entity, $relationName, $foreign); - } - return false; - } - - public function unrelate(Entity $entity, $relationName, $foreign) - { - if ($foreign instanceof Entity) { - return $this->getMapper()->unrelate($entity, $relationName, $foreign); - } - if (is_string($foreign)) { - return $this->getMapper()->removeRelation($entity, $relationName, $foreign); - } - if ($foreign === true) { - return $this->getMapper()->removeAllRelations($entity, $relationName); - } - return false; - } - - public function getAll() - { - $this->reset(); - return $this->find(); - } - - public function count(array $params = array()) - { - $this->handleSelectParams($params); - - $params = $this->getSelectParams($params); - return $this->getMapper()->count($this->seed, $params); - } - - public function max($field) - { - $params = $this->getSelectParams(); - return $this->getMapper()->max($this->seed, $params, $field); - } - - public function min($field) - { - $params = $this->getSelectParams(); - return $this->getMapper()->min($this->seed, $params, $field); - } - - public function sum($field) - { - $params = $this->getSelectParams(); - return $this->getMapper()->sum($this->seed, $params, $field); - } - - // @TODO use abstract class for list params - // @TODO join conditions - public function join() - { - $args = func_get_args(); - - if (empty($this->listParams['joins'])) { - $this->listParams['joins'] = array(); - } - - foreach ($args as &$param) { - if (is_array($param)) { - foreach ($param as $k => $v) { - $this->listParams['joins'][] = array(); - } - } else { - $this->listParams['joins'][] = $param; - } - } - - return $this; - } - - public function distinct() - { - $this->listParams['distinct'] = true; - return $this; - } - - public function where($param1 = array(), $param2 = null) - { - if (is_array($param1)) { - $this->whereClause = $param1 + $this->whereClause; - - } else { - if (!is_null($param2)) { - $this->whereClause[$param1] = $param2; - } - } - - return $this; - } - - public function order($field = 'id', $direction = "ASC") - { - $this->listParams['orderBy'] = $field; - $this->listParams['order'] = $direction; - - return $this; - } - - public function limit($offset, $limit) - { - $this->listParams['offset'] = $offset; - $this->listParams['limit'] = $limit; - - return $this; - } - - public function setListParams(array $params = array()) - { - $this->listParams = $params; - } - - public function getListParams() - { - return $this->listParams; - } - - protected function getSelectParams(array $params = array()) - { - if (isset($params['whereClause'])) { - $params['whereClause'] = $params['whereClause'] + $this->whereClause; - } else { - $params['whereClause'] = $this->whereClause; - } - $params = $params + $this->listParams; - - return $params; - } - - protected function getPDO() - { - return $this->getEntityManager()->getPDO(); - } + abstract public function count(array $params); } diff --git a/application/Espo/Repositories/Email.php b/application/Espo/Repositories/Email.php index 4b91a11c7c..22e2ab36bf 100644 --- a/application/Espo/Repositories/Email.php +++ b/application/Espo/Repositories/Email.php @@ -4,7 +4,7 @@ namespace Espo\Repositories; use Espo\ORM\Entity; -class Email extends \Espo\Core\ORM\Repository +class Email extends \Espo\Core\ORM\Repositories\RDB { protected function prepareAddressess(Entity $entity, $type) { diff --git a/application/Espo/Repositories/EmailAddress.php b/application/Espo/Repositories/EmailAddress.php index 1bd15f8a56..0c2b942842 100644 --- a/application/Espo/Repositories/EmailAddress.php +++ b/application/Espo/Repositories/EmailAddress.php @@ -4,7 +4,7 @@ namespace Espo\Repositories; use Espo\ORM\Entity; -class EmailAddress extends \Espo\Core\ORM\Repository +class EmailAddress extends \Espo\Core\ORM\Repositories\RDB { public function getIds($arr = array()) { diff --git a/application/Espo/Repositories/Preferences.php b/application/Espo/Repositories/Preferences.php index f30ebd1114..ec9e87df57 100644 --- a/application/Espo/Repositories/Preferences.php +++ b/application/Espo/Repositories/Preferences.php @@ -6,18 +6,33 @@ use Espo\ORM\Entity; class Preferences extends \Espo\Core\ORM\Repository { + protected $dependencies = array( + 'fileManager' + ); + protected $data = array(); protected $entityName = 'Preferences'; + protected function getFileManager() + { + return $this->getInjection('fileManager'); + } + + protected function getFilePath($id) + { + return 'data/preferences/' . $id . '.php'; + } + public function get($id = null) { if ($id) { $entity = $this->entityFactory->create('Preferences'); + $entity->id = $id; if (empty($this->data[$id])) { - $fileName = 'data/preferences/' . $id; + $fileName = $this->getFilePath($id); if (file_exists($fileName)) { - $this->data[$id] = include ($fileName); + $this->data[$id] = $this->getFileManager()->getContent($fileName); } else { $fields = $this->getMetadata()->get('entityDefs.Preferences.fields'); $defaults = array(); @@ -33,6 +48,41 @@ class Preferences extends \Espo\Core\ORM\Repository return $entity; } } + + public function save(Entity $entity) + { + if ($entity->id) { + $this->data[$entity->id] = $entity->toArray(); + + $fileName = $this->getFilePath($entity->id); + $this->getFileManager()->setContentPHP($this->data[$entity->id], $fileName); + return $entity; + } + } + + public function remove(Entity $entity) + { + $fileName = $this->getFilePath($id); + unlink($fileName); + if (!file_exists($fileName)) { + return true; + } + } + public function find(array $params) + { + } + + public function findOne(array $params) + { + } + + public function getAll() + { + } + + public function count(array $params) + { + } }