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