(dev/core#3141) Membership should be listed chronologically by join date, the most...
[civicrm-core.git] / CRM / Member / Page / MembershipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
e0470c41
EM
18use Civi\Api4\MembershipType;
19
6a488035
TO
20/**
21 * Page for displaying list of membership types
22 */
23class CRM_Member_Page_MembershipType extends CRM_Core_Page {
24
25 /**
fe482240 26 * The action links that we need to display for the browse screen.
6a488035
TO
27 *
28 * @var array
6a488035 29 */
971e129b 30 public static $_links = NULL;
6a488035 31
96f50de2
CW
32 public $useLivePageJS = TRUE;
33
6a488035 34 /**
fe482240 35 * Get action Links.
6a488035 36 *
a6c01b45
CW
37 * @return array
38 * (reference) of action links
6a488035 39 */
00be9182 40 public function &links() {
6a488035 41 if (!(self::$_links)) {
be2fb01f
CW
42 self::$_links = [
43 CRM_Core_Action::UPDATE => [
6a488035
TO
44 'name' => ts('Edit'),
45 'url' => 'civicrm/admin/member/membershipType/add',
46 'qs' => 'action=update&id=%%id%%&reset=1',
47 'title' => ts('Edit Membership Type'),
be2fb01f
CW
48 ],
49 CRM_Core_Action::DISABLE => [
6a488035 50 'name' => ts('Disable'),
4d17a233 51 'ref' => 'crm-enable-disable',
6a488035 52 'title' => ts('Disable Membership Type'),
be2fb01f
CW
53 ],
54 CRM_Core_Action::ENABLE => [
6a488035 55 'name' => ts('Enable'),
4d17a233 56 'ref' => 'crm-enable-disable',
6a488035 57 'title' => ts('Enable Membership Type'),
be2fb01f
CW
58 ],
59 CRM_Core_Action::DELETE => [
6a488035
TO
60 'name' => ts('Delete'),
61 'url' => 'civicrm/admin/member/membershipType/add',
62 'qs' => 'action=delete&id=%%id%%',
63 'title' => ts('Delete Membership Type'),
be2fb01f
CW
64 ],
65 ];
6a488035
TO
66 }
67 return self::$_links;
68 }
69
70 /**
71 * Run the page.
72 *
73 * This method is called after the page is created. It checks for the
74 * type of action and executes that action.
75 * Finally it calls the parent's run method.
76 *
77 * @return void
6a488035 78 */
00be9182 79 public function run() {
6a488035
TO
80 $this->browse();
81
82 // parent run
83 return parent::run();
84 }
85
86 /**
87 * Browse all membership types.
88 *
6a488035 89 * @return void
6a488035 90 */
00be9182 91 public function browse() {
e0470c41
EM
92 // Ensure an action is assigned, even null - since this page is overloaded for other uses
93 // we need to avoid e-notices.
94 $this->assign('action');
95 $membershipType = (array) MembershipType::get()
96 ->addOrderBy('weight')
97 ->setSelect([
98 'id',
99 'domain_id',
100 'name',
101 'fixed_period_start_day',
102 'fixed_period_rollover_day',
103 'max_related',
104 'relationship_type_id',
105 'relationship_direction',
106 'member_of_contact_id',
107 'financial_type_id',
108 'minimum_fee',
109 'duration_unit',
110 'duration_interval',
111 'period_type:label',
112 'visibility:label',
113 'weight',
114 'auto_renew',
115 'is_active',
116 ])->execute()->indexBy('id');
117
118 foreach ($membershipType as $type) {
119 $links = $this->links();
6a488035 120 //adding column for relationship type label. CRM-4178.
e0470c41
EM
121 $membershipType[$type['id']]['relationshipTypeName'] = NULL;
122 // Ideally the v4 template would handle the v4 names for these fields - however, that
123 // requires updating edit-in-place so it is a 'todo' for now.
124 $membershipType[$type['id']]['visibility'] = $type['visibility:label'];
125 $membershipType[$type['id']]['period_type'] = $type['period_type:label'];
126 $membershipType[$type['id']]['relationshipTypeName'] = NULL;
127 if ($type['relationship_type_id']) {
6a488035 128 //If membership associated with 2 or more relationship then display all relationship with comma separated
e0470c41
EM
129 $membershipType[$type['id']]['relationshipTypeName'] = NULL;
130 foreach ($type['relationship_type_id'] as $key => $value) {
131 $relationshipName = 'label_' . $type['relationship_direction'][$key];
132 if ($membershipType[$type['id']]['relationshipTypeName']) {
133 $membershipType[$type['id']]['relationshipTypeName'] .= ', ';
6a488035 134 }
e0470c41 135 $membershipType[$type['id']]['relationshipTypeName'] .= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType',
6a488035
TO
136 $value, $relationshipName
137 );
138 }
6a488035 139 }
e0470c41 140 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($type['financial_type_id']))) {
ad268ba4
E
141 unset($links[CRM_Core_Action::UPDATE], $links[CRM_Core_Action::ENABLE], $links[CRM_Core_Action::DISABLE]);
142 }
e0470c41 143 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($type['financial_type_id']))) {
ad268ba4
E
144 unset($links[CRM_Core_Action::DELETE]);
145 }
6a488035
TO
146 // form all action links
147 $action = array_sum(array_keys($this->links()));
148
e0470c41
EM
149 // update enable/disable links depending on if it is is_reserved or is_active
150 if ($type['is_active']) {
8f90bd14
EM
151 $action -= CRM_Core_Action::ENABLE;
152 }
153 else {
154 $action -= CRM_Core_Action::DISABLE;
6a488035 155 }
e0470c41
EM
156
157 $membershipType[$type['id']]['action'] = CRM_Core_Action::formLink($links, $action,
158 ['id' => $type['id']],
8f90bd14
EM
159 ts('more'),
160 FALSE,
161 'membershipType.manage.action',
162 'MembershipType',
e0470c41 163 $type['id']
8f90bd14 164 );
6a488035
TO
165 }
166
e0470c41 167 $returnURL = CRM_Utils_System::url('civicrm/admin/member/membershipType', 'reset=1&action=browse');
6a488035
TO
168 CRM_Utils_Weight::addOrder($membershipType, 'CRM_Member_DAO_MembershipType',
169 'id', $returnURL
170 );
171
172 CRM_Member_BAO_MembershipType::convertDayFormat($membershipType);
173 $this->assign('rows', $membershipType);
174 }
96025800 175
6a488035 176}