X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FMember%2FBAO%2FMembership.php;h=7daf555876735d2be9f89262ec2be52bc86ad9ba;hb=58a42056991e42e91e8aae204bd25298c31748f1;hp=6c954d976c8c9cb9e61db45462a72dce8d8cff7f;hpb=0ddfb1d0e6541a07edf7c1e50c3c8d79f8b6877e;p=civicrm-core.git diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 6c954d976c..7daf555876 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -2362,8 +2362,7 @@ WHERE {$whereClause}"; } /** - * Returns the membership types for a particular contact - * who has lifetime membership without end date. + * Returns the membership types for a contact, optionally filtering to lifetime memberships only. * * @param int $contactID * @param bool $isTest @@ -2371,33 +2370,26 @@ WHERE {$whereClause}"; * * @return array */ - public static function getAllContactMembership($contactID, $isTest = FALSE, $onlyLifeTime = FALSE) { + public static function getAllContactMembership($contactID, $isTest = FALSE, $onlyLifeTime = FALSE) : array { $contactMembershipType = []; if (!$contactID) { return $contactMembershipType; } - $dao = new CRM_Member_DAO_Membership(); - $dao->contact_id = $contactID; - $pendingStatusId = array_search('Pending', CRM_Member_PseudoConstant::membershipStatus()); - $dao->whereAdd("status_id != $pendingStatusId"); - - if ($isTest) { - $dao->is_test = $isTest; - } - else { - $dao->whereAdd('is_test IS NULL OR is_test = 0'); - } + $membershipQuery = \Civi\Api4\Membership::get(FALSE) + ->addWhere('contact_id', '=', $contactID) + ->addWhere('status_id:name', '<>', 'Pending') + ->addWhere('is_test', '=', $isTest) + //CRM-4297 + ->addOrderBy('end_date', 'DESC'); if ($onlyLifeTime) { - $dao->whereAdd('end_date IS NULL'); + // membership#14 - use duration_unit for calculating lifetime, not join/end date. + $membershipQuery->addWhere('membership_type_id.duration_unit', '=', 'lifetime'); } - - $dao->find(); - while ($dao->fetch()) { - $membership = []; - CRM_Core_DAO::storeValues($dao, $membership); - $contactMembershipType[$dao->membership_type_id] = $membership; + $memberships = $membershipQuery->execute(); + foreach ($memberships as $membership) { + $contactMembershipType[$membership['membership_type_id']] = $membership; } return $contactMembershipType; }