Skip to content

Commit

Permalink
dev/membership#4 - Admin Membership type is displayed on Public contr…
Browse files Browse the repository at this point in the history
…ibution page
  • Loading branch information
Jitendra Purohit committed Jun 4, 2018
1 parent 0e4e454 commit 5afcf70
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
60 changes: 29 additions & 31 deletions CRM/Member/BAO/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,41 +807,39 @@ public static function createMembershipPriceField($params, $previousID, $members
* @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);
}

}
2 changes: 1 addition & 1 deletion templates/CRM/Member/Form/MembershipType.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
<tr class="crm-membership-type-form-block-visibility">
<td class="label">{$form.visibility.label}</td>
<td>{$form.visibility.html}<br />
<span class="description">{ts}Is this membership type available for self-service signups ('Public') or assigned by CiviCRM 'staff' users only ('Admin'){/ts}</span>
<span class="description">{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}</span>
</td>
</tr>
<tr class="crm-membership-type-form-block-weight">
Expand Down
36 changes: 33 additions & 3 deletions tests/phpunit/api/v3/MembershipTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,14 @@ public function testMembershipTypeGetList() {
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',
Expand Down Expand Up @@ -297,6 +301,32 @@ public function testEnableMembershipTypeOnContributionPage() {
$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));
}
Expand Down

0 comments on commit 5afcf70

Please sign in to comment.