Merge pull request #18395 from eileenmcnaughton/memtype
[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
18/**
19 * Page for displaying list of membership types
20 */
21class CRM_Member_Page_MembershipType extends CRM_Core_Page {
22
23 /**
fe482240 24 * The action links that we need to display for the browse screen.
6a488035
TO
25 *
26 * @var array
6a488035 27 */
971e129b 28 public static $_links = NULL;
6a488035 29
96f50de2
CW
30 public $useLivePageJS = TRUE;
31
6a488035 32 /**
fe482240 33 * Get action Links.
6a488035 34 *
a6c01b45
CW
35 * @return array
36 * (reference) of action links
6a488035 37 */
00be9182 38 public function &links() {
6a488035 39 if (!(self::$_links)) {
be2fb01f
CW
40 self::$_links = [
41 CRM_Core_Action::UPDATE => [
6a488035
TO
42 'name' => ts('Edit'),
43 'url' => 'civicrm/admin/member/membershipType/add',
44 'qs' => 'action=update&id=%%id%%&reset=1',
45 'title' => ts('Edit Membership Type'),
be2fb01f
CW
46 ],
47 CRM_Core_Action::DISABLE => [
6a488035 48 'name' => ts('Disable'),
4d17a233 49 'ref' => 'crm-enable-disable',
6a488035 50 'title' => ts('Disable Membership Type'),
be2fb01f
CW
51 ],
52 CRM_Core_Action::ENABLE => [
6a488035 53 'name' => ts('Enable'),
4d17a233 54 'ref' => 'crm-enable-disable',
6a488035 55 'title' => ts('Enable Membership Type'),
be2fb01f
CW
56 ],
57 CRM_Core_Action::DELETE => [
6a488035
TO
58 'name' => ts('Delete'),
59 'url' => 'civicrm/admin/member/membershipType/add',
60 'qs' => 'action=delete&id=%%id%%',
61 'title' => ts('Delete Membership Type'),
be2fb01f
CW
62 ],
63 ];
6a488035
TO
64 }
65 return self::$_links;
66 }
67
68 /**
69 * Run the page.
70 *
71 * This method is called after the page is created. It checks for the
72 * type of action and executes that action.
73 * Finally it calls the parent's run method.
74 *
75 * @return void
6a488035 76 */
00be9182 77 public function run() {
6a488035
TO
78 $this->browse();
79
80 // parent run
81 return parent::run();
82 }
83
84 /**
85 * Browse all membership types.
86 *
87 *
88 * @return void
6a488035 89 */
00be9182 90 public function browse() {
6a488035 91 // get all membership types sorted by weight
be2fb01f 92 $membershipType = [];
6a488035
TO
93 $dao = new CRM_Member_DAO_MembershipType();
94
95 $dao->orderBy('weight');
96 $dao->find();
97
6a488035 98 while ($dao->fetch()) {
40c655aa 99 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
66af7c48
PN
100 && !CRM_Core_Permission::check('view contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))
101 ) {
ad268ba4
E
102 continue;
103 }
104 $links = self::links();
be2fb01f 105 $membershipType[$dao->id] = [];
40c655aa 106 CRM_Core_DAO::storeValues($dao, $membershipType[$dao->id]);
6a488035 107
dbd82592
CW
108 $membershipType[$dao->id]['period_type'] = CRM_Utils_Array::value($dao->period_type, CRM_Core_SelectValues::periodType(), '');
109 $membershipType[$dao->id]['visibility'] = CRM_Utils_Array::value($dao->visibility, CRM_Core_SelectValues::memberVisibility(), '');
110
6a488035
TO
111 //adding column for relationship type label. CRM-4178.
112 if ($dao->relationship_type_id) {
113 //If membership associated with 2 or more relationship then display all relationship with comma separated
114 $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->relationship_type_id);
115 $relTypeNames = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->relationship_direction);
116 $membershipType[$dao->id]['relationshipTypeName'] = NULL;
117 foreach ($relTypeIds as $key => $value) {
118 $relationshipName = 'label_' . $relTypeNames[$key];
119 if ($membershipType[$dao->id]['relationshipTypeName']) {
120 $membershipType[$dao->id]['relationshipTypeName'] .= ", ";
121 }
122 $membershipType[$dao->id]['relationshipTypeName'] .= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType',
123 $value, $relationshipName
124 );
125 }
9c1bc317 126 $membershipType[$dao->id]['maxRelated'] = $membershipType[$dao->id]['max_related'] ?? NULL;
6a488035 127 }
66af7c48 128 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) {
ad268ba4
E
129 unset($links[CRM_Core_Action::UPDATE], $links[CRM_Core_Action::ENABLE], $links[CRM_Core_Action::DISABLE]);
130 }
66af7c48 131 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) {
ad268ba4
E
132 unset($links[CRM_Core_Action::DELETE]);
133 }
6a488035
TO
134 // form all action links
135 $action = array_sum(array_keys($this->links()));
136
137 // update enable/disable links depending on if it is is_reserved or is_active
138 if (!isset($dao->is_reserved)) {
139 if ($dao->is_active) {
140 $action -= CRM_Core_Action::ENABLE;
141 }
142 else {
143 $action -= CRM_Core_Action::DISABLE;
144 }
145 $membershipType[$dao->id]['order'] = $membershipType[$dao->id]['weight'];
ad268ba4 146 $membershipType[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
be2fb01f 147 ['id' => $dao->id],
87dab4a4
AH
148 ts('more'),
149 FALSE,
150 'membershipType.manage.action',
151 'MembershipType',
152 $dao->id
6a488035
TO
153 );
154 }
155 }
156
157 $returnURL = CRM_Utils_System::url('civicrm/admin/member/membershipType', "reset=1&action=browse");
158 CRM_Utils_Weight::addOrder($membershipType, 'CRM_Member_DAO_MembershipType',
159 'id', $returnURL
160 );
161
162 CRM_Member_BAO_MembershipType::convertDayFormat($membershipType);
163 $this->assign('rows', $membershipType);
164 }
96025800 165
6a488035 166}