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 | ||
19b53e5b C |
13 | namespace Civi\Api4\Generic; |
14 | ||
15 | /** | |
16 | * Base class for all batch actions (Update, Delete, Replace). | |
17 | * | |
18 | * This differs from the AbstractQuery class in that the "Where" clause is required. | |
19 | * | |
20 | * @package Civi\Api4\Generic | |
21 | */ | |
22 | abstract class AbstractBatchAction extends AbstractQueryAction { | |
23 | ||
24 | /** | |
e3c6d5ff | 25 | * Criteria for selecting $ENTITIES to process. |
19b53e5b C |
26 | * |
27 | * @var array | |
28 | * @required | |
29 | */ | |
30 | protected $where = []; | |
31 | ||
32 | /** | |
33 | * @var array | |
34 | */ | |
35 | private $select; | |
36 | ||
37 | /** | |
38 | * BatchAction constructor. | |
39 | * @param string $entityName | |
40 | * @param string $actionName | |
41 | * @param string|array $select | |
42 | * One or more fields to load for each item. | |
43 | */ | |
44 | public function __construct($entityName, $actionName, $select = 'id') { | |
45 | $this->select = (array) $select; | |
46 | parent::__construct($entityName, $actionName); | |
47 | } | |
48 | ||
49 | /** | |
8047edec TO |
50 | * Get a list of records for this batch. |
51 | * | |
19b53e5b C |
52 | * @return array |
53 | */ | |
54 | protected function getBatchRecords() { | |
8047edec TO |
55 | return (array) $this->getBatchAction()->execute(); |
56 | } | |
57 | ||
58 | /** | |
59 | * Get a query which resolves the list of records for this batch. | |
60 | * | |
61 | * This is similar to `getBatchRecords()`, but you may further refine the | |
62 | * API call (e.g. selecting different fields or data-pages) before executing. | |
63 | * | |
64 | * @return \Civi\Api4\Generic\AbstractGetAction | |
65 | */ | |
66 | protected function getBatchAction() { | |
19b53e5b C |
67 | $params = [ |
68 | 'checkPermissions' => $this->checkPermissions, | |
69 | 'where' => $this->where, | |
70 | 'orderBy' => $this->orderBy, | |
71 | 'limit' => $this->limit, | |
72 | 'offset' => $this->offset, | |
73 | ]; | |
74 | if (empty($this->reload)) { | |
75 | $params['select'] = $this->select; | |
76 | } | |
8047edec | 77 | return \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4] + $params); |
19b53e5b C |
78 | } |
79 | ||
80 | /** | |
81 | * @return array | |
82 | */ | |
83 | protected function getSelect() { | |
84 | return $this->select; | |
85 | } | |
86 | ||
87 | } |