Merge pull request #22955 from colemanw/schemaTrait
[civicrm-core.git] / Civi / Api4 / Generic / AbstractBatchAction.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\Api4\Utils\CoreUtil;
16
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 /**
27 * Criteria for selecting $ENTITIES to process.
28 *
29 * @var array
30 * @required
31 */
32 protected $where = [];
33
34 /**
35 * Get a list of records for this batch.
36 *
37 * @return array
38 */
39 protected function getBatchRecords() {
40 return (array) $this->getBatchAction()->execute();
41 }
42
43 /**
44 * Get an API action object which resolves the list of records for this batch.
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() {
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)) {
60 $params['select'] = $this->getSelect();
61 }
62 return \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4] + $params);
63 }
64
65 /**
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[]
71 */
72 protected function getSelect() {
73 return CoreUtil::getInfoItem($this->getEntityName(), 'primary_key');
74 }
75
76 }