Commit | Line | Data |
---|---|---|
a2bf5923 CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
15 | * @copyright CiviCRM LLC https://civicrm.org/licensing | |
16 | */ | |
17 | ||
18 | /** | |
19 | * This class contains functions for managed entities. | |
20 | */ | |
21 | class CRM_Core_BAO_Managed extends CRM_Core_DAO_Managed implements Civi\Test\HookInterface { | |
22 | ||
23 | /** | |
24 | * Callback for hook_civicrm_post(). | |
25 | * @param \Civi\Core\Event\PostEvent $event | |
26 | */ | |
27 | public static function on_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { | |
28 | // When an entity is deleted, delete the corresponding Managed record | |
29 | if ($event->action === 'delete' && $event->id && self::isApi4ManagedType($event->entity)) { | |
30 | \Civi\Api4\Managed::delete(FALSE) | |
31 | ->addWhere('entity_type', '=', $event->entity) | |
32 | ->addWhere('entity_id', '=', $event->id) | |
33 | ->execute(); | |
34 | } | |
69e13f9b CW |
35 | // When an entity is updated, update the timestamp in corresponding Managed record |
36 | elseif ($event->action === 'edit' && $event->id && self::isApi4ManagedType($event->entity)) { | |
37 | CRM_Core_DAO::executeQuery('UPDATE civicrm_managed SET entity_modified_date = CURRENT_TIMESTAMP WHERE entity_type = %1 AND entity_id = %2', [ | |
38 | 1 => [$event->entity, 'String'], | |
39 | 2 => [$event->id, 'Integer'], | |
40 | ]); | |
41 | } | |
a2bf5923 CW |
42 | } |
43 | ||
44 | /** | |
45 | * @param string $entityName | |
46 | * @return bool | |
47 | */ | |
48 | public static function isApi4ManagedType(string $entityName) { | |
49 | $type = \Civi\Api4\Utils\CoreUtil::getInfoItem($entityName, 'type'); | |
50 | return $type && in_array('ManagedEntity', $type, TRUE); | |
51 | } | |
52 | ||
53 | } |