3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
35 class CRM_Member_BAO_MembershipType
extends CRM_Member_DAO_MembershipType
{
38 * Static holder for the default Membership Type.
40 static $_defaultMembershipType = NULL;
42 static $_membershipTypeInfo = array();
47 public function __construct() {
48 parent
::__construct();
52 * Fetch object based on array of properties.
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $defaults
57 * (reference ) an assoc array to hold the flattened values.
59 * @return CRM_Member_BAO_MembershipType
61 public static function retrieve(&$params, &$defaults) {
62 $membershipType = new CRM_Member_DAO_MembershipType();
63 $membershipType->copyValues($params);
64 if ($membershipType->find(TRUE)) {
65 CRM_Core_DAO
::storeValues($membershipType, $defaults);
66 return $membershipType;
72 * Update the is_active flag in the db.
75 * Id of the database record.
76 * @param bool $is_active
77 * Value we want to set the is_active field.
80 * DAO object on success, null otherwise
82 public static function setIsActive($id, $is_active) {
83 return CRM_Core_DAO
::setFieldValue('CRM_Member_DAO_MembershipType', $id, 'is_active', $is_active);
87 * Add the membership types.
89 * @param array $params
90 * Reference array contains the values submitted by the form.
92 * Array contains the id (deprecated).
94 * @return \CRM_Member_DAO_MembershipType
95 * @throws \CiviCRM_API3_Exception
97 public static function add(&$params, $ids = array()) {
98 // DEPRECATED Check if membershipType ID was passed in via $ids
99 if (empty($params['id'])) {
100 if (isset($ids['membershipType'])) {
101 Civi
::log()->warning('Deprecated: Passing membershipType by $ids array in CRM_Member_BAO_MembershipType::add');
103 $params['id'] = CRM_Utils_Array
::value('membershipType', $ids);
106 $hook = empty($params['id']) ?
'create' : 'edit';
107 CRM_Utils_Hook
::pre($hook, 'MembershipType', CRM_Utils_Array
::value('id', $params), $params);
109 $membershipTypeId = CRM_Utils_Array
::value('id', $params);
111 if (!$membershipTypeId) {
112 if (!isset($params['is_active'])) {
114 $params['is_active'] = FALSE;
116 if (!isset($params['domain_id'])) {
117 $params['domain_id'] = CRM_Core_Config
::domainID();
121 // $previousID is the old organization id for membership type i.e 'member_of_contact_id'. This is used when an organization is changed.
123 if ($membershipTypeId) {
124 $previousID = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $membershipTypeId, 'member_of_contact_id');
127 // action is taken depending upon the mode
128 $membershipType = new CRM_Member_DAO_MembershipType();
129 $membershipType->copyValues($params);
130 $membershipType->save();
132 if ($membershipTypeId) {
133 // on update we may need to retrieve some details for the price field function - otherwise we get e-notices on attempts to retrieve
134 // name etc - the presence of previous id tells us this is an update
135 $params = array_merge(civicrm_api3('membership_type', 'getsingle', array('id' => $membershipType->id
)), $params);
137 self
::createMembershipPriceField($params, $previousID, $membershipType->id
);
138 // update all price field value for quick config when membership type is set CRM-11718
139 if ($membershipTypeId) {
140 self
::updateAllPriceFieldValue($membershipTypeId, $params);
143 CRM_Utils_Hook
::post($hook, 'MembershipType', $membershipType->id
, $membershipType);
146 return $membershipType;
150 * Flush anywhere that membership types might be cached.
152 * @throws \CiviCRM_API3_Exception
154 public static function flush() {
155 CRM_Member_PseudoConstant
::membershipType(NULL, TRUE);
156 civicrm_api3('membership', 'getfields', array('cache_clear' => 1, 'fieldname' => 'membership_type_id'));
157 civicrm_api3('profile', 'getfields', array('action' => 'submit', 'cache_clear' => 1));
161 * Delete membership Types.
163 * @param int $membershipTypeId
165 * @throws CRM_Core_Exception
168 public static function del($membershipTypeId) {
169 // Check dependencies.
173 'Membership' => 'membership_type_id',
174 'MembershipBlock' => 'membership_type_default',
177 foreach ($dependency as $name => $field) {
178 $baoString = 'CRM_Member_BAO_' . $name;
179 $dao = new $baoString();
180 $dao->$field = $membershipTypeId;
181 /** @noinspection PhpUndefinedMethodInspection */
182 if ($dao->find(TRUE)) {
189 $message = ts('This membership type cannot be deleted due to following reason(s):');
190 if (in_array('Membership', $status)) {
191 $findMembersURL = CRM_Utils_System
::url('civicrm/member/search', 'reset=1');
192 $deleteURL = CRM_Utils_System
::url('civicrm/contact/search/advanced', 'reset=1');
193 $message .= '<br/>' . ts('%3. There are some contacts who have this membership type assigned to them. Search for contacts with this membership type from <a href=\'%1\'>Find Members</a>. If you are still getting this message after deleting these memberships, there may be contacts in the Trash (deleted) with this membership type. Try using <a href="%2">Advanced Search</a> and checking "Search in Trash".', array(
194 1 => $findMembersURL,
201 if (in_array('MembershipBlock', $status)) {
202 $deleteURL = CRM_Utils_System
::url('civicrm/admin/contribute', 'reset=1');
203 $message .= ts('%2. This Membership Type is used in an <a href=\'%1\'>Online Contribution page</a>. Uncheck this membership type in the Memberships tab.', array(
207 throw new CRM_Core_Exception($message);
210 CRM_Utils_Weight
::delWeight('CRM_Member_DAO_MembershipType', $membershipTypeId);
211 //delete from membership Type table
212 $membershipType = new CRM_Member_DAO_MembershipType();
213 $membershipType->id
= $membershipTypeId;
215 //fix for membership type delete api
217 if ($membershipType->find(TRUE)) {
218 return $membershipType->delete();
225 * Convert membership type's 'start day' & 'rollover day' to human readable formats.
227 * @param array $membershipType
228 * An array of membershipType-details.
230 public static function convertDayFormat(&$membershipType) {
232 'fixed_period_start_day',
233 'fixed_period_rollover_day',
235 foreach ($membershipType as $id => $details) {
236 foreach ($periodDays as $pDay) {
237 if (!empty($details[$pDay])) {
238 if ($details[$pDay] > 31) {
239 $month = substr($details[$pDay], 0, strlen($details[$pDay]) - 2);
240 $day = substr($details[$pDay], -2);
255 $membershipType[$id][$pDay] = $monthMap[$month] . ' ' . $day;
258 $membershipType[$id][$pDay] = $details[$pDay];
266 * Get membership Types.
268 * @param bool $public
272 public static function getMembershipTypes($public = TRUE) {
273 $membershipTypes = array();
274 $membershipType = new CRM_Member_DAO_MembershipType();
275 $membershipType->is_active
= 1;
277 $membershipType->visibility
= 'Public';
279 $membershipType->orderBy(' weight');
280 $membershipType->find();
281 while ($membershipType->fetch()) {
282 $membershipTypes[$membershipType->id
] = $membershipType->name
;
284 $membershipType->free();
285 return $membershipTypes;
289 * Get membership Type Details.
291 * @param int $membershipTypeId
295 public static function getMembershipTypeDetails($membershipTypeId) {
296 $membershipTypeDetails = array();
298 $membershipType = new CRM_Member_DAO_MembershipType();
299 $membershipType->is_active
= 1;
300 $membershipType->id
= $membershipTypeId;
301 if ($membershipType->find(TRUE)) {
302 CRM_Core_DAO
::storeValues($membershipType, $membershipTypeDetails);
303 $membershipType->free();
304 return $membershipTypeDetails;
312 * Calculate start date and end date for new membership.
314 * @param int $membershipTypeId
315 * Membership type id.
316 * @param string $joinDate
317 * Member since ( in mysql date format ).
318 * @param string $startDate
319 * Start date ( in mysql date format ).
320 * @param null $endDate
321 * @param int $numRenewTerms
322 * How many membership terms are being added to end date (default is 1).
325 * associated array with start date, end date and join date for the membership
327 public static function getDatesForMembershipType($membershipTypeId, $joinDate = NULL, $startDate = NULL, $endDate = NULL, $numRenewTerms = 1) {
328 $membershipTypeDetails = self
::getMembershipTypeDetails($membershipTypeId);
330 // Convert all dates to 'Y-m-d' format.
336 if (!empty($
$dateParam)) {
337 $
$dateParam = CRM_Utils_Date
::processDate($
$dateParam, NULL, FALSE, 'Y-m-d');
341 $joinDate = date('Y-m-d');
343 $actualStartDate = $joinDate;
345 $actualStartDate = $startDate;
348 $fixed_period_rollover = FALSE;
349 if (CRM_Utils_Array
::value('period_type', $membershipTypeDetails) == 'rolling') {
351 $startDate = $joinDate;
353 $actualStartDate = $startDate;
355 elseif (CRM_Utils_Array
::value('period_type', $membershipTypeDetails) == 'fixed') {
356 // calculate start date
357 // if !$startDate then use $joinDate
358 $toDay = explode('-', (empty($startDate) ?
$joinDate : $startDate));
363 if ($membershipTypeDetails['duration_unit'] == 'year') {
365 //get start fixed day
366 $startMonth = substr($membershipTypeDetails['fixed_period_start_day'], 0,
367 strlen($membershipTypeDetails['fixed_period_start_day']) - 2
369 $startDay = substr($membershipTypeDetails['fixed_period_start_day'], -2);
371 if (date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year)) <= date('Y-m-d', mktime(0, 0, 0, $month, $day, $year))) {
372 $actualStartDate = date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year));
375 $actualStartDate = date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year - 1));
378 $fixed_period_rollover = self
::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $actualStartDate);
381 $startDate = $actualStartDate;
384 elseif ($membershipTypeDetails['duration_unit'] == 'month') {
385 // Check if we are on or after rollover day of the month - CRM-10585
386 // If so, set fixed_period_rollover TRUE so we increment end_date month below.
387 $dateParts = explode('-', $actualStartDate);
388 if ($dateParts[2] >= $membershipTypeDetails['fixed_period_rollover_day']) {
389 $fixed_period_rollover = TRUE;
392 // Start date is always first day of actualStartDate month
394 $actualStartDate = $startDate = $year . '-' . $month . '-01';
399 // Calculate end date if it is not passed by user.
401 //end date calculation
402 $date = explode('-', $actualStartDate);
407 switch ($membershipTypeDetails['duration_unit']) {
409 $year = $year +
($numRenewTerms * $membershipTypeDetails['duration_interval']);
410 //extend membership date by duration interval.
411 if ($fixed_period_rollover) {
417 $month = $month +
($numRenewTerms * $membershipTypeDetails['duration_interval']);
418 //duration interval is month
419 if ($fixed_period_rollover) {
426 $day = $day +
($numRenewTerms * $membershipTypeDetails['duration_interval']);
428 if ($fixed_period_rollover) {
429 //Fix Me: Currently we don't allow rollover if
430 //duration interval is day
435 if ($membershipTypeDetails['duration_unit'] == 'lifetime') {
439 $endDate = date('Y-m-d', mktime(0, 0, 0, $month, $day - 1, $year));
443 $membershipDates = array(
444 'start_date' => CRM_Utils_Date
::customFormat($startDate, '%Y%m%d'),
445 'end_date' => CRM_Utils_Date
::customFormat($endDate, '%Y%m%d'),
446 'join_date' => CRM_Utils_Date
::customFormat($joinDate, '%Y%m%d'),
449 return $membershipDates;
453 * Does this membership start between the rollover date and the start of the next period.
454 * (in which case they will get an extra membership period)
455 * ie if annual memberships run June - May & the rollover is in May memberships between
456 * May and June will return TRUE and between June and May will return FALSE
458 * @param string $startDate start date of current membership period
459 * @param array $membershipTypeDetails
461 * @param string $actualStartDate
462 * @return bool is this in the window where the membership gets an extra part-period added
464 public static function isDuringFixedAnnualRolloverPeriod($startDate, $membershipTypeDetails, $year, $actualStartDate) {
466 $rolloverMonth = substr($membershipTypeDetails['fixed_period_rollover_day'], 0,
467 strlen($membershipTypeDetails['fixed_period_rollover_day']) - 2
469 $rolloverDay = substr($membershipTypeDetails['fixed_period_rollover_day'], -2);
471 $calculatedRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year));
473 //CRM-7825 -membership date rules are :
474 //1. Membership should not be start in future.
475 //2. rollover window should be subset of membership window.
477 //get the fixed end date here.
478 $dateParts = explode('-', $actualStartDate);
479 $endDateOfFirstYearMembershipPeriod = date('Y-m-d', mktime(0, 0, 0,
485 //we know the month and day of the rollover date but not the year (we're just
486 //using the start date year at the moment. So if it's after the end
487 // of the first year of membership then it's the next period & we'll adjust back a year. If it's
488 // before the start_date then it's too early & we'll adjust forward.
489 if ($endDateOfFirstYearMembershipPeriod < $calculatedRolloverDate) {
490 $calculatedRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year - 1));
492 if ($calculatedRolloverDate < $actualStartDate) {
493 $calculatedRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year +
1));
496 if ($calculatedRolloverDate <= $startDate) {
503 * Calculate start date and end date for membership updates.
505 * Note that this function is called by the api for any membership update although it was
506 * originally written for renewal (which feels a bit fragile but hey....).
508 * @param int $membershipId
509 * @param $changeToday
510 * @param int $membershipTypeID
511 * If provided, overrides the membership type of the $membershipID membership.
512 * @param int $numRenewTerms
513 * How many membership terms are being added to end date (default is 1).
515 * CRM-7297 Membership Upsell - Added $membershipTypeID param to facilitate calculations of dates when membership type changes
518 * array fo the start date, end date and join date of the membership
520 public static function getRenewalDatesForMembershipType($membershipId, $changeToday = NULL, $membershipTypeID = NULL, $numRenewTerms = 1) {
521 $params = array('id' => $membershipId);
522 $membershipDetails = CRM_Member_BAO_Membership
::getValues($params, $values);
523 $membershipDetails = $membershipDetails[$membershipId];
524 $statusID = $membershipDetails->status_id
;
525 $membershipDates = array();
526 if (!empty($membershipDetails->join_date
)) {
527 $membershipDates['join_date'] = CRM_Utils_Date
::customFormat($membershipDetails->join_date
, '%Y%m%d');
530 $oldPeriodType = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
531 CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $membershipId, 'membership_type_id'), 'period_type');
533 // CRM-7297 Membership Upsell
534 if (is_null($membershipTypeID)) {
535 $membershipTypeDetails = self
::getMembershipTypeDetails($membershipDetails->membership_type_id
);
538 $membershipTypeDetails = self
::getMembershipTypeDetails($membershipTypeID);
540 $statusDetails = CRM_Member_BAO_MembershipStatus
::getMembershipStatus($statusID);
542 if ($statusDetails['is_current_member'] == 1) {
543 $startDate = $membershipDetails->start_date
;
544 // CRM=7297 Membership Upsell: we need to handle null end_date in case we are switching
545 // from a lifetime to a different membership type
546 if (is_null($membershipDetails->end_date
)) {
547 $date = date('Y-m-d');
550 $date = $membershipDetails->end_date
;
552 $date = explode('-', $date);
553 // We have to add 1 day first in case it's the end of the month, then subtract afterwards
554 // eg. 2018-02-28 should renew to 2018-03-31, if we just added 1 month we'd get 2018-03-28
555 $logStartDate = date('Y-m-d', mktime(0, 0, 0,
557 (double) ($date[2] +
1),
561 $date = explode('-', $logStartDate);
566 switch ($membershipTypeDetails['duration_unit']) {
568 //need to check if the upsell is from rolling to fixed and adjust accordingly
569 if ($membershipTypeDetails['period_type'] == 'fixed' && $oldPeriodType == 'rolling') {
570 $month = substr($membershipTypeDetails['fixed_period_start_day'], 0, strlen($membershipTypeDetails['fixed_period_start_day']) - 2);
571 $day = substr($membershipTypeDetails['fixed_period_start_day'], -2);
575 $year = $year +
($numRenewTerms * $membershipTypeDetails['duration_interval']);
580 $month = $month +
($numRenewTerms * $membershipTypeDetails['duration_interval']);
584 $day = $day +
($numRenewTerms * $membershipTypeDetails['duration_interval']);
587 if ($membershipTypeDetails['duration_unit'] == 'lifetime') {
591 $endDate = date('Y-m-d', mktime(0, 0, 0,
597 $today = date('Y-m-d');
598 $membershipDates['today'] = CRM_Utils_Date
::customFormat($today, '%Y%m%d');
599 $membershipDates['start_date'] = CRM_Utils_Date
::customFormat($startDate, '%Y%m%d');
600 $membershipDates['end_date'] = CRM_Utils_Date
::customFormat($endDate, '%Y%m%d');
601 $membershipDates['log_start_date'] = CRM_Utils_Date
::customFormat($logStartDate, '%Y%m%d');
604 $today = date('Y-m-d');
606 $today = CRM_Utils_Date
::processDate($changeToday, NULL, FALSE, 'Y-m-d');
608 // Calculate new start/end dates when join date is today
609 $renewalDates = self
::getDatesForMembershipType($membershipTypeDetails['id'],
610 $today, NULL, NULL, $numRenewTerms
612 $membershipDates['today'] = CRM_Utils_Date
::customFormat($today, '%Y%m%d');
613 $membershipDates['start_date'] = $renewalDates['start_date'];
614 $membershipDates['end_date'] = $renewalDates['end_date'];
615 $membershipDates['log_start_date'] = $renewalDates['start_date'];
616 // CRM-18503 - set join_date as today in case the membership type is fixed.
617 if ($membershipTypeDetails['period_type'] == 'fixed' && !isset($membershipDates['join_date'])) {
618 $membershipDates['join_date'] = $renewalDates['join_date'];
621 if (!isset($membershipDates['join_date'])) {
622 $membershipDates['join_date'] = $membershipDates['start_date'];
625 return $membershipDates;
629 * @deprecated Please use the Membership API
630 * Retrieve all Membership Types associated with an Organization
633 * Id of Organization.
636 * array of the details of membership types
638 public static function getMembershipTypesByOrg($orgID) {
639 CRM_Core_Error
::deprecatedFunctionWarning('membership_type api');
640 $memberTypesSameParentOrg = civicrm_api3('MembershipType', 'get', array(
641 'member_of_contact_id' => $orgID,
646 return CRM_Utils_Array
::value('values', $memberTypesSameParentOrg, array());
650 * Retrieve all Membership Types with Member of Contact id.
652 * @param array $membershipTypes
653 * array of membership type ids
655 * array of the details of membership types with Member of Contact id
657 public static function getMemberOfContactByMemTypes($membershipTypes) {
658 $memTypeOrganizations = array();
659 if (empty($membershipTypes)) {
660 return $memTypeOrganizations;
663 $result = CRM_Core_DAO
::executeQuery("SELECT id, member_of_contact_id FROM civicrm_membership_type WHERE id IN (" . implode(',', $membershipTypes) . ")");
664 while ($result->fetch()) {
665 $memTypeOrganizations[$result->id
] = $result->member_of_contact_id
;
668 return $memTypeOrganizations;
672 * The function returns all the Organization for all membershipTypes .
674 * @param int $membershipTypeId
678 public static function getMembershipTypeOrganization($membershipTypeId = NULL) {
679 $allMembershipTypes = array();
681 $membershipType = new CRM_Member_DAO_MembershipType();
683 if (isset($membershipTypeId)) {
684 $membershipType->id
= $membershipTypeId;
686 $membershipType->find();
688 while ($membershipType->fetch()) {
689 $allMembershipTypes[$membershipType->id
] = $membershipType->member_of_contact_id
;
691 return $allMembershipTypes;
695 * Function to retrieve organization and associated membership
699 * arrays of organization and membership types
702 public static function getMembershipTypeInfo() {
703 if (!self
::$_membershipTypeInfo) {
704 $orgs = $types = array();
706 $query = 'SELECT memType.id, memType.name, memType.member_of_contact_id, c.sort_name
707 FROM civicrm_membership_type memType INNER JOIN civicrm_contact c ON c.id = memType.member_of_contact_id
708 WHERE memType.is_active = 1 ';
709 $dao = CRM_Core_DAO
::executeQuery($query);
710 while ($dao->fetch()) {
711 $orgs[$dao->member_of_contact_id
] = $dao->sort_name
;
712 $types[$dao->member_of_contact_id
][$dao->id
] = $dao->name
;
715 self
::$_membershipTypeInfo = array($orgs, $types);
717 return self
::$_membershipTypeInfo;
722 * @param array $params
723 * @param int $previousID
724 * @param int $membershipTypeId
726 public static function createMembershipPriceField($params, $previousID, $membershipTypeId) {
728 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name');
730 if (!empty($params['member_of_contact_id'])) {
731 $fieldName = $params['member_of_contact_id'];
734 $fieldName = $previousID;
736 $fieldLabel = 'Membership Amount';
738 $fieldParams = array(
739 'price_set_id ' => $priceSetId,
740 'name' => $fieldName,
743 CRM_Price_BAO_PriceField
::retrieve($fieldParams, $results);
744 if (empty($results)) {
745 $fieldParams = array();
746 $fieldParams['label'] = $fieldLabel;
747 $fieldParams['name'] = $fieldName;
748 $fieldParams['price_set_id'] = $priceSetId;
749 $fieldParams['html_type'] = 'Radio';
750 $fieldParams['is_display_amounts'] = $fieldParams['is_required'] = 0;
751 $fieldParams['weight'] = $fieldParams['option_weight'][1] = 1;
752 $fieldParams['option_label'][1] = $params['name'];
753 $fieldParams['option_description'][1] = CRM_Utils_Array
::value('description', $params);
755 $fieldParams['membership_type_id'][1] = $membershipTypeId;
756 $fieldParams['option_amount'][1] = empty($params['minimum_fee']) ?
0 : $params['minimum_fee'];
757 $fieldParams['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params);
760 CRM_Member_Form_MembershipType
::checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, $optionsIds);
761 $fieldParams['option_id'] = CRM_Utils_Array
::value('option_id', $optionsIds);
763 CRM_Price_BAO_PriceField
::create($fieldParams);
766 $fieldID = $results['id'];
767 $fieldValueParams = array(
768 'price_field_id' => $fieldID,
769 'membership_type_id' => $membershipTypeId,
772 CRM_Price_BAO_PriceFieldValue
::retrieve($fieldValueParams, $results);
773 if (!empty($results)) {
774 $results['label'] = $results['name'] = $params['name'];
775 $results['amount'] = empty($params['minimum_fee']) ?
0 : $params['minimum_fee'];
776 $optionsIds['id'] = $results['id'];
780 'price_field_id' => $fieldID,
781 'name' => $params['name'],
782 'label' => $params['name'],
783 'amount' => empty($params['minimum_fee']) ?
0 : $params['minimum_fee'],
784 'membership_type_id' => $membershipTypeId,
790 CRM_Member_Form_MembershipType
::checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, $optionsIds);
791 if (!empty($optionsIds['option_id'])) {
792 $optionsIds['id'] = current(CRM_Utils_Array
::value('option_id', $optionsIds));
795 $results['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params);
796 $results['description'] = CRM_Utils_Array
::value('description', $params);
797 CRM_Price_BAO_PriceFieldValue
::add($results, $optionsIds);
802 * This function updates all price field value for quick config
803 * price set which has membership type
805 * @param int $membershipTypeId membership type id
807 * @param array $params
809 public static function updateAllPriceFieldValue($membershipTypeId, $params) {
811 $fieldsToUpdate = array(
812 'financial_type_id' => 'financial_type_id',
814 'minimum_fee' => 'amount',
815 'description' => 'description',
816 'visibility' => 'visibility_id',
818 $priceFieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
819 $priceFieldValueBAO->membership_type_id
= $membershipTypeId;
820 $priceFieldValueBAO->find();
821 while ($priceFieldValueBAO->fetch()) {
822 $updateParams = array(
823 'id' => $priceFieldValueBAO->id
,
824 'price_field_id' => $priceFieldValueBAO->price_field_id
,
826 //Get priceset details.
827 $fieldParams = array('fid' => $priceFieldValueBAO->price_field_id
);
828 $setID = CRM_Price_BAO_PriceSet
::getSetId($fieldParams);
829 $setParams = array('id' => $setID);
830 $setValues = CRM_Price_BAO_PriceSet
::retrieve($setParams, $defaults);
831 if (!empty($setValues->is_quick_config
) && $setValues->name
!= 'default_membership_type_amount') {
832 foreach ($fieldsToUpdate as $key => $value) {
833 if ($value == 'visibility_id' && !empty($params['visibility'])) {
834 $updateParams['visibility_id'] = CRM_Price_BAO_PriceField
::getVisibilityOptionID(strtolower($params['visibility']));
837 $updateParams[$value] = CRM_Utils_Array
::value($key, $params);
840 CRM_Price_BAO_PriceFieldValue
::add($updateParams);