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