From: Seamus Lee Date: Tue, 11 Apr 2017 00:43:44 +0000 (+1000) Subject: CRM-20410 Reformat CRM_SMS_BAO_Provider to use standard create function style rather... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=fddb91130d6d57b30446500b133ca56d3532862f;p=civicrm-core.git CRM-20410 Reformat CRM_SMS_BAO_Provider to use standard create function style rather than the split Fix posthook --- diff --git a/CRM/SMS/BAO/Provider.php b/CRM/SMS/BAO/Provider.php index 4d34704501..24243c152e 100644 --- a/CRM/SMS/BAO/Provider.php +++ b/CRM/SMS/BAO/Provider.php @@ -88,30 +88,41 @@ class CRM_SMS_BAO_Provider extends CRM_SMS_DAO_Provider { } /** - * Save a new record into the database - * @todo create a create function to do this work - * @param $values + * Create or Update an SMS provider + * @param array $params + * @param array $ids to update + * @return array saved values */ - public static function saveRecord($values) { - $values['domain_id'] = CRM_Utils_Array::value('domain_id', $values, CRM_Core_Config::domainID()); - $dao = new CRM_SMS_DAO_Provider(); - $dao->copyValues($values); - $dao->save(); - } + public static function create($params, $ids = array()) { + $id = CRM_Utils_Array::value('id', $ids, CRM_Utils_Array::value('id', $params)); - /** - * Update an SMS provider in the database. - * @todo combine with saveRecord in a create function - * @param $values - * @param int $providerId - */ - public static function updateRecord($values, $providerId) { - $dao = new CRM_SMS_DAO_Provider(); - $dao->id = $providerId; - if ($dao->find(TRUE)) { - $dao->copyValues($values); - $dao->save(); + if ($id) { + CRM_Utils_Hook::pre('edit', 'SmsProvider', $id, $params); + } + else { + CRM_Utils_Hook::pre('create', 'SmsProvider', NULL, $params); + } + + $provider = new static(); + if ($id) { + $provider->id = $id; + $provider->find(TRUE); + } + if ($id) { + $provider->domain_id = CRM_Utils_Array::value('domain_id', $params, $provider->domain_id); + } + else { + $provider->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); + } + $provider->copyValues($params); + $result = $provider->save(); + if ($id) { + CRM_Utils_Hook::post('edit', 'SmsProvider', $provider->id, $provider); + } + else { + CRM_Utils_Hook::post('create', 'SmsProvider', NULL, $provider); } + return $result; } /** diff --git a/CRM/SMS/Form/Provider.php b/CRM/SMS/Form/Provider.php index bbc86f6103..cd3084db4c 100644 --- a/CRM/SMS/Form/Provider.php +++ b/CRM/SMS/Form/Provider.php @@ -171,11 +171,14 @@ class CRM_SMS_Form_Provider extends CRM_Core_Form { $recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0); $recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0); - if ($this->_action & CRM_Core_Action::UPDATE) { - CRM_SMS_BAO_Provider::updateRecord($recData, $this->_id); - } - elseif ($this->_action & CRM_Core_Action::ADD) { - CRM_SMS_BAO_Provider::saveRecord($recData); + if ($this->_action && (CRM_Core_Action::UPDATE || CRM_Core_Action::ADD)) { + if (!empty($this->_id)) { + $ids = array('id' => $this->_id); + } + else { + $ids = array(); + } + CRM_SMS_BAO_Provider::create($recData, $ids); } } diff --git a/tests/phpunit/CRM/SMS/BAO/ProviderTest.php b/tests/phpunit/CRM/SMS/BAO/ProviderTest.php index c4fb26452e..db155e54c9 100644 --- a/tests/phpunit/CRM/SMS/BAO/ProviderTest.php +++ b/tests/phpunit/CRM/SMS/BAO/ProviderTest.php @@ -64,12 +64,12 @@ class CRM_SMS_BAO_ProviderTest extends CiviUnitTestCase { 'is_active' => 1, 'api_type' => 1, ); - CRM_SMS_BAO_Provider::saveRecord($values); + CRM_SMS_BAO_Provider::create($values, array()); $provider = $this->callAPISuccess('SmsProvider', 'getsingle', array('title' => 'test SMS provider')); $domain_id = CRM_Core_DAO::getFieldValue('CRM_SMS_DAO_Provider', $provider['id'], 'domain_id'); $this->assertNull($domain_id); $values2 = array('title' => 'Test SMS Provider2'); - CRM_SMS_BAO_Provider::updateRecord($values2, $provider['id']); + CRM_SMS_BAO_Provider::create($values2, array('id' => $provider['id'])); $provider = $this->callAPISuccess('SmsProvider', 'getsingle', array('id' => $provider['id'])); $this->assertEquals('Test SMS Provider2', $provider['title']); $domain_id = CRM_Core_DAO::getFieldValue('CRM_SMS_DAO_Provider', $provider['id'], 'domain_id'); @@ -89,7 +89,7 @@ class CRM_SMS_BAO_ProviderTest extends CiviUnitTestCase { 'is_active' => 1, 'api_type' => 1, ); - CRM_SMS_BAO_Provider::saveRecord($values); + CRM_SMS_BAO_Provider::create($values); $provider = $this->callAPISuccess('SmsProvider', 'getsingle', array('title' => 'test SMS provider')); $domain_id = CRM_Core_DAO::getFieldValue('CRM_SMS_DAO_Provider', $provider['id'], 'domain_id'); $this->assertEquals(CRM_Core_Config::domainID(), $domain_id);