From 8d8bd076f913fff40473c05b0969e13c56517ca8 Mon Sep 17 00:00:00 2001 From: kurund Date: Tue, 13 Aug 2013 14:12:56 +0530 Subject: [PATCH] cleaned up membership edit/view to show associated contributions and also notices fixes --- CRM/Member/BAO/Membership.php | 3 +- CRM/Member/Form/MembershipView.php | 86 ++++++++++++------ CRM/Member/Page/Tab.php | 136 +++++++++++++---------------- 3 files changed, 119 insertions(+), 106 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 1ec61981d2..30a1adb520 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -310,7 +310,8 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { //record contribution for this membership if (CRM_Utils_Array::value('contribution_status_id', $params) && !CRM_Utils_Array::value('relate_contribution_id', $params)) { - $params['contribution'] = self::recordMembershipContribution( array_merge($params, array('membership_id' => $membership->id)), $ids); + $memInfo = array_merge($params, array('membership_id' => $membership->id)); + $params['contribution'] = self::recordMembershipContribution($memInfo, $ids); } //insert payment record for this membership diff --git a/CRM/Member/Form/MembershipView.php b/CRM/Member/Form/MembershipView.php index 451514fa75..e658ae4ec6 100644 --- a/CRM/Member/Form/MembershipView.php +++ b/CRM/Member/Form/MembershipView.php @@ -101,27 +101,29 @@ class CRM_Member_Form_MembershipView extends CRM_Core_Form { $relatedContactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this); $relatedDisplayName = CRM_Contact_BAO_Contact::displayName($relatedContactId); CRM_Member_BAO_Membership::del($id); - CRM_Core_Session::setStatus(ts('Related membership for %1 has been deleted.', array(1 => $relatedDisplayName)), ts('Membership Deleted'), 'success'); + CRM_Core_Session::setStatus(ts('Related membership for %1 has been deleted.', array(1 => $relatedDisplayName)), + ts('Membership Deleted'), 'success'); break; case 'create': $ids = array(); $params = array( - 'contact_id' => CRM_Utils_Request::retrieve('rid', 'Positive', $this), - 'membership_type_id' => $owner['membership_type_id'], - 'owner_membership_id' => $owner['id'], - 'join_date' => CRM_Utils_Date::processDate($owner['join_date'], NULL, TRUE, 'Ymd'), - 'start_date' => CRM_Utils_Date::processDate($owner['start_date'], NULL, TRUE, 'Ymd'), - 'end_date' => CRM_Utils_Date::processDate($owner['end_date'], NULL, TRUE, 'Ymd'), - 'source' => ts('Manual Assignment of Related Membership'), - 'is_test' => $owner['is_test'], - 'campaign_id' => CRM_Utils_Array::value('campaign_id', $owner), - 'status_id' => $owner['status_id'], - 'skipStatusCal' => TRUE, - 'createActivity' => TRUE, + 'contact_id' => CRM_Utils_Request::retrieve('rid', 'Positive', $this), + 'membership_type_id' => $owner['membership_type_id'], + 'owner_membership_id' => $owner['id'], + 'join_date' => CRM_Utils_Date::processDate($owner['join_date'], NULL, TRUE, 'Ymd'), + 'start_date' => CRM_Utils_Date::processDate($owner['start_date'], NULL, TRUE, 'Ymd'), + 'end_date' => CRM_Utils_Date::processDate($owner['end_date'], NULL, TRUE, 'Ymd'), + 'source' => ts('Manual Assignment of Related Membership'), + 'is_test' => $owner['is_test'], + 'campaign_id' => CRM_Utils_Array::value('campaign_id', $owner), + 'status_id' => $owner['status_id'], + 'skipStatusCal' => TRUE, + 'createActivity' => TRUE, ); CRM_Member_BAO_Membership::create($params, $ids); $relatedDisplayName = CRM_Contact_BAO_Contact::displayName($params['contact_id']); - CRM_Core_Session::setStatus(ts('Related membership for %1 has been created.', array(1 => $relatedDisplayName)), ts('Membership Added'), 'success'); + CRM_Core_Session::setStatus(ts('Related membership for %1 has been created.', array(1 => $relatedDisplayName)), + ts('Membership Added'), 'success'); break; default: CRM_Core_Error::fatal(ts("Invalid action specified in URL")); @@ -164,7 +166,11 @@ class CRM_Member_Form_MembershipView extends CRM_Core_Form { } // build associated contributions - CRM_Member_Page_Tab::associatedContribution($values['contact_id'], $id); + $this->assign('accessContribution', FALSE); + if (CRM_Core_Permission::access('CiviContribute')) { + $this->assign('accessContribution', TRUE); + CRM_Member_Page_Tab::associatedContribution($values['contact_id'], $id); + } //Provide information about membership source when it is the result of a relationship (CRM-1901) $values['owner_membership_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', @@ -217,7 +223,8 @@ END AS 'relType' $this->assign('has_related', FALSE); // if membership can be granted, and we are the owner of the membership if (CRM_Utils_Array::value('relationship_type_id', $membershipType) - && !CRM_Utils_Array::value('owner_membership_id', $values)) { + && !CRM_Utils_Array::value('owner_membership_id', $values) + ) { // display related contacts/membership block $this->assign('has_related', TRUE); $this->assign('max_related', CRM_Utils_Array::value('max_related', $values, ts('Unlimited'))); @@ -238,24 +245,38 @@ SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment, FROM civicrm_relationship r LEFT JOIN civicrm_relationship_type rt ON rt.id = r.relationship_type_id LEFT JOIN civicrm_contact c ON c.id = r.contact_id_x - 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) + 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) LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id WHERE r.contact_id_y = {$values['contact_id']} AND r.is_active = 1 AND c.is_deleted = 0"; $query = ''; - foreach (array('a', 'b') as $dir ) { + foreach (array('a', 'b') as $dir) { if (CRM_Utils_Array::value($dir, $relTypeDir)) { $query .= ($query ? ' UNION ' : '') - . str_replace('_y', '_'.$dir, str_replace('_x', '_'.($dir=='a'?'b':'a'), $select)) - . ' AND r.relationship_type_id IN (' . implode(',', $relTypeDir[$dir]) .')'; + . str_replace('_y', '_' . $dir, str_replace('_x', '_' . ($dir == 'a' ? 'b' : 'a'), $select)) + . ' AND r.relationship_type_id IN (' . implode(',', $relTypeDir[$dir]) . ')'; } } $query .= " ORDER BY is_current_member DESC"; $dao = CRM_Core_DAO::executeQuery($query); $related = array(); $relatedRemaining = CRM_Utils_Array::value('max_related', $values, PHP_INT_MAX); + $rowElememts = array( + 'id', + 'cid', + 'name', + 'comment', + 'relation', + 'mid', + 'start_date', + 'end_date', + 'is_current_member', + 'status' + ); + while ($dao->fetch()) { $row = array(); - foreach (array('id', 'cid', 'name', 'comment', 'relation', 'mid', 'start_date', 'end_date', 'is_current_member', 'status') as $field) { + foreach ($rowElememts as $field) { $row[$field] = $dao->$field; } if ($row['mid'] && ($row['is_current_member'] == 1)) { @@ -267,7 +288,9 @@ SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment, 'mid' => $row['mid'], ) ); - } else if ($relatedRemaining > 0) { + } + else { + if ($relatedRemaining > 0) { $row['action'] = CRM_Core_Action::formLink(self::links(), CRM_Core_Action::ADD, array( 'id' => CRM_Utils_Request::retrieve('id', 'Positive', $this), @@ -275,16 +298,21 @@ SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment, 'rid' => $row['cid'], ) ); + } } $related[] = $row; } $this->assign('related', $related); if ($relatedRemaining <= 0) { $this->assign('related_text', ts('None available')); - } else if ($relatedRemaining < 100000) { - $this->assign('related_text', ts('%1 available', array(1 => $relatedRemaining))); - } else { - $this->assign('related_text', ts('Unlimited', array(1 => $relatedRemaining))); + } + else { + if ($relatedRemaining < 100000) { + $this->assign('related_text', ts('%1 available', array(1 => $relatedRemaining))); + } + else { + $this->assign('related_text', ts('Unlimited', array(1 => $relatedRemaining))); + } } } @@ -296,8 +324,8 @@ SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment, $displayName .= ' (' . ts('default organization') . ')'; } - // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container - CRM_Utils_System::setTitle(ts('View Membership for') . ' ' . $displayName); + // omitting contactImage from title for now since the summary overlay css doesn't work outside crm-container + CRM_Utils_System::setTitle(ts('View Membership for') . ' ' . $displayName); // add viewed membership to recent items list $recentTitle = $displayName . ' - ' . ts('Membership Type:') . ' ' . $values['membership_type']; @@ -325,7 +353,7 @@ SELECT r.id, c.id as cid, c.display_name as name, c.job_title as comment, $recentOther ); - CRM_Member_Page_Tab::setContext($values['contact_id']); + CRM_Member_Page_Tab::setContext($this, $values['contact_id']); $memType = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $id, "membership_type_id"); diff --git a/CRM/Member/Page/Tab.php b/CRM/Member/Page/Tab.php index fa337d57c3..4807df6168 100644 --- a/CRM/Member/Page/Tab.php +++ b/CRM/Member/Page/Tab.php @@ -55,10 +55,10 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { function browse() { $links = self::links('all', $this->_isPaymentProcessor, $this->_accessContribution); - $membership = array(); - $dao = new CRM_Member_DAO_Membership(); + $membership = array(); + $dao = new CRM_Member_DAO_Membership(); $dao->contact_id = $this->_contactId; - $dao->is_test = 0; + $dao->is_test = 0; //$dao->orderBy('name'); $dao->find(); @@ -89,8 +89,7 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { //get the membership status and type values. $statusANDType = CRM_Member_BAO_Membership::getStatusANDTypeValues($dao->id); - foreach (array( - 'status', 'membership_type') as $fld) { + foreach (array('status', 'membership_type') as $fld) { $membership[$dao->id][$fld] = CRM_Utils_Array::value($fld, $statusANDType[$dao->id]); } if (CRM_Utils_Array::value('is_current_member', $statusANDType[$dao->id])) { @@ -103,13 +102,15 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { $currentMask = $currentMask & ~CRM_Core_Action::RENEW & ~CRM_Core_Action::FOLLOWUP; } - $isUpdateBilling = false; - $paymentObject = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($membership[$dao->id]['membership_id'], 'membership', 'obj'); + $isUpdateBilling = FALSE; + $paymentObject = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity( + $membership[$dao->id]['membership_id'], 'membership', 'obj'); if (!empty($paymentObject)) { $isUpdateBilling = $paymentObject->isSupported('updateSubscriptionBillingInfo'); } - $isCancelSupported = CRM_Member_BAO_Membership::isCancelSubscriptionSupported($membership[$dao->id]['membership_id']); + $isCancelSupported = CRM_Member_BAO_Membership::isCancelSubscriptionSupported( + $membership[$dao->id]['membership_id']); $membership[$dao->id]['action'] = CRM_Core_Action::formLink(self::links('all', NULL, @@ -145,9 +146,10 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { } // if relevant, count related memberships - if (CRM_Utils_Array::value('is_current_member', $statusANDType[$dao->id]) // membership is active - && CRM_Utils_Array::value('relationship_type_id', $statusANDType[$dao->id]) // membership type allows inheritance - && empty($dao->owner_membership_id)) { // not an related membership + if (CRM_Utils_Array::value('is_current_member', $statusANDType[$dao->id]) // membership is active + && CRM_Utils_Array::value('relationship_type_id', $statusANDType[$dao->id]) // membership type allows inheritance + && empty($dao->owner_membership_id) + ) { // not an related membership $query = " SELECT COUNT(m.id) FROM civicrm_membership m @@ -160,7 +162,8 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { ts('%1 created', array(1 => $num_related)) : ts('%1 out of %2', array(1 => $num_related, 2 => $max_related)) ); - } else { + } + else { $membership[$dao->id]['related_count'] = ts('N/A'); } } @@ -222,9 +225,13 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { CRM_Utils_System::redirectToSSL(); } - if ($this->_action != CRM_Core_Action::ADD) { - // get associated contributions only on edit/renew/delete - $this->associatedContribution(); + // build associated contributions ( note: this is called to show associated contributions in edit mode ) + if ($this->_action & CRM_Core_Action::UPDATE) { + $this->assign('accessContribution', FALSE); + if (CRM_Core_Permission::access('CiviContribute')) { + $this->assign('accessContribution', TRUE); + CRM_Member_Page_Tab::associatedContribution($this->_contactId, $this->_id); + } } if ($this->_action & CRM_Core_Action::RENEW) { @@ -235,9 +242,8 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { $path = 'CRM_Member_Form_Membership'; $title = ts('Create Membership'); } - $controller = new CRM_Core_Controller_Simple( - $path, $title, $this->_action - ); + + $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action); $controller->setEmbedded(TRUE); $controller->set('BAOName', $this->getBAOName()); $controller->set('id', $this->_id); @@ -246,9 +252,9 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { } function preProcess() { - $context = CRM_Utils_Request::retrieve('context', 'String', $this); + $context = CRM_Utils_Request::retrieve('context', 'String', $this); $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); - $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); + $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); if ($context == 'standalone') { $this->_action = CRM_Core_Action::ADD; @@ -304,44 +310,39 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { if ($this->_action & CRM_Core_Action::VIEW) { $this->view(); } - elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) { - $this->setContext(); + elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE + | CRM_Core_Action::RENEW)) { + self::setContext($this); $this->edit(); } else { - $this->setContext(); + self::setContext($this); $this->browse(); } return parent::run(); } - function setContext($contactId = NULL) { - $context = CRM_Utils_Request::retrieve('context', - 'String', $this, FALSE, 'search' - ); + public static function setContext(&$form, $contactId = NULL) { + $context = CRM_Utils_Request::retrieve('context', 'String', $form, FALSE, 'search' ); - $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this); + $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form); //validate the qfKey if (!CRM_Utils_Rule::qfKey($qfKey)) { $qfKey = NULL; } if (!$contactId) { - $contactId = $this->_contactId; + $contactId = $form->_contactId; } switch ($context) { case 'dashboard': - $url = CRM_Utils_System::url('civicrm/member', - 'reset=1' - ); + $url = CRM_Utils_System::url('civicrm/member', 'reset=1'); break; case 'membership': - $url = CRM_Utils_System::url('civicrm/contact/view', - "reset=1&force=1&cid={$contactId}&selectedChild=member" - ); + $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member"); break; case 'search': @@ -349,7 +350,7 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { if ($qfKey) { $urlParams .= "&qfKey=$qfKey"; } - $this->assign('searchKey', $qfKey); + $form->assign('searchKey', $qfKey); $url = CRM_Utils_System::url('civicrm/member/search', $urlParams); break; @@ -369,12 +370,12 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { break; case 'fulltext': - $action = CRM_Utils_Request::retrieve('action', 'String', $this); - $keyName = '&qfKey'; + $action = CRM_Utils_Request::retrieve('action', 'String', $form); + $keyName = '&qfKey'; $urlParams = 'force=1'; $urlString = 'civicrm/contact/search/custom'; if ($action == CRM_Core_Action::UPDATE) { - if ($this->_contactId) { + if ($form->_contactId) { $urlParams .= '&cid=' . $this->_contactId; } $keyName = '&key'; @@ -384,7 +385,7 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { if ($qfKey) { $urlParams .= "$keyName=$qfKey"; } - $this->assign('searchKey', $qfKey); + $form->assign('searchKey', $qfKey); $url = CRM_Utils_System::url($urlString, $urlParams); break; @@ -393,9 +394,7 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { if ($contactId) { $cid = '&cid=' . $contactId; } - $url = CRM_Utils_System::url('civicrm/member/search', - 'force=1' . $cid - ); + $url = CRM_Utils_System::url('civicrm/member/search', 'force=1' . $cid); break; } @@ -410,10 +409,10 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { * @static */ static function &links($status = 'all', - $isPaymentProcessor = NULL, - $accessContribution = NULL, - $isCancelSupported = FALSE, - $isUpdateBilling = FALSE + $isPaymentProcessor = NULL, + $accessContribution = NULL, + $isCancelSupported = FALSE, + $isUpdateBilling = FALSE ) { if (!CRM_Utils_Array::value('view', self::$_links)) { self::$_links['view'] = array( @@ -524,35 +523,20 @@ class CRM_Member_Page_Tab extends CRM_Core_Page { * @access public */ public static function associatedContribution($contactId = NULL, $membershipId = NULL) { - if (!$contactId) { - $contactId = $this->_contactId; - } - - if (!$membershipId) { - $membershipId = $this->_id; - } - - // retrieive membership contributions if the $membershipId is set - if (CRM_Core_Permission::access('CiviContribute') && $membershipId) { - $this->assign('accessContribution', TRUE); - $controller = new CRM_Core_Controller_Simple( - 'CRM_Contribute_Form_Search', - ts('Contributions'), - NULL, - FALSE, FALSE, TRUE - ); - $controller->setEmbedded(TRUE); - $controller->reset(); - $controller->set('force', 1); - $controller->set('cid', $contactId); - $controller->set('memberId', $membershipId); - $controller->set('context', 'contribution'); - $controller->process(); - $controller->run(); - } - else { - $this->assign('accessContribution', FALSE); - } + $controller = new CRM_Core_Controller_Simple( + 'CRM_Contribute_Form_Search', + ts('Contributions'), + NULL, + FALSE, FALSE, TRUE + ); + $controller->setEmbedded(TRUE); + $controller->reset(); + $controller->set('force', 1); + $controller->set('cid', $contactId); + $controller->set('memberId', $membershipId); + $controller->set('context', 'contribution'); + $controller->process(); + $controller->run(); } /** -- 2.25.1