Merge pull request #18395 from eileenmcnaughton/memtype
[civicrm-core.git] / CRM / Member / Page / MembershipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Page for displaying list of membership types
20 */
21 class CRM_Member_Page_MembershipType extends CRM_Core_Page {
22
23 /**
24 * The action links that we need to display for the browse screen.
25 *
26 * @var array
27 */
28 public static $_links = NULL;
29
30 public $useLivePageJS = TRUE;
31
32 /**
33 * Get action Links.
34 *
35 * @return array
36 * (reference) of action links
37 */
38 public function &links() {
39 if (!(self::$_links)) {
40 self::$_links = [
41 CRM_Core_Action::UPDATE => [
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'),
46 ],
47 CRM_Core_Action::DISABLE => [
48 'name' => ts('Disable'),
49 'ref' => 'crm-enable-disable',
50 'title' => ts('Disable Membership Type'),
51 ],
52 CRM_Core_Action::ENABLE => [
53 'name' => ts('Enable'),
54 'ref' => 'crm-enable-disable',
55 'title' => ts('Enable Membership Type'),
56 ],
57 CRM_Core_Action::DELETE => [
58 'name' => ts('Delete'),
59 'url' => 'civicrm/admin/member/membershipType/add',
60 'qs' => 'action=delete&id=%%id%%',
61 'title' => ts('Delete Membership Type'),
62 ],
63 ];
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
76 */
77 public function run() {
78 $this->browse();
79
80 // parent run
81 return parent::run();
82 }
83
84 /**
85 * Browse all membership types.
86 *
87 *
88 * @return void
89 */
90 public function browse() {
91 // get all membership types sorted by weight
92 $membershipType = [];
93 $dao = new CRM_Member_DAO_MembershipType();
94
95 $dao->orderBy('weight');
96 $dao->find();
97
98 while ($dao->fetch()) {
99 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
100 && !CRM_Core_Permission::check('view contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))
101 ) {
102 continue;
103 }
104 $links = self::links();
105 $membershipType[$dao->id] = [];
106 CRM_Core_DAO::storeValues($dao, $membershipType[$dao->id]);
107
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
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 }
126 $membershipType[$dao->id]['maxRelated'] = $membershipType[$dao->id]['max_related'] ?? NULL;
127 }
128 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) {
129 unset($links[CRM_Core_Action::UPDATE], $links[CRM_Core_Action::ENABLE], $links[CRM_Core_Action::DISABLE]);
130 }
131 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) {
132 unset($links[CRM_Core_Action::DELETE]);
133 }
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'];
146 $membershipType[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
147 ['id' => $dao->id],
148 ts('more'),
149 FALSE,
150 'membershipType.manage.action',
151 'MembershipType',
152 $dao->id
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 }
165
166 }