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