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 | /** | |
fc95d9a5 CW |
25 | * Delete one or more $ENTITYs. |
26 | * | |
27 | * $ENTITYs are deleted based on criteria specified in `where` parameter (required). | |
19b53e5b C |
28 | */ |
29 | class 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()) { | |
57 | foreach ($items as $item) { | |
58 | $this->checkContactPermissions($baoName, $item); | |
59 | } | |
60 | } | |
61 | ||
62 | if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) { | |
63 | foreach ($items as $item) { | |
64 | $args = [$item['id']]; | |
65 | $bao = call_user_func_array([$baoName, 'del'], $args); | |
66 | if ($bao !== FALSE) { | |
67 | $ids[] = ['id' => $item['id']]; | |
68 | } | |
69 | else { | |
70 | throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}"); | |
71 | } | |
72 | } | |
73 | } | |
74 | else { | |
75 | foreach ($items as $item) { | |
76 | $bao = new $baoName(); | |
77 | $bao->id = $item['id']; | |
78 | // delete it | |
79 | $action_result = $bao->delete(); | |
80 | if ($action_result) { | |
81 | $ids[] = ['id' => $item['id']]; | |
82 | } | |
83 | else { | |
84 | throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}"); | |
85 | } | |
86 | } | |
87 | } | |
88 | return $ids; | |
89 | } | |
90 | ||
91 | } |