* $batch batch object
*/
public static function create(&$params) {
- $op = 'edit';
- $batchId = $params['id'] ?? NULL;
- if (!$batchId) {
- $op = 'create';
+ if (empty($params['id']) && empty($params['name'])) {
$params['name'] = CRM_Utils_String::titleToVar($params['title']);
}
- CRM_Utils_Hook::pre($op, 'Batch', $batchId, $params);
- $batch = new CRM_Batch_DAO_Batch();
- $batch->copyValues($params);
- $batch->save();
-
- CRM_Utils_Hook::post($op, 'Batch', $batch->id, $batch);
-
- return $batch;
+ return self::writeRecord($params);
}
/**
}
if (empty($params['id'])) {
-
if (empty($params['created_id'])) {
- $session = CRM_Core_Session::singleton();
- $params['created_id'] = $session->get('userID');
+ $params['created_id'] = CRM_Core_Session::getLoggedInContactID();
}
if (empty($params['created_date'])) {
if (empty($params['name'])) {
$params['name'] = CRM_Utils_String::titleToVar($params['title'], 64);
}
-
- CRM_Utils_Hook::pre('create', 'Campaign', NULL, $params);
- }
- else {
- CRM_Utils_Hook::pre('edit', 'Campaign', $params['id'], $params);
}
- $campaign = new CRM_Campaign_DAO_Campaign();
- $campaign->copyValues($params);
- $campaign->save();
-
- if (!empty($params['id'])) {
- CRM_Utils_Hook::post('edit', 'Campaign', $campaign->id, $campaign);
- }
- else {
- CRM_Utils_Hook::post('create', 'Campaign', $campaign->id, $campaign);
- }
+ $campaign = self::writeRecord($params);
/* Create the campaign group record */
-
$groupTableName = CRM_Contact_BAO_Group::getTableName();
if (isset($params['groups']) && !empty($params['groups']['include']) && is_array($params['groups']['include'])) {
}
//store custom data
- if (!empty($params['custom']) &&
- is_array($params['custom'])
- ) {
+ if (!empty($params['custom']) && is_array($params['custom'])) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_campaign', $campaign->id);
}
CRM_Core_DAO::executeQuery($query);
}
- if (!(CRM_Utils_Array::value('id', $params))) {
-
- if (!(CRM_Utils_Array::value('created_id', $params))) {
- $session = CRM_Core_Session::singleton();
- $params['created_id'] = $session->get('userID');
+ if (empty($params['id'])) {
+ if (empty($params['created_id'])) {
+ $params['created_id'] = CRM_Core_Session::getLoggedInContactID();
}
- if (!(CRM_Utils_Array::value('created_date', $params))) {
+
+ if (empty($params['created_date'])) {
$params['created_date'] = date('YmdHis');
}
-
- CRM_Utils_Hook::pre('create', 'Survey', NULL, $params);
}
- else {
- CRM_Utils_Hook::pre('edit', 'Survey', $params['id'], $params);
- }
-
- $dao = new CRM_Campaign_DAO_Survey();
- $dao->copyValues($params);
- $dao->save();
- if (!empty($params['id'])) {
- CRM_Utils_Hook::post('edit', 'Survey', $dao->id, $dao);
- }
- else {
- CRM_Utils_Hook::post('create', 'Survey', $dao->id, $dao);
- }
+ $dao = self::writeRecord($params);
- if (!empty($params['custom']) &&
- is_array($params['custom'])
- ) {
+ if (!empty($params['custom']) && is_array($params['custom'])) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_survey', $dao->id);
}
return $dao;
* @return CRM_Case_BAO_CaseContact
*/
public static function create($params) {
- $hook = empty($params['id']) ? 'create' : 'edit';
- CRM_Utils_Hook::pre($hook, 'CaseContact', CRM_Utils_Array::value('id', $params), $params);
-
- $caseContact = new self();
- $caseContact->copyValues($params);
- $caseContact->save();
-
- CRM_Utils_Hook::post($hook, 'CaseContact', $caseContact->id, $caseContact);
+ $caseContact = self::writeRecord($params);
// add to recently viewed
$caseType = CRM_Case_BAO_Case::getCaseType($caseContact->case_id);
}
// action is taken depending upon the mode
- $relationshipType = new CRM_Contact_DAO_RelationshipType();
-
- $hook = empty($params['id']) ? 'create' : 'edit';
- CRM_Utils_Hook::pre($hook, 'RelationshipType', CRM_Utils_Array::value('id', $params), $params);
-
- $relationshipType->copyValues($params);
- $relationshipType->save();
-
- CRM_Utils_Hook::post($hook, 'RelationshipType', $relationshipType->id, $relationshipType);
+ $relationshipType = self::writeRecord($params);
CRM_Core_PseudoConstant::relationshipType('label', TRUE);
CRM_Core_PseudoConstant::relationshipType('name', TRUE);
* @return array
*/
public static function create($params) {
- $actionLog = new CRM_Core_DAO_ActionLog();
+ $params['action_date_time'] = $params['action_date_time'] ?? date('YmdHis');
- $params['action_date_time'] = CRM_Utils_Array::value('action_date_time', $params, date('YmdHis'));
-
- $actionLog->copyValues($params);
-
- $edit = (bool) $actionLog->id;
- if ($edit) {
- CRM_Utils_Hook::pre('edit', 'ActionLog', $actionLog->id, $actionLog);
- }
- else {
- CRM_Utils_Hook::pre('create', 'ActionLog', NULL, $actionLog);
- }
-
- $actionLog->save();
-
- if ($edit) {
- CRM_Utils_Hook::post('edit', 'ActionLog', $actionLog->id, $actionLog);
- }
- else {
- CRM_Utils_Hook::post('create', 'ActionLog', NULL, $actionLog);
- }
-
- return $actionLog;
+ return self::writeRecord($params);
}
}
// Ensure mysql phone function exists
CRM_Core_DAO::checkSqlFunctionsExist();
- $hook = empty($params['id']) ? 'create' : 'edit';
- CRM_Utils_Hook::pre($hook, 'Phone', $params['id'] ?? NULL, $params);
-
- $phone = new CRM_Core_DAO_Phone();
- $phone->copyValues($params);
- $phone->save();
-
- CRM_Utils_Hook::post($hook, 'Phone', $phone->id, $phone);
- return $phone;
+ return self::writeRecord($params);
}
/**
}
/**
- * Add pledgeBlock.
+ * Add or update pledgeBlock.
*
* @param array $params
- * Reference array contains the values submitted by the form.
- *
*
* @return object
*/
- public static function add(&$params) {
-
- if (!empty($params['id'])) {
- CRM_Utils_Hook::pre('edit', 'PledgeBlock', $params['id'], $params);
- }
- else {
- CRM_Utils_Hook::pre('create', 'PledgeBlock', NULL, $params);
- }
-
- $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
-
- // fix for pledge_frequency_unit
- $freqUnits = $params['pledge_frequency_unit'] ?? NULL;
-
- if ($freqUnits && is_array($freqUnits)) {
- unset($params['pledge_frequency_unit']);
- $newFreqUnits = array();
- foreach ($freqUnits as $k => $v) {
- if ($v) {
- $newFreqUnits[$k] = $v;
- }
- }
-
- $freqUnits = $newFreqUnits;
- if (is_array($freqUnits) && !empty($freqUnits)) {
- $freqUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($freqUnits));
- $pledgeBlock->pledge_frequency_unit = $freqUnits;
- }
- else {
- $pledgeBlock->pledge_frequency_unit = '';
- }
+ public static function add($params) {
+ // FIXME: This is assuming checkbox input like ['foo' => 1, 'bar' => 0, 'baz' => 1]. Not API friendly.
+ if (!empty($params['pledge_frequency_unit']) && is_array($params['pledge_frequency_unit'])) {
+ $params['pledge_frequency_unit'] = array_keys(array_filter($params['pledge_frequency_unit']));
}
-
- $pledgeBlock->copyValues($params);
- $result = $pledgeBlock->save();
-
- if (!empty($params['id'])) {
- CRM_Utils_Hook::post('edit', 'PledgeBlock', $pledgeBlock->id, $pledgeBlock);
- }
- else {
- CRM_Utils_Hook::post('create', 'Pledge', $pledgeBlock->id, $pledgeBlock);
- }
-
- return $result;
+ return self::writeRecord($params);
}
/**
* pledge payment id
*/
public static function add($params) {
- if (!empty($params['id'])) {
- CRM_Utils_Hook::pre('edit', 'PledgePayment', $params['id'], $params);
- }
- else {
- CRM_Utils_Hook::pre('create', 'PledgePayment', NULL, $params);
- }
-
- $payment = new CRM_Pledge_DAO_PledgePayment();
- $payment->copyValues($params);
-
// set currency for CRM-1496
- if (!isset($payment->currency)) {
- $payment->currency = CRM_Core_Config::singleton()->defaultCurrency;
+ if (empty($params['id']) && !isset($params['currency'])) {
+ $params['currency'] = CRM_Core_Config::singleton()->defaultCurrency;
}
-
- $result = $payment->save();
-
- if (!empty($params['id'])) {
- CRM_Utils_Hook::post('edit', 'PledgePayment', $payment->id, $payment);
- }
- else {
- CRM_Utils_Hook::post('create', 'PledgePayment', $payment->id, $payment);
- }
-
- return $result;
+ return self::writeRecord($params);
}
/**
$pledgeFrequencyUnit = [
'week' => 1,
- 'month' => 1,
+ 'month' => 0,
'year' => 1,
];
+ $pledgeFrequencySerialized = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys(array_filter($pledgeFrequencyUnit)));
$params = [
'entity_id' => $this->_contributionPageId,
// check for add pledge block
$pledgeBlock = CRM_Pledge_BAO_PledgeBlock::add($params);
+ // This param is expected to get serialized
+ $params['pledge_frequency_unit'] = $pledgeFrequencySerialized;
foreach ($params as $param => $value) {
$this->assertEquals($value, $pledgeBlock->$param);
}
// also check for edit pledge block
$pledgeBlock = CRM_Pledge_BAO_PledgeBlock::add($params);
+ // This param is expected to get serialized
+ $params['pledge_frequency_unit'] = $pledgeFrequencySerialized;
foreach ($params as $param => $value) {
$this->assertEquals($value, $pledgeBlock->$param);
}
'month' => 1,
'year' => 1,
];
+ $pledgeFrequencySerialized = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys(array_filter($pledgeFrequencyUnit)));
$params = [
'entity_id' => $this->_contributionPageId,
// use getPledgeBlock() method
$getPledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_contributionPageId);
+ // This param is expected to get serialized
+ $params['pledge_frequency_unit'] = $pledgeFrequencySerialized;
// check on both retrieve and getPledgeBlock values
foreach ($params as $param => $value) {
$this->assertEquals($value, $retrievePledgeBlock->$param);