//.........这里部分代码省略.........
// but CRM-16124 if $input['is_email_receipt'] is set then that should not be overridden.
$values['is_email_receipt'] = $recurContrib->is_email_receipt;
}
if (!empty($memberships)) {
foreach ($memberships as $membershipTypeIdKey => $membership) {
if ($membership) {
$membershipParams = array('id' => $membership->id, 'contact_id' => $membership->contact_id, 'is_test' => $membership->is_test, 'membership_type_id' => $membership->membership_type_id);
$currentMembership = CRM_Member_BAO_Membership::getContactMembership($membershipParams['contact_id'], $membershipParams['membership_type_id'], $membershipParams['is_test'], $membershipParams['id']);
// CRM-8141 update the membership type with the value recorded in log when membership created/renewed
// this picks up membership type changes during renewals
$sql = "\nSELECT membership_type_id\nFROM civicrm_membership_log\nWHERE membership_id={$membershipParams['id']}\nORDER BY id DESC\nLIMIT 1;";
$dao = CRM_Core_DAO::executeQuery($sql);
if ($dao->fetch()) {
if (!empty($dao->membership_type_id)) {
$membershipParams['membership_type_id'] = $dao->membership_type_id;
}
}
$dao->free();
$membershipParams['num_terms'] = $contribution->getNumTermsByContributionAndMembershipType($membershipParams['membership_type_id'], $primaryContributionID);
$dates = array_fill_keys(array('join_date', 'start_date', 'end_date'), NULL);
if ($currentMembership) {
/*
* Fixed FOR CRM-4433
* In BAO/Membership.php(renewMembership function), we skip the extend membership date and status
* when Contribution mode is notify and membership is for renewal )
*/
CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeDate);
// @todo - we should pass membership_type_id instead of null here but not
// adding as not sure of testing
$dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membershipParams['id'], $changeDate, NULL, $membershipParams['num_terms']);
$dates['join_date'] = $currentMembership['join_date'];
}
//get the status for membership.
$calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'], $dates['end_date'], $dates['join_date'], 'today', TRUE, $membershipParams['membership_type_id'], $membershipParams);
$membershipParams['status_id'] = CRM_Utils_Array::value('id', $calcStatus, 'New');
//we might be renewing membership,
//so make status override false.
$membershipParams['is_override'] = FALSE;
//CRM-17723 - reset static $relatedContactIds array()
$var = TRUE;
CRM_Member_BAO_Membership::createRelatedMemberships($var, $var, TRUE);
civicrm_api3('Membership', 'create', $membershipParams);
}
}
}
} else {
if (empty($input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'])) {
if ($event->is_email_confirm) {
// @todo this should be set by the function that sends the mail after sending.
$contributionParams['receipt_date'] = $changeDate;
}
$participantParams['id'] = $participant->id;
$participantParams['status_id'] = 'Registered';
civicrm_api3('Participant', 'create', $participantParams);
}
}
$contributionParams['id'] = $contribution->id;
// CRM-19309 - if you update the contribution here with financial_type_id it can/will mess with $lineItem
// unsetting it here does NOT cause any other contribution test to fail!
unset($contributionParams['financial_type_id']);
$contributionResult = civicrm_api3('Contribution', 'create', $contributionParams);
// Add new soft credit against current $contribution.
if (CRM_Utils_Array::value('contributionRecur', $objects) && $objects['contributionRecur']->id) {
CRM_Contribute_BAO_ContributionRecur::addrecurSoftCredit($objects['contributionRecur']->id, $contribution->id);
}
$contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', array('labelColumn' => 'name', 'flip' => 1));
/**
* Derives the Membership Status of a given Membership Reocrd
*
* This API is used for deriving Membership Status of a given Membership
* record using the rules encoded in the membership_status table.
*
* @param Int $membershipID Id of a membership
* @param String $statusDate
*
* @return Array Array of status id and status name
* @public
*/
function civicrm_api3_membership_status_calc($membershipParams)
{
if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) {
return civicrm_api3_create_error('membershipParams do not contain membership_id');
}
$query = "\nSELECT start_date, end_date, join_date\n FROM civicrm_membership\n WHERE id = %1\n";
$params = array(1 => array($membershipID, 'Integer'));
$dao =& CRM_Core_DAO::executeQuery($query, $params);
if ($dao->fetch()) {
require_once 'CRM/Member/BAO/MembershipStatus.php';
// Take the is_admin column in MembershipStatus into consideration when requested
if (!CRM_Utils_Array::value('ignore_admin_only', $membershipParams)) {
$result =& CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date, 'today', TRUE);
} else {
$result =& CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date);
}
//make is error zero only when valid status found.
if (CRM_Utils_Array::value('id', $result)) {
$result['is_error'] = 0;
}
} else {
$result = civicrm_api3_create_error('did not find a membership record');
}
$dao->free();
return $result;
}
/**
* Derives the Membership Status of a given Membership Record.
*
* This API is used for deriving Membership Status of a given Membership
* record using the rules encoded in the membership_status table.
*
* @param array $membershipParams
*
* @throws API_Exception
*
* @return array
* Array of status id and status name
*/
function civicrm_api3_membership_status_calc($membershipParams)
{
if (!($membershipID = CRM_Utils_Array::value('membership_id', $membershipParams))) {
throw new API_Exception('membershipParams do not contain membership_id');
}
if (empty($membershipParams['id'])) {
//for consistency lets make sure id is set as this will get passed to hooks downstream
$membershipParams['id'] = $membershipID;
}
$query = "\nSELECT start_date, end_date, join_date, membership_type_id\n FROM civicrm_membership\n WHERE id = %1\n";
$params = array(1 => array($membershipID, 'Integer'));
$dao = CRM_Core_DAO::executeQuery($query, $params);
if ($dao->fetch()) {
$membershipTypeID = empty($membershipParams['membership_type_id']) ? $dao->membership_type_id : $membershipParams['membership_type_id'];
$result = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date, 'today', CRM_Utils_Array::value('ignore_admin_only', $membershipParams), $membershipTypeID, $membershipParams);
//make is error zero only when valid status found.
if (!empty($result['id'])) {
$result['is_error'] = 0;
}
} else {
$dao->free();
throw new API_Exception('did not find a membership record');
}
$dao->free();
return $result;
}
/**
* Get contact Membership record.
*
* This api will return the membership records for the contacts
* having membership based on the relationship with the direct members.
*
* @param array $params
* Key/value pairs for contact_id and some.
* options affecting the desired results; has legacy support
* for just passing the contact_id itself as the argument
*
* @return array
* Array of all found membership property values.
*/
function civicrm_api3_membership_get($params)
{
$activeOnly = $membershipTypeId = $membershipType = NULL;
$contactID = CRM_Utils_Array::value('contact_id', $params);
if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) {
$activeOnly = $params['filters']['is_current'];
unset($params['filters']['is_current']);
}
$activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
if ($activeOnly && empty($params['status_id'])) {
$params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent());
}
$options = _civicrm_api3_get_options_from_params($params, TRUE, 'Membership', 'get');
if ($options['is_count']) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}
$membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Membership');
$return = $options['return'];
if (empty($membershipValues) || !empty($return) && !array_key_exists('related_contact_id', $return) && !array_key_exists('relationship_name', $return)) {
return civicrm_api3_create_success($membershipValues, $params, 'Membership', 'get');
}
$members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
return civicrm_api3_create_success($members, $params, 'Membership', 'get');
}
/**
* Derives the Membership Status of a given Membership Reocrd
*
* This API is used for deriving Membership Status of a given Membership
* record using the rules encoded in the membership_status table.
*
* @param Int $membershipID Id of a membership
* @param String $statusDate
*
* @return Array Array of status id and status name
* @public
*/
function crm_calc_membership_status($membershipID)
{
if (empty($membershipID)) {
return _crm_error('Invalid value for membershipID');
}
$query = "\nSELECT start_date, end_date, join_date\n FROM civicrm_membership\n WHERE id = %1\n";
$params = array(1 => array($membershipID, 'Integer'));
$dao =& CRM_Core_DAO::executeQuery($query, $params);
if ($dao->fetch()) {
require_once 'CRM/Member/BAO/MembershipStatus.php';
$result =& CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date);
} else {
$result = null;
}
$dao->free();
return $result;
}
/**
* Function for validation
*
* @param array $params (ref.) an assoc array of name/value pairs
*
* @return mixed true or array of errors
* @access public
* @static
*/
static function formRule($params, $files, $contributionPageId = null)
{
$errors = array();
if (CRM_Utils_Array::value('is_active', $params)) {
// don't allow price set w/ membership signup, CRM-5095
require_once 'CRM/Price/BAO/Set.php';
if ($contributionPageId && CRM_Price_BAO_Set::getFor('civicrm_contribution_page', $contributionPageId)) {
$errors['is_active'] = ts('You cannot enable both Membership Signup and Price Set on the same online contribution page.');
return $errors;
}
if (!isset($params['membership_type']) || !is_array($params['membership_type'])) {
$errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.');
} else {
$membershipType = array_values($params['membership_type']);
if (array_sum($membershipType) == 0) {
$errors['membership_type'] = ts('Please select at least one Membership Type to include in the Membership section of this page.');
}
}
//for CRM-1302
//if Membership status is not present, then display an error message
require_once 'CRM/Member/BAO/MembershipStatus.php';
$dao = new CRM_Member_BAO_MembershipStatus();
if (!$dao->find()) {
$errors['_qf_default'] = ts('Add status rules, before configuring membership');
}
//give error if default is selected for an unchecked membership type
if (isset($params['membership_type_default']) && !$params['membership_type'][$params['membership_type_default']]) {
$errors['membership_type_default'] = ts('Can\'t set default option for an unchecked membership type.');
}
if ($contributionPageId) {
require_once "CRM/Contribute/DAO/ContributionPage.php";
$amountBlock = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'amount_block_is_active');
if (!$amountBlock && CRM_Utils_Array::value('is_separate_payment', $params)) {
$errors['is_separate_payment'] = ts('Please enable the contribution amount section to use this option.');
}
}
}
return empty($errors) ? true : $errors;
}
/**
* Function to process the form
*
* @access public
* @return None
*/
public function postProcess()
{
require_once 'CRM/Member/BAO/MembershipStatus.php';
if ($this->_action & CRM_Core_Action::DELETE) {
$wt = CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $this->_id);
CRM_Member_BAO_MembershipStatus::del($this->_id);
CRM_Core_Session::setStatus(ts('Selected membership status has been deleted.'));
} else {
$params = $ids = array();
// store the submitted values in an array
$params = $this->exportValues();
if ($this->_action & CRM_Core_Action::UPDATE) {
$ids['membershipStatus'] = $this->_id;
}
if ($this->_id) {
$oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $this->_id, 'weight', 'id');
}
$params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipStatus', $oldWeight, $params['weight']);
// only for add mode, set label to name.
if ($this->_action & CRM_Core_Action::ADD) {
$params['name'] = $params['label'];
}
$membershipStatus = CRM_Member_BAO_MembershipStatus::add($params, $ids);
CRM_Core_Session::setStatus(ts('The membership status \'%1\' has been saved.', array(1 => $membershipStatus->label)));
}
}
请发表评论