From 5afcf7069c68dc602fb29f8f4242216cdf82ab35 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Tue, 22 May 2018 15:31:37 +0530 Subject: [PATCH] dev/membership#4 - Admin Membership type is displayed on Public contribution page --- CRM/Member/BAO/MembershipType.php | 60 ++++++++++---------- templates/CRM/Member/Form/MembershipType.tpl | 2 +- tests/phpunit/api/v3/MembershipTypeTest.php | 36 +++++++++++- 3 files changed, 63 insertions(+), 35 deletions(-) diff --git a/CRM/Member/BAO/MembershipType.php b/CRM/Member/BAO/MembershipType.php index 64fb342d47..85b307dcd1 100644 --- a/CRM/Member/BAO/MembershipType.php +++ b/CRM/Member/BAO/MembershipType.php @@ -807,41 +807,39 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { * @param array $params */ public static function updateAllPriceFieldValue($membershipTypeId, $params) { - $updateFields = array(); - if (!empty($params['minimum_fee'])) { - $amount = $params['minimum_fee']; - } - else { - $amount = 0; - } - - $updateValues = array( - 2 => array('financial_type_id', 'financial_type_id', 'Integer'), - 3 => array('label', 'name', 'String'), - 4 => array('amount', 'minimum_fee', 'Float'), - 5 => array('description', 'description', 'String'), + $defaults = array(); + $fieldsToUpdate = array( + 'financial_type_id' => 'financial_type_id', + 'name' => 'label', + 'minimum_fee' => 'amount', + 'description' => 'description', + 'visibility' => 'visibility_id', ); - - $queryParams = array(1 => array($membershipTypeId, 'Integer')); - foreach ($updateValues as $key => $value) { - if (array_key_exists($value[1], $params)) { - $updateFields[] = "cpfv." . $value[0] . " = %$key"; - if ($value[1] == 'minimum_fee') { - $fieldValue = $amount; - } - else { - $fieldValue = $params[$value[1]]; + $priceFieldValueBAO = new CRM_Price_BAO_PriceFieldValue(); + $priceFieldValueBAO->membership_type_id = $membershipTypeId; + $priceFieldValueBAO->find(); + while ($priceFieldValueBAO->fetch()) { + $updateParams = array( + 'id' => $priceFieldValueBAO->id, + 'price_field_id' => $priceFieldValueBAO->price_field_id, + ); + //Get priceset details. + $fieldParams = array('fid' => $priceFieldValueBAO->price_field_id); + $setID = CRM_Price_BAO_PriceSet::getSetId($fieldParams); + $setParams = array('id' => $setID); + $setValues = CRM_Price_BAO_PriceSet::retrieve($setParams, $defaults); + if (!empty($setValues->is_quick_config) && $setValues->name != 'default_membership_type_amount') { + foreach ($fieldsToUpdate as $key => $value) { + if ($value == 'visibility_id' && !empty($params['visibility'])) { + $updateParams['visibility_id'] = CRM_Price_BAO_PriceField::getVisibilityOptionID(strtolower($params['visibility'])); + } + else { + $updateParams[$value] = CRM_Utils_Array::value($key, $params); + } } - $queryParams[$key] = array($fieldValue, $value[2]); + CRM_Price_BAO_PriceFieldValue::add($updateParams); } } - - $query = "UPDATE `civicrm_price_field_value` cpfv -INNER JOIN civicrm_price_field cpf on cpf.id = cpfv.price_field_id -INNER JOIN civicrm_price_set cps on cps.id = cpf.price_set_id -SET " . implode(' , ', $updateFields) . " WHERE cpfv.membership_type_id = %1 -AND cps.is_quick_config = 1 AND cps.name != 'default_membership_type_amount'"; - CRM_Core_DAO::executeQuery($query, $queryParams); } } diff --git a/templates/CRM/Member/Form/MembershipType.tpl b/templates/CRM/Member/Form/MembershipType.tpl index 3c6bc9a3d0..1e679fcc9f 100644 --- a/templates/CRM/Member/Form/MembershipType.tpl +++ b/templates/CRM/Member/Form/MembershipType.tpl @@ -128,7 +128,7 @@ {$form.visibility.label} {$form.visibility.html}
- {ts}Is this membership type available for self-service signups ('Public') or assigned by CiviCRM 'staff' users only ('Admin'){/ts} + {ts}Can this membership type be used for self-service signups ('Public'), or is it only for CiviCRM users with 'Edit Contributions' permission ('Admin').{/ts} diff --git a/tests/phpunit/api/v3/MembershipTypeTest.php b/tests/phpunit/api/v3/MembershipTypeTest.php index a3731534a0..af809934e5 100644 --- a/tests/phpunit/api/v3/MembershipTypeTest.php +++ b/tests/phpunit/api/v3/MembershipTypeTest.php @@ -258,10 +258,14 @@ class api_v3_MembershipTypeTest extends CiviUnitTestCase { public function testEnableMembershipTypeOnContributionPage() { $memType = array(); $memType[1] = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'minimum_fee' => 100)); - $priceSet = $this->callAPISuccess('price_set', 'getvalue', array( - 'name' => 'default_membership_type_amount', - 'return' => 'id', + $priceSet = $this->callAPISuccess('price_set', 'create', array( + 'title' => "test priceset", + 'name' => "test_priceset", + 'extends' => "CiviMember", + 'is_quick_config' => 1, + 'financial_type_id' => "Member Dues", )); + $priceSet = $priceSet['id']; $field = $this->callAPISuccess('price_field', 'create', array( 'price_set_id' => $priceSet, 'name' => 'membership_amount', @@ -297,6 +301,32 @@ class api_v3_MembershipTypeTest extends CiviUnitTestCase { $priceField = CRM_Price_BAO_PriceField::create($fieldParams); $this->assertEquals($priceField->id, $fieldParams['id']); + //Update membership type name and visibility + $updateParams = array( + 'id' => $memType[1], + 'name' => 'General - Edited', + 'visibility' => 'Admin', + 'financial_type_id' => 1, + 'minimum_fee' => 300, + 'description' => 'Test edit description', + ); + $this->callAPISuccess('membership_type', 'create', $updateParams); + $priceFieldValue = $this->callAPISuccess('PriceFieldValue', 'get', array( + 'sequential' => 1, + 'membership_type_id' => $memType[1], + )); + //Verify if membership type updates are copied to pricefield value. + foreach ($priceFieldValue['values'] as $key => $value) { + $setId = $this->callAPISuccessGetValue('PriceField', array('return' => "price_set_id", 'id' => $value['price_field_id'])); + if ($setId == $priceSet) { + $this->assertEquals($value['label'], $updateParams['name']); + $this->assertEquals($value['description'], $updateParams['description']); + $this->assertEquals((int) $value['amount'], $updateParams['minimum_fee']); + $this->assertEquals($value['financial_type_id'], $updateParams['financial_type_id']); + $this->assertEquals($value['visibility_id'], CRM_Price_BAO_PriceField::getVisibilityOptionID(strtolower($updateParams['visibility']))); + } + } + foreach ($memType as $type) { $this->callAPISuccess('membership_type', 'delete', array('id' => $type)); } -- 2.25.1