/**
* @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;
}
}
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;
}
/**
--- /dev/null
+<?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;
+ }
+
+}
--- /dev/null
+<?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;
+ }
+
+}
--- /dev/null
+<?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;
+
+}
--- /dev/null
+<?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;
+ }
+
+}
--- /dev/null
+<?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;
+
+}
--- /dev/null
+<?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;
+
+}
/**
* @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);
}
}
*/
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
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);
+ }
+
}
/**
* @inheritDoc
*/
- protected function writeObjects(&$items) {
+ protected function writeObjects($items) {
$fields = $this->entityFields();
foreach ($items as $idx => $item) {
FormattingUtil::formatWriteParams($item, $fields);
use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable;
use Civi\Api4\Utils\FormattingUtil;
use Civi\Api4\Utils\CoreUtil;
+use Civi\Api4\Utils\ReflectionUtils;
/**
* @method string getLanguage()
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
- protected function writeObjects(&$items) {
- $baoName = $this->getBaoName();
+ protected function writeObjects($items) {
$updateWeights = FALSE;
-
- // TODO: Opt-in more entities to use the new writeRecords BAO method.
- $functionNames = [
- 'Address' => 'add',
- 'CustomField' => 'writeRecords',
- 'EntityTag' => 'add',
- 'GroupContact' => 'add',
- 'Navigation' => 'writeRecords',
- 'WordReplacement' => 'writeRecords',
- ];
- $method = $functionNames[$this->getEntityName()] ?? NULL;
- if (!isset($method)) {
- $method = method_exists($baoName, 'create') ? 'create' : (method_exists($baoName, 'add') ? 'add' : 'writeRecords');
- }
-
// Adjust weights for sortable entities
if (in_array('SortableEntity', CoreUtil::getInfoItem($this->getEntityName(), 'type'))) {
$weightField = CoreUtil::getInfoItem($this->getEntityName(), 'order_by');
$this->updateWeight($item);
}
- // Skip individual processing if using writeRecords
- if ($method === 'writeRecords') {
- continue;
- }
$item['check_permissions'] = $this->getCheckPermissions();
+ }
- // For some reason the contact bao requires this
- if ($entityId && $this->getEntityName() === 'Contact') {
- $item['contact_id'] = $entityId;
- }
-
- if ($this->getEntityName() === 'Address') {
- $createResult = $baoName::$method($item, $this->fixAddress);
- }
- else {
- $createResult = $baoName::$method($item);
- }
+ // Ensure array keys start at 0
+ $items = array_values($items);
- if (!$createResult) {
+ foreach ($this->write($items) as $index => $dao) {
+ if (!$dao) {
$errMessage = sprintf('%s write operation failed', $this->getEntityName());
throw new \API_Exception($errMessage);
}
-
- $result[] = $this->baoToArray($createResult, $item);
- }
-
- // Use bulk `writeRecords` method if the BAO doesn't have a create or add method
- // TODO: reverse this from opt-in to opt-out and default to using `writeRecords` for all BAOs
- if ($method === 'writeRecords') {
- $items = array_values($items);
- foreach ($baoName::writeRecords($items) as $i => $createResult) {
- $result[] = $this->baoToArray($createResult, $items[$i]);
- }
+ $result[] = $this->baoToArray($dao, $items[$index]);
}
\CRM_Utils_API_HTMLInputCoder::singleton()->decodeRows($result);
return $result;
}
+ /**
+ * Overrideable function to save items using the appropriate BAO function
+ *
+ * @param array[] $items
+ * Items already formatted by self::writeObjects
+ * @return \CRM_Core_DAO[]
+ * Array of saved DAO records
+ */
+ protected function write(array $items) {
+ $saved = [];
+ $baoName = $this->getBaoName();
+
+ $method = method_exists($baoName, 'create') ? 'create' : (method_exists($baoName, 'add') ? 'add' : NULL);
+ // Use BAO create or add method if not deprecated
+ if ($method && !ReflectionUtils::isMethodDeprecated($baoName, $method)) {
+ foreach ($items as $item) {
+ $saved[] = $baoName::$method($item);
+ }
+ }
+ else {
+ $saved = $baoName::writeRecords($items);
+ }
+ return $saved;
+ }
+
/**
* @inheritDoc
*/