Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
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 | /** | |
fe482240 | 43 | * The action links that we need to display for the browse screen. |
6a488035 TO |
44 | * |
45 | * @var array | |
6a488035 TO |
46 | */ |
47 | static $_links = NULL; | |
48 | ||
49 | /** | |
fe482240 | 50 | * Add context information at the end of a link. |
6a488035 | 51 | * |
72b3a70c CW |
52 | * @return string |
53 | * extra query parameters | |
6a488035 | 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 | /** | |
fe482240 | 66 | * Get action Links. |
6a488035 | 67 | * |
a6c01b45 CW |
68 | * @return array |
69 | * (reference) of action links | |
6a488035 | 70 | */ |
00be9182 | 71 | public function &links() { |
6a488035 TO |
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 | /** | |
fe482240 | 92 | * Perform create or delete action on related memberships. |
6a488035 | 93 | * |
b2363ea8 TO |
94 | * @param string $action |
95 | * Create or delete. | |
96 | * @param array $owner | |
97 | * Primary membership info (membership_id, contact_id, membership_type ...). | |
6a488035 | 98 | */ |
00be9182 | 99 | public function relAction($action, $owner) { |
6a488035 TO |
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); | |
3506b6cd | 105 | CRM_Member_BAO_Membership::del($id); |
8d8bd076 | 106 | CRM_Core_Session::setStatus(ts('Related membership for %1 has been deleted.', array(1 => $relatedDisplayName)), |
107 | ts('Membership Deleted'), 'success'); | |
6a488035 | 108 | break; |
b09fe5ed | 109 | |
6a488035 TO |
110 | case 'create': |
111 | $ids = array(); | |
112 | $params = array( | |
8d8bd076 | 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, | |
6a488035 TO |
125 | ); |
126 | CRM_Member_BAO_Membership::create($params, $ids); | |
127 | $relatedDisplayName = CRM_Contact_BAO_Contact::displayName($params['contact_id']); | |
8d8bd076 | 128 | CRM_Core_Session::setStatus(ts('Related membership for %1 has been created.', array(1 => $relatedDisplayName)), |
129 | ts('Membership Added'), 'success'); | |
6a488035 | 130 | break; |
b09fe5ed | 131 | |
6a488035 TO |
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 | /** | |
fe482240 | 146 | * Set variables up before form is built. |
6a488035 TO |
147 | * |
148 | * @return void | |
6a488035 TO |
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 | |
8d8bd076 | 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 | } | |
6a488035 TO |
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, | |
03e04002 | 202 | CASE |
6a488035 TO |
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' | |
03e04002 | 206 | FROM civicrm_relationship |
6a488035 TO |
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 | |
8cc574cf | 228 | if (!empty($membershipType['relationship_type_id']) && empty($values['owner_membership_id'])) { |
6a488035 TO |
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 | |
8d8bd076 | 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) | |
6a488035 TO |
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 = ''; | |
8d8bd076 | 254 | foreach (array('a', 'b') as $dir) { |
2eef47ee | 255 | if (isset($relTypeDir[$dir])) { |
6a488035 | 256 | $query .= ($query ? ' UNION ' : '') |
8d8bd076 | 257 | . str_replace('_y', '_' . $dir, str_replace('_x', '_' . ($dir == 'a' ? 'b' : 'a'), $select)) |
258 | . ' AND r.relationship_type_id IN (' . implode(',', $relTypeDir[$dir]) . ')'; | |
6a488035 TO |
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); | |
8d8bd076 | 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', | |
21dfd5f5 | 275 | 'status', |
8d8bd076 | 276 | ); |
277 | ||
6a488035 TO |
278 | while ($dao->fetch()) { |
279 | $row = array(); | |
8d8bd076 | 280 | foreach ($rowElememts as $field) { |
6a488035 TO |
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'], | |
87dab4a4 AH |
290 | ), |
291 | ts('more'), | |
292 | FALSE, | |
293 | 'membership.relationship.action', | |
294 | 'Relationship', | |
295 | CRM_Utils_Request::retrieve('id', 'Positive', $this) | |
6a488035 | 296 | ); |
8d8bd076 | 297 | } |
298 | else { | |
299 | if ($relatedRemaining > 0) { | |
6a488035 TO |
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'], | |
87dab4a4 AH |
305 | ), |
306 | ts('more'), | |
307 | FALSE, | |
308 | 'membership.relationship.action', | |
309 | 'Relationship', | |
310 | CRM_Utils_Request::retrieve('id', 'Positive', $this) | |
6a488035 | 311 | ); |
8d8bd076 | 312 | } |
6a488035 TO |
313 | } |
314 | $related[] = $row; | |
315 | } | |
316 | $this->assign('related', $related); | |
317 | if ($relatedRemaining <= 0) { | |
318 | $this->assign('related_text', ts('None available')); | |
8d8bd076 | 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 | } | |
6a488035 TO |
327 | } |
328 | } | |
329 | ||
330 | $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']); | |
331 | $this->assign('displayName', $displayName); | |
03e04002 | 332 | |
6a488035 TO |
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 | ||
8d8bd076 | 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); | |
03e04002 | 340 | |
6a488035 TO |
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 | ||
8d8bd076 | 367 | CRM_Member_Page_Tab::setContext($this, $values['contact_id']); |
6a488035 TO |
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 | ||
a7488080 | 379 | if (!empty($values['is_test'])) { |
6a488035 TO |
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 | /** | |
fe482240 | 396 | * Build the form object. |
6a488035 | 397 | * |
355ba699 | 398 | * @return void |
6a488035 TO |
399 | */ |
400 | public function buildQuickForm() { | |
401 | $this->addButtons(array( | |
402 | array( | |
403 | 'type' => 'cancel', | |
404 | 'name' => ts('Done'), | |
405 | 'spacing' => ' ', | |
406 | 'isDefault' => TRUE, | |
407 | ), | |
408 | ) | |
409 | ); | |
410 | } | |
96025800 | 411 | |
6a488035 | 412 | } |