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