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 | ||
29ab318b CW |
15 | use Civi\Api4\Utils\CoreUtil; |
16 | ||
19b53e5b C |
17 | /** |
18 | * Base class for all batch actions (Update, Delete, Replace). | |
19 | * | |
20 | * This differs from the AbstractQuery class in that the "Where" clause is required. | |
21 | * | |
22 | * @package Civi\Api4\Generic | |
23 | */ | |
24 | abstract class AbstractBatchAction extends AbstractQueryAction { | |
25 | ||
26 | /** | |
e3c6d5ff | 27 | * Criteria for selecting $ENTITIES to process. |
19b53e5b C |
28 | * |
29 | * @var array | |
30 | * @required | |
31 | */ | |
32 | protected $where = []; | |
33 | ||
19b53e5b | 34 | /** |
8047edec TO |
35 | * Get a list of records for this batch. |
36 | * | |
19b53e5b C |
37 | * @return array |
38 | */ | |
39 | protected function getBatchRecords() { | |
8047edec TO |
40 | return (array) $this->getBatchAction()->execute(); |
41 | } | |
42 | ||
43 | /** | |
29ab318b | 44 | * Get an API action object which resolves the list of records for this batch. |
8047edec TO |
45 | * |
46 | * This is similar to `getBatchRecords()`, but you may further refine the | |
47 | * API call (e.g. selecting different fields or data-pages) before executing. | |
48 | * | |
49 | * @return \Civi\Api4\Generic\AbstractGetAction | |
50 | */ | |
51 | protected function getBatchAction() { | |
19b53e5b C |
52 | $params = [ |
53 | 'checkPermissions' => $this->checkPermissions, | |
54 | 'where' => $this->where, | |
55 | 'orderBy' => $this->orderBy, | |
56 | 'limit' => $this->limit, | |
57 | 'offset' => $this->offset, | |
58 | ]; | |
59 | if (empty($this->reload)) { | |
29ab318b | 60 | $params['select'] = $this->getSelect(); |
19b53e5b | 61 | } |
8047edec | 62 | return \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4] + $params); |
19b53e5b C |
63 | } |
64 | ||
65 | /** | |
29ab318b CW |
66 | * Determines what fields will be returned by getBatchRecords |
67 | * | |
68 | * Defaults to an entity's primary key(s), typically ['id'] | |
69 | * | |
70 | * @return string[] | |
19b53e5b C |
71 | */ |
72 | protected function getSelect() { | |
29ab318b | 73 | return CoreUtil::getInfoItem($this->getEntityName(), 'primary_key'); |
19b53e5b C |
74 | } |
75 | ||
76 | } |