Switch membership type to use apiv4
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 4 Jul 2021 23:36:39 +0000 (11:36 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 12 Jul 2021 10:45:23 +0000 (22:45 +1200)
url is civicrm/admin/member/membershipType?reset=1
test cover in https://github.com/civicrm/civicrm-core/pull/20761

CRM/Member/Page/MembershipType.php
tests/phpunit/CRM/Member/Page/MembershipTypeTest.php

index 4bf35564df67bd026f7b9a4081cef5bc2a479886..ab117fef18bf7992251ae6897dae7ba24e15a36a 100644 (file)
@@ -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
     );
index 74e92ad4ef78c8a9b16f453ee9a8552a83d391ba..a31d3770984a3c286b387ae8c200f46b12e75a26 100644 (file)
@@ -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' => '<span><a href="/index.php?q=civicrm/admin/member/membershipType/add&amp;action=update&amp;id=' . $id . '&amp;reset=1" class="action-item crm-hover-button" title=\'Edit Membership Type\' >Edit</a><a href="#" class="action-item crm-hover-button crm-enable-disable" title=\'Disable Membership Type\' >Disable</a><a href="/index.php?q=civicrm/admin/member/membershipType/add&amp;action=delete&amp;id=1" class="action-item crm-hover-button small-popup" title=\'Delete Membership Type\' >Delete</a></span>',
+        'action' => '<span><a href="/index.php?q=civicrm/admin/member/membershipType/add&amp;action=update&amp;id=' . $id . '&amp;reset=1" class="action-item crm-hover-button" title=\'Edit Membership Type\' >Edit</a><a href="#" class="action-item crm-hover-button crm-enable-disable" title=\'Disable Membership Type\' >Disable</a><a href="/index.php?q=civicrm/admin/member/membershipType/add&amp;action=delete&amp;id=' . $id . '" class="action-item crm-hover-button small-popup" title=\'Delete Membership Type\' >Delete</a></span>',
       ],
     ], $assigned['rows']);
   }