/** * Takes an associative array and creates a membership object. * * the function extracts all the params it needs to initialize the created * membership object. The params array could contain additional unused name/value * pairs * * @param array $params * (reference ) an assoc array of name/value pairs. * @param array $ids * The array that holds all the db ids. * * @return CRM_Member_BAO_Membership */ public static function add(&$params, $ids = array()) { $oldStatus = $oldType = NULL; $params['id'] = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membership', $ids)); if ($params['id']) { CRM_Utils_Hook::pre('edit', 'Membership', $params['id'], $params); } else { CRM_Utils_Hook::pre('create', 'Membership', NULL, $params); } $id = $params['id']; // we do this after the hooks are called in case it has been altered if ($id) { $membershipObj = new CRM_Member_DAO_Membership(); $membershipObj->id = $id; $membershipObj->find(); while ($membershipObj->fetch()) { $oldStatus = $membershipObj->status_id; $oldType = $membershipObj->membership_type_id; } } if (array_key_exists('is_override', $params) && !$params['is_override']) { $params['is_override'] = 'null'; } $membership = new CRM_Member_BAO_Membership(); $membership->copyValues($params); $membership->id = $id; $membership->save(); $membership->free(); if (empty($membership->contact_id) || empty($membership->status_id)) { // this means we are in renewal mode and are just updating the membership // record or this is an API update call and all fields are not present in the update record // however the hooks don't care and want all data CRM-7784 $tempMembership = new CRM_Member_DAO_Membership(); $tempMembership->id = $membership->id; $tempMembership->find(TRUE); $membership = $tempMembership; } //get the log start date. //it is set during renewal of membership. $logStartDate = CRM_Utils_Array::value('log_start_date', $params); $logStartDate = $logStartDate ? CRM_Utils_Date::isoToMysql($logStartDate) : CRM_Utils_Date::isoToMysql($membership->start_date); $values = self::getStatusANDTypeValues($membership->id); $membershipLog = array('membership_id' => $membership->id, 'status_id' => $membership->status_id, 'start_date' => $logStartDate, 'end_date' => CRM_Utils_Date::isoToMysql($membership->end_date), 'modified_date' => date('Ymd'), 'membership_type_id' => $values[$membership->id]['membership_type_id'], 'max_related' => $membership->max_related); $session = CRM_Core_Session::singleton(); // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id if ($session->get('userID')) { $membershipLog['modified_id'] = $session->get('userID'); } elseif (!empty($ids['userId'])) { $membershipLog['modified_id'] = $ids['userId']; } else { $membershipLog['modified_id'] = $membership->contact_id; } CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray); // reset the group contact cache since smart groups might be affected due to this CRM_Contact_BAO_GroupContactCache::remove(); if ($id) { if ($membership->status_id != $oldStatus) { $allStatus = CRM_Member_BAO_Membership::buildOptions('status_id', 'get'); $activityParam = array('subject' => "Status changed from {$allStatus[$oldStatus]} to {$allStatus[$membership->status_id]}", 'source_contact_id' => $membershipLog['modified_id'], 'target_contact_id' => $membership->contact_id, 'source_record_id' => $membership->id, 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Membership Status'), 'status_id' => 2, 'priority_id' => 2, 'activity_date_time' => date('Y-m-d H:i:s')); civicrm_api3('activity', 'create', $activityParam); } if (isset($membership->membership_type_id) && $membership->membership_type_id != $oldType) { $membershipTypes = CRM_Member_BAO_Membership::buildOptions('membership_type_id', 'get'); $activityParam = array('subject' => "Type changed from {$membershipTypes[$oldType]} to {$membershipTypes[$membership->membership_type_id]}", 'source_contact_id' => $membershipLog['modified_id'], 'target_contact_id' => $membership->contact_id, 'source_record_id' => $membership->id, 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Membership Type'), 'status_id' => 2, 'priority_id' => 2, 'activity_date_time' => date('Y-m-d H:i:s')); civicrm_api3('activity', 'create', $activityParam); } CRM_Utils_Hook::post('edit', 'Membership', $membership->id, $membership); } else { CRM_Utils_Hook::post('create', 'Membership', $membership->id, $membership); } return $membership; }
/** * Load objects relations to contribution object. * Objects are stored in the $_relatedObjects property * In the first instance we are just moving functionality from BASEIpn - * @see http://issues.civicrm.org/jira/browse/CRM-9996 * * Note that the unit test for the BaseIPN class tests this function * * @param array $input * Input as delivered from Payment Processor. * @param array $ids * Ids as Loaded by Payment Processor. * @param bool $loadAll * Load all related objects - even where id not passed in? (allows API to call this). * * @return bool * @throws Exception */ public function loadRelatedObjects(&$input, &$ids, $loadAll = FALSE) { if ($loadAll) { $ids = array_merge($this->getComponentDetails($this->id), $ids); if (empty($ids['contact']) && isset($this->contact_id)) { $ids['contact'] = $this->contact_id; } } if (empty($this->_component)) { if (!empty($ids['event'])) { $this->_component = 'event'; } else { $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute')); } } // If the object is not fully populated then make sure it is - this is a more about legacy paths & cautious // refactoring than anything else, and has unit test coverage. if (empty($this->financial_type_id)) { $this->find(TRUE); } $paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $input, CRM_Utils_Array::value('paymentProcessor', $ids)); if (!$paymentProcessorID && $this->contribution_page_id) { $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->contribution_page_id, 'payment_processor'); if ($paymentProcessorID) { $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage; } } $ids['contributionType'] = $this->financial_type_id; $ids['financialType'] = $this->financial_type_id; $entities = array('contact' => 'CRM_Contact_BAO_Contact', 'contributionRecur' => 'CRM_Contribute_BAO_ContributionRecur', 'contributionType' => 'CRM_Financial_BAO_FinancialType', 'financialType' => 'CRM_Financial_BAO_FinancialType'); foreach ($entities as $entity => $bao) { if (!empty($ids[$entity])) { $this->_relatedObjects[$entity] = new $bao(); $this->_relatedObjects[$entity]->id = $ids[$entity]; if (!$this->_relatedObjects[$entity]->find(TRUE)) { throw new CRM_Core_Exception($entity . ' could not be loaded'); } } } if (!empty($ids['contributionRecur']) && !$paymentProcessorID) { $paymentProcessorID = $this->_relatedObjects['contributionRecur']->payment_processor_id; } if (!empty($ids['pledge_payment'])) { foreach ($ids['pledge_payment'] as $key => $paymentID) { if (empty($paymentID)) { continue; } $payment = new CRM_Pledge_BAO_PledgePayment(); $payment->id = $paymentID; if (!$payment->find(TRUE)) { throw new Exception("Could not find pledge payment record: " . $paymentID); } $this->_relatedObjects['pledge_payment'][] = $payment; } } if ($this->_component == 'contribute') { // retrieve the other optional objects first so // stuff down the line can use this info and do things // CRM-6056 //in any case get the memberships associated with the contribution //because we now support multiple memberships w/ price set // see if there are any other memberships to be considered for same contribution. $query = "\n SELECT membership_id\n FROM civicrm_membership_payment\nWHERE contribution_id = %1 "; $params = array(1 => array($this->id, 'Integer')); $dao = CRM_Core_DAO::executeQuery($query, $params); while ($dao->fetch()) { if ($dao->membership_id) { if (!is_array($ids['membership'])) { $ids['membership'] = array(); } $ids['membership'][] = $dao->membership_id; } } if (array_key_exists('membership', $ids) && is_array($ids['membership'])) { foreach ($ids['membership'] as $id) { if (!empty($id)) { $membership = new CRM_Member_BAO_Membership(); $membership->id = $id; if (!$membership->find(TRUE)) { throw new Exception("Could not find membership record: {$id}"); } $membership->join_date = CRM_Utils_Date::isoToMysql($membership->join_date); $membership->start_date = CRM_Utils_Date::isoToMysql($membership->start_date); $membership->end_date = CRM_Utils_Date::isoToMysql($membership->end_date); $this->_relatedObjects['membership'][$membership->membership_type_id] = $membership; $membership->free(); } } } } else { // we are in event mode // make sure event exists and is valid $event = new CRM_Event_BAO_Event(); $event->id = $ids['event']; if ($ids['event'] && !$event->find(TRUE)) { throw new Exception("Could not find event: " . $ids['event']); } $this->_relatedObjects['event'] =& $event; $participant = new CRM_Event_BAO_Participant(); $participant->id = $ids['participant']; if ($ids['participant'] && !$participant->find(TRUE)) { throw new Exception("Could not find participant: " . $ids['participant']); } $participant->register_date = CRM_Utils_Date::isoToMysql($participant->register_date); $this->_relatedObjects['participant'] =& $participant; // get the payment processor id from event - this is inaccurate see CRM-16923 // in future we should look at throwing an exception here rather than an dubious guess. if (!$paymentProcessorID) { $paymentProcessorID = $this->_relatedObjects['event']->payment_processor; if ($paymentProcessorID) { $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromEvent; } } } if ($paymentProcessorID) { $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, $this->is_test ? 'test' : 'live'); $ids['paymentProcessor'] = $paymentProcessorID; $this->_relatedObjects['paymentProcessor'] = $paymentProcessor; } return TRUE; }
/** * takes an associative array and creates a membership object * * the function extract all the params it needs to initialize the create a * membership object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Member_BAO_Membership object * @access public * @static */ static function &add(&$params, &$ids) { require_once 'CRM/Utils/Hook.php'; if (CRM_Utils_Array::value('membership', $ids)) { CRM_Utils_Hook::pre('edit', 'Membership', $ids['membership'], $params); } else { CRM_Utils_Hook::pre('create', 'Membership', null, $params); } // converting dates to mysql format if (isset($params['join_date'])) { $params['join_date'] = CRM_Utils_Date::isoToMysql($params['join_date']); } if (isset($params['start_date'])) { $params['start_date'] = CRM_Utils_Date::isoToMysql($params['start_date']); } if (CRM_Utils_Array::value('end_date', $params)) { $params['end_date'] = CRM_Utils_Date::isoToMysql($params['end_date']); } else { $params['end_date'] = 'null'; } if (CRM_Utils_Array::value('reminder_date', $params)) { $params['reminder_date'] = CRM_Utils_Date::isoToMysql($params['reminder_date']); } else { $params['reminder_date'] = 'null'; } if (!CRM_Utils_Array::value('is_override', $params)) { $params['is_override'] = 'null'; } $membership = new CRM_Member_BAO_Membership(); $membership->copyValues($params); $membership->id = CRM_Utils_Array::value('membership', $ids); $membership->save(); $membership->free(); $session =& CRM_Core_Session::singleton(); //get the log start date. //it is set during renewal of membership. $logStartDate = CRM_Utils_array::value('log_start_date', $params); $logStartDate = $logStartDate ? CRM_Utils_Date::isoToMysql($logStartDate) : $membership->start_date; $membershipLog = array('membership_id' => $membership->id, 'status_id' => $membership->status_id, 'start_date' => $logStartDate, 'end_date' => $membership->end_date, 'renewal_reminder_date' => $membership->reminder_date, 'modified_id' => CRM_Utils_Array::value('userId', $ids), 'modified_date' => date('Ymd')); require_once 'CRM/Member/BAO/MembershipLog.php'; CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray); // reset the group contact cache since smart groups might be affected due to this require_once 'CRM/Contact/BAO/GroupContactCache.php'; CRM_Contact_BAO_GroupContactCache::remove(); if (CRM_Utils_Array::value('membership', $ids)) { CRM_Utils_Hook::post('edit', 'Membership', $membership->id, $membership); } else { CRM_Utils_Hook::post('create', 'Membership', $membership->id, $membership); } return $membership; }
/** * Load objects relations to contribution object. * Objects are stored in the $_relatedObjects property * In the first instance we are just moving functionality from BASEIpn - * @see http://issues.civicrm.org/jira/browse/CRM-9996 * * Note that the unit test for the BaseIPN class tests this function * * @param array $input * Input as delivered from Payment Processor. * @param array $ids * Ids as Loaded by Payment Processor. * @param bool $required * Is Payment processor / contribution page required. * @param bool $loadAll * Load all related objects - even where id not passed in? (allows API to call this). * * @return bool * @throws Exception */ public function loadRelatedObjects(&$input, &$ids, $required = FALSE, $loadAll = FALSE) { if ($loadAll) { $ids = array_merge($this->getComponentDetails($this->id), $ids); if (empty($ids['contact']) && isset($this->contact_id)) { $ids['contact'] = $this->contact_id; } } if (empty($this->_component)) { if (!empty($ids['event'])) { $this->_component = 'event'; } else { $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute')); } } $paymentProcessorID = CRM_Utils_Array::value('paymentProcessor', $ids); $contributionType = new CRM_Financial_BAO_FinancialType(); $contributionType->id = $this->financial_type_id; $contributionType->find(TRUE); if (!empty($ids['contact'])) { $this->_relatedObjects['contact'] = new CRM_Contact_BAO_Contact(); $this->_relatedObjects['contact']->id = $ids['contact']; $this->_relatedObjects['contact']->find(TRUE); } $this->_relatedObjects['contributionType'] = $contributionType; if ($this->_component == 'contribute') { // retrieve the other optional objects first so // stuff down the line can use this info and do things // CRM-6056 //in any case get the memberships associated with the contribution //because we now support multiple memberships w/ price set // see if there are any other memberships to be considered for same contribution. $query = "\n SELECT membership_id\n FROM civicrm_membership_payment\nWHERE contribution_id = %1 "; $params = array(1 => array($this->id, 'Integer')); $dao = CRM_Core_DAO::executeQuery($query, $params); while ($dao->fetch()) { if ($dao->membership_id) { if (!is_array($ids['membership'])) { $ids['membership'] = array(); } $ids['membership'][] = $dao->membership_id; } } if (array_key_exists('membership', $ids) && is_array($ids['membership'])) { foreach ($ids['membership'] as $id) { if (!empty($id)) { $membership = new CRM_Member_BAO_Membership(); $membership->id = $id; if (!$membership->find(TRUE)) { throw new Exception("Could not find membership record: {$id}"); } $membership->join_date = CRM_Utils_Date::isoToMysql($membership->join_date); $membership->start_date = CRM_Utils_Date::isoToMysql($membership->start_date); $membership->end_date = CRM_Utils_Date::isoToMysql($membership->end_date); $this->_relatedObjects['membership'][$membership->membership_type_id] = $membership; $membership->free(); } } } if (!empty($ids['pledge_payment'])) { foreach ($ids['pledge_payment'] as $key => $paymentID) { if (empty($paymentID)) { continue; } $payment = new CRM_Pledge_BAO_PledgePayment(); $payment->id = $paymentID; if (!$payment->find(TRUE)) { throw new Exception("Could not find pledge payment record: " . $paymentID); } $this->_relatedObjects['pledge_payment'][] = $payment; } } if (!empty($ids['contributionRecur'])) { $recur = new CRM_Contribute_BAO_ContributionRecur(); $recur->id = $ids['contributionRecur']; if (!$recur->find(TRUE)) { throw new Exception("Could not find recur record: " . $ids['contributionRecur']); } $this->_relatedObjects['contributionRecur'] =& $recur; //get payment processor id from recur object. $paymentProcessorID = $recur->payment_processor_id; } //for normal contribution get the payment processor id. if (!$paymentProcessorID) { if ($this->contribution_page_id) { // get the payment processor id from contribution page $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->contribution_page_id, 'payment_processor'); } elseif (empty($ids['pledge_payment'])) { $loadObjectSuccess = TRUE; if ($required) { throw new Exception("Could not find contribution page for contribution record: " . $this->id); } return $loadObjectSuccess; } } } else { // we are in event mode // make sure event exists and is valid $event = new CRM_Event_BAO_Event(); $event->id = $ids['event']; if ($ids['event'] && !$event->find(TRUE)) { throw new Exception("Could not find event: " . $ids['event']); } $this->_relatedObjects['event'] =& $event; $participant = new CRM_Event_BAO_Participant(); $participant->id = $ids['participant']; if ($ids['participant'] && !$participant->find(TRUE)) { throw new Exception("Could not find participant: " . $ids['participant']); } $participant->register_date = CRM_Utils_Date::isoToMysql($participant->register_date); $this->_relatedObjects['participant'] =& $participant; if (!$paymentProcessorID) { $paymentProcessorID = $this->_relatedObjects['event']->payment_processor; } } if ($paymentProcessorID) { $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, $this->is_test ? 'test' : 'live'); $ids['paymentProcessor'] = $paymentProcessorID; $this->_relatedObjects['paymentProcessor'] = $paymentProcessor; } elseif ($required) { throw new Exception("Could not find payment processor for contribution record: " . $this->id); } return TRUE; }
/** * Load related memberships. * * Note that in theory it should be possible to retrieve these from the line_item table * with the membership_payment table being deprecated. Attempting to do this here causes tests to fail * as it seems the api is not correctly linking the line items when the contribution is created in the flow * where the contribution is created in the API, followed by the membership (using the api) followed by the membership * payment. The membership payment BAO does have code to address this but it doesn't appear to be working. * * I don't know if it never worked or broke as a result of https://issues.civicrm.org/jira/browse/CRM-14918. * * @param array $ids * * @throws Exception */ public function loadRelatedMembershipObjects(&$ids) { $query = "\n SELECT membership_id\n FROM civicrm_membership_payment\n WHERE contribution_id = %1 "; $params = array(1 => array($this->id, 'Integer')); $dao = CRM_Core_DAO::executeQuery($query, $params); while ($dao->fetch()) { if ($dao->membership_id) { if (!is_array($ids['membership'])) { $ids['membership'] = array(); } $ids['membership'][] = $dao->membership_id; } } if (array_key_exists('membership', $ids) && is_array($ids['membership'])) { foreach ($ids['membership'] as $id) { if (!empty($id)) { $membership = new CRM_Member_BAO_Membership(); $membership->id = $id; if (!$membership->find(TRUE)) { throw new Exception("Could not find membership record: {$id}"); } $membership->join_date = CRM_Utils_Date::isoToMysql($membership->join_date); $membership->start_date = CRM_Utils_Date::isoToMysql($membership->start_date); $membership->end_date = CRM_Utils_Date::isoToMysql($membership->end_date); $this->_relatedObjects['membership'][$membership->membership_type_id] = $membership; $membership->free(); } } } }
/** * process the form after the input has been submitted and validated * * @access public * @return None */ public function postProcess() { $params = $this->exportValues(); if (isset($params['field'])) { $customFields = array(); foreach ($params['field'] as $key => $value) { $ids['membership'] = $key; if ($value['membership_source']) { $value['source'] = $value['membership_source']; } unset($value['membership_source']); //Get the membership status $membership = new CRM_Member_BAO_Membership(); $membership->id = CRM_Utils_Array::value('membership', $ids); $membership->find(true); $membership->free(); $value['status_id'] = $membership->status_id; if (empty($customFields)) { // membership type custom data $customFields = CRM_Core_BAO_CustomField::getFields('Membership', false, false, $membership->membership_type_id); $customFields = CRM_Utils_Array::crmArrayMerge($customFields, CRM_Core_BAO_CustomField::getFields('Membership', false, false, null, null, true)); } //check for custom data $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key], $customFields, $key, 'Membership', $membership->membership_type_id); $membership = CRM_Member_BAO_Membership::add($value, $ids); // add custom field values if (CRM_Utils_Array::value('custom', $value) && is_array($value['custom'])) { require_once 'CRM/Core/BAO/CustomValueTable.php'; CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id); } } CRM_Core_Session::setStatus("Your updates have been saved."); } else { CRM_Core_Session::setStatus("No updates have been saved."); } }