diff --git a/application/Espo/Core/Console/Commands/RunJob.php b/application/Espo/Core/Console/Commands/RunJob.php index 017f111c4f..a60198d090 100644 --- a/application/Espo/Core/Console/Commands/RunJob.php +++ b/application/Espo/Core/Console/Commands/RunJob.php @@ -58,7 +58,6 @@ class RunJob implements Command $jobName = ucfirst(Util::hyphenToCamelCase($jobName)); - $entityManager = $this->entityManager; $job = $entityManager->createEntity('Job', [ diff --git a/application/Espo/Core/Htmlizer/Factory.php b/application/Espo/Core/Htmlizer/Factory.php index b630c44ca4..fb8ae194b7 100644 --- a/application/Espo/Core/Htmlizer/Factory.php +++ b/application/Espo/Core/Htmlizer/Factory.php @@ -30,22 +30,31 @@ namespace Espo\Core\Htmlizer; use Espo\Core\InjectableFactory; +use Espo\Core\Utils\DateTime; class Factory { protected $injectableFactory; + protected $dateTime; - public function __construct(InjectableFactory $injectableFactory) { + public function __construct(InjectableFactory $injectableFactory, DateTime $dateTime) { $this->injectableFactory = $injectableFactory; + $this->dateTime = $dateTime; } - public function create(bool $skipAcl = false) : Htmlizer + public function create(bool $skipAcl = false, ?string $timezone = null) : Htmlizer { $with = []; if ($skipAcl) { $with['acl'] = null; } + if ($timezone) { + $dateTime = clone($this->dateTime); + $dateTime->setTimezone($timezone); + $with['dateTime'] = $dateTime; + } + return $this->injectableFactory->createWith(Htmlizer::class, $with); } } diff --git a/application/Espo/Jobs/AuthTokenControl.php b/application/Espo/Jobs/AuthTokenControl.php index 10cc3ac150..9ce4387622 100644 --- a/application/Espo/Jobs/AuthTokenControl.php +++ b/application/Espo/Jobs/AuthTokenControl.php @@ -29,14 +29,27 @@ namespace Espo\Jobs; -use \Espo\Core\Exceptions; +use Espo\Core\{ + Utils\Config, + ORM\EntityManager, + Jobs\Job, +}; -class AuthTokenControl extends \Espo\Core\Jobs\Base +class AuthTokenControl implements Job { + protected $config; + protected $entityManager; + + public function __construct(Config $config, EntityManager $entityManager) + { + $this->config = $config; + $this->entityManager = $entityManager; + } + public function run() { - $authTokenLifetime = $this->getConfig()->get('authTokenLifetime'); - $authTokenMaxIdleTime = $this->getConfig()->get('authTokenMaxIdleTime'); + $authTokenLifetime = $this->config->get('authTokenLifetime'); + $authTokenMaxIdleTime = $this->config->get('authTokenMaxIdleTime'); if (!$authTokenLifetime && !$authTokenMaxIdleTime) { return; @@ -62,12 +75,11 @@ class AuthTokenControl extends \Espo\Core\Jobs\Base $whereClause['lastAccess<'] = $authTokenMaxIdleTimeThreshold; } - $tokenList = $this->getEntityManager()->getRepository('AuthToken')->where($whereClause)->limit(0, 500)->find(); + $tokenList = $this->entityManager->getRepository('AuthToken')->where($whereClause)->limit(0, 500)->find(); foreach ($tokenList as $token) { $token->set('isActive', false); - $this->getEntityManager()->saveEntity($token); + $this->entityManager->saveEntity($token); } } } - diff --git a/application/Espo/Jobs/CheckNewExtensionVersion.php b/application/Espo/Jobs/CheckNewExtensionVersion.php index c3a48d73ea..c83148167c 100644 --- a/application/Espo/Jobs/CheckNewExtensionVersion.php +++ b/application/Espo/Jobs/CheckNewExtensionVersion.php @@ -35,19 +35,19 @@ class CheckNewExtensionVersion extends CheckNewVersion { public function run() { - if (!$this->getConfig()->get('adminNotifications') || !$this->getConfig()->get('adminNotificationsNewExtensionVersion')) { + if (!$this->config->get('adminNotifications') || !$this->config->get('adminNotificationsNewExtensionVersion')) { return true; } - $job = $this->getEntityManager()->getEntity('Job'); - $job->set(array( + $job = $this->entityManager->getEntity('Job'); + $job->set([ 'name' => 'Check for new versions of installed extensions (job)', 'serviceName' => 'AdminNotifications', 'methodName' => 'jobCheckNewExtensionVersion', - 'executeTime' => $this->getRunTime() - )); + 'executeTime' => $this->getRunTime(), + ]); - $this->getEntityManager()->saveEntity($job); + $this->entityManager->saveEntity($job); return true; } diff --git a/application/Espo/Jobs/CheckNewVersion.php b/application/Espo/Jobs/CheckNewVersion.php index a7e2468692..b4926690d2 100644 --- a/application/Espo/Jobs/CheckNewVersion.php +++ b/application/Espo/Jobs/CheckNewVersion.php @@ -29,25 +29,38 @@ namespace Espo\Jobs; -use Espo\Core\Exceptions; +use Espo\Core\{ + Utils\Config, + ORM\EntityManager, + Jobs\Job, +}; -class CheckNewVersion extends \Espo\Core\Jobs\Base +class CheckNewVersion implements Job { + protected $config; + protected $entityManager; + + public function __construct(Config $config, EntityManager $entityManager) + { + $this->config = $config; + $this->entityManager = $entityManager; + } + public function run() { - if (!$this->getConfig()->get('adminNotifications') || !$this->getConfig()->get('adminNotificationsNewVersion')) { + if (!$this->config->get('adminNotifications') || !$this->config->get('adminNotificationsNewVersion')) { return true; } - $job = $this->getEntityManager()->getEntity('Job'); - $job->set(array( + $job = $this->entityManager->getEntity('Job'); + $job->set([ 'name' => 'Check for New Version (job)', 'serviceName' => 'AdminNotifications', 'methodName' => 'jobCheckNewVersion', - 'executeTime' => $this->getRunTime() - )); + 'executeTime' => $this->getRunTime(), + ]); - $this->getEntityManager()->saveEntity($job); + $this->entityManager->saveEntity($job); return true; } @@ -60,7 +73,7 @@ class CheckNewVersion extends \Espo\Core\Jobs\Base $nextDay = new \DateTime('+ 1 day'); $time = $nextDay->format('Y-m-d') . ' ' . $hour . ':' . $minute . ':00'; - $timeZone = $this->getConfig()->get('timeZone'); + $timeZone = $this->config->get('timeZone'); if (empty($timeZone)) { $timeZone = 'UTC'; } @@ -69,4 +82,4 @@ class CheckNewVersion extends \Espo\Core\Jobs\Base return $datetime->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s'); } -} \ No newline at end of file +} diff --git a/application/Espo/Jobs/Cleanup.php b/application/Espo/Jobs/Cleanup.php index 11a8e3ed9b..1067e9919b 100644 --- a/application/Espo/Jobs/Cleanup.php +++ b/application/Espo/Jobs/Cleanup.php @@ -29,28 +29,59 @@ namespace Espo\Jobs; -use \Espo\Core\Exceptions; +use Espo\Core\Exceptions; -class Cleanup extends \Espo\Core\Jobs\Base +use Espo\Core\{ + Utils\Config, + ORM\EntityManager, + Jobs\Job, + Utils\Metadata, + Utils\File\Manager as FileManager, + InjectableFactory, + SelectManagerFactory, + ServiceFactory, +}; + +use Espo\ORM\Entity; + +class Cleanup implements Job { protected $cleanupJobPeriod = '10 days'; - protected $cleanupActionHistoryPeriod = '15 days'; - protected $cleanupAuthTokenPeriod = '1 month'; - protected $cleanupAuthLogPeriod = '2 months'; - protected $cleanupNotificationsPeriod = '2 months'; - protected $cleanupAttachmentsPeriod = '15 days'; - protected $cleanupAttachmentsFromPeriod = '3 months'; - protected $cleanupBackupPeriod = '2 month'; - protected $cleanupDeletedRecordsPeriod = '3 months'; + protected $config; + protected $entityManager; + protected $metedata; + protected $fileManager; + protected $injectableFactory; + protected $selectManagerFactory; + protected $serviceFactory; + + public function __construct( + Config $config, + EntityManager $entityManager, + Metadata $metadata, + FileManager $fileManager, + InjectableFactory $injectableFactory, + SelectManagerFactory $selectManagerFactory, + ServiceFactory $serviceFactory + ) { + $this->config = $config; + $this->entityManager = $entityManager; + $this->metadata = $metadata; + $this->fileManager = $fileManager; + $this->injectableFactory = $injectableFactory; + $this->selectManagerFactory = $selectManagerFactory; + $this->serviceFactory = $serviceFactory; + } + public function run() { $this->cleanupJobs(); @@ -65,7 +96,7 @@ class Cleanup extends \Espo\Core\Jobs\Base $this->cleanupUniqueIds(); $this->cleanupDeletedRecords(); - $items = $this->getMetadata()->get(['app', 'cleanup']) ?? []; + $items = $this->metadata->get(['app', 'cleanup']) ?? []; usort($items, function ($a, $b) { $o1 = $a['order'] ?? 0; @@ -73,7 +104,7 @@ class Cleanup extends \Espo\Core\Jobs\Base return $o1 > $o2; }); - $injectableFactory = $this->getContainer()->get('injectableFactory'); + $injectableFactory = $this->injectableFactory; foreach ($items as $name => $item) { try { @@ -87,7 +118,7 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupJobs() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); $query = "DELETE FROM `job` WHERE DATE(modified_at) < ".$pdo->quote($this->getCleanupJobFromDate())." AND status <> 'Pending'"; $sth = $pdo->prepare($query); @@ -100,7 +131,7 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupUniqueIds() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); $query = "DELETE FROM `unique_id` WHERE terminate_at IS NOT NULL AND terminate_at < ".$pdo->quote(date('Y-m-d H:i:s')).""; @@ -110,7 +141,7 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupScheduledJobLog() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); $sql = "SELECT id FROM scheduled_job"; $sth = $pdo->prepare($sql); @@ -139,9 +170,9 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupActionHistory() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); - $period = '-' . $this->getConfig()->get('cleanupActionHistoryPeriod', $this->cleanupActionHistoryPeriod); + $period = '-' . $this->config->get('cleanupActionHistoryPeriod', $this->cleanupActionHistoryPeriod); $datetime = new \DateTime(); $datetime->modify($period); @@ -153,9 +184,9 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupAuthToken() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); - $period = '-' . $this->getConfig()->get('cleanupAuthTokenPeriod', $this->cleanupAuthTokenPeriod); + $period = '-' . $this->config->get('cleanupAuthTokenPeriod', $this->cleanupAuthTokenPeriod); $datetime = new \DateTime(); $datetime->modify($period); @@ -167,9 +198,9 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupAuthLog() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); - $period = '-' . $this->getConfig()->get('cleanupAuthLogPeriod', $this->cleanupAuthLogPeriod); + $period = '-' . $this->config->get('cleanupAuthLogPeriod', $this->cleanupAuthLogPeriod); $datetime = new \DateTime(); $datetime->modify($period); @@ -181,7 +212,7 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function getCleanupJobFromDate() { - $period = '-' . $this->getConfig()->get('cleanupJobPeriod', $this->cleanupJobPeriod); + $period = '-' . $this->config->get('cleanupJobPeriod', $this->cleanupJobPeriod); $datetime = new \DateTime(); $datetime->modify($period); return $datetime->format('Y-m-d'); @@ -189,13 +220,13 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupAttachments() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); - $period = '-' . $this->getConfig()->get('cleanupAttachmentsPeriod', $this->cleanupAttachmentsPeriod); + $period = '-' . $this->config->get('cleanupAttachmentsPeriod', $this->cleanupAttachmentsPeriod); $datetime = new \DateTime(); $datetime->modify($period); - $collection = $this->getEntityManager()->getRepository('Attachment')->where(array( + $collection = $this->entityManager->getRepository('Attachment')->where(array( 'OR' => array( array( 'role' => ['Export File', 'Mail Merge', 'Mass Pdf'] @@ -205,11 +236,11 @@ class Cleanup extends \Espo\Core\Jobs\Base ))->limit(0, 5000)->find(); foreach ($collection as $e) { - $this->getEntityManager()->removeEntity($e); + $this->entityManager->removeEntity($e); } - if ($this->getConfig()->get('cleanupOrphanAttachments')) { - $selectManager = $this->getContainer()->get('selectManagerFactory')->create('Attachment'); + if ($this->config->get('cleanupOrphanAttachments')) { + $selectManager = $this->selectManagerFactory->create('Attachment'); $selectParams = $selectManager->getEmptySelectParams(); $selectManager->applyFilter('orphan', $selectParams); @@ -219,29 +250,29 @@ class Cleanup extends \Espo\Core\Jobs\Base 'createdAt>' => '2018-01-01 00:00:00', ]; - $collection = $this->getEntityManager()->getRepository('Attachment')->limit(0, 5000)->find($selectParams); + $collection = $this->entityManager->getRepository('Attachment')->limit(0, 5000)->find($selectParams); foreach ($collection as $e) { - $this->getEntityManager()->removeEntity($e); + $this->entityManager->removeEntity($e); } } - $fromPeriod = '-' . $this->getConfig()->get('cleanupAttachmentsFromPeriod', $this->cleanupAttachmentsFromPeriod); + $fromPeriod = '-' . $this->config->get('cleanupAttachmentsFromPeriod', $this->cleanupAttachmentsFromPeriod); $datetimeFrom = new \DateTime(); $datetimeFrom->modify($fromPeriod); - $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(['scopes', $scope, 'object']) && $scope !== 'Note') continue; - if (!$this->getMetadata()->get(['entityDefs', $scope, 'fields', 'modifiedAt'])) continue; + if (!$this->metadata->get(['scopes', $scope, 'entity'])) continue; + if (!$this->metadata->get(['scopes', $scope, 'object']) && $scope !== 'Note') continue; + if (!$this->metadata->get(['entityDefs', $scope, 'fields', 'modifiedAt'])) continue; $hasAttachmentField = false; if ($scope === 'Note') { $hasAttachmentField = true; } if (!$hasAttachmentField) { - foreach ($this->getMetadata()->get(['entityDefs', $scope, 'fields']) as $field => $defs) { + foreach ($this->metadata->get(['entityDefs', $scope, 'fields']) as $field => $defs) { if (empty($defs['type'])) continue; if (in_array($defs['type'], ['file', 'image', 'attachmentMultiple'])) { $hasAttachmentField = true; @@ -251,8 +282,8 @@ class Cleanup extends \Espo\Core\Jobs\Base } if (!$hasAttachmentField) continue; - if (!$this->getEntityManager()->hasRepository($scope)) continue; - $repository = $this->getEntityManager()->getRepository($scope); + if (!$this->entityManager->hasRepository($scope)) continue; + $repository = $this->entityManager->getRepository($scope); if (!method_exists($repository, 'find')) continue; if (!method_exists($repository, 'where')) continue; @@ -263,7 +294,7 @@ class Cleanup extends \Espo\Core\Jobs\Base ])->find(['withDeleted' => true]); foreach ($deletedEntityList as $deletedEntity) { - $attachmentToRemoveList = $this->getEntityManager()->getRepository('Attachment')->where([ + $attachmentToRemoveList = $this->entityManager->getRepository('Attachment')->where([ 'OR' => [ [ 'relatedType' => $scope, @@ -277,7 +308,7 @@ class Cleanup extends \Espo\Core\Jobs\Base ])->find(); foreach ($attachmentToRemoveList as $attachmentToRemove) { - $this->getEntityManager()->removeEntity($attachmentToRemove); + $this->entityManager->removeEntity($attachmentToRemove); } } } @@ -288,7 +319,7 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupEmails() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); $dateBefore = date('Y-m-d H:i:s', time() - 3600 * 24 * 20); @@ -297,12 +328,12 @@ class Cleanup extends \Espo\Core\Jobs\Base $sth->execute(); while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { $id = $row['id']; - $attachments = $this->getEntityManager()->getRepository('Attachment')->where(array( + $attachments = $this->entityManager->getRepository('Attachment')->where(array( 'parentId' => $id, 'parentType' => 'Email' ))->find(); foreach ($attachments as $attachment) { - $this->getEntityManager()->removeEntity($attachment); + $this->entityManager->removeEntity($attachment); } $sqlDel = "DELETE FROM email WHERE deleted = 1 AND id = ".$pdo->quote($id); $pdo->query($sqlDel); @@ -313,9 +344,9 @@ class Cleanup extends \Espo\Core\Jobs\Base protected function cleanupNotifications() { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->entityManager->getPDO(); - $period = '-' . $this->getConfig()->get('cleanupNotificationsPeriod', $this->cleanupNotificationsPeriod); + $period = '-' . $this->config->get('cleanupNotificationsPeriod', $this->cleanupNotificationsPeriod); $datetime = new \DateTime(); $datetime->modify($period); @@ -325,7 +356,7 @@ class Cleanup extends \Espo\Core\Jobs\Base $sth->execute(); while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { $id = $row['id']; - $this->getEntityManager()->getRepository('Notification')->deleteFromDb($id); + $this->entityManager->getRepository('Notification')->deleteFromDb($id); } } @@ -335,7 +366,7 @@ class Cleanup extends \Espo\Core\Jobs\Base $datetime = new \DateTime('-' . $this->cleanupBackupPeriod); if (file_exists($path)) { - $fileManager = $this->getContainer()->get('fileManager'); + $fileManager = $this->fileManager; $fileList = $fileManager->getFileList($path, false, '', false); foreach ($fileList as $dirName) { @@ -349,16 +380,16 @@ class Cleanup extends \Espo\Core\Jobs\Base } } - protected function cleanupDeletedEntity(\Espo\ORM\Entity $e) + protected function cleanupDeletedEntity(Entity $e) { $scope = $e->getEntityType(); if (!$e->get('deleted')) return; - $repository = $this->getEntityManager()->getRepository($scope); + $repository = $this->entityManager->getRepository($scope); $repository->deleteFromDb($e->id); - $query = $this->getEntityManager()->getQuery(); + $query = $this->entityManager->getQuery(); foreach ($e->getRelationList() as $relation) { if ($e->getRelationType($relation) !== 'manyMany') continue;; @@ -386,11 +417,11 @@ class Cleanup extends \Espo\Core\Jobs\Base $sql = "DELETE FROM `{$relationTable}` WHERE " . implode(' AND ', $partList); - $this->getEntityManager()->getPDO()->query($sql); + $this->entityManager->getPDO()->query($sql); } catch (\Exception $e) {} } - $noteList = $this->getEntityManager()->getRepository('Note')->where([ + $noteList = $this->entityManager->getRepository('Note')->where([ 'OR' => [ [ 'relatedType' => $scope, @@ -403,38 +434,38 @@ class Cleanup extends \Espo\Core\Jobs\Base ] ])->find(['withDeleted' => true]); foreach ($noteList as $note) { - $this->getEntityManager()->removeEntity($note); + $this->entityManager->removeEntity($note); $note->set('deleted', true); $this->cleanupDeletedEntity($note); } if ($scope === 'Note') { - $attachmentList = $this->getEntityManager()->getRepository('Attachment')->where([ + $attachmentList = $this->entityManager->getRepository('Attachment')->where([ 'parentId' => $e->id, 'parentType' => 'Note' ])->find(); foreach ($attachmentList as $attachment) { - $this->getEntityManager()->removeEntity($attachment); - $this->getEntityManager()->getRepository('Attachment')->deleteFromDb($attachment->id); + $this->entityManager->removeEntity($attachment); + $this->entityManager->getRepository('Attachment')->deleteFromDb($attachment->id); } } } protected function cleanupDeletedRecords() { - if (!$this->getConfig()->get('cleanupDeletedRecords')) return; - $period = '-' . $this->getConfig()->get('cleanupDeletedRecordsPeriod', $this->cleanupDeletedRecordsPeriod); + if (!$this->config->get('cleanupDeletedRecords')) return; + $period = '-' . $this->config->get('cleanupDeletedRecordsPeriod', $this->cleanupDeletedRecordsPeriod); $datetime = new \DateTime($period); - $serviceFactory = $this->getServiceFactory(); + $serviceFactory = $this->serviceFactory; - $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->metadata->get(['scopes', $scope, 'entity'])) continue; if ($scope === 'Attachment') continue; - if (!$this->getEntityManager()->hasRepository($scope)) continue; - $repository = $this->getEntityManager()->getRepository($scope); + if (!$this->entityManager->hasRepository($scope)) continue; + $repository = $this->entityManager->getRepository($scope); if (!$repository) continue; if (!method_exists($repository, 'find')) continue; if (!method_exists($repository, 'where')) continue; @@ -454,9 +485,9 @@ class Cleanup extends \Espo\Core\Jobs\Base 'deleted' => 1, ]; - if ($this->getMetadata()->get(['entityDefs', $scope, 'fields', 'modifiedAt'])) { + if ($this->metadata->get(['entityDefs', $scope, 'fields', 'modifiedAt'])) { $whereClause['modifiedAt<'] = $datetime->format('Y-m-d H:i:s'); - } else if ($this->getMetadata()->get(['entityDefs', $scope, 'fields', 'createdAt'])) { + } else if ($this->metadata->get(['entityDefs', $scope, 'fields', 'createdAt'])) { $whereClause['createdAt<'] = $datetime->format('Y-m-d H:i:s'); } diff --git a/application/Espo/Jobs/Dummy.php b/application/Espo/Jobs/Dummy.php index 547fc85048..62cbe04d1e 100644 --- a/application/Espo/Jobs/Dummy.php +++ b/application/Espo/Jobs/Dummy.php @@ -29,10 +29,13 @@ namespace Espo\Jobs; -class Dummy extends \Espo\Core\Jobs\Base +use Espo\Core\{ + Jobs\Job, +}; + +class Dummy implements Job { public function run() { - } -} \ No newline at end of file +} diff --git a/application/Espo/Jobs/ProcessWebhookQueue.php b/application/Espo/Jobs/ProcessWebhookQueue.php index f8aac35a34..f675bcb92d 100644 --- a/application/Espo/Jobs/ProcessWebhookQueue.php +++ b/application/Espo/Jobs/ProcessWebhookQueue.php @@ -29,21 +29,37 @@ namespace Espo\Jobs; -use Espo\Core\Exceptions; +use Espo\Core\{ + Jobs\Job, + AclManager, + Utils\Config, + ORM\EntityManager, + Webhook\Sender, + Webhook\Queue, +}; -class ProcessWebhookQueue extends \Espo\Core\Jobs\Base +class ProcessWebhookQueue implements Job { + protected $config; + protected $entityManager; + protected $aclManager; + + public function __construct(Config $config, EntityManager $entityManager, AclManager $aclManager) + { + $this->config = $config; + $this->entityManager = $entityManager; + $this->aclManager = $aclManager; + } + public function run() { - $sender = new \Espo\Core\Webhook\Sender( - $this->getContainer()->get('config') - ); + $sender = new Sender($this->config); - $webhookQueue = new \Espo\Core\Webhook\Queue( + $webhookQueue = new Queue( $sender, - $this->getContainer()->get('config'), - $this->getContainer()->get('entityManager'), - $this->getContainer()->get('aclManager') + $this->config, + $this->entityManager, + $this->aclManager ); $webhookQueue->process(); diff --git a/application/Espo/Jobs/SendEmailNotifications.php b/application/Espo/Jobs/SendEmailNotifications.php index b58f19c6ad..c62ab2ec9f 100644 --- a/application/Espo/Jobs/SendEmailNotifications.php +++ b/application/Espo/Jobs/SendEmailNotifications.php @@ -29,14 +29,23 @@ namespace Espo\Jobs; -use Espo\Core\Exceptions; +use Espo\Core\{ + ServiceFactory, + Jobs\Job, +}; -class SendEmailNotifications extends \Espo\Core\Jobs\Base +class SendEmailNotifications implements Job { + protected $serviceFactory; + + public function __construct(ServiceFactory $serviceFactory) + { + $this->serviceFactory = $serviceFactory; + } + public function run() { - $service = $this->getServiceFactory()->create('EmailNotification'); + $service = $this->serviceFactory->create('EmailNotification'); $service->process(); } } - diff --git a/application/Espo/Modules/Crm/Business/Reminder/EmailReminder.php b/application/Espo/Modules/Crm/Business/Reminder/EmailReminder.php index 520332c8a2..5d47bd6072 100644 --- a/application/Espo/Modules/Crm/Business/Reminder/EmailReminder.php +++ b/application/Espo/Modules/Crm/Business/Reminder/EmailReminder.php @@ -29,34 +29,43 @@ namespace Espo\Modules\Crm\Business\Reminder; -use \Espo\ORM\Entity; +use Espo\ORM\Entity; use Espo\Core\Utils\Util; +use Espo\Core\{ + ORM\EntityManager, + Utils\TemplateFileManager, + Mail\Sender, + Utils\Config, + Htmlizer\Factory as HtmlizerFactory, + Utils\Language, +}; + class EmailReminder { protected $entityManager; - protected $mailSender; - protected $config; - protected $dateTime; - protected $templateFileManager; - protected $language; + protected $htmlizerFactory; - public function __construct($entityManager, $templateFileManager, $mailSender, $config, $fileManager, $dateTime, $number, $language) - { + public function __construct( + EntityManager $entityManager, + TemplateFileManager $templateFileManager, + Sender $mailSender, + Config $config, + HtmlizerFactory $htmlizerFactory, + Language $language + ) { $this->entityManager = $entityManager; + $this->templateFileManager = $templateFileManager; $this->mailSender = $mailSender; $this->config = $config; - $this->dateTime = $dateTime; $this->language = $language; - $this->number = $number; - $this->fileManager = $fileManager; - $this->templateFileManager = $templateFileManager; + $this->htmlizerFactory = $htmlizerFactory; } protected function getEntityManager() @@ -117,12 +126,12 @@ class EmailReminder $preferences = $this->getEntityManager()->getEntity('Preferences', $user->id); $timezone = $preferences->get('timeZone'); - $dateTime = clone($this->dateTime); - if ($timezone) { - $dateTime->setTimezone($timezone); + + if (!$timezone) { + $timezone = null; } - $htmlizer = new \Espo\Core\Htmlizer\Htmlizer($this->fileManager, $dateTime, $this->number, null); + $htmlizer = $this->htmlizerFactory->create(true, $timezone); $subject = $htmlizer->render($entity, $subjectTpl, 'reminder-email-subject-' . $entity->getEntityType(), $data, true); $body = $htmlizer->render($entity, $bodyTpl, 'reminder-email-body-' . $entity->getEntityType(), $data, false); diff --git a/application/Espo/Modules/Crm/Jobs/CheckEmailAccounts.php b/application/Espo/Modules/Crm/Jobs/CheckEmailAccounts.php index e8d38db2a5..797142a58e 100644 --- a/application/Espo/Modules/Crm/Jobs/CheckEmailAccounts.php +++ b/application/Espo/Modules/Crm/Jobs/CheckEmailAccounts.php @@ -73,7 +73,6 @@ class CheckEmailAccounts implements JobTargeted } catch (\Exception $e) { throw new Error('Job CheckEmailAccounts '.$entity->id.': [' . $e->getCode() . '] ' .$e->getMessage()); } - return true; } public function prepare(ScheduledJob $scheduledJob, string $executeTime) @@ -111,7 +110,5 @@ class CheckEmailAccounts implements JobTargeted ]); $this->entityManager->saveEntity($jobEntity); } - - return true; } } diff --git a/application/Espo/Modules/Crm/Jobs/CheckInboundEmails.php b/application/Espo/Modules/Crm/Jobs/CheckInboundEmails.php index 1da72f6b8b..37ce4c6d39 100644 --- a/application/Espo/Modules/Crm/Jobs/CheckInboundEmails.php +++ b/application/Espo/Modules/Crm/Jobs/CheckInboundEmails.php @@ -73,7 +73,6 @@ class CheckInboundEmails implements JobTargeted } catch (\Exception $e) { throw new Error('Job CheckInboundEmails '.$entity->id.': [' . $e->getCode() . '] ' .$e->getMessage()); } - return true; } public function prepare($scheduledJob, $executeTime) @@ -110,7 +109,5 @@ class CheckInboundEmails implements JobTargeted ]); $this->entityManager->saveEntity($jobEntity); } - - return true; } } diff --git a/application/Espo/Modules/Crm/Jobs/ControlKnowledgeBaseArticleStatus.php b/application/Espo/Modules/Crm/Jobs/ControlKnowledgeBaseArticleStatus.php index c3cd5b60eb..5e025795b8 100644 --- a/application/Espo/Modules/Crm/Jobs/ControlKnowledgeBaseArticleStatus.php +++ b/application/Espo/Modules/Crm/Jobs/ControlKnowledgeBaseArticleStatus.php @@ -22,23 +22,32 @@ namespace Espo\Modules\Crm\Jobs; -use \Espo\Core\Exceptions; +use Espo\Core\{ + ORM\EntityManager, + Jobs\Job, +}; -class ControlKnowledgeBaseArticleStatus extends \Espo\Core\Jobs\Base +class ControlKnowledgeBaseArticleStatus implements Job { + protected $entityManager; + + public function __construct(EntityManager $entityManager) + { + $this->entityManager = $entityManager; + } + public function run() { - $list = $this->getEntityManager()->getRepository('KnowledgeBaseArticle')->where(array( + $list = $this->entityManager->getRepository('KnowledgeBaseArticle')->where([ 'expirationDate<=' => date('Y-m-d'), - 'status' => 'Published' - ))->find(); + 'status' => 'Published', + ])->find(); foreach ($list as $e) { $e->set('status', 'Archived'); - $this->getEntityManager()->saveEntity($e); + $this->entityManager->saveEntity($e); } return true; } } - diff --git a/application/Espo/Modules/Crm/Jobs/ProcessMassEmail.php b/application/Espo/Modules/Crm/Jobs/ProcessMassEmail.php index 4635e3d8a6..f516c7b1fc 100644 --- a/application/Espo/Modules/Crm/Jobs/ProcessMassEmail.php +++ b/application/Espo/Modules/Crm/Jobs/ProcessMassEmail.php @@ -29,38 +29,52 @@ namespace Espo\Modules\Crm\Jobs; -use \Espo\Core\Exceptions; +use Espo\Core\{ + ServiceFactory, + ORM\EntityManager, + Jobs\Job, +}; -class ProcessMassEmail extends \Espo\Core\Jobs\Base +class ProcessMassEmail implements Job { + protected $serviceFactory; + protected $entityManager; + + public function __construct(ServiceFactory $serviceFactory, EntityManager $entityManager) + { + $this->serviceFactory = $serviceFactory; + $this->entityManager = $entityManager; + } + public function run() { - $service = $this->getServiceFactory()->create('MassEmail'); + $service = $this->serviceFactory->create('MassEmail'); - $massEmailList = $this->getEntityManager()->getRepository('MassEmail')->where(array( + $massEmailList = $this->entityManager->getRepository('MassEmail')->where([ 'status' => 'Pending', - 'startAt<=' => date('Y-m-d H:i:s') - ))->find(); + 'startAt<=' => date('Y-m-d H:i:s'), + ])->find(); foreach ($massEmailList as $massEmail) { try { $service->createQueue($massEmail); } catch (\Exception $e) { - $GLOBALS['log']->error('Job ProcessMassEmail#createQueue '.$massEmail->id.': [' . $e->getCode() . '] ' .$e->getMessage()); + $GLOBALS['log']->error( + 'Job ProcessMassEmail#createQueue '.$massEmail->id.': [' . $e->getCode() . '] ' .$e->getMessage() + ); } } - $massEmailList = $this->getEntityManager()->getRepository('MassEmail')->where(array( - 'status' => 'In Process' - ))->find(); + $massEmailList = $this->entityManager->getRepository('MassEmail')->where([ + 'status' => 'In Process', + ])->find(); foreach ($massEmailList as $massEmail) { try { $service->processSending($massEmail); } catch (\Exception $e) { - $GLOBALS['log']->error('Job ProcessMassEmail#processSending '.$massEmail->id.': [' . $e->getCode() . '] ' .$e->getMessage()); + $GLOBALS['log']->error( + 'Job ProcessMassEmail#processSending '.$massEmail->id.': [' . $e->getCode() . '] ' .$e->getMessage() + ); } } - - return true; } } - diff --git a/application/Espo/Modules/Crm/Jobs/SendEmailReminders.php b/application/Espo/Modules/Crm/Jobs/SendEmailReminders.php index 5737b2e5c2..1a95d2b776 100644 --- a/application/Espo/Modules/Crm/Jobs/SendEmailReminders.php +++ b/application/Espo/Modules/Crm/Jobs/SendEmailReminders.php @@ -29,12 +29,27 @@ namespace Espo\Modules\Crm\Jobs; -use \Espo\Core\Exceptions; +use Espo\Core\{ + InjectableFactory, + ORM\EntityManager, + Jobs\Job, +}; -class SendEmailReminders extends \Espo\Core\Jobs\Base +use Espo\Modules\Crm\Business\Reminder\EmailReminder; + +class SendEmailReminders implements Job { const MAX_PORTION_SIZE = 10; + protected $injectableFactory; + protected $entityManager; + + public function __construct(InjectableFactory $injectableFactory, EntityManager $entityManager) + { + $this->injectableFactory = $injectableFactory; + $this->entityManager = $entityManager; + } + public function run() { $dt = new \DateTime(); @@ -42,27 +57,20 @@ class SendEmailReminders extends \Espo\Core\Jobs\Base $now = $dt->format('Y-m-d H:i:s'); $nowShifted = $dt->sub(new \DateInterval('PT1H'))->format('Y-m-d H:i:s'); - $collection = $this->getEntityManager()->getRepository('Reminder')->where(array( + $collection = $this->entityManager->getRepository('Reminder')->where([ 'type' => 'Email', 'remindAt<=' => $now, - 'startAt>' => $nowShifted - ))->find(); + 'startAt>' => $nowShifted, + ])->find(); - if (!empty($collection)) { - $emailReminder = new \Espo\Modules\Crm\Business\Reminder\EmailReminder( - $this->getEntityManager(), - $this->getContainer()->get('templateFileManager'), - $this->getContainer()->get('mailSender'), - $this->getConfig(), - $this->getContainer()->get('fileManager'), - $this->getContainer()->get('dateTime'), - $this->getContainer()->get('number'), - $this->getContainer()->get('language') - - ); - $pdo = $this->getEntityManager()->getPDO(); + if (empty($collection)) { + return; } + $emailReminder = $this->injectableFactory->create(EmailReminder::class); + + $pdo = $this->entityManager->getPDO(); + foreach ($collection as $i => $entity) { if ($i >= self::MAX_PORTION_SIZE) { break; @@ -72,12 +80,7 @@ class SendEmailReminders extends \Espo\Core\Jobs\Base } catch (\Exception $e) { $GLOBALS['log']->error('Job SendEmailReminders '.$entity->id.': [' . $e->getCode() . '] ' .$e->getMessage()); } - - $sql = "DELETE FROM `reminder` WHERE id = ". $pdo->quote($entity->id); - $pdo->query($sql); - $this->getEntityManager()->removeEntity($entity); + $this->entityManager->getRepository('Reminder')->deleteFromDb($entity->id); } - return true; } } - diff --git a/application/Espo/Modules/Crm/Jobs/SubmitPopupReminders.php b/application/Espo/Modules/Crm/Jobs/SubmitPopupReminders.php index 42fb5f184b..934e36f483 100644 --- a/application/Espo/Modules/Crm/Jobs/SubmitPopupReminders.php +++ b/application/Espo/Modules/Crm/Jobs/SubmitPopupReminders.php @@ -29,24 +29,40 @@ namespace Espo\Modules\Crm\Jobs; -use \Espo\Core\Exceptions; +use Espo\Core\{ + ORM\EntityManager, + Utils\Config, + WebSocket\Submission as WebSocketSubmission, + Jobs\Job, +}; -class SubmitPopupReminders extends \Espo\Core\Jobs\Base +class SubmitPopupReminders implements Job { const REMINDER_PAST_HOURS = 24; + protected $entityManager; + protected $config; + protected $webSocketSubmission; + + public function __construct(EntityManager $entityManager, Config $config, WebSocketSubmission $webSocketSubmission) + { + $this->entityManager = $entityManager; + $this->config = $config; + $this->webSocketSubmission = $webSocketSubmission; + } + public function run() { - if (!$this->getConfig()->get('useWebSocket')) return; + if (!$this->config->get('useWebSocket')) return; $dt = new \DateTime(); $now = $dt->format('Y-m-d H:i:s'); - $pastHours = $this->getConfig()->get('reminderPastHours', self::REMINDER_PAST_HOURS); + $pastHours = $this->config->get('reminderPastHours', self::REMINDER_PAST_HOURS); $nowShifted = $dt->modify('-' . $pastHours . ' hours')->format('Y-m-d H:i:s'); - $reminderList = $this->getEntityManager()->getRepository('Reminder')->where([ + $reminderList = $this->entityManager->getRepository('Reminder')->where([ 'type' => 'Popup', 'remindAt<=' => $now, 'startAt>' => $nowShifted, @@ -65,7 +81,7 @@ class SubmitPopupReminders extends \Espo\Core\Jobs\Base continue; } - $entity = $this->getEntityManager()->getEntity($entityType, $entityId); + $entity = $this->entityManager->getEntity($entityType, $entityId); if (!$entity) { $this->deleteReminder($reminder); @@ -104,7 +120,7 @@ class SubmitPopupReminders extends \Espo\Core\Jobs\Base $reminder->set('isSubmitted', true); - $this->getEntityManager()->saveEntity($reminder); + $this->entityManager->saveEntity($reminder); } foreach ($submitData as $userId => $list) { @@ -122,6 +138,6 @@ class SubmitPopupReminders extends \Espo\Core\Jobs\Base protected function deleteReminder($reminder) { - $this->getEntityManager()->getRepository('Reminder')->deleteFromDb($reminder->id); + $this->entityManager->getRepository('Reminder')->deleteFromDb($reminder->id); } }