From 64d24a642ae7e8ce9c18938b4bba3855b6fbb743 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 20 Oct 2014 10:01:50 +1300 Subject: [PATCH] RM-15491 fix membership_status api to be more appropriate on update --- CRM/Contribute/BAO/Contribution.php | 14 +------ CRM/Core/DAO.php | 18 ++++++++ CRM/Member/BAO/MembershipStatus.php | 42 ++++++++++++------- CRM/Price/BAO/PriceFieldValue.php | 14 +------ api/v3/MembershipStatus.php | 10 ----- .../phpunit/api/v3/SyntaxConformanceTest.php | 1 - 6 files changed, 47 insertions(+), 52 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 61aedbb19b..268187dd22 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -136,7 +136,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { //set defaults in create mode if (!$contributionID) { - self::setDefaults($params); + CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); self::calculateMissingAmountParams($params); } @@ -214,20 +214,8 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { ), 'contribution_status_id' => CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'), ); - } - /** - * Set defaults when creating new entity - * @param $params - */ - static function setDefaults(&$params) { - foreach (self::getDefaults() as $key => $value) { - if (empty($params[$key])) { - $params[$key] = $value; - } - } - } /** * Given the list of params in the params array, fetch the object * and store the values in the values array diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 3bd298dcd6..c0f9c5b055 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1666,6 +1666,24 @@ SELECT contact_id } } + /** + * Set defaults when creating new entity + * (don't call this set defaults as already in use with different signature in some places) + * + * @param $params + * @param $defaults + */ + static function setCreateDefaults(&$params, $defaults) { + if (isset($params['id'])) { + return; + } + foreach ($defaults as $key => $value) { + if (!array_key_exists($key, $params) || $params[$key] === NULL) { + $params[$key] = $value; + } + } + } + /** * @param string $prefix * @param bool $addRandomString diff --git a/CRM/Member/BAO/MembershipStatus.php b/CRM/Member/BAO/MembershipStatus.php index 5c1c8f35b7..e0f424299b 100644 --- a/CRM/Member/BAO/MembershipStatus.php +++ b/CRM/Member/BAO/MembershipStatus.php @@ -122,40 +122,52 @@ class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus { * @return object */ static function add(&$params, $ids = array()) { - $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); - $params['is_current_member'] = CRM_Utils_Array::value('is_current_member', $params, FALSE); - $params['is_admin'] = CRM_Utils_Array::value('is_admin', $params, FALSE); - $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); + $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membershipStatus', $ids)); + if (!$id) { + CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); + //copy name to label when not passed. + if (empty($params['label']) && !empty($params['name'])) { + $params['label'] = $params['name']; + } + + if (empty($params['name']) && !empty($params['label'])) { + $params['name'] = $params['label']; + } + } // set all other defaults to false. - if ($params['is_default']) { + if (!empty($params['is_default'])) { $query = "UPDATE civicrm_membership_status SET is_default = 0"; CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray ); } - //copy name to label when not passed. - if (empty($params['label']) && !empty($params['name'])) { - $params['label'] = $params['name']; - } - //for add mode, copy label to name. - $statusId = !empty($params['id']) ? $params['id'] : CRM_Utils_Array::value('membershipStatus', $ids); - if (!$statusId && !empty($params['label']) && empty($params['name'])) { - $params['name'] = $params['label']; - } // action is taken depending upon the mode $membershipStatus = new CRM_Member_DAO_MembershipStatus(); $membershipStatus->copyValues($params); - $membershipStatus->id = $statusId; + $membershipStatus->id = $id; $membershipStatus->save(); return $membershipStatus; } + /** + * Get defaults for new entity + * @return array + */ + static function getDefaults() { + return array( + 'is_active' => FALSE, + 'is_current_member' => FALSE, + 'is_admin' => FALSE, + 'is_default' => FALSE, + ); + } + /** * Function to get membership status * diff --git a/CRM/Price/BAO/PriceFieldValue.php b/CRM/Price/BAO/PriceFieldValue.php index 57a9d91cb9..73a22e9406 100644 --- a/CRM/Price/BAO/PriceFieldValue.php +++ b/CRM/Price/BAO/PriceFieldValue.php @@ -101,7 +101,7 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { } else { if (!$id) { - self::setDefaults($params); + CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); if (empty($params['name'])) { $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64); } @@ -122,18 +122,6 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { } - /** - * Set defaults when creating new entity - * @param $params - */ - static function setDefaults(&$params) { - foreach (self::getDefaults() as $key => $value) { - if (empty($params[$key])) { - $params[$key] = $value; - } - } - } - /** * Takes a bunch of params that are needed to match certain criteria and * retrieves the relevant objects. diff --git a/api/v3/MembershipStatus.php b/api/v3/MembershipStatus.php index ddaf0bc6e1..82490d371c 100644 --- a/api/v3/MembershipStatus.php +++ b/api/v3/MembershipStatus.php @@ -52,16 +52,6 @@ function civicrm_api3_membership_status_create($params) { return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); } -/** - * Adjust Metadata for Create action - * - * The metadata is used for setting defaults, documentation & validation - * @param array $params array or parameters determined by getfields - */ -function _civicrm_api3_membership_status_create_spec(&$params) { - $params['name']['api.aliases'] = array('label'); -} - /** * Get a membership status. * diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index ca1243984f..3593ffbad2 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -321,7 +321,6 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { 'Note', 'OptionGroup', 'Membership', - 'MembershipStatus', 'Group', 'GroupOrganization', 'GroupNesting', -- 2.25.1