Commit | Line | Data |
---|---|---|
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 |
22 | namespace Civi\Api4\Generic; |
23 | ||
24 | /** | |
25 | * Delete one or more items, based on criteria specified in Where param (required). | |
26 | */ | |
27 | class DAODeleteAction extends AbstractBatchAction { | |
28 | use Traits\DAOActionTrait; | |
29 | ||
30 | /** | |
31 | * Batch delete function | |
32 | */ | |
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'); | |
37 | } | |
38 | ||
39 | $items = $this->getObjects(); | |
40 | ||
41 | if (!$items) { | |
42 | throw new \API_Exception('Cannot delete ' . $this->getEntityName() . ', no records found with ' . $this->whereClauseToString()); | |
43 | } | |
44 | ||
45 | $ids = $this->deleteObjects($items); | |
46 | ||
47 | $result->exchangeArray($ids); | |
48 | } | |
49 | ||
50 | /** | |
51 | * @param $items | |
52 | * @return array | |
53 | * @throws \API_Exception | |
54 | */ | |
55 | protected function deleteObjects($items) { | |
56 | $ids = []; | |
57 | $baoName = $this->getBaoName(); | |
58 | ||
59 | if ($this->getCheckPermissions()) { | |
60 | foreach ($items as $item) { | |
61 | $this->checkContactPermissions($baoName, $item); | |
62 | } | |
63 | } | |
64 | ||
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); | |
69 | if ($bao !== FALSE) { | |
70 | $ids[] = ['id' => $item['id']]; | |
71 | } | |
72 | else { | |
73 | throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}"); | |
74 | } | |
75 | } | |
76 | } | |
77 | else { | |
78 | foreach ($items as $item) { | |
79 | $bao = new $baoName(); | |
80 | $bao->id = $item['id']; | |
81 | // delete it | |
82 | $action_result = $bao->delete(); | |
83 | if ($action_result) { | |
84 | $ids[] = ['id' => $item['id']]; | |
85 | } | |
86 | else { | |
87 | throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}"); | |
88 | } | |
89 | } | |
90 | } | |
91 | return $ids; | |
92 | } | |
93 | ||
94 | } |