Merge pull request #16785 from demeritcowboy/weird-casetype-check
[civicrm-core.git] / Civi / Api4 / Generic / DAODeleteAction.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 * $Id$
18 *
19 */
20
21
19b53e5b
C
22namespace Civi\Api4\Generic;
23
24/**
e3c6d5ff 25 * Delete one or more $ENTITIES.
fc95d9a5 26 *
e3c6d5ff 27 * $ENTITIES are deleted based on criteria specified in `where` parameter (required).
19b53e5b
C
28 */
29class DAODeleteAction extends AbstractBatchAction {
30 use Traits\DAOActionTrait;
31
32 /**
33 * Batch delete function
34 */
35 public function _run(Result $result) {
36 $defaults = $this->getParamDefaults();
8a0c3604 37 if ($defaults['where'] && $this->where === $defaults['where']) {
19b53e5b
C
38 throw new \API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
39 }
40
41 $items = $this->getObjects();
8a0c3604
CW
42 if ($items) {
43 $result->exchangeArray($this->deleteObjects($items));
19b53e5b 44 }
19b53e5b
C
45 }
46
47 /**
48 * @param $items
49 * @return array
50 * @throws \API_Exception
51 */
52 protected function deleteObjects($items) {
53 $ids = [];
54 $baoName = $this->getBaoName();
55
56 if ($this->getCheckPermissions()) {
2ee9afab
CW
57 foreach (array_keys($items) as $key) {
58 $items[$key]['check_permissions'] = TRUE;
59 $this->checkContactPermissions($baoName, $items[$key]);
19b53e5b
C
60 }
61 }
62
63 if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) {
64 foreach ($items as $item) {
65 $args = [$item['id']];
66 $bao = call_user_func_array([$baoName, 'del'], $args);
67 if ($bao !== FALSE) {
68 $ids[] = ['id' => $item['id']];
69 }
70 else {
71 throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");
72 }
73 }
74 }
75 else {
76 foreach ($items as $item) {
2ee9afab
CW
77 $baoName::deleteRecord($item);
78 $ids[] = ['id' => $item['id']];
19b53e5b
C
79 }
80 }
81 return $ids;
82 }
83
84}