Merge pull request #15916 from civicrm/5.20
[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 * $Id$
17 *
18 */
19
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 *
89 *
90 * @return void
6a488035 91 */
00be9182 92 public function browse() {
6a488035 93 // get all membership types sorted by weight
be2fb01f 94 $membershipType = [];
6a488035
TO
95 $dao = new CRM_Member_DAO_MembershipType();
96
97 $dao->orderBy('weight');
98 $dao->find();
99
6a488035 100 while ($dao->fetch()) {
40c655aa 101 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
66af7c48
PN
102 && !CRM_Core_Permission::check('view contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))
103 ) {
ad268ba4
E
104 continue;
105 }
106 $links = self::links();
be2fb01f 107 $membershipType[$dao->id] = [];
40c655aa 108 CRM_Core_DAO::storeValues($dao, $membershipType[$dao->id]);
6a488035 109
dbd82592
CW
110 $membershipType[$dao->id]['period_type'] = CRM_Utils_Array::value($dao->period_type, CRM_Core_SelectValues::periodType(), '');
111 $membershipType[$dao->id]['visibility'] = CRM_Utils_Array::value($dao->visibility, CRM_Core_SelectValues::memberVisibility(), '');
112
6a488035
TO
113 //adding column for relationship type label. CRM-4178.
114 if ($dao->relationship_type_id) {
115 //If membership associated with 2 or more relationship then display all relationship with comma separated
116 $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->relationship_type_id);
117 $relTypeNames = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->relationship_direction);
118 $membershipType[$dao->id]['relationshipTypeName'] = NULL;
119 foreach ($relTypeIds as $key => $value) {
120 $relationshipName = 'label_' . $relTypeNames[$key];
121 if ($membershipType[$dao->id]['relationshipTypeName']) {
122 $membershipType[$dao->id]['relationshipTypeName'] .= ", ";
123 }
124 $membershipType[$dao->id]['relationshipTypeName'] .= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType',
125 $value, $relationshipName
126 );
127 }
128 $membershipType[$dao->id]['maxRelated'] = CRM_Utils_Array::value('max_related', $membershipType[$dao->id]);
129 }
66af7c48 130 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) {
ad268ba4
E
131 unset($links[CRM_Core_Action::UPDATE], $links[CRM_Core_Action::ENABLE], $links[CRM_Core_Action::DISABLE]);
132 }
66af7c48 133 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) {
ad268ba4
E
134 unset($links[CRM_Core_Action::DELETE]);
135 }
6a488035
TO
136 // form all action links
137 $action = array_sum(array_keys($this->links()));
138
139 // update enable/disable links depending on if it is is_reserved or is_active
140 if (!isset($dao->is_reserved)) {
141 if ($dao->is_active) {
142 $action -= CRM_Core_Action::ENABLE;
143 }
144 else {
145 $action -= CRM_Core_Action::DISABLE;
146 }
147 $membershipType[$dao->id]['order'] = $membershipType[$dao->id]['weight'];
ad268ba4 148 $membershipType[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
be2fb01f 149 ['id' => $dao->id],
87dab4a4
AH
150 ts('more'),
151 FALSE,
152 'membershipType.manage.action',
153 'MembershipType',
154 $dao->id
6a488035
TO
155 );
156 }
157 }
158
159 $returnURL = CRM_Utils_System::url('civicrm/admin/member/membershipType', "reset=1&action=browse");
160 CRM_Utils_Weight::addOrder($membershipType, 'CRM_Member_DAO_MembershipType',
161 'id', $returnURL
162 );
163
164 CRM_Member_BAO_MembershipType::convertDayFormat($membershipType);
165 $this->assign('rows', $membershipType);
166 }
96025800 167
6a488035 168}