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
22 namespace Civi\Api4\Generic
;
25 * Delete one or more items, based on criteria specified in Where param (required).
27 class DAODeleteAction
extends AbstractBatchAction
{
28 use Traits\DAOActionTrait
;
31 * Batch delete function
33 public function _run(Result
$result) {
34 $defaults = $this->getParamDefaults();
35 if ($defaults['where'] && !array_diff_key($this->where
, $defaults['where'])) {
36 throw new \
API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
39 $items = $this->getObjects();
42 throw new \
API_Exception('Cannot delete ' . $this->getEntityName() . ', no records found with ' . $this->whereClauseToString());
45 $ids = $this->deleteObjects($items);
47 $result->exchangeArray($ids);
53 * @throws \API_Exception
55 protected function deleteObjects($items) {
57 $baoName = $this->getBaoName();
59 if ($this->getCheckPermissions()) {
60 foreach ($items as $item) {
61 $this->checkContactPermissions($baoName, $item);
65 if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) {
66 foreach ($items as $item) {
67 $args = [$item['id']];
68 $bao = call_user_func_array([$baoName, 'del'], $args);
70 $ids[] = ['id' => $item['id']];
73 throw new \
API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");
78 foreach ($items as $item) {
79 $bao = new $baoName();
80 $bao->id
= $item['id'];
82 $action_result = $bao->delete();
84 $ids[] = ['id' => $item['id']];
87 throw new \
API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");