Commit | Line | Data |
---|---|---|
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 | * This class generates form components for Payment-Instrument | |
6a488035 TO |
20 | */ |
21 | class CRM_Member_Form_MembershipView extends CRM_Core_Form { | |
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 | |
669fc25e CR |
30 | /** |
31 | * The id of the membership being viewed. | |
32 | * | |
33 | * @var int | |
34 | */ | |
35 | private $membershipID; | |
36 | ||
37 | /** | |
38 | * Contact's ID. | |
39 | * | |
40 | * @var int | |
41 | */ | |
42 | private $contactID; | |
43 | ||
6a488035 | 44 | /** |
fe482240 | 45 | * Add context information at the end of a link. |
6a488035 | 46 | * |
72b3a70c CW |
47 | * @return string |
48 | * extra query parameters | |
6a488035 | 49 | */ |
00be9182 | 50 | public function addContext() { |
6a488035 | 51 | $extra = ''; |
be2fb01f | 52 | foreach (['context', 'selectedChild'] as $arg) { |
6a488035 TO |
53 | if ($value = CRM_Utils_Request::retrieve($arg, 'String', $this)) { |
54 | $extra .= "&{$arg}={$value}"; | |
55 | } | |
56 | } | |
57 | return $extra; | |
58 | } | |
59 | ||
60 | /** | |
fe482240 | 61 | * Get action Links. |
6a488035 | 62 | * |
a6c01b45 CW |
63 | * @return array |
64 | * (reference) of action links | |
6a488035 | 65 | */ |
00be9182 | 66 | public function &links() { |
6a488035 | 67 | if (!(self::$_links)) { |
be2fb01f CW |
68 | self::$_links = [ |
69 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
70 | 'name' => ts('Delete'), |
71 | 'url' => 'civicrm/contact/view/membership', | |
72 | 'qs' => 'action=view&id=%%id%%&cid=%%cid%%&relAction=delete&mid=%%mid%%&reset=1' . $this->addContext(), | |
73 | 'title' => ts('Cancel Related Membership'), | |
be2fb01f CW |
74 | ], |
75 | CRM_Core_Action::ADD => [ | |
6a488035 TO |
76 | 'name' => ts('Create'), |
77 | 'url' => 'civicrm/contact/view/membership', | |
78 | 'qs' => 'action=view&id=%%id%%&cid=%%cid%%&relAction=create&rid=%%rid%%&reset=1' . $this->addContext(), | |
79 | 'title' => ts('Create Related Membership'), | |
be2fb01f CW |
80 | ], |
81 | ]; | |
6a488035 TO |
82 | } |
83 | return self::$_links; | |
84 | } | |
85 | ||
86 | /** | |
fe482240 | 87 | * Perform create or delete action on related memberships. |
6a488035 | 88 | * |
b2363ea8 TO |
89 | * @param string $action |
90 | * Create or delete. | |
91 | * @param array $owner | |
92 | * Primary membership info (membership_id, contact_id, membership_type ...). | |
8800fc1f | 93 | * |
94 | * @throws \CRM_Core_Exception | |
95 | * @throws \CiviCRM_API3_Exception | |
6a488035 | 96 | */ |
00be9182 | 97 | public function relAction($action, $owner) { |
6a488035 TO |
98 | switch ($action) { |
99 | case 'delete': | |
100 | $id = CRM_Utils_Request::retrieve('mid', 'Positive', $this); | |
101 | $relatedContactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
102 | $relatedDisplayName = CRM_Contact_BAO_Contact::displayName($relatedContactId); | |
3506b6cd | 103 | CRM_Member_BAO_Membership::del($id); |
be2fb01f | 104 | CRM_Core_Session::setStatus(ts('Related membership for %1 has been deleted.', [1 => $relatedDisplayName]), |
8d8bd076 | 105 | ts('Membership Deleted'), 'success'); |
6a488035 | 106 | break; |
b09fe5ed | 107 | |
6a488035 | 108 | case 'create': |
be2fb01f | 109 | $params = [ |
8d8bd076 | 110 | 'contact_id' => CRM_Utils_Request::retrieve('rid', 'Positive', $this), |
111 | 'membership_type_id' => $owner['membership_type_id'], | |
112 | 'owner_membership_id' => $owner['id'], | |
113 | 'join_date' => CRM_Utils_Date::processDate($owner['join_date'], NULL, TRUE, 'Ymd'), | |
114 | 'start_date' => CRM_Utils_Date::processDate($owner['start_date'], NULL, TRUE, 'Ymd'), | |
115 | 'end_date' => CRM_Utils_Date::processDate($owner['end_date'], NULL, TRUE, 'Ymd'), | |
116 | 'source' => ts('Manual Assignment of Related Membership'), | |
117 | 'is_test' => $owner['is_test'], | |
6b409353 | 118 | 'campaign_id' => $owner['campaign_id'] ?? NULL, |
8d8bd076 | 119 | 'status_id' => $owner['status_id'], |
120 | 'skipStatusCal' => TRUE, | |
121 | 'createActivity' => TRUE, | |
be2fb01f | 122 | ]; |
8800fc1f | 123 | CRM_Member_BAO_Membership::create($params); |
6a488035 | 124 | $relatedDisplayName = CRM_Contact_BAO_Contact::displayName($params['contact_id']); |
be2fb01f | 125 | CRM_Core_Session::setStatus(ts('Related membership for %1 has been created.', [1 => $relatedDisplayName]), |
8d8bd076 | 126 | ts('Membership Added'), 'success'); |
6a488035 | 127 | break; |
b09fe5ed | 128 | |
6a488035 | 129 | default: |
8800fc1f | 130 | throw new CRM_Core_Exception(ts('Invalid action specified in URL')); |
6a488035 TO |
131 | } |
132 | ||
133 | // Redirect back to membership view page for the owner, without the relAction parameters | |
134 | CRM_Utils_System::redirect( | |
135 | CRM_Utils_System::url( | |
136 | 'civicrm/contact/view/membership', | |
137 | "action=view&reset=1&id={$owner['membership_id']}&cid={$owner['contact_id']}" . $this->addContext() | |
138 | ) | |
139 | ); | |
140 | } | |
141 | ||
142 | /** | |
fe482240 | 143 | * Set variables up before form is built. |
6a488035 TO |
144 | * |
145 | * @return void | |
6a488035 TO |
146 | */ |
147 | public function preProcess() { | |
be2fb01f | 148 | $values = []; |
669fc25e CR |
149 | $this->membershipID = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
150 | $this->contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
6a488035 TO |
151 | |
152 | // Make sure context is assigned to template for condition where we come here view civicrm/membership/view | |
edc80cda | 153 | $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
154 | $this->assign('context', $context); |
155 | ||
669fc25e | 156 | if ($this->membershipID) { |
8477025c BT |
157 | $values = \Civi\Api4\Membership::get() |
158 | ->addSelect('*', 'status_id:label', 'membership_type_id:label', 'membership_type_id.financial_type_id', 'status_id.is_current_member') | |
159 | ->addWhere('id', '=', $this->membershipID) | |
160 | ->execute() | |
161 | ->first(); | |
162 | ||
163 | // Ensure keys expected by MembershipView.tpl are set correctly | |
164 | // Some of these defaults are overwritten dependant on context below | |
165 | $values['financialTypeId'] = $values['membership_type_id.financial_type_id']; | |
166 | $values['membership_type'] = $values['membership_type_id:label']; | |
167 | $values['status'] = $values['status_id:label']; | |
168 | $values['active'] = $values['status_id.is_current_member']; | |
169 | $values['owner_contact_id'] = FALSE; | |
170 | $values['owner_display_name'] = FALSE; | |
171 | $values['campaign'] = FALSE; | |
172 | ||
66af7c48 PN |
173 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { |
174 | $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values['membership_type_id'], 'financial_type_id'); | |
175 | $finType = CRM_Contribute_PseudoConstant::financialType($finTypeId); | |
66af7c48 | 176 | if (!CRM_Core_Permission::check('view contributions of type ' . $finType)) { |
4627f70f | 177 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
66af7c48 | 178 | } |
c826cd95 | 179 | } |
895d596d E |
180 | else { |
181 | $this->assign('noACL', TRUE); | |
182 | } | |
6a488035 TO |
183 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); |
184 | ||
185 | // Do the action on related Membership if needed | |
186 | $relAction = CRM_Utils_Request::retrieve('relAction', 'String', $this); | |
187 | if ($relAction) { | |
188 | $this->relAction($relAction, $values); | |
189 | } | |
190 | ||
191 | // build associated contributions | |
8d8bd076 | 192 | $this->assign('accessContribution', FALSE); |
193 | if (CRM_Core_Permission::access('CiviContribute')) { | |
194 | $this->assign('accessContribution', TRUE); | |
669fc25e | 195 | CRM_Member_Page_Tab::associatedContribution($values['contact_id'], $this->membershipID); |
8d8bd076 | 196 | } |
6a488035 TO |
197 | |
198 | //Provide information about membership source when it is the result of a relationship (CRM-1901) | |
199 | $values['owner_membership_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
669fc25e | 200 | $this->membershipID, |
6a488035 TO |
201 | 'owner_membership_id' |
202 | ); | |
203 | ||
204 | if (isset($values['owner_membership_id'])) { | |
205 | $values['owner_contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
206 | $values['owner_membership_id'], | |
207 | 'contact_id', | |
208 | 'id' | |
209 | ); | |
210 | ||
211 | $values['owner_display_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
212 | $values['owner_contact_id'], | |
213 | 'display_name', | |
214 | 'id' | |
215 | ); | |
216 | ||
217 | $direction = strrev($membershipType['relationship_direction']); | |
218 | // To display relationship type in view membership page | |
219 | $relTypeIds = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ",", $membershipType['relationship_type_id']); | |
220 | $sql = " | |
221 | SELECT relationship_type_id, | |
03e04002 | 222 | CASE |
6a488035 TO |
223 | WHEN contact_id_a = {$values['owner_contact_id']} AND contact_id_b = {$values['contact_id']} THEN 'b_a' |
224 | WHEN contact_id_b = {$values['owner_contact_id']} AND contact_id_a = {$values['contact_id']} THEN 'a_b' | |
225 | END AS 'relType' | |
03e04002 | 226 | FROM civicrm_relationship |
6a488035 TO |
227 | WHERE relationship_type_id IN ($relTypeIds)"; |
228 | $dao = CRM_Core_DAO::executeQuery($sql); | |
229 | $values['relationship'] = NULL; | |
230 | while ($dao->fetch()) { | |
231 | $typeId = $dao->relationship_type_id; | |
232 | $direction = $dao->relType; | |
233 | if ($direction && $typeId) { | |
234 | if ($values['relationship']) { | |
235 | $values['relationship'] .= ','; | |
236 | } | |
237 | $values['relationship'] .= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', | |
238 | $typeId, | |
239 | "name_$direction", | |
240 | 'id' | |
241 | ); | |
242 | } | |
243 | } | |
244 | } | |
245 | ||
246 | $this->assign('has_related', FALSE); | |
247 | // if membership can be granted, and we are the owner of the membership | |
8cc574cf | 248 | if (!empty($membershipType['relationship_type_id']) && empty($values['owner_membership_id'])) { |
6a488035 TO |
249 | // display related contacts/membership block |
250 | $this->assign('has_related', TRUE); | |
251 | $this->assign('max_related', CRM_Utils_Array::value('max_related', $values, ts('Unlimited'))); | |
252 | // split the relations in 2 arrays based on direction | |
253 | $relTypeId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']); | |
254 | $relDirection = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']); | |
255 | foreach ($relTypeId as $rid) { | |
3030f6f5 | 256 | $relTypeDir[substr($relDirection[0], 0, 1)][] = $rid; |
6a488035 TO |
257 | } |
258 | // build query in 2 parts with a UNION if necessary | |
259 | // _x and _y are replaced with _a and _b first, then vice-versa | |
260 | // comment is a qualifier for the relationship - now just job_title | |
261 | $select = " | |
262 | SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment, | |
263 | rt.name_x_y as relation, r.start_date, r.end_date, | |
264 | m.id as mid, ms.is_current_member, ms.label as status | |
265 | FROM civicrm_relationship r | |
266 | LEFT JOIN civicrm_relationship_type rt ON rt.id = r.relationship_type_id | |
267 | LEFT JOIN civicrm_contact c ON c.id = r.contact_id_x | |
8d8bd076 | 268 | LEFT JOIN civicrm_membership m ON (m.owner_membership_id = {$values['id']} |
269 | AND m.contact_id = r.contact_id_x AND m.is_test = 0) | |
6a488035 TO |
270 | LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id |
271 | WHERE r.contact_id_y = {$values['contact_id']} AND r.is_active = 1 AND c.is_deleted = 0"; | |
272 | $query = ''; | |
be2fb01f | 273 | foreach (['a', 'b'] as $dir) { |
2eef47ee | 274 | if (isset($relTypeDir[$dir])) { |
6a488035 | 275 | $query .= ($query ? ' UNION ' : '') |
8d8bd076 | 276 | . str_replace('_y', '_' . $dir, str_replace('_x', '_' . ($dir == 'a' ? 'b' : 'a'), $select)) |
277 | . ' AND r.relationship_type_id IN (' . implode(',', $relTypeDir[$dir]) . ')'; | |
6a488035 TO |
278 | } |
279 | } | |
280 | $query .= " ORDER BY is_current_member DESC"; | |
281 | $dao = CRM_Core_DAO::executeQuery($query); | |
be2fb01f | 282 | $related = []; |
6a488035 | 283 | $relatedRemaining = CRM_Utils_Array::value('max_related', $values, PHP_INT_MAX); |
be2fb01f | 284 | $rowElememts = [ |
8d8bd076 | 285 | 'id', |
286 | 'cid', | |
287 | 'name', | |
288 | 'comment', | |
289 | 'relation', | |
290 | 'mid', | |
291 | 'start_date', | |
292 | 'end_date', | |
293 | 'is_current_member', | |
21dfd5f5 | 294 | 'status', |
be2fb01f | 295 | ]; |
8d8bd076 | 296 | |
6a488035 | 297 | while ($dao->fetch()) { |
be2fb01f | 298 | $row = []; |
8d8bd076 | 299 | foreach ($rowElememts as $field) { |
6a488035 TO |
300 | $row[$field] = $dao->$field; |
301 | } | |
302 | if ($row['mid'] && ($row['is_current_member'] == 1)) { | |
303 | $relatedRemaining--; | |
304 | $row['action'] = CRM_Core_Action::formLink(self::links(), CRM_Core_Action::DELETE, | |
be2fb01f | 305 | [ |
6a488035 TO |
306 | 'id' => CRM_Utils_Request::retrieve('id', 'Positive', $this), |
307 | 'cid' => $row['cid'], | |
308 | 'mid' => $row['mid'], | |
be2fb01f | 309 | ], |
87dab4a4 AH |
310 | ts('more'), |
311 | FALSE, | |
312 | 'membership.relationship.action', | |
313 | 'Relationship', | |
314 | CRM_Utils_Request::retrieve('id', 'Positive', $this) | |
6a488035 | 315 | ); |
8d8bd076 | 316 | } |
317 | else { | |
318 | if ($relatedRemaining > 0) { | |
6a488035 | 319 | $row['action'] = CRM_Core_Action::formLink(self::links(), CRM_Core_Action::ADD, |
be2fb01f | 320 | [ |
6a488035 TO |
321 | 'id' => CRM_Utils_Request::retrieve('id', 'Positive', $this), |
322 | 'cid' => $row['cid'], | |
323 | 'rid' => $row['cid'], | |
be2fb01f | 324 | ], |
87dab4a4 AH |
325 | ts('more'), |
326 | FALSE, | |
327 | 'membership.relationship.action', | |
328 | 'Relationship', | |
329 | CRM_Utils_Request::retrieve('id', 'Positive', $this) | |
6a488035 | 330 | ); |
8d8bd076 | 331 | } |
6a488035 TO |
332 | } |
333 | $related[] = $row; | |
334 | } | |
335 | $this->assign('related', $related); | |
336 | if ($relatedRemaining <= 0) { | |
337 | $this->assign('related_text', ts('None available')); | |
8d8bd076 | 338 | } |
339 | else { | |
340 | if ($relatedRemaining < 100000) { | |
be2fb01f | 341 | $this->assign('related_text', ts('%1 available', [1 => $relatedRemaining])); |
8d8bd076 | 342 | } |
343 | else { | |
be2fb01f | 344 | $this->assign('related_text', ts('Unlimited', [1 => $relatedRemaining])); |
8d8bd076 | 345 | } |
6a488035 TO |
346 | } |
347 | } | |
348 | ||
349 | $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']); | |
350 | $this->assign('displayName', $displayName); | |
03e04002 | 351 | |
6a488035 TO |
352 | // Check if this is default domain contact CRM-10482 |
353 | if (CRM_Contact_BAO_Contact::checkDomainContact($values['contact_id'])) { | |
354 | $displayName .= ' (' . ts('default organization') . ')'; | |
355 | } | |
356 | ||
8d8bd076 | 357 | // omitting contactImage from title for now since the summary overlay css doesn't work outside crm-container |
203b4734 | 358 | $this->setTitle(ts('View Membership for') . ' ' . $displayName); |
03e04002 | 359 | |
6a488035 TO |
360 | // add viewed membership to recent items list |
361 | $recentTitle = $displayName . ' - ' . ts('Membership Type:') . ' ' . $values['membership_type']; | |
362 | $url = CRM_Utils_System::url('civicrm/contact/view/membership', | |
363 | "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" | |
364 | ); | |
365 | ||
be2fb01f | 366 | $recentOther = []; |
6a488035 TO |
367 | if (CRM_Core_Permission::checkActionPermission('CiviMember', CRM_Core_Action::UPDATE)) { |
368 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', | |
369 | "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" | |
370 | ); | |
371 | } | |
372 | if (CRM_Core_Permission::checkActionPermission('CiviMember', CRM_Core_Action::DELETE)) { | |
373 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', | |
374 | "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" | |
375 | ); | |
376 | } | |
377 | CRM_Utils_Recent::add($recentTitle, | |
378 | $url, | |
379 | $values['id'], | |
380 | 'Membership', | |
381 | $values['contact_id'], | |
382 | NULL, | |
383 | $recentOther | |
384 | ); | |
385 | ||
8d8bd076 | 386 | CRM_Member_Page_Tab::setContext($this, $values['contact_id']); |
6a488035 | 387 | |
669fc25e | 388 | $memType = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $this->membershipID, "membership_type_id"); |
6a488035 | 389 | |
317103ab CW |
390 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Membership', NULL, $this->membershipID, 0, $memType, NULL, |
391 | TRUE, NULL, FALSE, CRM_Core_Permission::VIEW); | |
669fc25e | 392 | CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->membershipID); |
6a488035 | 393 | |
669fc25e | 394 | $isRecur = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->membershipID, 'contribution_recur_id'); |
6a488035 | 395 | |
f7dbf5d9 | 396 | $autoRenew = (bool) $isRecur; |
6a488035 TO |
397 | } |
398 | ||
a7488080 | 399 | if (!empty($values['is_test'])) { |
cd355351 | 400 | $values['membership_type'] = CRM_Core_TestEntity::appendTestText($values['membership_type']); |
6a488035 TO |
401 | } |
402 | ||
669fc25e | 403 | $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled($this->membershipID); |
6a488035 TO |
404 | $values['auto_renew'] = ($autoRenew && !$subscriptionCancelled) ? 'Yes' : 'No'; |
405 | ||
406 | //do check for campaigns | |
407 | if ($campaignId = CRM_Utils_Array::value('campaign_id', $values)) { | |
408 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); | |
409 | $values['campaign'] = $campaigns[$campaignId]; | |
410 | } | |
411 | ||
412 | $this->assign($values); | |
413 | } | |
414 | ||
415 | /** | |
fe482240 | 416 | * Build the form object. |
6a488035 | 417 | * |
355ba699 | 418 | * @return void |
6a488035 TO |
419 | */ |
420 | public function buildQuickForm() { | |
be2fb01f CW |
421 | $this->addButtons([ |
422 | [ | |
c5c263ca AH |
423 | 'type' => 'cancel', |
424 | 'name' => ts('Done'), | |
425 | 'spacing' => ' ', | |
426 | 'isDefault' => TRUE, | |
be2fb01f CW |
427 | ], |
428 | ]); | |
6a488035 | 429 | } |
96025800 | 430 | |
6a488035 | 431 | } |