Merge pull request #15964 from mlutfy/financial109
[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 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace Civi\Api4\Generic;
23
24 /**
25 * Delete one or more $ENTITIES.
26 *
27 * $ENTITIES are deleted based on criteria specified in `where` parameter (required).
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();
37 if ($defaults['where'] && $this->where === $defaults['where']) {
38 throw new \API_Exception('Cannot delete ' . $this->getEntityName() . ' with no "where" parameter specified');
39 }
40
41 $items = $this->getObjects();
42 if ($items) {
43 $result->exchangeArray($this->deleteObjects($items));
44 }
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 }