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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace Civi\Api4\Generic
;
22 use Civi\API\Exception\UnauthorizedException
;
23 use Civi\Api4\Utils\CoreUtil
;
26 * Delete one or more $ENTITIES.
28 * $ENTITIES are deleted based on criteria specified in `where` parameter (required).
30 class DAODeleteAction
extends AbstractBatchAction
{
31 use Traits\DAOActionTrait
;
34 * Batch delete function
36 public function _run(Result
$result) {
37 $defaults = $this->getParamDefaults();
38 if ($defaults['where'] && $this->where
=== $defaults['where']) {
39 throw new \
API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
42 $items = $this->getBatchRecords();
44 if ($this->getCheckPermissions()) {
45 foreach ($items as $key => $item) {
46 if (!CoreUtil
::checkAccess($this->getEntityName(), $this->getActionName(), $item)) {
47 throw new UnauthorizedException("ACL check failed");
49 $items[$key]['check_permissions'] = TRUE;
53 $result->exchangeArray($this->deleteObjects($items));
60 * @throws \API_Exception
62 protected function deleteObjects($items) {
64 $baoName = $this->getBaoName();
66 if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) {
67 foreach ($items as $item) {
68 $args = [$item['id']];
69 $bao = call_user_func_array([$baoName, 'del'], $args);
71 $ids[] = ['id' => $item['id']];
74 throw new \
API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");
79 foreach ($baoName::deleteRecords($items) as $instance) {
80 $ids[] = ['id' => $instance->id
];