Skip to content

Commit

Permalink
Merge pull request #22403 from colemanw/api4WriteRecords
Browse files Browse the repository at this point in the history
[REF] Improve APIv4 save functions
  • Loading branch information
eileenmcnaughton authored Jan 13, 2022
2 parents f03499d + 5a44345 commit 4526b69
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 59 deletions.
8 changes: 5 additions & 3 deletions Civi/Api4/Action/Address/AddressSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ trait AddressSaveTrait {
/**
* @inheritDoc
*/
protected function writeObjects(&$items) {
foreach ($items as &$item) {
protected function write(array $items) {
$saved = [];
foreach ($items as $item) {
if ($this->streetParsing && !empty($item['street_address'])) {
$item = array_merge($item, \CRM_Core_BAO_Address::parseStreetAddress($item['street_address']));
}
$item['skip_geocode'] = $this->skipGeocode;
$saved[] = \CRM_Core_BAO_Address::add($item, $this->fixAddress);
}
return parent::writeObjects($items);
return $saved;
}

}
17 changes: 8 additions & 9 deletions Civi/Api4/Action/CiviCase/CiviCaseSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@
trait CiviCaseSaveTrait {

/**
* @param array $cases
* @param array $items
* @return array
*/
protected function writeObjects(&$cases) {
$cases = array_values($cases);
$result = parent::writeObjects($cases);

// If the case doesn't have an id, it's new & needs to be opened.
foreach ($cases as $idx => $case) {
protected function write(array $items) {
$saved = [];
foreach ($items as $case) {
$saved[] = $result = \CRM_Case_BAO_Case::create($case);
// If the case doesn't have an id, it's new & needs to be opened.
if (empty($case['id'])) {
$this->openCase($case, $result[$idx]['id']);
$this->openCase($case, $result->id);
}
}
return $result;
return $saved;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions Civi/Api4/Action/Contact/Save.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Action\Contact;

/**
* @inheritDoc
*/
class Save extends \Civi\Api4\Generic\DAOSaveAction {

/**
* @param array $items
* @return array
*/
protected function write(array $items) {
$saved = [];
foreach ($items as $item) {
// For some reason the contact BAO requires this for updates
if (!empty($item['id']) && !\CRM_Utils_System::isNull($item['id'])) {
$item['contact_id'] = $item['id'];
}
$saved[] = \CRM_Contact_BAO_Contact::create($item);
}
return $saved;
}

}
34 changes: 34 additions & 0 deletions Civi/Api4/Action/Contact/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Action\Contact;

/**
* @inheritDoc
*/
class Update extends \Civi\Api4\Generic\DAOUpdateAction {

/**
* @param array $items
* @return array
*/
protected function write(array $items) {
$saved = [];
foreach ($items as $item) {
// For some reason the contact BAO requires this for updates
$item['contact_id'] = $item['id'];
$saved[] = \CRM_Contact_BAO_Contact::create($item);
}
return $saved;
}

}
21 changes: 21 additions & 0 deletions Civi/Api4/Action/EntityTag/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Action\EntityTag;

/**
* @inheritDoc
*/
class Create extends \Civi\Api4\Generic\DAOCreateAction {
use EntityTagSaveTrait;

}
34 changes: 34 additions & 0 deletions Civi/Api4/Action/EntityTag/EntityTagSaveTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Action\EntityTag;

/**
* @inheritDoc
*/
trait EntityTagSaveTrait {

/**
* Override method which defaults to 'create' for oddball DAO which uses 'add'
*
* @param array $items
* @return array
*/
protected function write(array $items) {
$saved = [];
foreach ($items as $item) {
$saved[] = \CRM_Core_BAO_EntityTag::add($item);
}
return $saved;
}

}
21 changes: 21 additions & 0 deletions Civi/Api4/Action/EntityTag/Save.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Action\EntityTag;

/**
* @inheritDoc
*/
class Save extends \Civi\Api4\Generic\DAOSaveAction {
use EntityTagSaveTrait;

}
21 changes: 21 additions & 0 deletions Civi/Api4/Action/EntityTag/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Action\EntityTag;

/**
* @inheritDoc
*/
class Update extends \Civi\Api4\Generic\DAOUpdateAction {
use EntityTagSaveTrait;

}
4 changes: 2 additions & 2 deletions Civi/Api4/Action/GroupContact/GroupContactSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ trait GroupContactSaveTrait {
/**
* @inheritDoc
*/
protected function writeObjects(&$items) {
protected function write(array $items) {
foreach ($items as &$item) {
$item['method'] = $this->method;
$item['tracking'] = $this->tracking;
}
return parent::writeObjects($items);
return \CRM_Contact_BAO_GroupContact::writeRecords($items);
}

}
18 changes: 18 additions & 0 deletions Civi/Api4/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@
*/
class Contact extends Generic\DAOEntity {

/**
* @param bool $checkPermissions
* @return Action\Contact\Update
*/
public static function update($checkPermissions = TRUE) {
return (new Action\Contact\Update(__CLASS__, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

/**
* @param bool $checkPermissions
* @return Action\Contact\Save
*/
public static function save($checkPermissions = TRUE) {
return (new Action\Contact\Save(__CLASS__, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

/**
* @param bool $checkPermissions
* @return Action\Contact\Delete
Expand Down
27 changes: 27 additions & 0 deletions Civi/Api4/EntityTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,31 @@
class EntityTag extends Generic\DAOEntity {
use Generic\Traits\EntityBridge;

/**
* @param bool $checkPermissions
* @return Action\EntityTag\Create
*/
public static function create($checkPermissions = TRUE) {
return (new Action\EntityTag\Create('EntityTag', __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

/**
* @param bool $checkPermissions
* @return Action\EntityTag\Save
*/
public static function save($checkPermissions = TRUE) {
return (new Action\EntityTag\Save('EntityTag', __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

/**
* @param bool $checkPermissions
* @return Action\EntityTag\Update
*/
public static function update($checkPermissions = TRUE) {
return (new Action\EntityTag\Update('EntityTag', __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

}
2 changes: 1 addition & 1 deletion Civi/Api4/Generic/Traits/CustomValueActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function isAuthorized(): bool {
/**
* @inheritDoc
*/
protected function writeObjects(&$items) {
protected function writeObjects($items) {
$fields = $this->entityFields();
foreach ($items as $idx => $item) {
FormattingUtil::formatWriteParams($item, $fields);
Expand Down
Loading

0 comments on commit 4526b69

Please sign in to comment.