Merge pull request #20115 from larssandergreen/fix-internal-anchor-URLs-in-mailings
[civicrm-core.git] / Civi / Api4 / Generic / DAODeleteAction.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 |
10 +--------------------------------------------------------------------+
11 */
12
13 namespace Civi\Api4\Generic;
14
15 use Civi\API\Exception\UnauthorizedException;
16 use Civi\Api4\Utils\CoreUtil;
17
18 /**
19 * Delete one or more $ENTITIES.
20 *
21 * $ENTITIES are deleted based on criteria specified in `where` parameter (required).
22 */
23 class DAODeleteAction extends AbstractBatchAction {
24 use Traits\DAOActionTrait;
25
26 /**
27 * Batch delete function
28 */
29 public function _run(Result $result) {
30 $defaults = $this->getParamDefaults();
31 if ($defaults['where'] && $this->where === $defaults['where']) {
32 throw new \API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
33 }
34
35 $items = $this->getBatchRecords();
36
37 if ($this->getCheckPermissions()) {
38 foreach ($items as $key => $item) {
39 if (!CoreUtil::checkAccessRecord($this, $item, \CRM_Core_Session::getLoggedInContactID() ?: 0)) {
40 throw new UnauthorizedException("ACL check failed");
41 }
42 $items[$key]['check_permissions'] = TRUE;
43 }
44 }
45 if ($items) {
46 $result->exchangeArray($this->deleteObjects($items));
47 }
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->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) {
60 foreach ($items as $item) {
61 $args = [$item['id']];
62 $bao = call_user_func_array([$baoName, 'del'], $args);
63 if ($bao !== FALSE) {
64 $ids[] = ['id' => $item['id']];
65 }
66 else {
67 throw new \API_Exception("Could not delete {$this->getEntityName()} id {$item['id']}");
68 }
69 }
70 }
71 else {
72 foreach ($baoName::deleteRecords($items) as $instance) {
73 $ids[] = ['id' => $instance->id];
74 }
75 }
76 return $ids;
77 }
78
79 }