Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
f299f7db | 7 | | Copyright CiviCRM LLC (c) 2004-2020 | |
380f3545 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
f299f7db | 32 | * @copyright CiviCRM LLC (c) 2004-2020 |
380f3545 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | ||
19b53e5b C |
38 | namespace Civi\Api4\Generic; |
39 | ||
40 | /** | |
41 | * Update one or more records with new values. | |
42 | * | |
43 | * Use the where clause (required) to select them. | |
44 | */ | |
45 | class DAOUpdateAction extends AbstractUpdateAction { | |
46 | use Traits\DAOActionTrait; | |
47 | ||
48 | /** | |
49 | * Criteria for selecting items to update. | |
50 | * | |
51 | * Required if no id is supplied in values. | |
52 | * | |
53 | * @var array | |
54 | */ | |
55 | protected $where = []; | |
56 | ||
57 | /** | |
58 | * @inheritDoc | |
59 | */ | |
60 | public function _run(Result $result) { | |
61 | // Add ID from values to WHERE clause and check for mismatch | |
62 | if (!empty($this->values['id'])) { | |
63 | $wheres = array_column($this->where, NULL, 0); | |
64 | if (!isset($wheres['id'])) { | |
65 | $this->addWhere('id', '=', $this->values['id']); | |
66 | } | |
67 | elseif (!($wheres['id'][1] === '=' && $wheres['id'][2] == $this->values['id'])) { | |
68 | throw new \Exception("Cannot update the id of an existing " . $this->getEntityName() . '.'); | |
69 | } | |
70 | } | |
71 | ||
72 | // Require WHERE if we didn't get an ID from values | |
73 | if (!$this->where) { | |
74 | throw new \API_Exception('Parameter "where" is required unless an id is supplied in values.'); | |
75 | } | |
76 | ||
77 | // Update a single record by ID unless select requires more than id | |
78 | if ($this->getSelect() === ['id'] && count($this->where) === 1 && $this->where[0][0] === 'id' && $this->where[0][1] === '=' && !empty($this->where[0][2])) { | |
79 | $this->values['id'] = $this->where[0][2]; | |
80 | $result->exchangeArray($this->writeObjects([$this->values])); | |
81 | return; | |
82 | } | |
83 | ||
84 | // Batch update 1 or more records based on WHERE clause | |
85 | $items = $this->getObjects(); | |
86 | foreach ($items as &$item) { | |
87 | $item = $this->values + $item; | |
88 | } | |
89 | ||
90 | if (!$items) { | |
91 | throw new \API_Exception('Cannot ' . $this->getActionName() . ' ' . $this->getEntityName() . ', no records found with ' . $this->whereClauseToString()); | |
92 | } | |
93 | ||
94 | $result->exchangeArray($this->writeObjects($items)); | |
95 | } | |
96 | ||
97 | } |