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
;
19 * Delete one or more $ENTITIES.
21 * $ENTITIES are deleted based on criteria specified in `where` parameter (required).
23 class DAODeleteAction
extends AbstractBatchAction
{
24 use Traits\DAOActionTrait
;
27 * Batch delete function
29 public function _run(Result
$result) {
30 $defaults = $this->getParamDefaults();
31 if ($defaults['where'] && $this->where
=== $defaults['where']) {
32 throw new \
API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
35 $items = $this->getBatchRecords();
37 if ($this->getCheckPermissions()) {
38 foreach ($items as $key => $item) {
39 if (!CoreUtil
::checkAccessRecord($this, $item, \CRM_Core_Session
::getLoggedInContactID() ?
: 0)) {
40 throw new UnauthorizedException("ACL check failed");
42 $items[$key]['check_permissions'] = TRUE;
46 $result->exchangeArray($this->deleteObjects($items));
53 * @throws \API_Exception
55 protected function deleteObjects($items) {
57 $baoName = $this->getBaoName();
59 if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) {
60 foreach ($items as $item) {
61 $args = [$item['id']];
62 $bao = call_user_func_array([$baoName, 'del'], $args);
64 $ids[] = ['id' => $item['id']];
67 throw new \
API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");
72 foreach ($baoName::deleteRecords($items) as $instance) {
73 $ids[] = ['id' => $instance->id
];