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