Commit | Line | Data |
---|---|---|
9626d0a1 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 | namespace Civi\Api4\Generic\Traits; | |
13 | ||
14 | use Civi\Api4\Generic\BasicBatchAction; | |
4d8c2629 | 15 | use Civi\Api4\Generic\ExportAction; |
9626d0a1 CW |
16 | |
17 | /** | |
18 | * A managed entity includes extra fields and methods to revert from an overridden local to base state. | |
19 | * | |
20 | * Includes the extra fields `has_base` and `base_module` | |
21 | */ | |
22 | trait ManagedEntity { | |
23 | ||
24 | /** | |
25 | * @param bool $checkPermissions | |
26 | * @return \Civi\Api4\Generic\BasicBatchAction | |
27 | */ | |
28 | public static function revert($checkPermissions = TRUE) { | |
29 | return (new BasicBatchAction(static::getEntityName(), __FUNCTION__, function($item, BasicBatchAction $action) { | |
30 | $params = ['entity_type' => $action->getEntityName(), 'entity_id' => $item['id']]; | |
31 | if (\CRM_Core_ManagedEntities::singleton()->revert($params)) { | |
32 | return $item; | |
33 | } | |
34 | else { | |
35 | throw new \API_Exception('Cannot revert ' . $action->getEntityName() . ' with id ' . $item['id']); | |
36 | } | |
37 | }))->setCheckPermissions($checkPermissions); | |
38 | } | |
39 | ||
4d8c2629 CW |
40 | /** |
41 | * @param bool $checkPermissions | |
42 | * @return \Civi\Api4\Generic\ExportAction | |
43 | */ | |
44 | public static function export($checkPermissions = TRUE) { | |
45 | return (new ExportAction(static::getEntityName(), __FUNCTION__)) | |
46 | ->setCheckPermissions($checkPermissions); | |
47 | } | |
48 | ||
9626d0a1 | 49 | } |