Initial commit
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Crm\Classes\FieldProcessing\Call;
|
||||
|
||||
use Espo\Modules\Crm\Entities\Call;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\Modules\Crm\Entities\Lead;
|
||||
use Espo\Modules\Crm\Entities\Meeting;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Core\ORM\Entity as CoreEntity;
|
||||
use Espo\Core\FieldProcessing\Loader;
|
||||
use Espo\Core\FieldProcessing\Loader\Params;
|
||||
use Espo\Core\ORM\EntityManager;
|
||||
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @implements Loader<Call>
|
||||
*/
|
||||
class PhoneNumberMapLoader implements Loader
|
||||
{
|
||||
private const ERASED_PART = 'ERASED:';
|
||||
|
||||
public function __construct(private EntityManager $entityManager)
|
||||
{}
|
||||
|
||||
public function process(Entity $entity, Params $params): void
|
||||
{
|
||||
$map = (object) [];
|
||||
|
||||
assert($entity instanceof CoreEntity);
|
||||
|
||||
$contactIdList = $entity->getLinkMultipleIdList(Meeting::LINK_CONTACTS);
|
||||
|
||||
if (count($contactIdList)) {
|
||||
$this->populate($map, Contact::ENTITY_TYPE, $contactIdList);
|
||||
}
|
||||
|
||||
$leadIdList = $entity->getLinkMultipleIdList(Meeting::LINK_LEADS);
|
||||
|
||||
if (count($leadIdList)) {
|
||||
$this->populate($map, Lead::ENTITY_TYPE, $leadIdList);
|
||||
}
|
||||
|
||||
$entity->set('phoneNumbersMap', $map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $idList
|
||||
*/
|
||||
private function populate(stdClass $map, string $entityType, array $idList): void
|
||||
{
|
||||
$entityList = $this->entityManager
|
||||
->getRDBRepository($entityType)
|
||||
->where([
|
||||
Attribute::ID => $idList,
|
||||
])
|
||||
->select([Attribute::ID, 'phoneNumber'])
|
||||
->find();
|
||||
|
||||
foreach ($entityList as $entity) {
|
||||
$phoneNumber = $entity->get('phoneNumber');
|
||||
|
||||
if (!$phoneNumber) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str_starts_with($phoneNumber, self::ERASED_PART)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = $entity->getEntityType() . '_' . $entity->getId();
|
||||
|
||||
$map->$key = $phoneNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Crm\Classes\FieldProcessing\Campaign;
|
||||
|
||||
use Espo\Modules\Crm\Entities\Campaign;
|
||||
use Espo\Modules\Crm\Entities\CampaignLogRecord;
|
||||
use Espo\Modules\Crm\Entities\Lead;
|
||||
use Espo\Modules\Crm\Entities\Opportunity;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use Espo\Core\Acl;
|
||||
use Espo\Core\Currency\ConfigDataProvider as CurrencyConfigDataProvider;
|
||||
use Espo\Core\FieldProcessing\Loader;
|
||||
use Espo\Core\FieldProcessing\Loader\Params;
|
||||
use Espo\Core\ORM\EntityManager;
|
||||
|
||||
use Espo\ORM\Query\Part\Condition;
|
||||
use Espo\ORM\Query\Part\Expression;
|
||||
use Espo\ORM\Query\Part\Join;
|
||||
use Espo\ORM\Query\SelectBuilder;
|
||||
use PDO;
|
||||
|
||||
use const PHP_ROUND_HALF_EVEN;
|
||||
|
||||
/**
|
||||
* @implements Loader<Campaign>
|
||||
*/
|
||||
class StatsLoader implements Loader
|
||||
{
|
||||
private EntityManager $entityManager;
|
||||
private Acl $acl;
|
||||
private CurrencyConfigDataProvider $currencyDataProvider;
|
||||
|
||||
public function __construct(
|
||||
EntityManager $entityManager,
|
||||
Acl $acl,
|
||||
CurrencyConfigDataProvider $currencyDataProvider
|
||||
) {
|
||||
$this->entityManager = $entityManager;
|
||||
$this->acl = $acl;
|
||||
$this->currencyDataProvider = $currencyDataProvider;
|
||||
}
|
||||
|
||||
public function process(Entity $entity, Params $params): void
|
||||
{
|
||||
$sentCount = $this->entityManager
|
||||
->getRDBRepository(CampaignLogRecord::ENTITY_TYPE)
|
||||
->where([
|
||||
'campaignId' => $entity->getId(),
|
||||
'action' => CampaignLogRecord::ACTION_SENT,
|
||||
'isTest' => false,
|
||||
])
|
||||
->count();
|
||||
|
||||
if (!$sentCount) {
|
||||
$sentCount = null;
|
||||
}
|
||||
|
||||
$entity->set('sentCount', $sentCount);
|
||||
|
||||
$openedCount = $this->entityManager
|
||||
->getRDBRepository(CampaignLogRecord::ENTITY_TYPE)
|
||||
->where([
|
||||
'campaignId' => $entity->getId(),
|
||||
'action' => CampaignLogRecord::ACTION_OPENED,
|
||||
'isTest' => false,
|
||||
])
|
||||
->count();
|
||||
|
||||
$entity->set('openedCount', $openedCount);
|
||||
|
||||
$openedPercentage = null;
|
||||
|
||||
if ($sentCount > 0) {
|
||||
$openedPercentage = round($openedCount / $sentCount * 100, 2, PHP_ROUND_HALF_EVEN);
|
||||
}
|
||||
|
||||
$entity->set('openedPercentage', $openedPercentage);
|
||||
|
||||
$clickedCount = $this->entityManager
|
||||
->getRDBRepository(CampaignLogRecord::ENTITY_TYPE)
|
||||
->clone(
|
||||
SelectBuilder::create()
|
||||
->from(CampaignLogRecord::ENTITY_TYPE)
|
||||
->leftJoin(
|
||||
Join::createWithTableTarget(CampaignLogRecord::ENTITY_TYPE, 'b')
|
||||
->withConditions(
|
||||
Condition::and(
|
||||
Condition::equal(
|
||||
Expression::column('queueItemId'),
|
||||
Expression::column('b.queueItemId'),
|
||||
),
|
||||
Condition::greater(
|
||||
Expression::column('b.id'),
|
||||
Expression::column('id'),
|
||||
),
|
||||
Condition::equal(
|
||||
Expression::create('b.action'),
|
||||
CampaignLogRecord::ACTION_CLICKED
|
||||
),
|
||||
Condition::equal(
|
||||
Expression::create('b.isTest'),
|
||||
false
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
->where([
|
||||
'campaignId' => $entity->getId(),
|
||||
'action' => CampaignLogRecord::ACTION_CLICKED,
|
||||
'isTest' => false,
|
||||
'b.id' => null,
|
||||
])
|
||||
->build()
|
||||
)
|
||||
->count();
|
||||
|
||||
$entity->set('clickedCount', $clickedCount);
|
||||
|
||||
$clickedPercentage = null;
|
||||
|
||||
if ($sentCount > 0) {
|
||||
$clickedPercentage = round(
|
||||
$clickedCount / $sentCount * 100, 2,
|
||||
PHP_ROUND_HALF_EVEN
|
||||
);
|
||||
}
|
||||
|
||||
$entity->set('clickedPercentage', $clickedPercentage);
|
||||
|
||||
$optedInCount = $this->entityManager
|
||||
->getRDBRepository(CampaignLogRecord::ENTITY_TYPE)
|
||||
->where([
|
||||
'campaignId' => $entity->getId(),
|
||||
'action' => CampaignLogRecord::ACTION_OPTED_IN,
|
||||
'isTest' => false,
|
||||
])
|
||||
->count();
|
||||
|
||||
if (!$optedInCount) {
|
||||
$optedInCount = null;
|
||||
}
|
||||
|
||||
$entity->set('optedInCount', $optedInCount);
|
||||
|
||||
$optedOutCount = $this->entityManager
|
||||
->getRDBRepository(CampaignLogRecord::ENTITY_TYPE)
|
||||
->where([
|
||||
'campaignId' => $entity->getId(),
|
||||
'action' => CampaignLogRecord::ACTION_OPTED_OUT,
|
||||
'isTest' => false,
|
||||
])
|
||||
->count();
|
||||
|
||||
$entity->set('optedOutCount', $optedOutCount);
|
||||
|
||||
$optedOutPercentage = null;
|
||||
|
||||
if ($sentCount > 0) {
|
||||
$optedOutPercentage = round(
|
||||
$optedOutCount / $sentCount * 100, 2,
|
||||
PHP_ROUND_HALF_EVEN
|
||||
);
|
||||
}
|
||||
|
||||
$entity->set('optedOutPercentage', $optedOutPercentage);
|
||||
|
||||
$bouncedCount = $this->entityManager
|
||||
->getRDBRepository(CampaignLogRecord::ENTITY_TYPE)
|
||||
->where([
|
||||
'campaignId' => $entity->getId(),
|
||||
'action' => CampaignLogRecord::ACTION_BOUNCED,
|
||||
'isTest' => false,
|
||||
])
|
||||
->count();
|
||||
|
||||
$entity->set('bouncedCount', $bouncedCount);
|
||||
|
||||
$bouncedPercentage = null;
|
||||
|
||||
if ($sentCount && $sentCount > 0) {
|
||||
$bouncedPercentage = round(
|
||||
$bouncedCount / $sentCount * 100, 2,
|
||||
PHP_ROUND_HALF_EVEN
|
||||
);
|
||||
}
|
||||
|
||||
$entity->set('bouncedPercentage', $bouncedPercentage);
|
||||
|
||||
if ($this->acl->check(Lead::ENTITY_TYPE)) {
|
||||
$leadCreatedCount = $this->entityManager
|
||||
->getRDBRepository(Lead::ENTITY_TYPE)
|
||||
->where([
|
||||
'campaignId' => $entity->getId(),
|
||||
])
|
||||
->count();
|
||||
|
||||
if (!$leadCreatedCount) {
|
||||
$leadCreatedCount = null;
|
||||
}
|
||||
|
||||
$entity->set('leadCreatedCount', $leadCreatedCount);
|
||||
}
|
||||
|
||||
if ($this->acl->check(Opportunity::ENTITY_TYPE)) {
|
||||
$entity->set('revenueCurrency', $this->currencyDataProvider->getDefaultCurrency());
|
||||
|
||||
$query = $this->entityManager
|
||||
->getQueryBuilder()
|
||||
->select()
|
||||
->from(Opportunity::ENTITY_TYPE)
|
||||
->select(['SUM:amountConverted'])
|
||||
->where([
|
||||
'stage' => Opportunity::STAGE_CLOSED_WON,
|
||||
'campaignId' => $entity->getId(),
|
||||
])
|
||||
->group('opportunity.campaignId')
|
||||
->build();
|
||||
|
||||
$sth = $this->entityManager->getQueryExecutor()->execute($query);
|
||||
|
||||
$revenue = null;
|
||||
|
||||
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($row) {
|
||||
$revenue = floatval($row['SUM:amountConverted']);
|
||||
}
|
||||
|
||||
if (!$revenue) {
|
||||
$revenue = null;
|
||||
}
|
||||
|
||||
$entity->set('revenue', $revenue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Crm\Classes\FieldProcessing\Meeting;
|
||||
|
||||
use Espo\Entities\User;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
use Espo\Core\FieldProcessing\Loader;
|
||||
use Espo\Core\FieldProcessing\Loader\Params;
|
||||
|
||||
/**
|
||||
* @implements Loader<\Espo\Core\ORM\Entity>
|
||||
*/
|
||||
class AcceptanceStatusLoader implements Loader
|
||||
{
|
||||
private EntityManager $entityManager;
|
||||
private User $user;
|
||||
|
||||
private const ATTR_ACCEPTANCE_STATUS = 'acceptanceStatus';
|
||||
|
||||
public function __construct(EntityManager $entityManager, User $user)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function process(Entity $entity, Params $params): void
|
||||
{
|
||||
if (!$params->hasInSelect(self::ATTR_ACCEPTANCE_STATUS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->has(self::ATTR_ACCEPTANCE_STATUS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$attribute = self::ATTR_ACCEPTANCE_STATUS;
|
||||
|
||||
$user = $this->entityManager
|
||||
->getRDBRepository($entity->getEntityType())
|
||||
->getRelation($entity, 'users')
|
||||
->where([
|
||||
'id' => $this->user->getId(),
|
||||
])
|
||||
->select([$attribute])
|
||||
->findOne();
|
||||
|
||||
$value = null;
|
||||
|
||||
if ($user) {
|
||||
$value = $user->get($attribute);
|
||||
}
|
||||
|
||||
$entity->set(self::ATTR_ACCEPTANCE_STATUS, $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Crm\Classes\FieldProcessing\Meeting;
|
||||
|
||||
use Espo\Entities\Email;
|
||||
use Espo\Modules\Crm\Entities\Meeting;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\Core\FieldProcessing\Saver;
|
||||
use Espo\Core\FieldProcessing\Saver\Params;
|
||||
use Espo\Core\Mail\Event\EventFactory;
|
||||
|
||||
use ICal\ICal;
|
||||
|
||||
/**
|
||||
* @implements Saver<Meeting>
|
||||
*/
|
||||
class SourceEmailSaver implements Saver
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager)
|
||||
{}
|
||||
|
||||
/**
|
||||
* @param Meeting $entity
|
||||
*/
|
||||
public function process(Entity $entity, Params $params): void
|
||||
{
|
||||
if (!$entity->isNew()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$email = $this->getEmail($entity);
|
||||
|
||||
if (!$email) {
|
||||
return;
|
||||
}
|
||||
|
||||
$icsContents = $email->getIcsContents();
|
||||
|
||||
if ($icsContents === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$ical = new ICal();
|
||||
|
||||
$ical->initString($icsContents);
|
||||
|
||||
$espoEvent = EventFactory::createFromU01jmg3Ical($ical);
|
||||
|
||||
$email->set('createdEventId', $entity->getId());
|
||||
$email->set('createdEventType', $entity->getEntityType());
|
||||
$email->set('icsEventUid', $espoEvent->getUid());
|
||||
|
||||
$this->entityManager->saveEntity($email);
|
||||
}
|
||||
|
||||
private function getEmail(Meeting $entity): ?Email
|
||||
{
|
||||
$emailId = $entity->get('sourceEmailId');
|
||||
|
||||
if (!$emailId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->entityManager->getRDBRepositoryByClass(Email::class)->getById($emailId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Crm\Classes\FieldProcessing\TargetList;
|
||||
|
||||
use Espo\Modules\Crm\Entities\TargetList;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use Espo\Core\Utils\Metadata;
|
||||
|
||||
use Espo\Core\FieldProcessing\Loader;
|
||||
use Espo\Core\FieldProcessing\Loader\Params;
|
||||
use Espo\Core\ORM\EntityManager;
|
||||
|
||||
/**
|
||||
* @implements Loader<TargetList>
|
||||
*/
|
||||
class EntryCountLoader implements Loader
|
||||
{
|
||||
/** @var string[] */
|
||||
private array $targetLinkList;
|
||||
|
||||
private EntityManager $entityManager;
|
||||
private Metadata $metadata;
|
||||
|
||||
public function __construct(EntityManager $entityManager, Metadata $metadata)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->metadata = $metadata;
|
||||
|
||||
$this->targetLinkList = $this->metadata->get(['scopes', 'TargetList', 'targetLinkList']) ?? [];
|
||||
}
|
||||
|
||||
public function process(Entity $entity, Params $params): void
|
||||
{
|
||||
if (
|
||||
$params->hasSelect() &&
|
||||
!in_array('entryCount', $params->getSelect() ?? [])
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
|
||||
foreach ($this->targetLinkList as $link) {
|
||||
$count += $this->entityManager
|
||||
->getRDBRepository(TargetList::ENTITY_TYPE)
|
||||
->getRelation($entity, $link)
|
||||
->count();
|
||||
}
|
||||
|
||||
$entity->set('entryCount', $count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Crm\Classes\FieldProcessing\TargetList;
|
||||
|
||||
use Espo\ORM\Defs\Params\RelationParam;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Modules\Crm\Entities\TargetList;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Core\FieldProcessing\Loader;
|
||||
use Espo\Core\FieldProcessing\Loader\Params;
|
||||
use Espo\Core\ORM\EntityManager;
|
||||
|
||||
/**
|
||||
* @implements Loader<TargetList>
|
||||
*/
|
||||
class OptedOutCountLoader implements Loader
|
||||
{
|
||||
/** @var string[] */
|
||||
private array $targetLinkList;
|
||||
|
||||
public function __construct(private EntityManager $entityManager, private Metadata $metadata)
|
||||
{
|
||||
|
||||
$this->targetLinkList = $this->metadata->get(['scopes', 'TargetList', 'targetLinkList']) ?? [];
|
||||
}
|
||||
|
||||
public function process(Entity $entity, Params $params): void
|
||||
{
|
||||
if (
|
||||
$params->hasSelect() &&
|
||||
!in_array('optedOutCount', $params->getSelect() ?? [])
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert($entity instanceof TargetList);
|
||||
|
||||
$count = 0;
|
||||
|
||||
foreach ($this->targetLinkList as $link) {
|
||||
$foreignEntityType = $entity->getRelationParam($link, RelationParam::ENTITY);
|
||||
|
||||
$count += $this->entityManager
|
||||
->getRDBRepository($foreignEntityType)
|
||||
->join('targetLists')
|
||||
->where([
|
||||
'targetListsMiddle.targetListId' => $entity->getId(),
|
||||
'targetListsMiddle.optedOut' => true,
|
||||
])
|
||||
->count();
|
||||
}
|
||||
|
||||
$entity->set('optedOutCount', $count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user