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)), ts('Membership Deleted'), 'success');
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,
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');
127 CRM_Core_Error
::fatal(ts("Invalid action specified in URL"));
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()
140 * Function to set variables up before form is built
145 public function preProcess() {
148 $id = CRM_Utils_Request
::retrieve('id', 'Positive', $this);
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);
155 $params = array('id' => $id);
157 CRM_Member_BAO_Membership
::retrieve($params, $values);
158 $membershipType = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($values['membership_type_id']);
160 // Do the action on related Membership if needed
161 $relAction = CRM_Utils_Request
::retrieve('relAction', 'String', $this);
163 $this->relAction($relAction, $values);
166 // build associated contributions
167 CRM_Member_Page_Tab
::associatedContribution($values['contact_id'], $id);
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',
172 'owner_membership_id'
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'],
182 $values['owner_display_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
183 $values['owner_contact_id'],
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']);
192 SELECT relationship_type_id,
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'
197 FROM civicrm_relationship
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'] .= ',';
208 $values['relationship'] .= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType',
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;
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
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";
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]) .')';
252 $query .= " ORDER BY is_current_member DESC";
253 $dao = CRM_Core_DAO
::executeQuery($query);
255 $relatedRemaining = CRM_Utils_Array
::value('max_related', $values, PHP_INT_MAX
);
256 while ($dao->fetch()) {
258 foreach (array('id', 'cid', 'name', 'comment', 'relation', 'mid', 'start_date', 'end_date', 'is_current_member', 'status') as $field) {
259 $row[$field] = $dao->$field;
261 if ($row['mid'] && ($row['is_current_member'] == 1)) {
263 $row['action'] = CRM_Core_Action
::formLink(self
::links(), CRM_Core_Action
::DELETE
,
265 'id' => CRM_Utils_Request
::retrieve('id', 'Positive', $this),
266 'cid' => $row['cid'],
267 'mid' => $row['mid'],
270 } else if ($relatedRemaining > 0) {
271 $row['action'] = CRM_Core_Action
::formLink(self
::links(), CRM_Core_Action
::ADD
,
273 'id' => CRM_Utils_Request
::retrieve('id', 'Positive', $this),
274 'cid' => $row['cid'],
275 'rid' => $row['cid'],
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)));
287 $this->assign('related_text', ts('Unlimited', array(1 => $relatedRemaining)));
291 $displayName = CRM_Contact_BAO_Contact
::displayName($values['contact_id']);
292 $this->assign('displayName', $displayName);
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') . ')';
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);
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"
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"
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"
319 CRM_Utils_Recent
::add($recentTitle,
323 $values['contact_id'],
328 CRM_Member_Page_Tab
::setContext($values['contact_id']);
330 $memType = CRM_Core_DAO
::getFieldValue("CRM_Member_DAO_Membership", $id, "membership_type_id");
332 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Membership', $this, $id, 0, $memType);
333 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $groupTree);
335 $isRecur = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $id, 'contribution_recur_id');
337 $autoRenew = $isRecur ?
TRUE : FALSE;
340 if (CRM_Utils_Array
::value('is_test', $values)) {
341 $values['membership_type'] .= ' (test) ';
344 $subscriptionCancelled = CRM_Member_BAO_Membership
::isSubscriptionCancelled($id);
345 $values['auto_renew'] = ($autoRenew && !$subscriptionCancelled) ?
'Yes' : 'No';
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];
353 $this->assign($values);
357 * Function to build the form
362 public function buildQuickForm() {
363 $this->addButtons(array(
366 'name' => ts('Done'),
367 'spacing' => ' ',