services changes

This commit is contained in:
Yuri Kuznetsov
2020-06-20 11:50:13 +03:00
parent 813462744b
commit 626da4c8bb
4 changed files with 166 additions and 119 deletions
+21 -11
View File
@@ -29,26 +29,36 @@
namespace Espo\Services;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\{
BadRequest,
};
class CurrencyRate extends \Espo\Core\Services\Base
use Espo\Core\{
DataManager,
Utils\Config,
};
class CurrencyRate
{
protected function init()
{
$this->addDependency('config');
$this->addDependency('dataManager');
protected $config;
protected $dataManager;
public function __construct(
Config $config,
DataManager $dataManager
) {
$this->config = $config;
$this->dataManager = $dataManager;
}
public function get() : object
{
return (object) ($this->getInjection('config')->get('currencyRates') ?? []);
return (object) ($this->config->get('currencyRates') ?? []);
}
public function set(object $rates) : object
{
$config = $this->getInjection('config');
$config = $this->config;
$currencyList = $config->get('currencyList') ?? [];
foreach (get_object_vars($rates) as $key => $value) {
@@ -73,7 +83,7 @@ class CurrencyRate extends \Espo\Core\Services\Base
$config->setData($data);
$config->save();
$this->getInjection('dataManager')->rebuildDatabase([]);
$this->dataManager->rebuildDatabase([]);
return (object) ($config->get('currencyRates') ?? []);
}
+35 -28
View File
@@ -29,34 +29,41 @@
namespace Espo\Services;
class Language extends \Espo\Core\Services\Base
use Espo\Core\{
Acl,
Container,
Utils\Metadata,
};
use Espo\Entities\User;
class Language
{
protected $metadata;
protected $acl;
protected $user;
protected $container;
protected function init()
{
$this->addDependency('container');
$this->addDependency('metadata');
$this->addDependency('acl');
}
protected function getMetadata()
{
return $this->getInjection('metadata');
}
protected function getAcl()
{
return $this->getInjection('acl');
public function __construct(
Metadata $metadata,
Acl $acl,
User $user,
Container $container
) {
$this->metadata = $metadata;
$this->acl = $acl;
$this->user = $user;
$this->container = $container;
}
protected function getDefaultLanguage()
{
return $this->getInjection('container')->get('defaultLanguage');
return $this->container->get('defaultLanguage');
}
protected function getLanguage()
{
return $this->getInjection('container')->get('language');
return $this->container->get('language');
}
public function getDataForFrontend(bool $default = false)
@@ -68,7 +75,7 @@ class Language extends \Espo\Core\Services\Base
}
$data = $languageObj->getAll();
if ($this->getUser()->isSystem()) {
if ($this->user->isSystem()) {
unset($data['Global']['scopeNames']);
unset($data['Global']['scopeNamesPlural']);
unset($data['Global']['dashlets']);
@@ -94,20 +101,20 @@ class Language extends \Espo\Core\Services\Base
unset($data['Campaign']['tooltips']);
unset($data['Campaign']['presetFilters']);
} else {
$scopeList = array_keys($this->getMetadata()->get(['scopes'], []));
$scopeList = array_keys($this->metadata->get(['scopes'], []));
foreach ($scopeList as $scope) {
if (!$this->getMetadata()->get(['scopes', $scope, 'entity'])) continue;
if ($this->getMetadata()->get(['entityAcl', $scope, 'languageAclDisabled'])) continue;
if (!$this->metadata->get(['scopes', $scope, 'entity'])) continue;
if ($this->metadata->get(['entityAcl', $scope, 'languageAclDisabled'])) continue;
if (!$this->getAcl()->check($scope)) {
if (!$this->acl->check($scope)) {
unset($data[$scope]);
unset($data['Global']['scopeNames'][$scope]);
unset($data['Global']['scopeNamesPlural'][$scope]);
} else {
if (in_array($scope, ['EmailAccount', 'InboundEmail'])) continue;
foreach ($this->getAcl()->getScopeForbiddenFieldList($scope) as $field) {
foreach ($this->acl->getScopeForbiddenFieldList($scope) as $field) {
if (isset($data[$scope]['fields'])) unset($data[$scope]['fields'][$field]);
if (isset($data[$scope]['options'])) unset($data[$scope]['options'][$field]);
if (isset($data[$scope]['links'])) unset($data[$scope]['links'][$field]);
@@ -115,7 +122,7 @@ class Language extends \Espo\Core\Services\Base
}
}
if (!$this->getUser()->isAdmin()) {
if (!$this->user->isAdmin()) {
unset($data['Admin']);
unset($data['LayoutManager']);
unset($data['EntityManager']);
@@ -135,14 +142,14 @@ class Language extends \Espo\Core\Services\Base
],
];
foreach (($this->getMetadata()->get(['app', 'language', 'aclDependencies']) ?? []) as $target => $item) {
foreach (($this->metadata->get(['app', 'language', 'aclDependencies']) ?? []) as $target => $item) {
$targetArr = explode('.', $target);
$aclScope = $item['scope'] ?? null;;
$aclField = $item['field'] ?? null;
if (!$aclScope) continue;
if (!$this->getAcl()->check($aclScope)) continue;
if ($aclField && in_array($aclField, $this->getAcl()->getScopeForbiddenFieldList($aclScope))) continue;
if (!$this->acl->check($aclScope)) continue;
if ($aclField && in_array($aclField, $this->acl->getScopeForbiddenFieldList($aclScope))) continue;
$pointer =& $data;
foreach ($targetArr as $i => $k) {
+31 -14
View File
@@ -29,24 +29,42 @@
namespace Espo\Services;
use \Espo\ORM\Entity;
use Espo\Core\{
ServiceFactory,
Utils\Metadata,
Utils\Util,
ORM\EntityManager,
ORM\Entity,
};
class LastViewed extends \Espo\Core\Services\Base
use Espo\Entities\User;
class LastViewed
{
protected function init()
{
parent::init();
$this->addDependency('serviceFactory');
$this->addDependency('metadata');
protected $serviceFactory;
protected $metadata;
protected $entityManager;
protected $user;
public function __construct(
ServiceFactory $serviceFactory,
Metadata $metadata,
EntityManager $entityManager,
User $user
) {
$this->serviceFactory = $serviceFactory;
$this->metadata = $metadata;
$this->entityManager = $entityManager;
$this->user = $user;
}
public function getList($params)
public function getList($params) : object
{
$repository = $this->getEntityManager()->getRepository('ActionHistoryRecord');
$repository = $this->entityManager->getRepository('ActionHistoryRecord');
$actionHistoryRecordService = $this->getInjection('serviceFactory')->create('ActionHistoryRecord');
$actionHistoryRecordService = $this->serviceFactory->create('ActionHistoryRecord');
$scopes = $this->getInjection('metadata')->get('scopes');
$scopes = $this->metadata->get('scopes');
$targetTypeList = array_filter(array_keys($scopes), function ($item) use ($scopes) {
return !empty($scopes[$item]['object']) || !empty($scopes[$item]['lastViewed']);
@@ -57,7 +75,7 @@ class LastViewed extends \Espo\Core\Services\Base
$selectParams = [
'whereClause' => [
'userId' => $this->getUser()->id,
'userId' => $this->user->id,
'action' => 'read',
'targetType' => $targetTypeList
],
@@ -70,7 +88,7 @@ class LastViewed extends \Espo\Core\Services\Base
foreach ($collection as $i => $entity) {
$actionHistoryRecordService->loadParentNameFields($entity);
$entity->set('id', \Espo\Core\Utils\Util::generateId());
$entity->set('id', Util::generateId());
}
if ($maxSize && count($collection) > $maxSize) {
@@ -86,4 +104,3 @@ class LastViewed extends \Espo\Core\Services\Base
];
}
}
+79 -66
View File
@@ -29,52 +29,65 @@
namespace Espo\Services;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\{
Forbidden,
NotFound,
Error,
};
use Espo\ORM\Entity;
use Espo\Core\Htmlizer\Htmlizer;
use Espo\Core\{
ServiceFactory,
Acl,
Utils\Config,
Utils\Metadata,
Utils\Language,
Utils\Util,
Htmlizer\Htmlizer,
Htmlizer\Factory as HtmlizerFactory,
ORM\EntityManager,
ORM\Entity,
Pdf\Tcpdf,
};
class Pdf extends \Espo\Core\Services\Base
class Pdf
{
protected $fontFace = 'freesans';
protected $fontSize = 12;
protected $removeMassFilePeriod = '1 hour';
protected function init()
{
$this->addDependency('acl');
$this->addDependency('metadata');
$this->addDependency('serviceFactory');
$this->addDependency('entityManager');
$this->addDependency('defaultLanguage');
protected $config;
protected $serviceFactory;
protected $metadata;
protected $entityManager;
protected $acl;
protected $defaultLanguage;
protected $htmlizerFactory;
$this->addDependency('htmlizerFactory');
public function __construct(
Config $config,
ServiceFactory $serviceFactory,
Metadata $metadata,
EntityManager $entityManager,
Acl $acl,
Language $defaultLanguage,
HtmlizerFactory $htmlizerFactory
) {
$this->config = $config;
$this->serviceFactory = $serviceFactory;
$this->metadata = $metadata;
$this->entityManager = $entityManager;
$this->acl = $acl;
$this->defaultLanguage = $defaultLanguage;
$this->htmlizerFactory = $htmlizerFactory;
}
protected function getAcl()
{
return $this->getInjection('acl');
}
protected function getMetadata()
{
return $this->getInjection('metadata');
}
protected function getServiceFactory()
{
return $this->getInjection('serviceFactory');
}
protected function printEntity(Entity $entity, Entity $template, Htmlizer $htmlizer, \Espo\Core\Pdf\Tcpdf $pdf,
?array $additionalData = null)
{
$fontFace = $this->getConfig()->get('pdfFontFace', $this->fontFace);
protected function printEntity(
Entity $entity, Entity $template, Htmlizer $htmlizer, Tcpdf $pdf,
?array $additionalData = null
) {
$fontFace = $this->config->get('pdfFontFace', $this->fontFace);
if ($template->get('fontFace')) {
$fontFace = $template->get('fontFace');
}
@@ -131,13 +144,13 @@ class Pdf extends \Espo\Core\Services\Base
public function generateMailMerge($entityType, $entityList, Entity $template, $name, $campaignId = null)
{
$htmlizer = $this->createHtmlizer();
$pdf = new \Espo\Core\Pdf\Tcpdf();
$pdf = new Tcpdf();
$pdf->setUseGroupNumbers(true);
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');
}
foreach ($entityList as $entity) {
@@ -149,9 +162,9 @@ class Pdf extends \Espo\Core\Services\Base
$this->printEntity($entity, $template, $htmlizer, $pdf);
}
$filename = \Espo\Core\Utils\Util::sanitizeFileName($name) . '.pdf';
$filename = Util::sanitizeFileName($name) . '.pdf';
$attachment = $this->getEntityManager()->getEntity('Attachment');
$attachment = $this->entityManager->getEntity('Attachment');
$content = $pdf->output('', 'S');
@@ -164,52 +177,52 @@ class Pdf extends \Espo\Core\Services\Base
'contents' => $content
]);
$this->getEntityManager()->saveEntity($attachment);
$this->entityManager->saveEntity($attachment);
return $attachment->id;
}
public function massGenerate($entityType, $idList, $templateId, $checkAcl = false)
{
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');
}
$maxCount = $this->getConfig()->get('massPrintPdfMaxCount');
$maxCount = $this->config->get('massPrintPdfMaxCount');
if ($maxCount) {
if (count($idList) > $maxCount) {
throw new Error("Mass print to PDF max count exceeded.");
}
}
$template = $this->getEntityManager()->getEntity('Template', $templateId);
$template = $this->entityManager->getEntity('Template', $templateId);
if (!$template) {
throw new NotFound();
}
if ($checkAcl) {
if (!$this->getAcl()->check($template)) {
if (!$this->acl->check($template)) {
throw new Forbidden();
}
if (!$this->getAcl()->checkScope($entityType)) {
if (!$this->acl->checkScope($entityType)) {
throw new Forbidden();
}
}
$htmlizer = $this->createHtmlizer();
$pdf = new \Espo\Core\Pdf\Tcpdf();
$pdf = new Tcpdf();
$pdf->setUseGroupNumbers(true);
$entityList = $this->getEntityManager()->getRepository($entityType)->where([
$entityList = $this->entityManager->getRepository($entityType)->where([
'id' => $idList
])->find();
foreach ($entityList as $entity) {
if ($checkAcl) {
if (!$this->getAcl()->check($entity)) continue;
if (!$this->acl->check($entity)) continue;
}
$service->loadAdditionalFields($entity);
if (method_exists($service, 'loadAdditionalFieldsForPdf')) {
@@ -221,19 +234,19 @@ class Pdf extends \Espo\Core\Services\Base
$content = $pdf->output('', 'S');
$entityTypeTranslated = $this->getInjection('defaultLanguage')->translate($entityType, 'scopeNamesPlural');
$filename = \Espo\Core\Utils\Util::sanitizeFileName($entityTypeTranslated) . '.pdf';
$entityTypeTranslated = $this->defaultLanguage->translate($entityType, 'scopeNamesPlural');
$filename = Util::sanitizeFileName($entityTypeTranslated) . '.pdf';
$attachment = $this->getEntityManager()->getEntity('Attachment');
$attachment = $this->entityManager->getEntity('Attachment');
$attachment->set([
'name' => $filename,
'type' => 'application/pdf',
'role' => 'Mass Pdf',
'contents' => $content
]);
$this->getEntityManager()->saveEntity($attachment);
$this->entityManager->saveEntity($attachment);
$job = $this->getEntityManager()->getEntity('Job');
$job = $this->entityManager->getEntity('Job');
$job->set([
'serviceName' => 'Pdf',
'methodName' => 'removeMassFileJob',
@@ -243,7 +256,7 @@ class Pdf extends \Espo\Core\Services\Base
'executeTime' => (new \DateTime())->modify('+' . $this->removeMassFilePeriod)->format('Y-m-d H:i:s'),
'queue' => 'q1'
]);
$this->getEntityManager()->saveEntity($job);
$this->entityManager->saveEntity($job);
return $attachment->id;
}
@@ -253,20 +266,20 @@ class Pdf extends \Espo\Core\Services\Base
if (empty($data->id)) {
return;
}
$attachment = $this->getEntityManager()->getEntity('Attachment', $data->id);
$attachment = $this->entityManager->getEntity('Attachment', $data->id);
if (!$attachment) return;
if ($attachment->get('role') !== 'Mass Pdf') return;
$this->getEntityManager()->removeEntity($attachment);
$this->entityManager->removeEntity($attachment);
}
public function buildFromTemplate(Entity $entity, Entity $template, $displayInline = false, ?array $additionalData = null)
{
$entityType = $entity->getEntityType();
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->loadAdditionalFields($entity);
@@ -279,18 +292,18 @@ class Pdf extends \Espo\Core\Services\Base
throw new Forbidden();
}
if (!$this->getAcl()->check($entity, 'read') || !$this->getAcl()->check($template, 'read')) {
if (!$this->acl->check($entity, 'read') || !$this->acl->check($template, 'read')) {
throw new Forbidden();
}
$htmlizer = $this->createHtmlizer();
$pdf = new \Espo\Core\Pdf\Tcpdf();
$pdf = new Tcpdf();
$this->printEntity($entity, $template, $htmlizer, $pdf, $additionalData);
if ($displayInline) {
$name = $entity->get('name');
$name = \Espo\Core\Utils\Util::sanitizeFileName($name);
$name = Util::sanitizeFileName($name);
$fileName = $name . '.pdf';
$pdf->output($fileName, 'I');
@@ -302,6 +315,6 @@ class Pdf extends \Espo\Core\Services\Base
protected function createHtmlizer()
{
return $this->getInjection('htmlizerFactory')->create();
return $this->htmlizerFactory->create();
}
}