Merge pull request #20602 from ixiam/dev#PreserveActivityTabFilter
[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 /**
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 /**
25 * Criteria for selecting $ENTITIES to process.
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 /**
50 * Get a list of records for this batch.
51 *
52 * @return array
53 */
54 protected function getBatchRecords() {
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() {
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 }
77 return \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4] + $params);
78 }
79
80 /**
81 * @return array
82 */
83 protected function getSelect() {
84 return $this->select;
85 }
86
87 }