Merge pull request #15978 from civicrm/5.20
[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 * $Id$
17 *
18 */
19
20 /**
21 * Page for displaying list of membership types
22 */
23 class CRM_Member_Page_MembershipType extends CRM_Core_Page {
24
25 /**
26 * The action links that we need to display for the browse screen.
27 *
28 * @var array
29 */
30 public static $_links = NULL;
31
32 public $useLivePageJS = TRUE;
33
34 /**
35 * Get action Links.
36 *
37 * @return array
38 * (reference) of action links
39 */
40 public function &links() {
41 if (!(self::$_links)) {
42 self::$_links = [
43 CRM_Core_Action::UPDATE => [
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'),
48 ],
49 CRM_Core_Action::DISABLE => [
50 'name' => ts('Disable'),
51 'ref' => 'crm-enable-disable',
52 'title' => ts('Disable Membership Type'),
53 ],
54 CRM_Core_Action::ENABLE => [
55 'name' => ts('Enable'),
56 'ref' => 'crm-enable-disable',
57 'title' => ts('Enable Membership Type'),
58 ],
59 CRM_Core_Action::DELETE => [
60 'name' => ts('Delete'),
61 'url' => 'civicrm/admin/member/membershipType/add',
62 'qs' => 'action=delete&id=%%id%%',
63 'title' => ts('Delete Membership Type'),
64 ],
65 ];
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
78 */
79 public function run() {
80 $this->browse();
81
82 // parent run
83 return parent::run();
84 }
85
86 /**
87 * Browse all membership types.
88 *
89 *
90 * @return void
91 */
92 public function browse() {
93 // get all membership types sorted by weight
94 $membershipType = [];
95 $dao = new CRM_Member_DAO_MembershipType();
96
97 $dao->orderBy('weight');
98 $dao->find();
99
100 while ($dao->fetch()) {
101 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
102 && !CRM_Core_Permission::check('view contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))
103 ) {
104 continue;
105 }
106 $links = self::links();
107 $membershipType[$dao->id] = [];
108 CRM_Core_DAO::storeValues($dao, $membershipType[$dao->id]);
109
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
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 }
130 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) {
131 unset($links[CRM_Core_Action::UPDATE], $links[CRM_Core_Action::ENABLE], $links[CRM_Core_Action::DISABLE]);
132 }
133 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($dao->financial_type_id))) {
134 unset($links[CRM_Core_Action::DELETE]);
135 }
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'];
148 $membershipType[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
149 ['id' => $dao->id],
150 ts('more'),
151 FALSE,
152 'membershipType.manage.action',
153 'MembershipType',
154 $dao->id
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 }
167
168 }