diff --git a/.gitignore b/.gitignore index 37767b9e8f..d1f529798f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ data/logs/* data/cache/* data/upload/* +data/preferences/* application/config.php /test.php /test2.php diff --git a/application/Espo/Controllers/App.php b/application/Espo/Controllers/App.php index adb828ae4d..5ce9d9ee13 100644 --- a/application/Espo/Controllers/App.php +++ b/application/Espo/Controllers/App.php @@ -9,7 +9,7 @@ class App extends \Espo\Core\Controllers\Record return array( 'user' => $this->getUser()->toArray(), 'acl' => $this->getAcl()->toArray(), - 'preferences' => array(), + 'preferences' => $this->getPreferences()->toArray() ); } } diff --git a/application/Espo/Controllers/Preferences.php b/application/Espo/Controllers/Preferences.php new file mode 100644 index 0000000000..459c29fcb0 --- /dev/null +++ b/application/Espo/Controllers/Preferences.php @@ -0,0 +1,37 @@ +getContainer()->get('preferences'); + } + + protected function getEntityManager() + { + return $this->getContainer()->get('entityManager'); + } + + public function actionRead($params) + { + $userId = $params['id']; + if (!$this->getUser()->isAdmin()) { + if ($this->getUser()->id != $userId) { + throw new Forbidden(); + } + } + $entity = $this->getEntityManager()->getEntity('Preferences', $userId); + if ($entity) { + return $entity->toArray(); + } + throw new NotFound(); + } + +} + diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index e6bf088792..a139fa2eab 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -67,6 +67,11 @@ class Container ) ); } + + private function loadPreferences() + { + return $this->get('entityManager')->getEntity('Preferences', $this->get('user')->id); + } private function loadConfig() { @@ -123,7 +128,6 @@ class Container ); } - private function loadLayout() { return new \Espo\Core\Utils\Layout( @@ -165,7 +169,6 @@ class Container public function setUser($user) { $this->data['user'] = $user; - } - + } } diff --git a/application/Espo/Core/Controllers/Base.php b/application/Espo/Core/Controllers/Base.php index 23a0e9a61a..bb091c0a98 100644 --- a/application/Espo/Core/Controllers/Base.php +++ b/application/Espo/Core/Controllers/Base.php @@ -54,6 +54,11 @@ abstract class Base return $this->container->get('config'); } + protected function getPreferences() + { + return $this->container->get('preferences'); + } + protected function getMetadata() { return $this->container->get('metadata'); diff --git a/application/Espo/Core/ORM/DB/MysqlMapper.php b/application/Espo/Core/ORM/DB/MysqlMapper.php new file mode 100644 index 0000000000..477cc4fca7 --- /dev/null +++ b/application/Espo/Core/ORM/DB/MysqlMapper.php @@ -0,0 +1,9 @@ +espoMetadata = $espoMetadata; + $this->repositoryFactory->setEspoMetadata($espoMetadata); } public function setHookManager(\Espo\Core\HookManager $hookManager) @@ -45,11 +46,5 @@ class EntityManager extends \Espo\ORM\EntityManager { return $this->espoMetadata->getEntityPath($name); } - - public function init() - { - $this->mapper->setReturnCollection(false); - } - } diff --git a/application/Espo/Core/ORM/Repository.php b/application/Espo/Core/ORM/Repository.php index 88fb805723..494ed418fc 100644 --- a/application/Espo/Core/ORM/Repository.php +++ b/application/Espo/Core/ORM/Repository.php @@ -4,6 +4,16 @@ namespace Espo\Core\ORM; class Repository extends \Espo\ORM\Repository { + + protected function getMetadata() + { + return $this->metadata; + } + + public function setMetadata($metadata) + { + $this->metadata = $metadata; + } protected function handleSelectParams(&$params, $entityName = false) { diff --git a/application/Espo/Core/ORM/RepositoryFactory.php b/application/Espo/Core/ORM/RepositoryFactory.php index 3ac676f576..b1d05af3a9 100644 --- a/application/Espo/Core/ORM/RepositoryFactory.php +++ b/application/Espo/Core/ORM/RepositoryFactory.php @@ -3,9 +3,22 @@ namespace Espo\Core\ORM; class RepositoryFactory extends \Espo\ORM\RepositoryFactory -{ +{ + protected $defaultRepositoryClassName = '\\Espo\\Core\\ORM\\Repository'; - protected $defaultRepositoryClassName = '\\Espo\\Core\\ORM\\Repository'; + protected $espoMetadata = false; + + public function setEspoMetadata($espoMetadata) + { + $this->espoMetadata = $espoMetadata; + } + + public function create($name) + { + $repository = parent::create($name); + $repository->setMetadata($this->espoMetadata); + return $repository; + } } diff --git a/application/Espo/Entities/Preferences.php b/application/Espo/Entities/Preferences.php new file mode 100644 index 0000000000..d890eb6fbc --- /dev/null +++ b/application/Espo/Entities/Preferences.php @@ -0,0 +1,8 @@ +entityFactory = new $entityFactoryClassName($this, $this->metadata); - - $mapperClassName = '\\Espo\\ORM\\DB\\MysqlMapper'; - if (!empty($params['mapperClassName'])) { - $mapperClassName = $params['mapperClassName']; - } - $this->mapper = new $mapperClassName($this->pdo, $this->entityFactory); + $repositoryFactoryClassName = '\\Espo\\ORM\\RepositoryFactory'; if (!empty($params['repositoryFactoryClassName'])) { $repositoryFactoryClassName = $params['repositoryFactoryClassName']; } - $this->repositoryFactory = new $repositoryFactoryClassName($this, $this->entityFactory, $this->mapper); + $this->repositoryFactory = new $repositoryFactoryClassName($this, $this->entityFactory); $this->init(); } + + public function getMapper($className) + { + if (empty($this->mappers[$className])) { + $this->mappers[$className] = new $className($this->pdo, $this->entityFactory); + } + return $this->mappers[$className]; + } protected function initPDO($params) { diff --git a/application/Espo/ORM/Repository.php b/application/Espo/ORM/Repository.php index fb78000ce3..15e4ab4f1e 100644 --- a/application/Espo/ORM/Repository.php +++ b/application/Espo/ORM/Repository.php @@ -2,10 +2,12 @@ namespace Espo\ORM; - +// TODO make it abstract; use Mysql Repostitory class Repository { + public static $mapperClassName = '\\Espo\\Core\\ORM\\DB\\MysqlMapper'; + /** * @var EntityFactory EntityFactory object. */ @@ -17,7 +19,7 @@ class Repository protected $entityManager; /** - * @var \MyApp\DB\iMapper DB Mapper. + * @var Object Mapper. */ protected $mapper; @@ -46,16 +48,23 @@ class Repository */ protected $listParams = array(); - public function __construct($entityName, EntityManager $entityManager, EntityFactory $entityFactory, DB\iMapper $mapper) + + public function __construct($entityName, EntityManager $entityManager, EntityFactory $entityFactory) { $this->entityName = $entityName; $this->entityFactory = $entityFactory; $this->seed = $this->entityFactory->create($entityName); $this->entityClassName = get_class($this->seed); - $this->entityManager = $entityManager; - - $this->mapper = $mapper; - } + $this->entityManager = $entityManager; + } + + protected function getMapper() + { + if (empty($this->mapper)) { + $this->mapper = $this->getEntityManager()->getMapper(self::$mapperClassName); + } + return $this->mapper; + } protected function handleSelectParams(&$params) { @@ -90,7 +99,7 @@ class Repository $this->handleSelectParams($params); $entity = $this->entityFactory->create($this->entityName); - if ($this->mapper->selectById($entity, $id, $params)) { + if ($this->getMapper()->selectById($entity, $id, $params)) { $entity->setAsFetched(); return $entity; } @@ -117,12 +126,12 @@ class Repository { $this->beforeSave($entity); if ($entity->isNew()) { - $result = $this->mapper->insert($entity); + $result = $this->getMapper()->insert($entity); if ($result) { $entity->setIsNew(false); } } else { - $result = $this->mapper->update($entity); + $result = $this->getMapper()->update($entity); } if ($result) { $this->afterSave($entity); @@ -141,7 +150,7 @@ class Repository public function remove(Entity $entity) { $this->beforeRemove($entity); - $result = $this->mapper->delete($entity); + $result = $this->getMapper()->delete($entity); if ($result) { $this->afterRemove($entity); } @@ -153,7 +162,7 @@ class Repository $this->handleSelectParams($params); $params = $this->getSelectParams($params); - $dataArr = $this->mapper->select($this->seed, $params); + $dataArr = $this->getMapper()->select($this->seed, $params); $collection = new EntityCollection($dataArr, $this->entityName, $this->entityFactory); $this->reset(); @@ -175,7 +184,7 @@ class Repository $entityName = $entity->relations[$relationName]['entity']; $this->handleSelectParams($params, $entityName); - $dataArr = $this->mapper->selectRelated($entity, $relationName, $params); + $dataArr = $this->getMapper()->selectRelated($entity, $relationName, $params); $collection = new EntityCollection($dataArr, $entityName, $this->entityFactory); return $collection; @@ -186,16 +195,16 @@ class Repository $entityName = $entity->relations[$relationName]['entity']; $this->handleSelectParams($params, $entityName); - return $this->mapper->countRelated($entity, $relationName, $params); + return $this->getMapper()->countRelated($entity, $relationName, $params); } public function relate(Entity $entity, $relationName, $foreign) { if ($foreign instanceof Entity) { - return $this->mapper->relate($entity, $relationName, $foreign); + return $this->getMapper()->relate($entity, $relationName, $foreign); } if (is_string($foreign)) { - return $this->mapper->addRelation($entity, $relationName, $foreign); + return $this->getMapper()->addRelation($entity, $relationName, $foreign); } return false; } @@ -203,13 +212,13 @@ class Repository public function unrelate(Entity $entity, $relationName, $foreign) { if ($foreign instanceof Entity) { - return $this->mapper->unrelate($entity, $relationName, $foreign); + return $this->getMapper()->unrelate($entity, $relationName, $foreign); } if (is_string($foreign)) { - return $this->mapper->removeRelation($entity, $relationName, $foreign); + return $this->getMapper()->removeRelation($entity, $relationName, $foreign); } if ($foreign === true) { - return $this->mapper->removeAllRelations($entity, $relationName); + return $this->getMapper()->removeAllRelations($entity, $relationName); } return false; } @@ -225,25 +234,25 @@ class Repository $this->handleSelectParams($params); $params = $this->getSelectParams($params); - return $this->mapper->count($this->seed, $params); + return $this->getMapper()->count($this->seed, $params); } public function max($field) { $params = $this->getSelectParams(); - return $this->mapper->max($this->seed, $params, $field); + return $this->getMapper()->max($this->seed, $params, $field); } public function min($field) { $params = $this->getSelectParams(); - return $this->mapper->min($this->seed, $params, $field); + return $this->getMapper()->min($this->seed, $params, $field); } public function sum($field) { $params = $this->getSelectParams(); - return $this->mapper->sum($this->seed, $params, $field); + return $this->getMapper()->sum($this->seed, $params, $field); } // @TODO use abstract class for list params diff --git a/application/Espo/ORM/RepositoryFactory.php b/application/Espo/ORM/RepositoryFactory.php index d4f21365b7..0c6d0f5051 100644 --- a/application/Espo/ORM/RepositoryFactory.php +++ b/application/Espo/ORM/RepositoryFactory.php @@ -10,11 +10,10 @@ class RepositoryFactory protected $defaultRepositoryClassName = '\\Espo\\ORM\\Repository'; - public function __construct(EntityManager $entityManager, EntityFactory $entityFactroy, DB\IMapper $mapper) + public function __construct(EntityManager $entityManager, EntityFactory $entityFactroy) { $this->entityManager = $entityManager; $this->entityFactroy = $entityFactroy; - $this->mapper = $mapper; } public function create($name) @@ -24,7 +23,8 @@ class RepositoryFactory if (!class_exists($className)) { $className = $this->defaultRepositoryClassName; } - $repository = new $className($name, $this->entityManager, $this->entityFactroy, $this->mapper); + + $repository = new $className($name, $this->entityManager, $this->entityFactroy); return $repository; } diff --git a/application/Espo/Repositories/Preferences.php b/application/Espo/Repositories/Preferences.php new file mode 100644 index 0000000000..f30ebd1114 --- /dev/null +++ b/application/Espo/Repositories/Preferences.php @@ -0,0 +1,38 @@ +entityFactory->create('Preferences'); + if (empty($this->data[$id])) { + $fileName = 'data/preferences/' . $id; + if (file_exists($fileName)) { + $this->data[$id] = include ($fileName); + } else { + $fields = $this->getMetadata()->get('entityDefs.Preferences.fields'); + $defaults = array(); + foreach ($fields as $field => $d) { + if (array_key_exists('default', $d)) { + $defaults[$field] = $d['default']; + } + } + $this->data[$id] = $defaults; + } + } + $entity->set($this->data[$id]); + return $entity; + } + } + +} +