| 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\Core\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 | } |
| 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 | if (!array_key_exists('entity_modified_date', self::getSupportedFields())) { |
| 38 | // During upgrades this column may not exist yet |
| 39 | return; |
| 40 | } |
| 41 | CRM_Core_DAO::executeQuery('UPDATE civicrm_managed SET entity_modified_date = CURRENT_TIMESTAMP WHERE entity_type = %1 AND entity_id = %2', [ |
| 42 | 1 => [$event->entity, 'String'], |
| 43 | 2 => [$event->id, 'Integer'], |
| 44 | ]); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param string $entityName |
| 50 | * @return bool |
| 51 | */ |
| 52 | public static function isApi4ManagedType(string $entityName) { |
| 53 | $type = \Civi\Api4\Utils\CoreUtil::getInfoItem($entityName, 'type'); |
| 54 | return $type && in_array('ManagedEntity', $type, TRUE); |
| 55 | } |
| 56 | |
| 57 | } |