3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class generates form components for Payment-Instrument
40 class CRM_Member_Form_MembershipView
extends CRM_Core_Form
{
43 * The action links that we need to display for the browse screen
48 static $_links = NULL;
51 * Add context information at the end of a link
53 * @return text extra query parameters
55 function addContext() {
57 foreach (array('context', 'selectedChild') as $arg) {
58 if ($value = CRM_Utils_Request
::retrieve($arg, 'String', $this)) {
59 $extra .= "&{$arg}={$value}";
68 * @return array (reference) of action 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'),
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'),
91 * Perform create or delete action on related memberships
93 * @param string $action create or delete
94 * @param array $owner primary membership info (membership_id, contact_id, membership_type ...)
97 function relAction($action, $owner) {
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);
103 CRM_Member_BAO_Membership
::del($id);
104 CRM_Core_Session
::setStatus(ts('Related membership for %1 has been deleted.', array(1 => $relatedDisplayName)),
105 ts('Membership Deleted'), 'success');
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,
123 CRM_Member_BAO_Membership
::create($params, $ids);
124 $relatedDisplayName = CRM_Contact_BAO_Contact
::displayName($params['contact_id']);
125 CRM_Core_Session
::setStatus(ts('Related membership for %1 has been created.', array(1 => $relatedDisplayName)),
126 ts('Membership Added'), 'success');
129 CRM_Core_Error
::fatal(ts("Invalid action specified in URL"));
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()
142 * Function to set variables up before form is built
147 public function preProcess() {
150 $id = CRM_Utils_Request
::retrieve('id', 'Positive', $this);
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);
157 $params = array('id' => $id);
159 CRM_Member_BAO_Membership
::retrieve($params, $values);
160 $membershipType = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($values['membership_type_id']);
162 // Do the action on related Membership if needed
163 $relAction = CRM_Utils_Request
::retrieve('relAction', 'String', $this);
165 $this->relAction($relAction, $values);
168 // build associated contributions
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);
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',
178 'owner_membership_id'
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'],
188 $values['owner_display_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
189 $values['owner_contact_id'],
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']);
198 SELECT relationship_type_id,
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'
203 FROM civicrm_relationship
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'] .= ',';
214 $values['relationship'] .= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType',
223 $this->assign('has_related', FALSE);
224 // if membership can be granted, and we are the owner of the membership
225 if (CRM_Utils_Array
::value('relationship_type_id', $membershipType)
226 && !CRM_Utils_Array
::value('owner_membership_id', $values)
228 // display related contacts/membership block
229 $this->assign('has_related', TRUE);
230 $this->assign('max_related', CRM_Utils_Array
::value('max_related', $values, ts('Unlimited')));
231 // split the relations in 2 arrays based on direction
232 $relTypeId = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $membershipType['relationship_type_id']);
233 $relDirection = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $membershipType['relationship_direction']);
234 foreach ($relTypeId as $rid) {
235 $dir = each($relDirection);
236 $relTypeDir[substr($dir['value'], 0, 1)][] = $rid;
238 // build query in 2 parts with a UNION if necessary
239 // _x and _y are replaced with _a and _b first, then vice-versa
240 // comment is a qualifier for the relationship - now just job_title
242 SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment,
243 rt.name_x_y as relation, r.start_date, r.end_date,
244 m.id as mid, ms.is_current_member, ms.label as status
245 FROM civicrm_relationship r
246 LEFT JOIN civicrm_relationship_type rt ON rt.id = r.relationship_type_id
247 LEFT JOIN civicrm_contact c ON c.id = r.contact_id_x
248 LEFT JOIN civicrm_membership m ON (m.owner_membership_id = {$values['id']}
249 AND m.contact_id = r.contact_id_x AND m.is_test = 0)
250 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
251 WHERE r.contact_id_y = {$values['contact_id']} AND r.is_active = 1 AND c.is_deleted = 0";
253 foreach (array('a', 'b') as $dir) {
254 if (CRM_Utils_Array
::value($dir, $relTypeDir)) {
255 $query .= ($query ?
' UNION ' : '')
256 . str_replace('_y', '_' . $dir, str_replace('_x', '_' . ($dir == 'a' ?
'b' : 'a'), $select))
257 . ' AND r.relationship_type_id IN (' . implode(',', $relTypeDir[$dir]) . ')';
260 $query .= " ORDER BY is_current_member DESC";
261 $dao = CRM_Core_DAO
::executeQuery($query);
263 $relatedRemaining = CRM_Utils_Array
::value('max_related', $values, PHP_INT_MAX
);
264 $rowElememts = array(
277 while ($dao->fetch()) {
279 foreach ($rowElememts as $field) {
280 $row[$field] = $dao->$field;
282 if ($row['mid'] && ($row['is_current_member'] == 1)) {
284 $row['action'] = CRM_Core_Action
::formLink(self
::links(), CRM_Core_Action
::DELETE
,
286 'id' => CRM_Utils_Request
::retrieve('id', 'Positive', $this),
287 'cid' => $row['cid'],
288 'mid' => $row['mid'],
293 if ($relatedRemaining > 0) {
294 $row['action'] = CRM_Core_Action
::formLink(self
::links(), CRM_Core_Action
::ADD
,
296 'id' => CRM_Utils_Request
::retrieve('id', 'Positive', $this),
297 'cid' => $row['cid'],
298 'rid' => $row['cid'],
305 $this->assign('related', $related);
306 if ($relatedRemaining <= 0) {
307 $this->assign('related_text', ts('None available'));
310 if ($relatedRemaining < 100000) {
311 $this->assign('related_text', ts('%1 available', array(1 => $relatedRemaining)));
314 $this->assign('related_text', ts('Unlimited', array(1 => $relatedRemaining)));
319 $displayName = CRM_Contact_BAO_Contact
::displayName($values['contact_id']);
320 $this->assign('displayName', $displayName);
322 // Check if this is default domain contact CRM-10482
323 if (CRM_Contact_BAO_Contact
::checkDomainContact($values['contact_id'])) {
324 $displayName .= ' (' . ts('default organization') . ')';
327 // omitting contactImage from title for now since the summary overlay css doesn't work outside crm-container
328 CRM_Utils_System
::setTitle(ts('View Membership for') . ' ' . $displayName);
330 // add viewed membership to recent items list
331 $recentTitle = $displayName . ' - ' . ts('Membership Type:') . ' ' . $values['membership_type'];
332 $url = CRM_Utils_System
::url('civicrm/contact/view/membership',
333 "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
336 $recentOther = array();
337 if (CRM_Core_Permission
::checkActionPermission('CiviMember', CRM_Core_Action
::UPDATE
)) {
338 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/membership',
339 "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
342 if (CRM_Core_Permission
::checkActionPermission('CiviMember', CRM_Core_Action
::DELETE
)) {
343 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/membership',
344 "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
347 CRM_Utils_Recent
::add($recentTitle,
351 $values['contact_id'],
356 CRM_Member_Page_Tab
::setContext($this, $values['contact_id']);
358 $memType = CRM_Core_DAO
::getFieldValue("CRM_Member_DAO_Membership", $id, "membership_type_id");
360 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Membership', $this, $id, 0, $memType);
361 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $groupTree);
363 $isRecur = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $id, 'contribution_recur_id');
365 $autoRenew = $isRecur ?
TRUE : FALSE;
368 if (CRM_Utils_Array
::value('is_test', $values)) {
369 $values['membership_type'] .= ' (test) ';
372 $subscriptionCancelled = CRM_Member_BAO_Membership
::isSubscriptionCancelled($id);
373 $values['auto_renew'] = ($autoRenew && !$subscriptionCancelled) ?
'Yes' : 'No';
375 //do check for campaigns
376 if ($campaignId = CRM_Utils_Array
::value('campaign_id', $values)) {
377 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns($campaignId);
378 $values['campaign'] = $campaigns[$campaignId];
381 $this->assign($values);
385 * Function to build the form
390 public function buildQuickForm() {
391 $this->addButtons(array(
394 'name' => ts('Done'),
395 'spacing' => ' ',