4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Generic
;
15 use Civi\API\Exception\UnauthorizedException
;
16 use Civi\Api4\Utils\CoreUtil
;
17 use Civi\Api4\Utils\ReflectionUtils
;
20 * Delete one or more $ENTITIES.
22 * $ENTITIES are deleted based on criteria specified in `where` parameter (required).
24 class DAODeleteAction
extends AbstractBatchAction
{
25 use Traits\DAOActionTrait
;
28 * Batch delete function
30 public function _run(Result
$result) {
31 $defaults = $this->getParamDefaults();
32 if ($defaults['where'] && $this->where
=== $defaults['where']) {
33 throw new \
API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
36 $items = $this->getBatchRecords();
38 if ($this->getCheckPermissions()) {
39 foreach ($items as $key => $item) {
40 if (!CoreUtil
::checkAccessRecord($this, $item, \CRM_Core_Session
::getLoggedInContactID() ?
: 0)) {
41 throw new UnauthorizedException("ACL check failed");
43 $items[$key]['check_permissions'] = TRUE;
47 $result->exchangeArray($this->deleteObjects($items));
54 * @throws \API_Exception
56 protected function deleteObjects($items) {
58 $baoName = $this->getBaoName();
60 // Use BAO::del() method if it is not deprecated
61 if (method_exists($baoName, 'del') && !ReflectionUtils
::isMethodDeprecated($baoName, 'del')) {
62 foreach ($items as $item) {
63 $args = [$item['id']];
64 $bao = call_user_func_array([$baoName, 'del'], $args);
66 $ids[] = ['id' => $item['id']];
69 throw new \
API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");
74 foreach ($baoName::deleteRecords($items) as $instance) {
75 $ids[] = ['id' => $instance->id
];