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 | */ |
18 | ||
19 | ||
19b53e5b C |
20 | namespace Civi\Api4\Generic; |
21 | ||
929a9585 CW |
22 | use Civi\API\Exception\UnauthorizedException; |
23 | use Civi\Api4\Utils\CoreUtil; | |
24 | ||
19b53e5b | 25 | /** |
e3c6d5ff | 26 | * Delete one or more $ENTITIES. |
fc95d9a5 | 27 | * |
e3c6d5ff | 28 | * $ENTITIES are deleted based on criteria specified in `where` parameter (required). |
19b53e5b C |
29 | */ |
30 | class DAODeleteAction extends AbstractBatchAction { | |
31 | use Traits\DAOActionTrait; | |
32 | ||
33 | /** | |
34 | * Batch delete function | |
35 | */ | |
36 | public function _run(Result $result) { | |
37 | $defaults = $this->getParamDefaults(); | |
8a0c3604 | 38 | if ($defaults['where'] && $this->where === $defaults['where']) { |
19b53e5b C |
39 | throw new \API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified'); |
40 | } | |
41 | ||
3c7c8fa6 | 42 | $items = $this->getBatchRecords(); |
1d3cbc3c CW |
43 | |
44 | if ($this->getCheckPermissions()) { | |
45 | foreach ($items as $key => $item) { | |
849354a5 | 46 | if (!CoreUtil::checkAccessRecord($this, $item, \CRM_Core_Session::getLoggedInContactID())) { |
1d3cbc3c CW |
47 | throw new UnauthorizedException("ACL check failed"); |
48 | } | |
49 | $items[$key]['check_permissions'] = TRUE; | |
50 | } | |
51 | } | |
8a0c3604 CW |
52 | if ($items) { |
53 | $result->exchangeArray($this->deleteObjects($items)); | |
19b53e5b | 54 | } |
19b53e5b C |
55 | } |
56 | ||
57 | /** | |
58 | * @param $items | |
59 | * @return array | |
60 | * @throws \API_Exception | |
61 | */ | |
62 | protected function deleteObjects($items) { | |
63 | $ids = []; | |
64 | $baoName = $this->getBaoName(); | |
65 | ||
19b53e5b C |
66 | if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) { |
67 | foreach ($items as $item) { | |
68 | $args = [$item['id']]; | |
69 | $bao = call_user_func_array([$baoName, 'del'], $args); | |
70 | if ($bao !== FALSE) { | |
71 | $ids[] = ['id' => $item['id']]; | |
72 | } | |
73 | else { | |
74 | throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}"); | |
75 | } | |
76 | } | |
77 | } | |
78 | else { | |
236f858e CW |
79 | foreach ($baoName::deleteRecords($items) as $instance) { |
80 | $ids[] = ['id' => $instance->id]; | |
19b53e5b C |
81 | } |
82 | } | |
83 | return $ids; | |
84 | } | |
85 | ||
86 | } |