Initial commit
This commit is contained in:
84
application/Espo/Modules/Crm/Hooks/Account/Contacts.php
Normal file
84
application/Espo/Modules/Crm/Hooks/Account/Contacts.php
Normal file
@@ -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\Hooks\Account;
|
||||
|
||||
use Espo\ORM\{
|
||||
Entity,
|
||||
EntityManager,
|
||||
};
|
||||
|
||||
class Contacts
|
||||
{
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
public function __construct(EntityManager $entityManager)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function afterRelate(Entity $entity, array $options = [], array $data = []): void
|
||||
{
|
||||
$relationName = $data['relationName'] ?? null;
|
||||
$foreignEntity = $data['foreignEntity'] ?? null;
|
||||
|
||||
if ($relationName === 'contacts' && $foreignEntity) {
|
||||
if (!$foreignEntity->get('accountId') && $foreignEntity->has('accountId')) {
|
||||
$foreignEntity->set('accountId', $entity->getId());
|
||||
|
||||
$this->entityManager->saveEntity($foreignEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function afterUnrelate(Entity $entity, array $options = [], array $data = []): void
|
||||
{
|
||||
$relationName = $data['relationName'] ?? null;
|
||||
$foreignEntity = $data['foreignEntity'] ?? null;
|
||||
|
||||
if ($relationName === 'contacts' && $foreignEntity) {
|
||||
if ($foreignEntity->get('accountId') && $foreignEntity->get('accountId') === $entity->getId()) {
|
||||
$foreignEntity->set('accountId', null);
|
||||
|
||||
$this->entityManager->saveEntity($foreignEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
63
application/Espo/Modules/Crm/Hooks/Account/TargetList.php
Normal file
63
application/Espo/Modules/Crm/Hooks/Account/TargetList.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?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\Hooks\Account;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Core\ORM\Repository\Option\SaveOption;
|
||||
use Espo\Modules\Crm\Entities\Account;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Account>
|
||||
*/
|
||||
class TargetList implements AfterSave
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$options->get(SaveOption::IMPORT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targetListId = $entity->get('targetListId');
|
||||
|
||||
if (!$targetListId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->entityManager
|
||||
->getRDBRepositoryByClass(Account::class)
|
||||
->getRelation($entity, 'targetLists')
|
||||
->relateById($targetListId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?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\Hooks\Call;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterRemove;
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Core\WebSocket\Submission;
|
||||
use Espo\Modules\Crm\Entities\Call;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\RemoveOptions;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Call>
|
||||
* @implements AfterRemove<Call>
|
||||
*/
|
||||
class CalendarWebSocket implements AfterSave, AfterRemove
|
||||
{
|
||||
public function __construct(
|
||||
private Submission $submission,
|
||||
) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
$this->process($entity);
|
||||
}
|
||||
|
||||
public function afterRemove(Entity $entity, RemoveOptions $options): void
|
||||
{
|
||||
$this->process($entity);
|
||||
}
|
||||
|
||||
private function process(Call $entity): void
|
||||
{
|
||||
foreach ($entity->getUsers()->getIdList() as $userId) {
|
||||
$this->submission->submit('calendarUpdate', $userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
application/Espo/Modules/Crm/Hooks/Call/ParentLink.php
Normal file
35
application/Espo/Modules/Crm/Hooks/Call/ParentLink.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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\Hooks\Call;
|
||||
|
||||
use Espo\Modules\Crm\Hooks\Meeting\ParentLink as MeetingParentLink;
|
||||
|
||||
class ParentLink extends MeetingParentLink
|
||||
{}
|
||||
55
application/Espo/Modules/Crm/Hooks/Call/Uid.php
Normal file
55
application/Espo/Modules/Crm/Hooks/Call/Uid.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?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\Hooks\Call;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Core\Utils\Util;
|
||||
use Espo\Modules\Crm\Entities\Call;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<Call>
|
||||
*/
|
||||
class Uid implements BeforeSave
|
||||
{
|
||||
public function __construct() {}
|
||||
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isNew() || $entity->getUid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$uid = Util::generateUuid4();
|
||||
|
||||
$entity->setUid($uid);
|
||||
}
|
||||
}
|
||||
35
application/Espo/Modules/Crm/Hooks/Call/Users.php
Normal file
35
application/Espo/Modules/Crm/Hooks/Call/Users.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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\Hooks\Call;
|
||||
|
||||
use Espo\Modules\Crm\Hooks\Meeting\Users as MeetingUsers;
|
||||
|
||||
class Users extends MeetingUsers
|
||||
{}
|
||||
130
application/Espo/Modules/Crm/Hooks/CaseObj/Contacts.php
Normal file
130
application/Espo/Modules/Crm/Hooks/CaseObj/Contacts.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?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\Hooks\CaseObj;
|
||||
|
||||
use Espo\Core\FieldProcessing\Stream\FollowersLoader;
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Modules\Crm\Entities\CaseObj;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
use Espo\Tools\Stream\Service as StreamService;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<CaseObj>
|
||||
*/
|
||||
class Contacts implements AfterSave
|
||||
{
|
||||
private const ATTR_CONTACT_ID = 'contactId';
|
||||
private const RELATION_CONTACTS = 'contacts';
|
||||
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private FollowersLoader $followersLoader,
|
||||
private StreamService $streamService,
|
||||
) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isAttributeChanged(self::ATTR_CONTACT_ID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$contact = $entity->getContact();
|
||||
|
||||
/** @var ?string $fetchedContactId */
|
||||
$fetchedContactId = $entity->getFetched(self::ATTR_CONTACT_ID);
|
||||
|
||||
$contactsRelation = $this->entityManager->getRelation($entity, self::RELATION_CONTACTS);
|
||||
|
||||
if ($fetchedContactId) {
|
||||
$previousPortalUser = $this->entityManager
|
||||
->getRDBRepositoryByClass(User::class)
|
||||
->select([Attribute::ID])
|
||||
->where([
|
||||
'contactId' => $fetchedContactId,
|
||||
'type' => User::TYPE_PORTAL,
|
||||
])
|
||||
->findOne();
|
||||
|
||||
if ($previousPortalUser) {
|
||||
$this->streamService->unfollowEntity($entity, $previousPortalUser->getId());
|
||||
|
||||
$this->followersLoader->processFollowers($entity);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$contact && $fetchedContactId) {
|
||||
$contactsRelation->unrelateById($fetchedContactId);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$contact) {
|
||||
return;
|
||||
}
|
||||
|
||||
$portalUser = $this->getPortalUser($contact->getId());
|
||||
|
||||
if ($portalUser && !$entity->isInternal()) {
|
||||
// @todo Solve ACL check issue when a user is in multiple portals.
|
||||
$this->streamService->followEntity($entity, $portalUser->getId());
|
||||
|
||||
if (!$entity->isNew()) {
|
||||
$this->followersLoader->processFollowers($entity);
|
||||
}
|
||||
}
|
||||
|
||||
if (in_array($contact->getId(), $entity->getContacts()->getIdList())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($contactsRelation->isRelatedById($contact->getId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
$contactsRelation->relateById($contact->getId());
|
||||
}
|
||||
|
||||
private function getPortalUser(?string $contactId): ?User
|
||||
{
|
||||
return $this->entityManager
|
||||
->getRDBRepositoryByClass(User::class)
|
||||
->select([Attribute::ID])
|
||||
->where([
|
||||
'contactId' => $contactId,
|
||||
'type' => User::TYPE_PORTAL,
|
||||
'isActive' => true,
|
||||
])
|
||||
->findOne();
|
||||
}
|
||||
}
|
||||
65
application/Espo/Modules/Crm/Hooks/CaseObj/IsInternal.php
Normal file
65
application/Espo/Modules/Crm/Hooks/CaseObj/IsInternal.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?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\Hooks\CaseObj;
|
||||
|
||||
use Espo\Core\FieldProcessing\Stream\FollowersLoader;
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Modules\Crm\Entities\CaseObj;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
use Espo\Services\Stream;
|
||||
|
||||
|
||||
/**
|
||||
* @implements AfterSave<CaseObj>
|
||||
*/
|
||||
class IsInternal implements AfterSave
|
||||
{
|
||||
public function __construct(
|
||||
private Stream $streamService,
|
||||
private FollowersLoader $followersLoader,
|
||||
) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
$this->processUnfollowPortalUsers($entity);
|
||||
}
|
||||
|
||||
private function processUnfollowPortalUsers(CaseObj $entity): void
|
||||
{
|
||||
if (!$entity->isInternal() || $entity->isNew()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->streamService->unfollowPortalUsersFromEntity($entity);
|
||||
|
||||
$this->followersLoader->processFollowers($entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?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\Hooks\Common;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterRemove;
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Core\Templates\Entities\Event;
|
||||
use Espo\Core\WebSocket\Submission;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\RemoveOptions;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Entity>
|
||||
* @implements AfterRemove<Entity>
|
||||
*/
|
||||
class CalendarWebSocket implements AfterSave, AfterRemove
|
||||
{
|
||||
public function __construct(
|
||||
private Submission $submission,
|
||||
) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity instanceof Event) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->process($entity);
|
||||
}
|
||||
|
||||
public function afterRemove(Entity $entity, RemoveOptions $options): void
|
||||
{
|
||||
if (!$entity instanceof Event) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->process($entity);
|
||||
}
|
||||
|
||||
private function process(Event $entity): void
|
||||
{
|
||||
if ($entity->hasLinkMultipleField(Field::ASSIGNED_USERS)) {
|
||||
foreach ($entity->getLinkMultipleIdList(Field::ASSIGNED_USERS) as $userId) {
|
||||
$this->submission->submit('calendarUpdate', $userId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->getAssignedUser()) {
|
||||
$this->submission->submit('calendarUpdate', $entity->getAssignedUser()->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
97
application/Espo/Modules/Crm/Hooks/Contact/Accounts.php
Normal file
97
application/Espo/Modules/Crm/Hooks/Contact/Accounts.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?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\Hooks\Contact;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Contact>
|
||||
*/
|
||||
class Accounts implements AfterSave
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
/**
|
||||
* @param Contact $entity
|
||||
*/
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
$accountIdChanged = $entity->isAttributeChanged('accountId');
|
||||
$titleChanged = $entity->isAttributeChanged('title');
|
||||
|
||||
/** @var ?string $fetchedAccountId */
|
||||
$fetchedAccountId = $entity->getFetched('accountId');
|
||||
$accountId = $entity->getAccount()?->getId();
|
||||
$title = $entity->getTitle();
|
||||
|
||||
$relation = $this->entityManager
|
||||
->getRDBRepositoryByClass(Contact::class)
|
||||
->getRelation($entity, 'accounts');
|
||||
|
||||
if (!$accountId && $fetchedAccountId) {
|
||||
$relation->unrelateById($fetchedAccountId);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$accountIdChanged && !$titleChanged) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$accountId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$accountContact = $this->entityManager
|
||||
->getRDBRepository('AccountContact')
|
||||
->select(['role'])
|
||||
->where([
|
||||
'accountId' => $accountId,
|
||||
'contactId' => $entity->getId(),
|
||||
Attribute::DELETED => false,
|
||||
])
|
||||
->findOne();
|
||||
|
||||
if (!$accountContact && $accountIdChanged) {
|
||||
$relation->relateById($accountId, ['role' => $title]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($titleChanged && $accountContact && $title !== $accountContact->get('role')) {
|
||||
$relation->updateColumnsById($accountId, ['role' => $title]);
|
||||
}
|
||||
}
|
||||
}
|
||||
81
application/Espo/Modules/Crm/Hooks/Contact/Opportunities.php
Normal file
81
application/Espo/Modules/Crm/Hooks/Contact/Opportunities.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?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\Hooks\Contact;
|
||||
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class Opportunities
|
||||
{
|
||||
private EntityManager $entityManager;
|
||||
|
||||
public function __construct(EntityManager $entityManager)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function afterRelate(Entity $entity, array $options = [], array $data = []): void
|
||||
{
|
||||
$relationName = $data['relationName'] ?? null;
|
||||
/** @var ?Entity $foreignEntity */
|
||||
$foreignEntity = $data['foreignEntity'] ?? null;
|
||||
|
||||
if ($relationName === 'opportunities' && $foreignEntity) {
|
||||
if (!$foreignEntity->get('contactId') && $foreignEntity->has('contactId')) {
|
||||
$foreignEntity->set('contactId', $entity->getId());
|
||||
|
||||
$this->entityManager->saveEntity($foreignEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function afterUnrelate(Entity $entity, array $options = [], array $data = []): void
|
||||
{
|
||||
$relationName = $data['relationName'] ?? null;
|
||||
/** @var ?Entity $foreignEntity */
|
||||
$foreignEntity = $data['foreignEntity'] ?? null;
|
||||
|
||||
if ($relationName === 'opportunities' && $foreignEntity) {
|
||||
if ($foreignEntity->get('contactId') && $foreignEntity->get('contactId') === $entity->getId()) {
|
||||
$foreignEntity->set('contactId', null);
|
||||
|
||||
$this->entityManager->saveEntity($foreignEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
63
application/Espo/Modules/Crm/Hooks/Contact/TargetList.php
Normal file
63
application/Espo/Modules/Crm/Hooks/Contact/TargetList.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?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\Hooks\Contact;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Core\ORM\Repository\Option\SaveOption;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Contact>
|
||||
*/
|
||||
class TargetList implements AfterSave
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$options->get(SaveOption::IMPORT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targetListId = $entity->get('targetListId');
|
||||
|
||||
if (!$targetListId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->entityManager
|
||||
->getRDBRepositoryByClass(Contact::class)
|
||||
->getRelation($entity, 'targetLists')
|
||||
->relateById($targetListId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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\Hooks\KnowledgeBaseArticle;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Modules\Crm\Entities\KnowledgeBaseArticle;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
use Espo\Tools\Email\Util as EmailUtil;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<KnowledgeBaseArticle>
|
||||
*/
|
||||
class SetBodyPlain implements BeforeSave
|
||||
{
|
||||
private const ATTR_BODY = 'body';
|
||||
private const ATTR_BODY_PLAIN = 'bodyPlain';
|
||||
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isAttributeChanged(self::ATTR_BODY)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$bodyPlain = $this->stripHtml($entity->getBody());
|
||||
|
||||
$entity->set(self::ATTR_BODY_PLAIN, $bodyPlain);
|
||||
}
|
||||
|
||||
private function stripHtml(?string $body): ?string
|
||||
{
|
||||
if (!$body) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return EmailUtil::stripHtml($body) ?: null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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\Hooks\KnowledgeBaseArticle;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Modules\Crm\Entities\KnowledgeBaseArticle;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<KnowledgeBaseArticle>
|
||||
*/
|
||||
class SetOrder implements BeforeSave
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
$order = $entity->getOrder();
|
||||
|
||||
if (is_null($order)) {
|
||||
$order = $this->entityManager
|
||||
->getRDBRepository($entity->getEntityType())
|
||||
->min('order');
|
||||
|
||||
if (!$order) {
|
||||
$order = 9999;
|
||||
}
|
||||
|
||||
$order--;
|
||||
|
||||
$entity->set('order', $order);
|
||||
}
|
||||
}
|
||||
}
|
||||
62
application/Espo/Modules/Crm/Hooks/Lead/ConvertedAt.php
Normal file
62
application/Espo/Modules/Crm/Hooks/Lead/ConvertedAt.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?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\Hooks\Lead;
|
||||
|
||||
use Espo\Core\Field\DateTime;
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Modules\Crm\Entities\Lead;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<Lead>
|
||||
*/
|
||||
class ConvertedAt implements BeforeSave
|
||||
{
|
||||
/**
|
||||
* @param Lead $entity
|
||||
*/
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isAttributeChanged('status')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->getConvertedAt() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->getStatus() !== Lead::STATUS_CONVERTED) {
|
||||
return;
|
||||
}
|
||||
|
||||
$entity->setValueObject('convertedAt', DateTime::createNow());
|
||||
}
|
||||
}
|
||||
63
application/Espo/Modules/Crm/Hooks/Lead/TargetList.php
Normal file
63
application/Espo/Modules/Crm/Hooks/Lead/TargetList.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?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\Hooks\Lead;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Core\ORM\Repository\Option\SaveOption;
|
||||
use Espo\Modules\Crm\Entities\Lead;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Lead>
|
||||
*/
|
||||
class TargetList implements AfterSave
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$options->get(SaveOption::IMPORT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targetListId = $entity->get('targetListId');
|
||||
|
||||
if (!$targetListId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->entityManager
|
||||
->getRDBRepositoryByClass(Lead::class)
|
||||
->getRelation($entity, 'targetLists')
|
||||
->relateById($targetListId);
|
||||
}
|
||||
}
|
||||
57
application/Espo/Modules/Crm/Hooks/MassEmail/DeleteQueue.php
Normal file
57
application/Espo/Modules/Crm/Hooks/MassEmail/DeleteQueue.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?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\Hooks\MassEmail;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterRemove;
|
||||
use Espo\Modules\Crm\Entities\EmailQueueItem;
|
||||
use Espo\Modules\Crm\Entities\MassEmail;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Repository\Option\RemoveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterRemove<MassEmail>
|
||||
*/
|
||||
class DeleteQueue implements AfterRemove
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
public function afterRemove(Entity $entity, RemoveOptions $options): void
|
||||
{
|
||||
$deleteQuery = $this->entityManager
|
||||
->getQueryBuilder()
|
||||
->delete()
|
||||
->from(EmailQueueItem::ENTITY_TYPE)
|
||||
->where(['massEmailId' => $entity->getId()])
|
||||
->build();
|
||||
|
||||
$this->entityManager->getQueryExecutor()->execute($deleteQuery);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?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\Hooks\Meeting;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterRemove;
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Core\WebSocket\Submission;
|
||||
use Espo\Modules\Crm\Entities\Meeting;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\RemoveOptions;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Meeting>
|
||||
* @implements AfterRemove<Meeting>
|
||||
*/
|
||||
class CalendarWebSocket implements AfterSave, AfterRemove
|
||||
{
|
||||
public function __construct(
|
||||
private Submission $submission,
|
||||
) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
$this->process($entity);
|
||||
}
|
||||
|
||||
public function afterRemove(Entity $entity, RemoveOptions $options): void
|
||||
{
|
||||
$this->process($entity);
|
||||
}
|
||||
|
||||
private function process(Meeting $entity): void
|
||||
{
|
||||
foreach ($entity->getUsers()->getIdList() as $userId) {
|
||||
$this->submission->submit('calendarUpdate', $userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?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\Hooks\Meeting;
|
||||
|
||||
use Espo\Core\ORM\Repository\Option\SaveOption;
|
||||
use Espo\Entities\Email;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class EmailCreatedEvent
|
||||
{
|
||||
private EntityManager $entityManager;
|
||||
|
||||
public function __construct(EntityManager $entityManager)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
public function afterRemove(Entity $entity, array $options): void
|
||||
{
|
||||
if (!empty($options[SaveOption::SILENT])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$updateQuery = $this->entityManager
|
||||
->getQueryBuilder()
|
||||
->update()
|
||||
->in(Email::ENTITY_TYPE)
|
||||
->set([
|
||||
'createdEventId' => null,
|
||||
'createdEventType' => null,
|
||||
])
|
||||
->where([
|
||||
'createdEventId' => $entity->getId(),
|
||||
'createdEventType' => $entity->getEntityType()
|
||||
])
|
||||
->limit(1)
|
||||
->build();
|
||||
|
||||
$this->entityManager->getQueryExecutor()->execute($updateQuery);
|
||||
}
|
||||
}
|
||||
133
application/Espo/Modules/Crm/Hooks/Meeting/ParentLink.php
Normal file
133
application/Espo/Modules/Crm/Hooks/Meeting/ParentLink.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?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\Hooks\Meeting;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Core\ORM\Entity as CoreEntity;
|
||||
use Espo\Modules\Crm\Entities\Account;
|
||||
use Espo\Modules\Crm\Entities\Lead;
|
||||
use Espo\ORM\Defs\Params\RelationParam;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<Entity>
|
||||
*/
|
||||
class ParentLink implements BeforeSave
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) {
|
||||
$entity->set('accountId', null);
|
||||
}
|
||||
|
||||
if (!$entity->isAttributeChanged('parentId') && !$entity->isAttributeChanged('parentType')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$parent = null;
|
||||
|
||||
$parentId = $entity->get('parentId');
|
||||
$parentType = $entity->get('parentType');
|
||||
|
||||
if ($parentId && $parentType && $this->entityManager->hasRepository($parentType)) {
|
||||
$columnList = ['id', 'name'];
|
||||
|
||||
$defs = $this->entityManager->getMetadata()->getDefs();
|
||||
|
||||
if ($defs->getEntity($parentType)->hasAttribute('accountId')) {
|
||||
$columnList[] = 'accountId';
|
||||
|
||||
}
|
||||
|
||||
if ($parentType === Lead::ENTITY_TYPE) {
|
||||
$columnList[] = 'status';
|
||||
$columnList[] = 'createdAccountId';
|
||||
$columnList[] = 'createdAccountName';
|
||||
}
|
||||
|
||||
$parent = $this->entityManager
|
||||
->getRDBRepository($parentType)
|
||||
->select($columnList)
|
||||
->where([Attribute::ID => $parentId])
|
||||
->findOne();
|
||||
}
|
||||
|
||||
$accountId = null;
|
||||
$accountName = null;
|
||||
|
||||
if ($parent) {
|
||||
if ($parent instanceof Account) {
|
||||
$accountId = $parent->getId();
|
||||
$accountName = $parent->get(Field::NAME);
|
||||
} else if (
|
||||
$parent instanceof Lead &&
|
||||
$parent->getStatus() === Lead::STATUS_CONVERTED &&
|
||||
$parent->get('createdAccountId')
|
||||
) {
|
||||
$accountId = $parent->get('createdAccountId');
|
||||
$accountName = $parent->get('createdAccountName');
|
||||
}
|
||||
|
||||
if (
|
||||
!$accountId && $parent->get('accountId') &&
|
||||
$parent instanceof CoreEntity &&
|
||||
$parent->getRelationParam('account', RelationParam::ENTITY) === Account::ENTITY_TYPE
|
||||
) {
|
||||
$accountId = $parent->get('accountId');
|
||||
}
|
||||
|
||||
if ($accountId) {
|
||||
$entity->set('accountId', $accountId);
|
||||
$entity->set('accountName', $accountName);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
$entity->get('accountId') &&
|
||||
!$entity->get('accountName')
|
||||
) {
|
||||
$account = $this->entityManager
|
||||
->getRDBRepository(Account::ENTITY_TYPE)
|
||||
->select([Attribute::ID, Field::NAME])
|
||||
->where([Attribute::ID => $entity->get('accountId')])
|
||||
->findOne();
|
||||
|
||||
if ($account) {
|
||||
$entity->set('accountName', $account->get(Field::NAME));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
104
application/Espo/Modules/Crm/Hooks/Meeting/Uid.php
Normal file
104
application/Espo/Modules/Crm/Hooks/Meeting/Uid.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?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\Hooks\Meeting;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Core\Mail\Event\EventFactory;
|
||||
use Espo\Core\Utils\Util;
|
||||
use Espo\Entities\Email;
|
||||
use Espo\Modules\Crm\Entities\Meeting;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
use ICal\ICal;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<Meeting>
|
||||
*/
|
||||
class Uid implements BeforeSave
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isNew() || $entity->getUid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$uid = $this->getUid($entity);
|
||||
|
||||
$entity->setUid($uid);
|
||||
}
|
||||
|
||||
private function getUid(Meeting $entity): string
|
||||
{
|
||||
$uid = $this->getIcsUid($entity);
|
||||
|
||||
if ($uid) {
|
||||
return $uid;
|
||||
}
|
||||
|
||||
return Util::generateUuid4();
|
||||
}
|
||||
|
||||
private function getIcsUid(Meeting $entity): ?string
|
||||
{
|
||||
$email = $this->getEmail($entity);
|
||||
|
||||
if (!$email) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$icsContents = $email->getIcsContents();
|
||||
|
||||
if (!$icsContents) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$ical = new ICal();
|
||||
|
||||
$ical->initString($icsContents);
|
||||
|
||||
$espoEvent = EventFactory::createFromU01jmg3Ical($ical);
|
||||
|
||||
return $espoEvent->getUid();
|
||||
}
|
||||
|
||||
private function getEmail(Meeting $entity): ?Email
|
||||
{
|
||||
$emailId = $entity->get('sourceEmailId');
|
||||
|
||||
if (!$emailId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->entityManager->getRDBRepositoryByClass(Email::class)->getById($emailId);
|
||||
}
|
||||
}
|
||||
96
application/Espo/Modules/Crm/Hooks/Meeting/Users.php
Normal file
96
application/Espo/Modules/Crm/Hooks/Meeting/Users.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?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\Hooks\Meeting;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Core\ORM\Entity as CoreEntity;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Modules\Crm\Entities\Meeting;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<CoreEntity>
|
||||
*/
|
||||
class Users implements BeforeSave
|
||||
{
|
||||
public static int $order = 12;
|
||||
|
||||
public function __construct(
|
||||
private Config $config,
|
||||
private User $user
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param CoreEntity $entity
|
||||
*/
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$this->config->get('eventAssignedUserIsAttendeeDisabled')) {
|
||||
if ($entity->hasLinkMultipleField(Field::ASSIGNED_USERS)) {
|
||||
$assignedUserIdList = $entity->getLinkMultipleIdList(Field::ASSIGNED_USERS);
|
||||
|
||||
foreach ($assignedUserIdList as $assignedUserId) {
|
||||
$entity->addLinkMultipleId('users', $assignedUserId);
|
||||
$entity->setLinkMultipleName(
|
||||
'users',
|
||||
$assignedUserId,
|
||||
$entity->getLinkMultipleName(Field::ASSIGNED_USERS, $assignedUserId)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$assignedUserId = $entity->get('assignedUserId');
|
||||
|
||||
if ($assignedUserId) {
|
||||
$entity->addLinkMultipleId('users', $assignedUserId);
|
||||
$entity->setLinkMultipleName('users', $assignedUserId, $entity->get('assignedUserName'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$entity->isNew()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$currentUserId = $this->user->getId();
|
||||
|
||||
if (!$entity->hasLinkMultipleId('users', $currentUserId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$status = $entity->getLinkMultipleColumn('users', 'status', $currentUserId);
|
||||
|
||||
if (!$status || $status === Meeting::ATTENDEE_STATUS_NONE) {
|
||||
$entity->setLinkMultipleColumn('users', 'status', $currentUserId, Meeting::ATTENDEE_STATUS_ACCEPTED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?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\Hooks\Opportunity;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Modules\Crm\Entities\Opportunity;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Opportunity>
|
||||
*/
|
||||
class AmountWeightedConverted implements AfterSave
|
||||
{
|
||||
/**
|
||||
* @param Opportunity $entity
|
||||
*/
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (
|
||||
!$entity->isAttributeChanged('amount') &&
|
||||
!$entity->isAttributeChanged('probability')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$amountConverted = $entity->get('amountConverted');
|
||||
$probability = $entity->get('probability');
|
||||
|
||||
$amountWeightedConverted = round($amountConverted * $probability / 100, 2);
|
||||
|
||||
$entity->set('amountWeightedConverted', $amountWeightedConverted);
|
||||
}
|
||||
}
|
||||
95
application/Espo/Modules/Crm/Hooks/Opportunity/Contacts.php
Normal file
95
application/Espo/Modules/Crm/Hooks/Opportunity/Contacts.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?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\Hooks\Opportunity;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Core\ORM\Repository\Option\SaveContext;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\Modules\Crm\Entities\Opportunity;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Opportunity>
|
||||
*/
|
||||
class Contacts implements AfterSave
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
/**
|
||||
* @param Opportunity $entity
|
||||
*/
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isAttributeChanged('contactId')) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var ?string $contactId */
|
||||
$contactId = $entity->get('contactId');
|
||||
$contactIdList = $entity->get('contactsIds') ?? [];
|
||||
$fetchedContactId = $entity->getFetched('contactId');
|
||||
|
||||
$relation = $this->entityManager
|
||||
->getRDBRepositoryByClass(Opportunity::class)
|
||||
->getRelation($entity, 'contacts');
|
||||
|
||||
if (!$contactId) {
|
||||
if ($fetchedContactId && $relation->isRelatedById($fetchedContactId)) {
|
||||
$relation->unrelateById($fetchedContactId, [
|
||||
SaveContext::NAME => $options->get(SaveContext::NAME),
|
||||
]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (in_array($contactId, $contactIdList)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$contact = $this->entityManager
|
||||
->getRDBRepositoryByClass(Contact::class)
|
||||
->getById($contactId);
|
||||
|
||||
if (!$contact) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($relation->isRelated($contact)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$relation->relateById($contactId, null, [
|
||||
SaveContext::NAME => $options->get(SaveContext::NAME),
|
||||
]);
|
||||
}
|
||||
}
|
||||
165
application/Espo/Modules/Crm/Hooks/Opportunity/LastStage.php
Normal file
165
application/Espo/Modules/Crm/Hooks/Opportunity/LastStage.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?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\Hooks\Opportunity;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Modules\Crm\Entities\Opportunity;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<Opportunity>
|
||||
*/
|
||||
class LastStage implements BeforeSave
|
||||
{
|
||||
public static int $order = 8;
|
||||
|
||||
public function __construct(private Metadata $metadata) {}
|
||||
|
||||
/**
|
||||
* @param Opportunity $entity
|
||||
*/
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (
|
||||
$entity->isAttributeChanged('lastStage') ||
|
||||
!$entity->isAttributeChanged('stage')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stage = $entity->getStage();
|
||||
|
||||
$probability = $this->metadata->get("entityDefs.Opportunity.fields.stage.probabilityMap.$stage");
|
||||
|
||||
if ($probability !== 0) {
|
||||
$entity->setLastStage($entity->getStage());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Lost.
|
||||
|
||||
$probabilityMap = $this->getProbabilityMap();
|
||||
$stageList = $this->getStageList();
|
||||
|
||||
if (!count($stageList)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->isNew()) {
|
||||
// Created as Lost.
|
||||
|
||||
$min = 100;
|
||||
$minStage = null;
|
||||
|
||||
foreach ($stageList as $stage) {
|
||||
$itemProbability = $probabilityMap[$stage] ?? null;
|
||||
|
||||
if (
|
||||
$itemProbability === null ||
|
||||
$itemProbability == 100 ||
|
||||
$itemProbability === 0 ||
|
||||
$itemProbability >= $min
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$min = $itemProbability;
|
||||
$minStage = $stage;
|
||||
}
|
||||
|
||||
if (!$minStage) {
|
||||
return;
|
||||
}
|
||||
|
||||
$entity->setLastStage($minStage);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Won changed to Lost.
|
||||
|
||||
if (!$entity->getLastStage()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$lastStageProbability = $probabilityMap[$entity->getLastStage()] ?? null;
|
||||
|
||||
if ($lastStageProbability !== 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
$max = 0;
|
||||
$maxStage = null;
|
||||
|
||||
foreach ($stageList as $stage) {
|
||||
$itemProbability = $probabilityMap[$stage] ?? null;
|
||||
|
||||
if (
|
||||
$itemProbability === null ||
|
||||
$itemProbability === 100 ||
|
||||
$itemProbability === 0 ||
|
||||
$itemProbability <= $max
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$max = $itemProbability;
|
||||
$maxStage = $stage;
|
||||
}
|
||||
|
||||
if (!$maxStage) {
|
||||
return;
|
||||
}
|
||||
|
||||
$entity->setLastStage($maxStage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ?int>
|
||||
*/
|
||||
private function getProbabilityMap(): array
|
||||
{
|
||||
/** @var array<string, ?int> $probabilityMap */
|
||||
$probabilityMap = $this->metadata->get('entityDefs.Opportunity.fields.stage.probabilityMap') ?? [];
|
||||
|
||||
return $probabilityMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function getStageList(): array
|
||||
{
|
||||
return $this->metadata->get('entityDefs.Opportunity.fields.stage.options') ?? [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?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\Hooks\Opportunity;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Modules\Crm\Entities\Opportunity;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<Opportunity>
|
||||
*/
|
||||
class Probability implements BeforeSave
|
||||
{
|
||||
public static int $order = 7;
|
||||
|
||||
public function __construct(private Metadata $metadata) {}
|
||||
|
||||
/**
|
||||
* @param Opportunity $entity
|
||||
*/
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isNew()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->getProbability() !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stage = $entity->getStage();
|
||||
|
||||
if (!$stage) {
|
||||
return;
|
||||
}
|
||||
|
||||
$probability = $this->metadata->get("entityDefs.Opportunity.fields.stage.probabilityMap.$stage") ?? 0;
|
||||
|
||||
$entity->setProbability($probability);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?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\Hooks\Task;
|
||||
|
||||
use Espo\Core\Hook\Hook\AfterRemove;
|
||||
use Espo\Core\Hook\Hook\AfterSave;
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Core\WebSocket\Submission;
|
||||
use Espo\Modules\Crm\Entities\Task;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Option\RemoveOptions;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements AfterSave<Task>
|
||||
* @implements AfterRemove<Task>
|
||||
*/
|
||||
class CalendarWebSocket implements AfterSave, AfterRemove
|
||||
{
|
||||
public function __construct(
|
||||
private Submission $submission,
|
||||
) {}
|
||||
|
||||
public function afterSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
$this->process($entity);
|
||||
}
|
||||
|
||||
public function afterRemove(Entity $entity, RemoveOptions $options): void
|
||||
{
|
||||
$this->process($entity);
|
||||
}
|
||||
|
||||
private function process(Task $entity): void
|
||||
{
|
||||
if ($entity->hasLinkMultipleField(Field::ASSIGNED_USERS)) {
|
||||
foreach ($entity->getLinkMultipleIdList(Field::ASSIGNED_USERS) as $userId) {
|
||||
$this->submission->submit('calendarUpdate', $userId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->getAssignedUser()) {
|
||||
$this->submission->submit('calendarUpdate', $entity->getAssignedUser()->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
69
application/Espo/Modules/Crm/Hooks/Task/DateCompleted.php
Normal file
69
application/Espo/Modules/Crm/Hooks/Task/DateCompleted.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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\Hooks\Task;
|
||||
|
||||
use Espo\Core\Field\DateTime;
|
||||
use Espo\Core\ORM\Repository\Option\SaveOption;
|
||||
use Espo\Modules\Crm\Entities\Task;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class DateCompleted
|
||||
{
|
||||
private const FIELD_DATE_COMPLETED = 'dateCompleted';
|
||||
private const FIELD_STATUS = 'status';
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
/**
|
||||
* @param Task $entity
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
public function beforeSave(Entity $entity, array $options): void
|
||||
{
|
||||
if (!$entity->isAttributeChanged(self::FIELD_STATUS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
($options[SaveOption::IMPORT] ?? false) &&
|
||||
$entity->get(self::FIELD_DATE_COMPLETED)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->getStatus() !== Task::STATUS_COMPLETED) {
|
||||
$entity->set(self::FIELD_DATE_COMPLETED, null);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$entity->setValueObject(self::FIELD_DATE_COMPLETED, DateTime::createNow());
|
||||
}
|
||||
}
|
||||
183
application/Espo/Modules/Crm/Hooks/Task/ParentLink.php
Normal file
183
application/Espo/Modules/Crm/Hooks/Task/ParentLink.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?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\Hooks\Task;
|
||||
|
||||
use Espo\Core\Hook\Hook\BeforeSave;
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Core\ORM\Entity as CoreEntity;
|
||||
use Espo\Modules\Crm\Entities\Account;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\Modules\Crm\Entities\Lead;
|
||||
use Espo\Modules\Crm\Entities\Task;
|
||||
use Espo\ORM\Defs\Params\RelationParam;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use Espo\ORM\Repository\Option\SaveOptions;
|
||||
|
||||
/**
|
||||
* @implements BeforeSave<Task>
|
||||
*/
|
||||
class ParentLink implements BeforeSave
|
||||
{
|
||||
public function __construct(private EntityManager $entityManager) {}
|
||||
|
||||
/**
|
||||
* @param Task $entity
|
||||
*/
|
||||
public function beforeSave(Entity $entity, SaveOptions $options): void
|
||||
{
|
||||
if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) {
|
||||
$entity->set('accountId', null);
|
||||
$entity->set('contactId', null);
|
||||
$entity->set('accountName', null);
|
||||
$entity->set('contactName', null);
|
||||
}
|
||||
|
||||
if (!$entity->isAttributeChanged('parentId') && !$entity->isAttributeChanged('parentType')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$parent = null;
|
||||
|
||||
$parentId = $entity->get('parentId');
|
||||
$parentType = $entity->get('parentType');
|
||||
|
||||
if ($parentId && $parentType && $this->entityManager->hasRepository($parentType)) {
|
||||
$columnList = ['id', 'name'];
|
||||
|
||||
$defs = $this->entityManager->getMetadata()->getDefs();
|
||||
|
||||
if ($defs->getEntity($parentType)->hasAttribute('accountId')) {
|
||||
$columnList[] = 'accountId';
|
||||
}
|
||||
|
||||
if ($defs->getEntity($parentType)->hasAttribute('contactId')) {
|
||||
$columnList[] = 'contactId';
|
||||
}
|
||||
|
||||
if ($parentType === Lead::ENTITY_TYPE) {
|
||||
$columnList[] = 'status';
|
||||
$columnList[] = 'createdAccountId';
|
||||
$columnList[] = 'createdAccountName';
|
||||
$columnList[] = 'createdContactId';
|
||||
$columnList[] = 'createdContactName';
|
||||
}
|
||||
|
||||
$parent = $this->entityManager
|
||||
->getRDBRepository($parentType)
|
||||
->select($columnList)
|
||||
->where([Attribute::ID => $parentId])
|
||||
->findOne();
|
||||
}
|
||||
|
||||
$accountId = null;
|
||||
$contactId = null;
|
||||
$accountName = null;
|
||||
$contactName = null;
|
||||
|
||||
if ($parent) {
|
||||
if ($parent instanceof Account) {
|
||||
$accountId = $parent->getId();
|
||||
$accountName = $parent->get(Field::NAME);
|
||||
} else if (
|
||||
$parent instanceof Lead &&
|
||||
$parent->getStatus() == Lead::STATUS_CONVERTED
|
||||
) {
|
||||
if ($parent->get('createdAccountId')) {
|
||||
$accountId = $parent->get('createdAccountId');
|
||||
$accountName = $parent->get('createdAccountName');
|
||||
}
|
||||
|
||||
if ($parent->get('createdContactId')) {
|
||||
$contactId = $parent->get('createdContactId');
|
||||
$contactName = $parent->get('createdContactName');
|
||||
}
|
||||
} else if ($parent instanceof Contact) {
|
||||
$contactId = $parent->getId();
|
||||
$contactName = $parent->get(Field::NAME);
|
||||
}
|
||||
|
||||
if (
|
||||
!$accountId &&
|
||||
$parent->get('accountId') &&
|
||||
$parent instanceof CoreEntity &&
|
||||
$parent->getRelationParam('account', RelationParam::ENTITY) === Account::ENTITY_TYPE
|
||||
) {
|
||||
$accountId = $parent->get('accountId');
|
||||
}
|
||||
|
||||
if (
|
||||
!$contactId &&
|
||||
$parent->get('contactId') &&
|
||||
$parent instanceof CoreEntity &&
|
||||
$parent->getRelationParam('contact', RelationParam::ENTITY) === Contact::ENTITY_TYPE
|
||||
) {
|
||||
$contactId = $parent->get('contactId');
|
||||
}
|
||||
}
|
||||
|
||||
$entity->set('accountId', $accountId);
|
||||
$entity->set('accountName', $accountName);
|
||||
|
||||
$entity->set('contactId', $contactId);
|
||||
$entity->set('contactName', $contactName);
|
||||
|
||||
if (
|
||||
$entity->get('accountId') &&
|
||||
!$entity->get('accountName')
|
||||
) {
|
||||
$account = $this->entityManager
|
||||
->getRDBRepository(Account::ENTITY_TYPE)
|
||||
->select([Attribute::ID, 'name'])
|
||||
->where([Attribute::ID => $entity->get('accountId')])
|
||||
->findOne();
|
||||
|
||||
if ($account) {
|
||||
$entity->set('accountName', $account->get(Field::NAME));
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
$entity->get('contactId') &&
|
||||
!$entity->get('contactName')
|
||||
) {
|
||||
$contact = $this->entityManager
|
||||
->getRDBRepository(Contact::ENTITY_TYPE)
|
||||
->select([Attribute::ID, 'name'])
|
||||
->where([Attribute::ID => $entity->get('contactId')])
|
||||
->findOne();
|
||||
|
||||
if ($contact) {
|
||||
$entity->set('contactName', $contact->get(Field::NAME));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user