Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | */ | |
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 | * @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 | */ | |
55 | 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 (reference) of action links | |
69 | */ | |
70 | function &links() { | |
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 | */ | |
97 | function relAction($action, $owner) { | |
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 | /** | |
142 | * Function to set variables up before form is built | |
143 | * | |
144 | * @return void | |
145 | * @access public | |
146 | */ | |
147 | public function preProcess() { | |
148 | ||
149 | $values = array(); | |
150 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
151 | ||
152 | // Make sure context is assigned to template for condition where we come here view civicrm/membership/view | |
153 | $context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
154 | $this->assign('context', $context); | |
155 | ||
156 | if ($id) { | |
157 | $params = array('id' => $id); | |
158 | ||
159 | CRM_Member_BAO_Membership::retrieve($params, $values); | |
160 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); | |
161 | ||
162 | // Do the action on related Membership if needed | |
163 | $relAction = CRM_Utils_Request::retrieve('relAction', 'String', $this); | |
164 | if ($relAction) { | |
165 | $this->relAction($relAction, $values); | |
166 | } | |
167 | ||
168 | // build associated contributions | |
8d8bd076 | 169 | $this->assign('accessContribution', FALSE); |
170 | if (CRM_Core_Permission::access('CiviContribute')) { | |
171 | $this->assign('accessContribution', TRUE); | |
172 | CRM_Member_Page_Tab::associatedContribution($values['contact_id'], $id); | |
173 | } | |
6a488035 TO |
174 | |
175 | //Provide information about membership source when it is the result of a relationship (CRM-1901) | |
176 | $values['owner_membership_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
177 | $id, | |
178 | 'owner_membership_id' | |
179 | ); | |
180 | ||
181 | if (isset($values['owner_membership_id'])) { | |
182 | $values['owner_contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
183 | $values['owner_membership_id'], | |
184 | 'contact_id', | |
185 | 'id' | |
186 | ); | |
187 | ||
188 | $values['owner_display_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
189 | $values['owner_contact_id'], | |
190 | 'display_name', | |
191 | 'id' | |
192 | ); | |
193 | ||
194 | $direction = strrev($membershipType['relationship_direction']); | |
195 | // To display relationship type in view membership page | |
196 | $relTypeIds = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ",", $membershipType['relationship_type_id']); | |
197 | $sql = " | |
198 | SELECT relationship_type_id, | |
03e04002 | 199 | CASE |
6a488035 TO |
200 | WHEN contact_id_a = {$values['owner_contact_id']} AND contact_id_b = {$values['contact_id']} THEN 'b_a' |
201 | WHEN contact_id_b = {$values['owner_contact_id']} AND contact_id_a = {$values['contact_id']} THEN 'a_b' | |
202 | END AS 'relType' | |
03e04002 | 203 | FROM civicrm_relationship |
6a488035 TO |
204 | WHERE relationship_type_id IN ($relTypeIds)"; |
205 | $dao = CRM_Core_DAO::executeQuery($sql); | |
206 | $values['relationship'] = NULL; | |
207 | while ($dao->fetch()) { | |
208 | $typeId = $dao->relationship_type_id; | |
209 | $direction = $dao->relType; | |
210 | if ($direction && $typeId) { | |
211 | if ($values['relationship']) { | |
212 | $values['relationship'] .= ','; | |
213 | } | |
214 | $values['relationship'] .= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', | |
215 | $typeId, | |
216 | "name_$direction", | |
217 | 'id' | |
218 | ); | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | $this->assign('has_related', FALSE); | |
224 | // if membership can be granted, and we are the owner of the membership | |
8cc574cf | 225 | if (!empty($membershipType['relationship_type_id']) && empty($values['owner_membership_id'])) { |
6a488035 TO |
226 | // display related contacts/membership block |
227 | $this->assign('has_related', TRUE); | |
228 | $this->assign('max_related', CRM_Utils_Array::value('max_related', $values, ts('Unlimited'))); | |
229 | // split the relations in 2 arrays based on direction | |
230 | $relTypeId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']); | |
231 | $relDirection = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']); | |
232 | foreach ($relTypeId as $rid) { | |
233 | $dir = each($relDirection); | |
234 | $relTypeDir[substr($dir['value'], 0, 1)][] = $rid; | |
235 | } | |
236 | // build query in 2 parts with a UNION if necessary | |
237 | // _x and _y are replaced with _a and _b first, then vice-versa | |
238 | // comment is a qualifier for the relationship - now just job_title | |
239 | $select = " | |
240 | SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment, | |
241 | rt.name_x_y as relation, r.start_date, r.end_date, | |
242 | m.id as mid, ms.is_current_member, ms.label as status | |
243 | FROM civicrm_relationship r | |
244 | LEFT JOIN civicrm_relationship_type rt ON rt.id = r.relationship_type_id | |
245 | LEFT JOIN civicrm_contact c ON c.id = r.contact_id_x | |
8d8bd076 | 246 | LEFT JOIN civicrm_membership m ON (m.owner_membership_id = {$values['id']} |
247 | AND m.contact_id = r.contact_id_x AND m.is_test = 0) | |
6a488035 TO |
248 | LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id |
249 | WHERE r.contact_id_y = {$values['contact_id']} AND r.is_active = 1 AND c.is_deleted = 0"; | |
250 | $query = ''; | |
8d8bd076 | 251 | foreach (array('a', 'b') as $dir) { |
2eef47ee | 252 | if (isset($relTypeDir[$dir])) { |
6a488035 | 253 | $query .= ($query ? ' UNION ' : '') |
8d8bd076 | 254 | . str_replace('_y', '_' . $dir, str_replace('_x', '_' . ($dir == 'a' ? 'b' : 'a'), $select)) |
255 | . ' AND r.relationship_type_id IN (' . implode(',', $relTypeDir[$dir]) . ')'; | |
6a488035 TO |
256 | } |
257 | } | |
258 | $query .= " ORDER BY is_current_member DESC"; | |
259 | $dao = CRM_Core_DAO::executeQuery($query); | |
260 | $related = array(); | |
261 | $relatedRemaining = CRM_Utils_Array::value('max_related', $values, PHP_INT_MAX); | |
8d8bd076 | 262 | $rowElememts = array( |
263 | 'id', | |
264 | 'cid', | |
265 | 'name', | |
266 | 'comment', | |
267 | 'relation', | |
268 | 'mid', | |
269 | 'start_date', | |
270 | 'end_date', | |
271 | 'is_current_member', | |
272 | 'status' | |
273 | ); | |
274 | ||
6a488035 TO |
275 | while ($dao->fetch()) { |
276 | $row = array(); | |
8d8bd076 | 277 | foreach ($rowElememts as $field) { |
6a488035 TO |
278 | $row[$field] = $dao->$field; |
279 | } | |
280 | if ($row['mid'] && ($row['is_current_member'] == 1)) { | |
281 | $relatedRemaining--; | |
282 | $row['action'] = CRM_Core_Action::formLink(self::links(), CRM_Core_Action::DELETE, | |
283 | array( | |
284 | 'id' => CRM_Utils_Request::retrieve('id', 'Positive', $this), | |
285 | 'cid' => $row['cid'], | |
286 | 'mid' => $row['mid'], | |
87dab4a4 AH |
287 | ), |
288 | ts('more'), | |
289 | FALSE, | |
290 | 'membership.relationship.action', | |
291 | 'Relationship', | |
292 | CRM_Utils_Request::retrieve('id', 'Positive', $this) | |
6a488035 | 293 | ); |
8d8bd076 | 294 | } |
295 | else { | |
296 | if ($relatedRemaining > 0) { | |
6a488035 TO |
297 | $row['action'] = CRM_Core_Action::formLink(self::links(), CRM_Core_Action::ADD, |
298 | array( | |
299 | 'id' => CRM_Utils_Request::retrieve('id', 'Positive', $this), | |
300 | 'cid' => $row['cid'], | |
301 | 'rid' => $row['cid'], | |
87dab4a4 AH |
302 | ), |
303 | ts('more'), | |
304 | FALSE, | |
305 | 'membership.relationship.action', | |
306 | 'Relationship', | |
307 | CRM_Utils_Request::retrieve('id', 'Positive', $this) | |
6a488035 | 308 | ); |
8d8bd076 | 309 | } |
6a488035 TO |
310 | } |
311 | $related[] = $row; | |
312 | } | |
313 | $this->assign('related', $related); | |
314 | if ($relatedRemaining <= 0) { | |
315 | $this->assign('related_text', ts('None available')); | |
8d8bd076 | 316 | } |
317 | else { | |
318 | if ($relatedRemaining < 100000) { | |
319 | $this->assign('related_text', ts('%1 available', array(1 => $relatedRemaining))); | |
320 | } | |
321 | else { | |
322 | $this->assign('related_text', ts('Unlimited', array(1 => $relatedRemaining))); | |
323 | } | |
6a488035 TO |
324 | } |
325 | } | |
326 | ||
327 | $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']); | |
328 | $this->assign('displayName', $displayName); | |
03e04002 | 329 | |
6a488035 TO |
330 | // Check if this is default domain contact CRM-10482 |
331 | if (CRM_Contact_BAO_Contact::checkDomainContact($values['contact_id'])) { | |
332 | $displayName .= ' (' . ts('default organization') . ')'; | |
333 | } | |
334 | ||
8d8bd076 | 335 | // omitting contactImage from title for now since the summary overlay css doesn't work outside crm-container |
336 | CRM_Utils_System::setTitle(ts('View Membership for') . ' ' . $displayName); | |
03e04002 | 337 | |
6a488035 TO |
338 | // add viewed membership to recent items list |
339 | $recentTitle = $displayName . ' - ' . ts('Membership Type:') . ' ' . $values['membership_type']; | |
340 | $url = CRM_Utils_System::url('civicrm/contact/view/membership', | |
341 | "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" | |
342 | ); | |
343 | ||
344 | $recentOther = array(); | |
345 | if (CRM_Core_Permission::checkActionPermission('CiviMember', CRM_Core_Action::UPDATE)) { | |
346 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', | |
347 | "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" | |
348 | ); | |
349 | } | |
350 | if (CRM_Core_Permission::checkActionPermission('CiviMember', CRM_Core_Action::DELETE)) { | |
351 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', | |
352 | "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home" | |
353 | ); | |
354 | } | |
355 | CRM_Utils_Recent::add($recentTitle, | |
356 | $url, | |
357 | $values['id'], | |
358 | 'Membership', | |
359 | $values['contact_id'], | |
360 | NULL, | |
361 | $recentOther | |
362 | ); | |
363 | ||
8d8bd076 | 364 | CRM_Member_Page_Tab::setContext($this, $values['contact_id']); |
6a488035 TO |
365 | |
366 | $memType = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $id, "membership_type_id"); | |
367 | ||
368 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Membership', $this, $id, 0, $memType); | |
369 | CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree); | |
370 | ||
371 | $isRecur = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $id, 'contribution_recur_id'); | |
372 | ||
373 | $autoRenew = $isRecur ? TRUE : FALSE; | |
374 | } | |
375 | ||
a7488080 | 376 | if (!empty($values['is_test'])) { |
6a488035 TO |
377 | $values['membership_type'] .= ' (test) '; |
378 | } | |
379 | ||
380 | $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled($id); | |
381 | $values['auto_renew'] = ($autoRenew && !$subscriptionCancelled) ? 'Yes' : 'No'; | |
382 | ||
383 | //do check for campaigns | |
384 | if ($campaignId = CRM_Utils_Array::value('campaign_id', $values)) { | |
385 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); | |
386 | $values['campaign'] = $campaigns[$campaignId]; | |
387 | } | |
388 | ||
389 | $this->assign($values); | |
390 | } | |
391 | ||
392 | /** | |
393 | * Function to build the form | |
394 | * | |
355ba699 | 395 | * @return void |
6a488035 TO |
396 | * @access public |
397 | */ | |
398 | public function buildQuickForm() { | |
399 | $this->addButtons(array( | |
400 | array( | |
401 | 'type' => 'cancel', | |
402 | 'name' => ts('Done'), | |
403 | 'spacing' => ' ', | |
404 | 'isDefault' => TRUE, | |
405 | ), | |
406 | ) | |
407 | ); | |
408 | } | |
409 | } | |
410 |