From: Eileen McNaughton Date: Sun, 4 Jul 2021 23:36:39 +0000 (+1200) Subject: Switch membership type to use apiv4 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=e0470c4133c6941bb4174a6df8d052d65178f8c4;p=civicrm-core.git Switch membership type to use apiv4 url is civicrm/admin/member/membershipType?reset=1 test cover in https://github.com/civicrm/civicrm-core/pull/20761 --- diff --git a/CRM/Member/Page/MembershipType.php b/CRM/Member/Page/MembershipType.php index 4bf35564df..ab117fef18 100644 --- a/CRM/Member/Page/MembershipType.php +++ b/CRM/Member/Page/MembershipType.php @@ -15,6 +15,8 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Civi\Api4\MembershipType; + /** * Page for displaying list of membership types */ @@ -84,74 +86,85 @@ class CRM_Member_Page_MembershipType extends CRM_Core_Page { /** * Browse all membership types. * - * * @return void */ public function browse() { - // get all membership types sorted by weight - $membershipType = []; - $dao = new CRM_Member_DAO_MembershipType(); - - $dao->orderBy('weight'); - $dao->find(); - - while ($dao->fetch()) { - if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() - && !CRM_Core_Permission::check('view contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id)) - ) { - continue; - } - $links = self::links(); - $membershipType[$dao->id] = []; - CRM_Core_DAO::storeValues($dao, $membershipType[$dao->id]); - - $membershipType[$dao->id]['period_type'] = CRM_Utils_Array::value($dao->period_type, CRM_Core_SelectValues::periodType(), ''); - $membershipType[$dao->id]['visibility'] = CRM_Utils_Array::value($dao->visibility, CRM_Core_SelectValues::memberVisibility(), ''); - + // Ensure an action is assigned, even null - since this page is overloaded for other uses + // we need to avoid e-notices. + $this->assign('action'); + $membershipType = (array) MembershipType::get() + ->addOrderBy('weight') + ->setSelect([ + 'id', + 'domain_id', + 'name', + 'fixed_period_start_day', + 'fixed_period_rollover_day', + 'max_related', + 'relationship_type_id', + 'relationship_direction', + 'member_of_contact_id', + 'financial_type_id', + 'minimum_fee', + 'duration_unit', + 'duration_interval', + 'period_type:label', + 'visibility:label', + 'weight', + 'auto_renew', + 'is_active', + ])->execute()->indexBy('id'); + + foreach ($membershipType as $type) { + $links = $this->links(); //adding column for relationship type label. CRM-4178. - if ($dao->relationship_type_id) { + $membershipType[$type['id']]['relationshipTypeName'] = NULL; + // Ideally the v4 template would handle the v4 names for these fields - however, that + // requires updating edit-in-place so it is a 'todo' for now. + $membershipType[$type['id']]['visibility'] = $type['visibility:label']; + $membershipType[$type['id']]['period_type'] = $type['period_type:label']; + $membershipType[$type['id']]['relationshipTypeName'] = NULL; + if ($type['relationship_type_id']) { //If membership associated with 2 or more relationship then display all relationship with comma separated - $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->relationship_type_id); - $relTypeNames = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->relationship_direction); - $membershipType[$dao->id]['relationshipTypeName'] = NULL; - foreach ($relTypeIds as $key => $value) { - $relationshipName = 'label_' . $relTypeNames[$key]; - if ($membershipType[$dao->id]['relationshipTypeName']) { - $membershipType[$dao->id]['relationshipTypeName'] .= ", "; + $membershipType[$type['id']]['relationshipTypeName'] = NULL; + foreach ($type['relationship_type_id'] as $key => $value) { + $relationshipName = 'label_' . $type['relationship_direction'][$key]; + if ($membershipType[$type['id']]['relationshipTypeName']) { + $membershipType[$type['id']]['relationshipTypeName'] .= ', '; } - $membershipType[$dao->id]['relationshipTypeName'] .= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', + $membershipType[$type['id']]['relationshipTypeName'] .= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $value, $relationshipName ); } } - if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) { + if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($type['financial_type_id']))) { unset($links[CRM_Core_Action::UPDATE], $links[CRM_Core_Action::ENABLE], $links[CRM_Core_Action::DISABLE]); } - if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) { + if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($type['financial_type_id']))) { unset($links[CRM_Core_Action::DELETE]); } // form all action links $action = array_sum(array_keys($this->links())); - // update enable/disable links depending on if it is_active - if ($dao->is_active) { + // update enable/disable links depending on if it is is_reserved or is_active + if ($type['is_active']) { $action -= CRM_Core_Action::ENABLE; } else { $action -= CRM_Core_Action::DISABLE; } - $membershipType[$dao->id]['order'] = $membershipType[$dao->id]['weight']; - $membershipType[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action, - ['id' => $dao->id], + + $membershipType[$type['id']]['action'] = CRM_Core_Action::formLink($links, $action, + ['id' => $type['id']], ts('more'), FALSE, 'membershipType.manage.action', 'MembershipType', - $dao->id + $type['id'] ); } - $returnURL = CRM_Utils_System::url('civicrm/admin/member/membershipType', "reset=1&action=browse"); + $returnURL = CRM_Utils_System::url('civicrm/admin/member/membershipType', 'reset=1&action=browse'); CRM_Utils_Weight::addOrder($membershipType, 'CRM_Member_DAO_MembershipType', 'id', $returnURL ); diff --git a/tests/phpunit/CRM/Member/Page/MembershipTypeTest.php b/tests/phpunit/CRM/Member/Page/MembershipTypeTest.php index 74e92ad4ef..a31d377098 100644 --- a/tests/phpunit/CRM/Member/Page/MembershipTypeTest.php +++ b/tests/phpunit/CRM/Member/Page/MembershipTypeTest.php @@ -30,19 +30,26 @@ class CRM_Member_Page_MembershipTypeTest extends CiviUnitTestCase { 'id' => '1', 'domain_id' => '1', 'name' => 'General', - 'membership_type' => 'General', 'member_of_contact_id' => '3', 'financial_type_id' => '2', - 'minimum_fee' => '0.000000000', + 'minimum_fee' => 0.0, 'duration_unit' => 'year', 'duration_interval' => '1', 'period_type' => 'Rolling', 'visibility' => 'Public', 'weight' => '1', - 'auto_renew' => '0', - 'is_active' => '1', + 'auto_renew' => FALSE, + 'is_active' => TRUE, + 'fixed_period_start_day' => NULL, + 'fixed_period_rollover_day' => NULL, + 'max_related' => NULL, + 'relationship_type_id' => NULL, + 'relationship_direction' => NULL, + 'period_type:label' => 'Rolling', + 'visibility:label' => 'Public', + 'relationshipTypeName' => NULL, 'order' => NULL, - 'action' => 'EditDisableDelete', + 'action' => 'EditDisableDelete', ], ], $assigned['rows']); }