From 053d516bc9afa14fc05169f0fc43f763c37a7c76 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 2 Dec 2014 11:21:01 +0200 Subject: [PATCH 1/2] user avatars in user list view --- .../Resources/layouts/User/listSmall.json | 6 ++- .../Resources/metadata/entityDefs/User.json | 3 +- .../templates/user/fields/name/list-link.tpl | 1 + .../res/templates/user/fields/name/list.tpl | 1 + .../client/src/views/user/fields/avatar.js | 9 +++- frontend/client/src/views/user/fields/name.js | 51 +++++++++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 frontend/client/res/templates/user/fields/name/list-link.tpl create mode 100644 frontend/client/res/templates/user/fields/name/list.tpl create mode 100644 frontend/client/src/views/user/fields/name.js diff --git a/application/Espo/Resources/layouts/User/listSmall.json b/application/Espo/Resources/layouts/User/listSmall.json index d5c3dd5b4e..3be8ae57ec 100644 --- a/application/Espo/Resources/layouts/User/listSmall.json +++ b/application/Espo/Resources/layouts/User/listSmall.json @@ -1 +1,5 @@ -[{"name":"name","width":30,"link":true},{"name":"userName"},{"name":"emailAddress"}] +[ + {"name":"name", "width":35, "link":true}, + {"name":"userName"}, + {"name":"emailAddress"} +] diff --git a/application/Espo/Resources/metadata/entityDefs/User.json b/application/Espo/Resources/metadata/entityDefs/User.json index b85d76e9d3..82248f908b 100644 --- a/application/Espo/Resources/metadata/entityDefs/User.json +++ b/application/Espo/Resources/metadata/entityDefs/User.json @@ -12,7 +12,8 @@ "tooltip": true }, "name": { - "type": "personName" + "type": "personName", + "view": "User.Fields.Name" }, "password": { "type": "password", diff --git a/frontend/client/res/templates/user/fields/name/list-link.tpl b/frontend/client/res/templates/user/fields/name/list-link.tpl new file mode 100644 index 0000000000..35e0102e60 --- /dev/null +++ b/frontend/client/res/templates/user/fields/name/list-link.tpl @@ -0,0 +1 @@ +{{{avatar}}} {{value}} diff --git a/frontend/client/res/templates/user/fields/name/list.tpl b/frontend/client/res/templates/user/fields/name/list.tpl new file mode 100644 index 0000000000..6c3e12edbd --- /dev/null +++ b/frontend/client/res/templates/user/fields/name/list.tpl @@ -0,0 +1 @@ +{{{avatar}}} {{value}} \ No newline at end of file diff --git a/frontend/client/src/views/user/fields/avatar.js b/frontend/client/src/views/user/fields/avatar.js index 8283c5bd8e..e108c9b808 100644 --- a/frontend/client/src/views/user/fields/avatar.js +++ b/frontend/client/src/views/user/fields/avatar.js @@ -62,9 +62,14 @@ Espo.define('Views.User.Fields.Avatar', 'Views.Fields.Image', function (Dep) { var imgHtml; if (this.mode == 'detail') { - imgHtml = ''; + imgHtml = ''; } else { - imgHtml = ''; + var cache = this.getCache(); + if (cache) { + t = cache.get('app', 'timestamp'); + } + imgHtml = ''; + return imgHtml; } if (id) { diff --git a/frontend/client/src/views/user/fields/name.js b/frontend/client/src/views/user/fields/name.js new file mode 100644 index 0000000000..c37f76e05c --- /dev/null +++ b/frontend/client/src/views/user/fields/name.js @@ -0,0 +1,51 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + ************************************************************************/ +Espo.define('Views.User.Fields.Name', 'Views.Fields.PersonName', function (Dep) { + + return Dep.extend({ + + listTemplate: 'user.fields.name.list-link', + + listLinkTemplate: 'user.fields.name.list-link', + + data: function () { + return _.extend({ + avatar: this.getAvatarHtml() + }, Dep.prototype.data.call(this)); + }, + + getAvatarHtml: function () { + if (this.getConfig().get('disableAvatars')) { + return ''; + } + var t; + var cache = this.getCache(); + if (cache) { + t = cache.get('app', 'timestamp'); + } else { + t = Date.now(); + } + return ''; + }, + + }); + +}); From fe3cc09623f0ae25ccd9e00c1c65aa2932f97d38 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 2 Dec 2014 13:19:45 +0200 Subject: [PATCH 2/2] orm refactor --- .../Espo/Core/Loaders/EntityManager.php | 2 + .../Espo/Core/ORM/Repositories/RDB.php | 2 - application/Espo/ORM/DB/Mapper.php | 4 +- .../Espo/ORM/DB/{Query.php => Query/Base.php} | 4 +- .../DB/Query/Mysql.php} | 12 +++-- application/Espo/ORM/EntityManager.php | 48 +++++++++++++++++-- application/Espo/ORM/Repositories/RDB.php | 5 +- tests/Espo/ORM/DB/MapperTest.php | 4 +- tests/Espo/ORM/DB/QueryTest.php | 2 +- 9 files changed, 62 insertions(+), 21 deletions(-) rename application/Espo/ORM/DB/{Query.php => Query/Base.php} (99%) rename application/Espo/{Core/ORM/DB/MysqlMapper.php => ORM/DB/Query/Mysql.php} (87%) diff --git a/application/Espo/Core/Loaders/EntityManager.php b/application/Espo/Core/Loaders/EntityManager.php index 0d24be18f3..b76385ad6c 100644 --- a/application/Espo/Core/Loaders/EntityManager.php +++ b/application/Espo/Core/Loaders/EntityManager.php @@ -36,6 +36,8 @@ class EntityManager extends Base 'password' => $config->get('database.password'), 'metadata' => $this->getContainer()->get('metadata')->getOrmMetadata(), 'repositoryFactoryClassName' => '\\Espo\\Core\\ORM\\RepositoryFactory', + 'driver' => $config->get('database.driver'), + 'platform' => $config->get('database.platform') ); $entityManager = new \Espo\Core\ORM\EntityManager($params); diff --git a/application/Espo/Core/ORM/Repositories/RDB.php b/application/Espo/Core/ORM/Repositories/RDB.php index 0031d8aa38..7f847361e6 100644 --- a/application/Espo/Core/ORM/Repositories/RDB.php +++ b/application/Espo/Core/ORM/Repositories/RDB.php @@ -32,8 +32,6 @@ use \Espo\Core\Interfaces\Injectable; class RDB extends \Espo\ORM\Repositories\RDB implements Injectable { - public static $mapperClassName = '\\Espo\\Core\\ORM\\DB\\MysqlMapper'; - protected $dependencies = array( 'metadata' ); diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index 17c94202e3..bc53e448f3 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -43,7 +43,7 @@ abstract class Mapper implements IMapper protected $fieldsMapCache = array(); protected $aliasesCache = array(); - protected $returnCollection = true; + protected $returnCollection = false; protected $collectionClass = "\\Espo\\ORM\\EntityCollection"; @@ -76,7 +76,7 @@ abstract class Mapper implements IMapper 'additionalColumnsConditions' ); - public function __construct(PDO $pdo, \Espo\ORM\EntityFactory $entityFactory, Query $query) { + public function __construct(PDO $pdo, \Espo\ORM\EntityFactory $entityFactory, Query\Base $query) { $this->pdo = $pdo; $this->query = $query; $this->entityFactory = $entityFactory; diff --git a/application/Espo/ORM/DB/Query.php b/application/Espo/ORM/DB/Query/Base.php similarity index 99% rename from application/Espo/ORM/DB/Query.php rename to application/Espo/ORM/DB/Query/Base.php index 16def5faa9..368f8b4eb1 100644 --- a/application/Espo/ORM/DB/Query.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -20,14 +20,14 @@ * along with EspoCRM. If not, see http://www.gnu.org/licenses/. ************************************************************************/ -namespace Espo\ORM\DB; +namespace Espo\ORM\DB\Query; use Espo\ORM\Entity; use Espo\ORM\IEntity; use Espo\ORM\EntityFactory; use PDO; -class Query +class Base { protected static $selectParamList = array( 'select', diff --git a/application/Espo/Core/ORM/DB/MysqlMapper.php b/application/Espo/ORM/DB/Query/Mysql.php similarity index 87% rename from application/Espo/Core/ORM/DB/MysqlMapper.php rename to application/Espo/ORM/DB/Query/Mysql.php index 912c28777c..997e7145c3 100644 --- a/application/Espo/Core/ORM/DB/MysqlMapper.php +++ b/application/Espo/ORM/DB/Query/Mysql.php @@ -20,10 +20,14 @@ * along with EspoCRM. If not, see http://www.gnu.org/licenses/. ************************************************************************/ -namespace Espo\Core\ORM\DB; +namespace Espo\ORM\DB\Query; -class MysqlMapper extends \Espo\ORM\DB\MysqlMapper +use Espo\ORM\Entity; +use Espo\ORM\IEntity; +use Espo\ORM\EntityFactory; +use PDO; + +class Mysql extends Base { - protected $returnCollection = false; -} +} diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 310b57aeba..b46991d83b 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -41,12 +41,28 @@ class EntityManager protected $query; + protected $driverPlatformMap = array( + 'pdo_mysql' => 'Mysql', + 'mysqli' => 'Mysql', + ); + public function __construct($params) { $this->params = $params; $this->metadata = new Metadata(); + if (empty($this->params['platform'])) { + if (empty($this->params['driver'])) { + throw new \Exception('No database driver specified.'); + } + $driver = $this->params['driver']; + if (empty($this->driverPlatformMap[$driver])) { + throw new \Exception("Database driver '{$driver}' is not supported."); + } + $this->params['platform'] = $this->driverPlatformMap[$this->params['driver']]; + } + if (!empty($params['metadata'])) { $this->setMetadata($params['metadata']); } @@ -69,16 +85,36 @@ class EntityManager public function getQuery() { if (empty($this->query)) { - $this->query = new DB\Query($this->getPDO(), $this->entityFactory); + $platform = $this->params['platform']; + $className = '\\Espo\\ORM\\DB\\Query\\' . ucfirst($platform); + $this->query = new $className($this->getPDO(), $this->entityFactory); } return $this->query; } - public function getMapper($className) + protected function getMapperClassName($name) { - if (empty($this->mappers[$className])) { - // TODO use factory + $className = null; + + switch ($name) { + case 'RDB': + $platform = $this->params['platform']; + $className = '\\Espo\\ORM\\DB\\' . ucfirst($platform) . 'Mapper'; + break; + } + return $className; + } + + public function getMapper($name) + { + if ($name{0} == '\\') { + $className = $name; + } else { + $className = $this->getMapperClassName($name); + } + + if (empty($this->mappers[$className])) { $this->mappers[$className] = new $className($this->getPDO(), $this->entityFactory, $this->getQuery()); } return $this->mappers[$className]; @@ -90,7 +126,9 @@ class EntityManager $port = empty($params['port']) ? '' : 'port=' . $params['port'] . ';'; - $this->pdo = new \PDO('mysql:host='.$params['host'].';'.$port.'dbname=' . $params['dbname'] . ';charset=utf8', $params['user'], $params['password']); + $platform = strtolower($params['platform']); + + $this->pdo = new \PDO($platform . ':host='.$params['host'].';'.$port.'dbname=' . $params['dbname'] . ';charset=utf8', $params['user'], $params['password']); $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } diff --git a/application/Espo/ORM/Repositories/RDB.php b/application/Espo/ORM/Repositories/RDB.php index 23dc5da9ee..21d09e701d 100644 --- a/application/Espo/ORM/Repositories/RDB.php +++ b/application/Espo/ORM/Repositories/RDB.php @@ -31,9 +31,6 @@ use \Espo\ORM\IEntity; class RDB extends \Espo\ORM\Repository { - - public static $mapperClassName = '\\Espo\\ORM\\DB\\MysqlMapper'; - /** * @var Object Mapper. */ @@ -66,7 +63,7 @@ class RDB extends \Espo\ORM\Repository protected function getMapper() { if (empty($this->mapper)) { - $this->mapper = $this->getEntityManager()->getMapper(self::$mapperClassName); + $this->mapper = $this->getEntityManager()->getMapper('RDB'); } return $this->mapper; } diff --git a/tests/Espo/ORM/DB/MapperTest.php b/tests/Espo/ORM/DB/MapperTest.php index 6b584dee74..541ff76709 100644 --- a/tests/Espo/ORM/DB/MapperTest.php +++ b/tests/Espo/ORM/DB/MapperTest.php @@ -1,7 +1,7 @@ query = new Query($this->pdo, $this->entityFactory); $this->db = new MysqlMapper($this->pdo, $this->entityFactory, $this->query); + $this->db->setReturnCollection(true); + $this->post = new \Espo\Entities\Post(); $this->comment = new \Espo\Entities\Comment(); $this->tag = new \Espo\Entities\Tag(); diff --git a/tests/Espo/ORM/DB/QueryTest.php b/tests/Espo/ORM/DB/QueryTest.php index 78123d6e3a..61b3011477 100644 --- a/tests/Espo/ORM/DB/QueryTest.php +++ b/tests/Espo/ORM/DB/QueryTest.php @@ -1,6 +1,6 @@