services refactoring

This commit is contained in:
Yuri Kuznetsov
2020-06-26 16:06:28 +03:00
parent 451b8ea1e5
commit aee81c1533
18 changed files with 210 additions and 180 deletions
+21 -4
View File
@@ -33,9 +33,12 @@ use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Utils\Util;
use Espo\Core\Exceptions\ForbiddenSilent;
use Espo\Core\Utils\Util;
use Espo\Core\Utils\ControllerUtil;
use Espo\Core\Record\Collection as RecordCollection;
class Record extends Base
{
const MAX_SIZE_LIMIT = 200;
@@ -130,14 +133,21 @@ class Record extends Base
$result = $this->getRecordService()->find($params);
if ($result instanceof RecordCollection) {
return (object) [
'total' => $result->getTotal(),
'list' => $result->getValueMapList(),
];
}
if (is_array($result)) {
return [
return (object) [
'total' => $result['total'],
'list' => isset($result['collection']) ? $result['collection']->getValueMapList() : $result['list']
];
}
return [
return (object) [
'total' => $result->total,
'list' => isset($result->collection) ? $result->collection->getValueMapList() : $result->list
];
@@ -171,7 +181,7 @@ class Record extends Base
protected function fetchListParamsFromRequest(&$params, $request, $data)
{
\Espo\Core\Utils\ControllerUtil::fetchListParamsFromRequest($params, $request, $data);
ControllerUtil::fetchListParamsFromRequest($params, $request, $data);
}
public function actionListLinked($params, $data, $request)
@@ -192,6 +202,13 @@ class Record extends Base
$result = $this->getRecordService()->findLinked($id, $link, $params);
if ($result instanceof RecordCollection) {
return (object) [
'total' => $result->getTotal(),
'list' => $result->getValueMapList(),
];
}
if (is_array($result)) {
return [
'total' => $result['total'],
@@ -29,18 +29,16 @@
namespace Espo\Core\Controllers;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\NotFound;
use \Espo\Core\Exceptions\BadRequest;
use \Espo\Core\Utils\Util;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Utils\Util;
class RecordTree extends Record
{
public static $defaultAction = 'list';
// protected $defaultRecordServiceName = 'RecordTree';
public function actionListTree($params, $data, $request)
{
if (!$this->getAcl()->check($this->name, 'read')) {
@@ -52,14 +50,14 @@ class RecordTree extends Record
$maxDepth = $request->get('maxDepth');
$onlyNotEmpty = $request->get('onlyNotEmpty');
$collection = $this->getRecordService()->getTree($parentId, array(
$collection = $this->getRecordService()->getTree($parentId, [
'where' => $where,
'onlyNotEmpty' => $onlyNotEmpty
), 0, $maxDepth);
return array(
], 0, $maxDepth);
return (object) [
'list' => $collection->toArray(),
'path' => $this->getRecordService()->getTreeItemPath($parentId)
);
'path' => $this->getRecordService()->getTreeItemPath($parentId),
];
}
public function getActionLastChildrenIdList($params, $data, $request)
@@ -0,0 +1,74 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://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/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Record;
use Espo\ORM\{
ICollection,
};
/**
* Wrapper for ORM Collections. Contains a total number of records.
*/
class Collection
{
protected $collection;
protected $total;
public function __construct(ICollection $collection, ?int $total = null)
{
$this->collection = $collection;
$this->total = $total;
}
public function getTotal() : ?int
{
return $this->total;
}
public function getCollection() : ICollection
{
return $this->collection;
}
public function getValueMapList() : array
{
if (!$this->collection->getEntityType()) {
$list = [];
foreach ($this->collection as $e) {
$item = $e->getValueMap();
$item->_scope = $e->getEntityType();
$list[] = $item;
}
return $list;
}
return $this->collection->getValueMapList();
}
}
@@ -34,7 +34,7 @@ use Espo\Core\Exceptions\Error;
use Espo\Core\ServiceFactory;
use Espo\Core\Utils\Metadata;
use Espo\Core\Services\Record;
use Espo\Core\Services\Crud;
/**
* Container for record services. Lazy loading is used.
@@ -58,7 +58,7 @@ class RecordServiceContainer
$this->metadata = $metadata;
}
public function get(string $entityType) : Record
public function get(string $entityType) : Crud
{
$name = $entityType;
@@ -33,7 +33,7 @@ use Espo\ORM\Entity;
use StdClass;
interface Record
interface Crud
{
public function create(StdClass $data) : Entity;
@@ -42,6 +42,4 @@ interface Record
public function update(string $id, StdClass $data) : Entity;
public function delete(string $id);
public function find(array $params) : StdClass;
}
@@ -40,7 +40,7 @@ use Espo\Modules\Crm\Entities\Campaign;
use Espo\Core\Mail\Sender;
use Laminas\Mail\Message;
use StdClass;
use Espo\Core\Record\Collection as RecordCollection;
class MassEmail extends \Espo\Services\Record
{
@@ -530,7 +530,7 @@ class MassEmail extends \Espo\Services\Record
return $this->campaignService;
}
protected function findLinkedQueueItems(string $id, array $params) : StdClass
protected function findLinkedQueueItems(string $id, array $params) : RecordCollection
{
$link = 'queueItems';
@@ -557,10 +557,7 @@ class MassEmail extends \Espo\Services\Record
$total = $this->getRepository()->countRelated($entity, $link, $selectParams);
return (object) [
'total' => $total,
'collection' => $collection
];
return new RecordCollection($collection, $total);
}
public function getSmtpAccountDataList()
@@ -35,7 +35,7 @@ use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use StdClass;
use Espo\Core\Record\Collection as RecordCollection;
class TargetList extends \Espo\Services\Record
{
@@ -234,7 +234,7 @@ class TargetList extends \Espo\Services\Record
}
}
protected function findLinkedOptedOut(string $id, array $params) : StdClass
protected function findLinkedOptedOut(string $id, array $params) : RecordCollection
{
$pdo = $this->getEntityManager()->getPDO();
$query = $this->getEntityManager()->getQuery();
@@ -299,10 +299,15 @@ class TargetList extends \Espo\Services\Record
$row = $sth->fetch(\PDO::FETCH_ASSOC);
$count = $row['count'];
return (object) [
'total' => $count,
'list' => $arr
];
$collection = $this->getEntityManager()->createCollection();
foreach ($arr as $row) {
$e = $this->getEntityManager()->getEntity($row['_scope']);
$e->set($row);
$collection[] = $e;
}
return new RecordCollection($collection, $count);
}
public function optOut(string $id, string $targetType, string $targetId)
+2 -2
View File
@@ -254,7 +254,7 @@ class EntityCollection implements \Iterator, \Countable, \ArrayAccess, \Seekable
return $arr;
}
public function getValueMapList()
public function getValueMapList() : array
{
return $this->toArray(true);
}
@@ -269,7 +269,7 @@ class EntityCollection implements \Iterator, \Countable, \ArrayAccess, \Seekable
$this->isFetched = false;
}
public function isFetched()
public function isFetched() : bool
{
return $this->isFetched;
}
+1 -1
View File
@@ -231,7 +231,7 @@ class EntityManager
return $this->pdo;
}
public function createCollection(?string $entityType = null, $data = [])
public function createCollection(?string $entityType = null, array $data = [])
{
$collection = new EntityCollection($data, $entityType, $this->entityFactory);
return $collection;
+1 -1
View File
@@ -31,5 +31,5 @@ namespace Espo\ORM;
interface ICollection
{
public function getValueMapList() : array;
}
@@ -29,9 +29,11 @@
namespace Espo\Services;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\NotFound;
use Espo\ORM\Entity;
class ActionHistoryRecord extends Record
{
@@ -41,7 +43,7 @@ class ActionHistoryRecord extends Record
protected $forceSelectAllAttributes = true;
public function loadParentNameFields(\Espo\ORM\Entity $entity)
public function loadParentNameFields(Entity $entity)
{
if ($entity->get('targetId') && $entity->get('targetType')) {
$repository = $this->getEntityManager()->getRepository($entity->get('targetType'));
@@ -58,4 +60,3 @@ class ActionHistoryRecord extends Record
}
}
}
@@ -29,18 +29,22 @@
namespace Espo\Services;
class AdminNotifications extends \Espo\Core\Services\Base
use Espo\Core\Di;
class AdminNotifications implements
Di\ConfigAware,
Di\EntityManagerAware
{
use Di\ConfigSetter;
use Di\EntityManagerSetter;
/**
* Job for checking a new version of EspoCRM
*
* @param object $data
*
* @return boolean
* Job for checking a new version of EspoCRM.
*/
public function jobCheckNewVersion($data)
{
$config = $this->getConfig();
$config = $this->config;
if (!$config->get('adminNotifications') || !$config->get('adminNotificationsNewVersion')) {
return true;
@@ -72,7 +76,7 @@ class AdminNotifications extends \Espo\Core\Services\Base
}
/**
* Job for cheking a new version of installed extensions
* Job for cheking a new version of installed extensions.
*
* @param object $data
*
@@ -80,13 +84,13 @@ class AdminNotifications extends \Espo\Core\Services\Base
*/
public function jobCheckNewExtensionVersion($data)
{
$config = $this->getConfig();
$config = $this->config;
if (!$config->get('adminNotifications') || !$config->get('adminNotificationsNewExtensionVersion')) {
return true;
}
$pdo = $this->getEntityManager()->getPDO();
$pdo = $this->entityManager->getPDO();
$query = "
SELECT id, name, version, check_version_url as url
@@ -153,13 +157,8 @@ class AdminNotifications extends \Espo\Core\Services\Base
/**
* Get latest version
*
* @param string $url
* @param array $requestData
*
* @return array|null
*/
protected function getLatestRelease($url = null, array $requestData = [], $urlPath = 'release/latest')
protected function getLatestRelease(?string $url = null, array $requestData = [], string $urlPath = 'release/latest')
{
if (function_exists('curl_version')) {
$ch = curl_init();
@@ -183,5 +182,6 @@ class AdminNotifications extends \Espo\Core\Services\Base
}
}
}
return null;
}
}
+5 -5
View File
@@ -29,12 +29,12 @@
namespace Espo\Services;
use \Espo\ORM\Entity;
use Espo\ORM\Entity;
use \Espo\Core\Exceptions\BadRequest;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\NotFound;
class Attachment extends Record
{
@@ -29,10 +29,6 @@
namespace Espo\Services;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\NotFound;
class AuthLogRecord extends Record
{
protected $internalAttributeList = [];
@@ -40,5 +36,4 @@ class AuthLogRecord extends Record
protected $actionHistoryDisabled = true;
protected $forceSelectAllAttributes = true;
}
-4
View File
@@ -29,10 +29,6 @@
namespace Espo\Services;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\NotFound;
class AuthToken extends Record
{
protected $actionHistoryDisabled = true;
@@ -29,10 +29,10 @@
namespace Espo\Services;
use \Espo\ORM\Entity;
use Espo\ORM\Entity;
use \Espo\Core\Exceptions\NotFound;
use \Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\Forbidden;
class DashboardTemplate extends Record
{
+41 -89
View File
@@ -29,79 +29,55 @@
namespace Espo\Services;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\ORM\Entity;
use \Espo\Core\Htmlizer\Htmlizer;
use Espo\Core\Di;
class DataPrivacy extends \Espo\Core\Services\Base
class DataPrivacy implements
Di\AclAware,
Di\AclManagerAware,
Di\MetadataAware,
Di\ServiceFactoryAware,
Di\EntityManagerAware,
Di\FieldManagerUtilAware,
Di\UserAware
{
protected function init()
{
$this->addDependency('fileManager');
$this->addDependency('acl');
$this->addDependency('aclManager');
$this->addDependency('metadata');
$this->addDependency('serviceFactory');
$this->addDependency('dateTime');
$this->addDependency('number');
$this->addDependency('entityManager');
$this->addDependency('defaultLanguage');
$this->addDependency('fieldManagerUtil');
$this->addDependency('user');
}
use Di\AclSetter;
use Di\AclManagerSetter;
use Di\MetadataSetter;
use Di\ServiceFactorySetter;
use Di\EntityManagerSetter;
use Di\FieldManagerUtilSetter;
use Di\UserSetter;
protected function getAcl()
public function erase(string $entityType, string $id, array $fieldList)
{
return $this->getInjection('acl');
}
protected function getMetadata()
{
return $this->getInjection('metadata');
}
protected function getServiceFactory()
{
return $this->getInjection('serviceFactory');
}
protected function getFileManager()
{
return $this->getInjection('fileManager');
}
protected function getEntityManager()
{
return $this->getInjection('entityManager');
}
public function erase($entityType, $id, array $fieldList)
{
if ($this->getAcl()->get('dataPrivacyPermission') === 'no') {
if ($this->acl->get('dataPrivacyPermission') === 'no') {
throw new Forbidden();
}
if ($this->getServiceFactory()->checkExists($entityType)) {
$service = $this->getServiceFactory()->create($entityType);
if ($this->serviceFactory->checkExists($entityType)) {
$service = $this->serviceFactory->create($entityType);
} else {
$service = $this->getServiceFactory()->create('Record');
$service = $this->serviceFactory->create('Record');
$service->setEntityType($entityType);
}
$entity = $this->getEntityManager()->getEntity($entityType, $id);
$entity = $this->entityManager->getEntity($entityType, $id);
if (!$entity) {
throw new NotFound();
}
if (!$this->getAcl()->check($entity, 'edit')) {
if (!$this->acl->check($entity, 'edit')) {
throw new Forbidden("No edit access.");
}
$forbiddenFieldList = $this->getAcl()->getScopeForbiddenFieldList($entityType, 'edit');
$forbiddenFieldList = $this->acl->getScopeForbiddenFieldList($entityType, 'edit');
foreach ($fieldList as $field) {
if (in_array($field, $forbiddenFieldList)) {
@@ -111,10 +87,10 @@ class DataPrivacy extends \Espo\Core\Services\Base
$service->loadAdditionalFields($entity);
$filedManager = $this->getInjection('fieldManagerUtil');
$filedManager = $this->fieldManagerUtil;
foreach ($fieldList as $field) {
$type = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $field, 'type']);
$type = $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']);
$attributeList = $filedManager->getActualAttributeList($entityType, $field);
if ($type === 'email') {
@@ -122,13 +98,13 @@ class DataPrivacy extends \Espo\Core\Services\Base
foreach ($emailAddressList as $emailAddress) {
if (
$this
->getInjection('aclManager')
->getImplementation('EmailAddress')
->checkEditInEntity($this->getInjection('user'), $emailAddress, $entity)
->aclManager
->getImplementation('EmailAddress')
->checkEditInEntity($this->user, $emailAddress, $entity)
) {
$emailAddress->set('name', 'ERASED:' . $emailAddress->id);
$emailAddress->set('optOut', true);
$this->getEntityManager()->saveEntity($emailAddress);
$this->entityManager->saveEntity($emailAddress);
}
}
@@ -142,12 +118,12 @@ class DataPrivacy extends \Espo\Core\Services\Base
foreach ($phoneNumberList as $phoneNumber) {
if (
$this
->getInjection('aclManager')
->getImplementation('PhoneNumber')
->checkEditInEntity($this->getInjection('user'), $phoneNumber, $entity)
->aclManager
->getImplementation('PhoneNumber')
->checkEditInEntity($this->user, $phoneNumber, $entity)
) {
$phoneNumber->set('name', 'ERASED:' . $phoneNumber->id);
$this->getEntityManager()->saveEntity($phoneNumber);
$this->entityManager->saveEntity($phoneNumber);
}
}
@@ -159,15 +135,15 @@ class DataPrivacy extends \Espo\Core\Services\Base
else if ($type === 'file' || $type === 'image') {
$attachmentId = $entity->get($field . 'Id');
if ($attachmentId) {
$attachment = $this->getEntityManager()->getEntity('Attachment', $attachmentId);
$this->getEntityManager()->removeEntity($attachment);
$attachment = $this->entityManager->getEntity('Attachment', $attachmentId);
$this->entityManager->removeEntity($attachment);
}
}
else if ($type === 'attachmentMultiple') {
$attachmentList = $entity->get($field);
foreach ($attachmentList as $attachment) {
$this->getEntityManager()->removeEntity($attachment);
$this->entityManager->removeEntity($attachment);
}
}
@@ -180,32 +156,8 @@ class DataPrivacy extends \Espo\Core\Services\Base
}
}
$this->getEntityManager()->saveEntity($entity);
$this->entityManager->saveEntity($entity);
return true;
}
public function exportPdf()
{
$htmlizer = new Htmlizer(
$this->getFileManager(),
$this->getInjection('dateTime'),
$this->getInjection('number'),
$this->getAcl(),
$this->getInjection('entityManager'),
$this->getInjection('metadata'),
$this->getInjection('defaultLanguage')
);
$pdf = new \Espo\Core\Pdf\Tcpdf();
$fontFace = $this->getConfig()->get('pdfFontFace', $this->fontFace);
$pdf->setFont($fontFace, '', $this->fontSize, '', true);
$pdf->setPrintHeader(false);
}
}
+13 -16
View File
@@ -47,7 +47,8 @@ use Espo\Core\{
ORM\EntityManager,
AclManager,
Utils\Util,
Services\Record as RecordServiceInterface,
Services\Crud,
Record\Collection as RecordCollection,
};
use Espo\Core\Di;
@@ -58,7 +59,7 @@ use StdClass;
* A layer between Controller and Repository. For CRUD and other operations with records.
* If a service with the name of an entity type exists then it will be used instead this one.
*/
class Record implements RecordServiceInterface,
class Record implements Crud,
Di\ConfigAware,
Di\ServiceFactoryAware,
@@ -1129,7 +1130,7 @@ class Record implements RecordServiceInterface,
return $this->find($params);
}
public function find(array $params) : StdClass
public function find(array $params) : RecordCollection
{
$disableCount = false;
if (
@@ -1183,10 +1184,7 @@ class Record implements RecordServiceInterface,
}
}
return (object) [
'total' => $total,
'collection' => $collection,
];
return new RecordCollection($collection, $total);
}
public function getListKanban(array $params) : StdClass
@@ -1336,7 +1334,7 @@ class Record implements RecordServiceInterface,
return $this->findLinked($id, $link, $params);
}
public function findLinked(string $id, string $link, array $params) : StdClass
public function findLinked(string $id, string $link, array $params) : RecordCollection
{
$entity = $this->getRepository()->get($id);
if (!$entity) {
@@ -1450,10 +1448,7 @@ class Record implements RecordServiceInterface,
}
}
return (object) [
'total' => $total,
'collection' => $collection,
];
return new RecordCollection($collection, $total);
}
public function linkEntity($id, $link, $foreignId) //TODO Remove in 5.8
@@ -2684,8 +2679,9 @@ class Record implements RecordServiceInterface,
return $list;
}
protected function convertEntityCurrency(Entity $entity, string $targetCurrency, string $baseCurrency, $rates, bool $allFields = false, ?array $fieldList = null)
{
protected function convertEntityCurrency(
Entity $entity, string $targetCurrency, string $baseCurrency, $rates, bool $allFields = false, ?array $fieldList = null
) {
if (!$this->getAcl()->check($entity, 'edit')) return;
$data = $this->getConvertCurrencyValues($entity, $targetCurrency, $baseCurrency, $rates, $allFields, $fieldList);
@@ -2697,8 +2693,9 @@ class Record implements RecordServiceInterface,
}
}
public function getConvertCurrencyValues(Entity $entity, string $targetCurrency, string $baseCurrency, $rates, bool $allFields = false, ?array $fieldList = null)
{
public function getConvertCurrencyValues(
Entity $entity, string $targetCurrency, string $baseCurrency, $rates, bool $allFields = false, ?array $fieldList = null
) {
$fieldList = $fieldList ?? $this->getConvertCurrencyFieldList();
$data = (object) [];