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(); | |
8a0c3604 | 35 | if ($defaults['where'] && $this->where === $defaults['where']) { |
19b53e5b C |
36 | throw new \API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified'); |
37 | } | |
38 | ||
39 | $items = $this->getObjects(); | |
8a0c3604 CW |
40 | if ($items) { |
41 | $result->exchangeArray($this->deleteObjects($items)); | |
19b53e5b | 42 | } |
19b53e5b C |
43 | } |
44 | ||
45 | /** | |
46 | * @param $items | |
47 | * @return array | |
48 | * @throws \API_Exception | |
49 | */ | |
50 | protected function deleteObjects($items) { | |
51 | $ids = []; | |
52 | $baoName = $this->getBaoName(); | |
53 | ||
54 | if ($this->getCheckPermissions()) { | |
55 | foreach ($items as $item) { | |
56 | $this->checkContactPermissions($baoName, $item); | |
57 | } | |
58 | } | |
59 | ||
60 | if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) { | |
61 | foreach ($items as $item) { | |
62 | $args = [$item['id']]; | |
63 | $bao = call_user_func_array([$baoName, 'del'], $args); | |
64 | if ($bao !== FALSE) { | |
65 | $ids[] = ['id' => $item['id']]; | |
66 | } | |
67 | else { | |
68 | throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}"); | |
69 | } | |
70 | } | |
71 | } | |
72 | else { | |
73 | foreach ($items as $item) { | |
74 | $bao = new $baoName(); | |
75 | $bao->id = $item['id']; | |
76 | // delete it | |
77 | $action_result = $bao->delete(); | |
78 | if ($action_result) { | |
79 | $ids[] = ['id' => $item['id']]; | |
80 | } | |
81 | else { | |
82 | throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}"); | |
83 | } | |
84 | } | |
85 | } | |
86 | return $ids; | |
87 | } | |
88 | ||
89 | } |