Skip to content

Commit

Permalink
DAO - Update writeRecords function to also save custom data
Browse files Browse the repository at this point in the history
Refactors a couple BAOs which use writeRecords, as they no longer need to
write the custom data themselves.
  • Loading branch information
colemanw committed Jan 7, 2022
1 parent c046fd2 commit 6c41af3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 29 deletions.
5 changes: 0 additions & 5 deletions CRM/Campaign/BAO/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ public static function create(&$params) {
}
}

//store custom data
if (!empty($params['custom']) && is_array($params['custom'])) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_campaign', $campaign->id);
}

return $campaign;
}

Expand Down
3 changes: 0 additions & 3 deletions CRM/Campaign/BAO/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ public static function create(&$params) {

$dao = self::writeRecord($params);

if (!empty($params['custom']) && is_array($params['custom'])) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_survey', $dao->id);
}
return $dao;
}

Expand Down
11 changes: 8 additions & 3 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,20 +909,25 @@ public static function getAttribute($class, $fieldName = NULL) {
* @throws \CRM_Core_Exception
*/
public static function writeRecord(array $record): CRM_Core_DAO {
$hook = empty($record['id']) ? 'create' : 'edit';
$op = empty($record['id']) ? 'create' : 'edit';
$className = CRM_Core_DAO_AllCoreTables::getCanonicalClassName(static::class);
if ($className === 'CRM_Core_DAO') {
throw new CRM_Core_Exception('Function writeRecord must be called on a subclass of CRM_Core_DAO');
}
$entityName = CRM_Core_DAO_AllCoreTables::getBriefName($className);

\CRM_Utils_Hook::pre($hook, $entityName, $record['id'] ?? NULL, $record);
\CRM_Utils_Hook::pre($op, $entityName, $record['id'] ?? NULL, $record);
$instance = new $className();
// Ensure fields exist before attempting to write to them
$values = array_intersect_key($record, self::getSupportedFields());
$instance->copyValues($values);
$instance->save();
\CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);

if (!empty($record['custom']) && is_array($record['custom'])) {
CRM_Core_BAO_CustomValueTable::store($record['custom'], static::$_tableName, $instance->id, $op);
}

\CRM_Utils_Hook::post($op, $entityName, $instance->id, $instance);

return $instance;
}
Expand Down
19 changes: 1 addition & 18 deletions CRM/Pledge/BAO/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,7 @@ public static function create(array $params): CRM_Pledge_DAO_Pledge {
}
$paymentParams['status_id'] = $params['status_id'] ?? NULL;

CRM_Utils_Hook::pre($action, 'Pledge', $params['id'] ?? NULL, $params);
$pledge = new CRM_Pledge_DAO_Pledge();

// if pledge is complete update end date as current date
if ($pledge->status_id == 1) {
$pledge->end_date = date('Ymd');
}

$pledge->copyValues($params);
$pledge->save();
CRM_Utils_Hook::post($action, 'Pledge', $pledge->id, $pledge);

// handle custom data.
if (!empty($params['custom']) &&
is_array($params['custom'])
) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_pledge', $pledge->id);
}
$pledge = self::writeRecord($params);

// skip payment stuff in edit mode
if (empty($params['id']) || $isRecalculatePledgePayment) {
Expand Down

0 comments on commit 6c41af3

Please sign in to comment.