APIv4 - Deprecate and stop using PreSaveSubscriber
[civicrm-core.git] / Civi / Api4 / Event / Subscriber / Generic / PreSaveSubscriber.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\Event\Subscriber\Generic;
14
15 use Civi\API\Event\PrepareEvent;
16 use Civi\Api4\Generic\AbstractAction;
17 use Civi\Api4\Generic\AbstractCreateAction;
18 use Civi\Api4\Generic\AbstractUpdateAction;
19
20 /**
21 * @deprecated
22 */
23 abstract class PreSaveSubscriber extends AbstractPrepareSubscriber {
24
25 /**
26 * @var string
27 * create|update|both
28 */
29 public $supportedOperation = 'both';
30
31 /**
32 * @param \Civi\API\Event\PrepareEvent $event
33 */
34 public function onApiPrepare(PrepareEvent $event) {
35 $apiRequest = $event->getApiRequest();
36
37 if ($apiRequest instanceof AbstractAction && $this->applies($apiRequest)) {
38 \CRM_Core_Error::deprecatedWarning("Use of APIv4 'PreSaveSubscriber' is deprecated. '" . get_class($this) . "' should be removed ({$apiRequest->getEntityName()}::{$apiRequest->getActionName()}).");
39 if (
40 ($apiRequest instanceof AbstractCreateAction && $this->supportedOperation !== 'update') ||
41 ($apiRequest instanceof AbstractUpdateAction && $this->supportedOperation !== 'create')
42 ) {
43 $values = $apiRequest->getValues();
44 $this->modify($values, $apiRequest);
45 $apiRequest->setValues($values);
46 }
47 }
48 }
49
50 /**
51 * Modify the item about to be saved
52 *
53 * @param array $item
54 * @param \Civi\Api4\Generic\AbstractAction $request
55 *
56 */
57 abstract protected function modify(&$item, AbstractAction $request);
58
59 /**
60 * Check if this subscriber should be applied to the request
61 *
62 * @param \Civi\Api4\Generic\AbstractAction $request
63 *
64 * @return bool
65 */
66 abstract protected function applies(AbstractAction $request);
67
68 }