-
-
Notifications
You must be signed in to change notification settings - Fork 818
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31336 from johntwyman/api4/contact-mergedtofrom
Add APIv4 Contact.getMergedTo/getMergedFrom actions
- Loading branch information
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Api4\Action\Contact; | ||
|
||
use Civi\Api4\Generic\Result; | ||
|
||
/** | ||
* Get the prior contact a contact was merged from. | ||
* | ||
* @method $this setContactId(int $cid) Set contact ID (required) | ||
* @method int getContactId() Get contact ID param | ||
* @method $this setIsTest(bool $isTest) Set isTest param | ||
* @method bool getIsTest() Get isTest param | ||
*/ | ||
class GetMergedFrom extends \Civi\Api4\Generic\AbstractAction { | ||
|
||
/** | ||
* ID of contact to find prior contact for | ||
* | ||
* @var int | ||
* @required | ||
*/ | ||
protected $contactId; | ||
|
||
/** | ||
* Get test deletions rather than live? | ||
* @var bool | ||
*/ | ||
protected $isTest = FALSE; | ||
|
||
/** | ||
* @param \Civi\Api4\Generic\Result $result | ||
*/ | ||
public function _run(Result $result): void { | ||
$activities = []; | ||
$deleteActivities = \Civi\Api4\ActivityContact::get(FALSE) | ||
->addSelect('activity_id') | ||
->addWhere('contact_id', '=', $this->contactId) | ||
->addWhere('activity_id.activity_type_id:name', '=', 'Contact Merged') | ||
->addWhere('activity_id.is_deleted', '=', FALSE) | ||
->addWhere('activity_id.is_test', '=', $this->isTest) | ||
->addWhere('record_type_id:name', '=', 'Activity Targets') | ||
->execute(); | ||
foreach ($deleteActivities as $deleteActivity) { | ||
$activities[] = $deleteActivity['activity_id']; | ||
} | ||
if (empty($activities)) { | ||
$result[] = $activities; | ||
return; | ||
} | ||
|
||
$activityContacts = \Civi\Api4\ActivityContact::get(FALSE) | ||
->addSelect('contact_id') | ||
->addWhere('activity_id.parent_id', 'IN', $activities) | ||
->addWhere('record_type_id:name', '=', 'Activity Targets') | ||
->execute(); | ||
$contacts = []; | ||
foreach ($activityContacts as $activityContact) { | ||
$result[] = ['id' => $activityContact['contact_id']]; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Api4\Action\Contact; | ||
|
||
use Civi\Api4\Generic\Result; | ||
|
||
/** | ||
* Get the ultimate contact a contact was merged to. | ||
* | ||
* @method $this setContactId(int $cid) Set contact ID (required) | ||
* @method int getContactId() Get contact ID param | ||
* @method $this setIsTest(bool $isTest) Set isTest param | ||
* @method bool getIsTest() Get isTest param | ||
*/ | ||
class GetMergedTo extends \Civi\Api4\Generic\AbstractAction { | ||
|
||
/** | ||
* ID of contact to find ultimate contact for | ||
* | ||
* @var int | ||
* @required | ||
*/ | ||
protected $contactId; | ||
|
||
/** | ||
* Get test deletions rather than live? | ||
* @var bool | ||
*/ | ||
protected $isTest = FALSE; | ||
|
||
/** | ||
* @param \Civi\Api4\Generic\Result $result | ||
*/ | ||
public function _run(Result $result): void { | ||
$returnId = []; | ||
$deleteActivity = \Civi\Api4\ActivityContact::get(FALSE) | ||
->addSelect('activity_id.parent_id') | ||
->addWhere('contact_id', '=', $this->contactId) | ||
->addWhere('activity_id.activity_type_id:name', '=', 'Contact Deleted by Merge') | ||
->addWhere('activity_id.is_deleted', '=', FALSE) | ||
->addWhere('activity_id.is_test', '=', $this->isTest) | ||
->addWhere('record_type_id:name', '=', 'Activity Targets') | ||
->addOrderBy('activity_id.activity_date_time', 'DESC') | ||
->setLimit(1) | ||
->execute() | ||
->first(); | ||
if (!empty($deleteActivity)) { | ||
$returnId = \Civi\Api4\ActivityContact::get(FALSE) | ||
->addSelect('contact_id') | ||
->addWhere('activity_id', '=', $deleteActivity['activity_id.parent_id']) | ||
->addWhere('record_type_id:name', '=', 'Activity Targets') | ||
->execute() | ||
->first()['contact_id']; | ||
} | ||
$result[] = ['id' => $returnId]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters