3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 * Business object for managing price sets.
37 class CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set
extends CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set
{
42 public function __construct() {
43 parent
::__construct();
47 * Takes an associative array and creates a price set object.
49 * @param array $params
50 * (reference) an assoc array of name/value pairs.
52 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set
54 public static function create(&$params) {
55 $priceSetBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set();
56 $priceSetBAO->copyValues($params);
57 if (self
::eventPriceSetDomainID()) {
58 $priceSetBAO->domain_id
= CRM_Core_Config
::domainID();
60 return $priceSetBAO->save();
64 * Fetch object based on array of properties.
66 * @param array $params
67 * (reference ) an assoc array of name/value pairs.
68 * @param array $defaults
69 * (reference ) an assoc array to hold the flattened values.
71 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set
73 public static function retrieve(&$params, &$defaults) {
74 return CRM_Core_DAO
::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $params, $defaults);
78 * Update the is_active flag in the db.
81 * Id of the database record.
82 * @param bool $isActive
83 * Value we want to set the is_active field.
86 * DAO object on success, null otherwise
88 public static function setIsActive($id, $isActive) {
89 return CRM_Core_DAO
::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $id, 'is_active', $isActive);
93 * Calculate the default price set id assigned to the contribution/membership etc.
95 * @param string $entity
100 public static function getDefaultPriceSet($entity = 'contribution') {
101 if ($entity == 'contribution') {
102 $entityName = 'default_contribution_amount';
104 elseif ($entity == 'membership') {
105 $entityName = 'default_membership_type_amount';
109 SELECT ps.id AS setID, pfv.price_field_id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.name, pfv.label
110 FROM civicrm_price_set ps
111 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
112 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
113 WHERE ps.name = '{$entityName}'
116 $dao = CRM_Core_DAO
::executeQuery($sql);
117 $defaultPriceSet = array();
118 while ($dao->fetch()) {
119 $defaultPriceSet[$dao->priceFieldValueID
]['setID'] = $dao->setID
;
120 $defaultPriceSet[$dao->priceFieldValueID
]['priceFieldID'] = $dao->priceFieldID
;
121 $defaultPriceSet[$dao->priceFieldValueID
]['name'] = $dao->name
;
122 $defaultPriceSet[$dao->priceFieldValueID
]['label'] = $dao->label
;
125 return $defaultPriceSet;
129 * Get the price set title.
137 public static function getTitle($id) {
138 return CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $id, 'title');
142 * Return a list of all forms which use this price set.
146 * @param bool|\str $simpleReturn - get raw data. Possible values: 'entity', 'table'
150 public static function &getUsedBy($id, $simpleReturn = FALSE) {
151 $usedBy = $forms = $tables = array();
153 SELECT entity_table, entity_id
154 FROM civicrm_price_set_entity
155 WHERE price_set_id = %1";
156 $params = array(1 => array($id, 'Integer'));
157 $crmFormDAO = CRM_Core_DAO
::executeQuery($queryString, $params);
159 while ($crmFormDAO->fetch()) {
160 $forms[$crmFormDAO->entity_table
][] = $crmFormDAO->entity_id
;
161 $tables[] = $crmFormDAO->entity_table
;
163 // Return only tables
164 if ($simpleReturn == 'table') {
169 SELECT cli.entity_table, cli.entity_id
170 FROM civicrm_line_item cli
171 LEFT JOIN civicrm_price_field cpf ON cli.price_field_id = cpf.id
172 WHERE cpf.price_set_id = %1";
173 $params = array(1 => array($id, 'Integer'));
174 $crmFormDAO = CRM_Core_DAO
::executeQuery($queryString, $params);
175 while ($crmFormDAO->fetch()) {
176 $forms[$crmFormDAO->entity_table
][] = $crmFormDAO->entity_id
;
177 $tables[] = $crmFormDAO->entity_table
;
183 // Return only entity data
184 if ($simpleReturn == 'entity') {
187 foreach ($forms as $table => $entities) {
189 case 'civicrm_event':
190 $ids = implode(',', $entities);
191 $queryString = "SELECT ce.id as id, ce.title as title, ce.is_public as isPublic, ce.start_date as startDate, ce.end_date as endDate, civicrm_option_value.label as eventType
192 FROM civicrm_event ce
193 LEFT JOIN civicrm_option_value ON
194 ( ce.event_type_id = civicrm_option_value.value )
195 LEFT JOIN civicrm_option_group ON
196 ( civicrm_option_group.id = civicrm_option_value.option_group_id )
198 civicrm_option_group.name = 'event_type' AND
199 ( ce.is_template IS NULL OR ce.is_template = 0) AND
202 $crmDAO = CRM_Core_DAO
::executeQuery($queryString);
203 while ($crmDAO->fetch()) {
204 $usedBy[$table][$crmDAO->id
]['title'] = $crmDAO->title
;
205 $usedBy[$table][$crmDAO->id
]['eventType'] = $crmDAO->eventType
;
206 $usedBy[$table][$crmDAO->id
]['startDate'] = $crmDAO->startDate
;
207 $usedBy[$table][$crmDAO->id
]['endDate'] = $crmDAO->endDate
;
208 $usedBy[$table][$crmDAO->id
]['isPublic'] = $crmDAO->isPublic
;
212 case 'civicrm_contribution_page':
213 $ids = implode(',', $entities);
214 $queryString = "SELECT cp.id as id, cp.title as title, cp.start_date as startDate, cp.end_date as endDate,ct.name as type
215 FROM civicrm_contribution_page cp, civicrm_contribution_type ct
216 WHERE ct.id = cp.contribution_type_id AND
219 $crmDAO = CRM_Core_DAO
::executeQuery($queryString);
220 while ($crmDAO->fetch()) {
221 $usedBy[$table][$crmDAO->id
]['title'] = $crmDAO->title
;
222 $usedBy[$table][$crmDAO->id
]['type'] = $crmDAO->type
;
223 $usedBy[$table][$crmDAO->id
]['startDate'] = $crmDAO->startDate
;
224 $usedBy[$table][$crmDAO->id
]['endDate'] = $crmDAO->endDate
;
228 case 'civicrm_contribution':
229 case 'civicrm_membership':
230 case 'civicrm_participant':
235 CRM_Core_Error
::fatal("$table is not supported in PriceSet::usedBy()");
244 * Delete the price set.
250 * false if fields exist for this set, true if the
251 * set could be deleted
253 public static function deleteSet($id) {
254 // remove from all inactive forms
255 $usedBy = self
::getUsedBy($id);
256 if (isset($usedBy['civicrm_event'])) {
257 foreach ($usedBy['civicrm_event'] as $eventId => $unused) {
258 $eventDAO = new CRM_Event_DAO_Event();
259 $eventDAO->id
= $eventId;
261 while ($eventDAO->fetch()) {
262 self
::removeFrom('civicrm_event', $eventDAO->id
);
267 // delete price fields
268 $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field();
269 $priceField->price_set_id
= $id;
271 while ($priceField->fetch()) {
272 // delete options first
273 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
::deleteField($priceField->id
);
276 $set = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set();
278 return $set->delete();
282 * Link the price set with the specified table and id.
284 * @param string $entityTable
285 * @param int $entityId
286 * @param int $priceSetId
290 public static function addTo($entityTable, $entityId, $priceSetId) {
291 // verify that the price set exists
292 $dao = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set();
293 $dao->id
= $priceSetId;
299 $dao = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_SetEntity();
300 // find if this already exists
301 $dao->entity_id
= $entityId;
302 $dao->entity_table
= $entityTable;
305 // add or update price_set_id
306 $dao->price_set_id
= $priceSetId;
311 * Delete price set for the given entity and id.
313 * @param string $entityTable
314 * @param int $entityId
318 public static function removeFrom($entityTable, $entityId) {
319 $dao = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_SetEntity();
320 $dao->entity_table
= $entityTable;
321 $dao->entity_id
= $entityId;
322 return $dao->delete();
326 * Find a price_set_id associated with the given details.
328 * Used For value for events:1, contribution:2, membership:3
330 * @param string $entityTable
331 * @param int $entityId
332 * @param int $usedFor
333 * ( price set that extends/used for particular component ).
335 * @param null $isQuickConfig
336 * @param null $setName
339 * price_set_id, or false if none found
341 public static function getFor($entityTable, $entityId, $usedFor = NULL, $isQuickConfig = NULL, &$setName = NULL) {
342 if (!$entityTable ||
!$entityId) {
346 $sql = 'SELECT ps.id as price_set_id, ps.name as price_set_name
347 FROM civicrm_price_set ps
348 INNER JOIN civicrm_price_set_entity pse ON ps.id = pse.price_set_id
349 WHERE pse.entity_table = %1 AND pse.entity_id = %2 ';
350 if ($isQuickConfig) {
351 $sql .= " AND ps.is_quick_config = 0 ";
354 1 => array($entityTable, 'String'),
355 2 => array($entityId, 'Integer'),
358 $sql .= " AND ps.extends LIKE '%%3%' ";
359 $params[3] = array($usedFor, 'Integer');
362 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
364 $setName = (isset($dao->price_set_name
)) ?
$dao->price_set_name
: FALSE;
365 return (isset($dao->price_set_id
)) ?
$dao->price_set_id
: FALSE;
369 * Find a price_set_id associatied with the given option value or field ID.
371 * @param array $params
372 * (reference) an assoc array of name/value pairs.
373 * array may contain either option id or
377 * price set id on success, null otherwise
379 public static function getSetId(&$params) {
382 if ($oid = CRM_Utils_Array
::value('oid', $params)) {
383 $fieldValue = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
384 $fieldValue->id
= $oid;
385 if ($fieldValue->find(TRUE)) {
386 $fid = $fieldValue->price_field_id
;
390 $fid = CRM_Utils_Array
::value('fid', $params);
394 return CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $fid, 'price_set_id');
401 * Return an associative array of all price sets.
403 * @param bool $withInactive
404 * Whether or not to include inactive entries.
405 * @param bool|string $extendComponentName name of the component like 'CiviEvent','CiviContribute'
408 * associative array of id => name
410 public static function getAssoc($withInactive = FALSE, $extendComponentName = FALSE) {
413 DISTINCT ( price_set_id ) as id, title
418 civicrm_price_set.id = civicrm_price_field.price_set_id AND is_quick_config = 0 ";
420 if (!$withInactive) {
421 $query .= " AND civicrm_price_set.is_active = 1 ";
424 if (self
::eventPriceSetDomainID()) {
425 $query .= " AND civicrm_price_set.domain_id = " . CRM_Core_Config
::domainID();
428 $priceSets = array();
430 if ($extendComponentName) {
431 $componentId = CRM_Core_Component
::getComponentID($extendComponentName);
435 $query .= " AND civicrm_price_set.extends LIKE '%$componentId%' ";
438 $dao = CRM_Core_DAO
::executeQuery($query);
439 while ($dao->fetch()) {
440 $priceSets[$dao->id
] = $dao->title
;
446 * Get price set details.
448 * An array containing price set details (including price fields) is returned
451 * Price set id whose details are needed.
452 * @param bool $required
453 * @param bool $validOnly
456 * array consisting of field details
458 public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE) {
461 $select = $from = $where = $orderBy = '';
463 $priceFields = array(
472 'is_display_amounts',
481 if ($required == TRUE) {
482 $priceFields[] = 'is_required';
486 $select = 'SELECT ' . implode(',', $priceFields);
487 $from = ' FROM civicrm_price_field';
490 $params[1] = array($setID, 'Integer');
492 WHERE price_set_id = %1
497 $currentTime = date('YmdHis');
499 AND ( active_on IS NULL OR active_on <= {$currentTime} )
500 AND ( expire_on IS NULL OR expire_on >= {$currentTime} )
504 $orderBy = ' ORDER BY weight';
506 $sql = $select . $from . $where . $dateSelect . $orderBy;
508 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
510 $visibility = CRM_Core_PseudoConstant
::visibility('name');
511 while ($dao->fetch()) {
514 $setTree[$setID]['fields'][$fieldID] = array();
515 $setTree[$setID]['fields'][$fieldID]['id'] = $fieldID;
517 foreach ($priceFields as $field) {
518 if ($field == 'id' ||
is_null($dao->$field)) {
522 if ($field == 'visibility_id') {
523 $setTree[$setID]['fields'][$fieldID]['visibility'] = $visibility[$dao->$field];
525 $setTree[$setID]['fields'][$fieldID][$field] = $dao->$field;
527 $setTree[$setID]['fields'][$fieldID]['options'] = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
::getOptions($fieldID, FALSE);
530 // also get the pre and post help from this price set
532 SELECT extends, contribution_type_id, help_pre, help_post, is_quick_config
533 FROM civicrm_price_set
535 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
537 $setTree[$setID]['extends'] = $dao->extends;
538 $setTree[$setID]['contribution_type_id'] = $dao->contribution_type_id
;
539 $setTree[$setID]['help_pre'] = $dao->help_pre
;
540 $setTree[$setID]['help_post'] = $dao->help_post
;
541 $setTree[$setID]['is_quick_config'] = $dao->is_quick_config
;
549 * @param CRM_Core_Form $form
551 * @param string $entityTable
552 * @param bool $validOnly
553 * @param int $priceSetId
555 * @return bool|false|int|null
557 public static function initSet(&$form, $id, $entityTable = 'civicrm_event', $validOnly = FALSE, $priceSetId = NULL) {
559 $priceSetId = self
::getFor($entityTable, $id);
562 //check if priceset is is_config
563 if (is_numeric($priceSetId)) {
564 if (CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $priceSetId, 'is_quick_config') && $form->getVar('_name') != 'Participant') {
565 $form->assign('quickConfig', 1);
570 if ($form->_action
& CRM_Core_Action
::UPDATE
) {
571 $entityId = $entity = NULL;
573 switch ($entityTable) {
574 case 'civicrm_event':
575 $entity = 'participant';
576 if (CRM_Utils_System
::getClassName($form) == 'CRM_Event_Form_Participant') {
577 $entityId = $form->_id
;
580 $entityId = $form->_participantId
;
584 case 'civicrm_contribution_page':
585 case 'civicrm_contribution':
586 $entity = 'contribution';
587 $entityId = $form->_id
;
591 if ($entityId && $entity) {
592 $form->_values
['line_items'] = CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::getLineItems($entityId, $entity);
600 $form->_priceSetId
= $priceSetId;
601 $priceSet = self
::getSetDetail($priceSetId, $required, $validOnly);
602 $form->_priceSet
= CRM_Utils_Array
::value($priceSetId, $priceSet);
603 $form->_values
['fee'] = CRM_Utils_Array
::value('fields', $form->_priceSet
);
605 //get the price set fields participant count.
606 if ($entityTable == 'civicrm_event') {
607 //get option count info.
608 $form->_priceSet
['optionsCountTotal'] = self
::getPricesetCount($priceSetId);
609 if ($form->_priceSet
['optionsCountTotal']) {
610 $optionsCountDeails = array();
611 if (!empty($form->_priceSet
['fields'])) {
612 foreach ($form->_priceSet
['fields'] as $field) {
613 foreach ($field['options'] as $option) {
614 $count = CRM_Utils_Array
::value('count', $option, 0);
615 $optionsCountDeails['fields'][$field['id']]['options'][$option['id']] = $count;
619 $form->_priceSet
['optionsCountDetails'] = $optionsCountDeails;
622 //get option max value info.
623 $optionsMaxValueTotal = 0;
624 $optionsMaxValueDetails = array();
626 if (!empty($form->_priceSet
['fields'])) {
627 foreach ($form->_priceSet
['fields'] as $field) {
628 foreach ($field['options'] as $option) {
629 $maxVal = CRM_Utils_Array
::value('max_value', $option, 0);
630 $optionsMaxValueDetails['fields'][$field['id']]['options'][$option['id']] = $maxVal;
631 $optionsMaxValueTotal +
= $maxVal;
636 $form->_priceSet
['optionsMaxValueTotal'] = $optionsMaxValueTotal;
637 if ($optionsMaxValueTotal) {
638 $form->_priceSet
['optionsMaxValueDetails'] = $optionsMaxValueDetails;
641 $form->set('priceSetId', $form->_priceSetId
);
642 $form->set('priceSet', $form->_priceSet
);
652 * @param array $fields
653 * @param array $params
654 * @param array $lineItem
656 public static function processAmount(&$fields, &$params, &$lineItem) {
659 $radioLevel = $checkboxLevel = $selectLevel = $textLevel = array();
661 foreach ($fields as $id => $field) {
662 if (empty($params["price_{$id}"]) ||
663 (empty($params["price_{$id}"]) && $params["price_{$id}"] == NULL)
665 // skip if nothing was submitted for this field
669 switch ($field['html_type']) {
671 $params["price_{$id}"] = array(key($field['options']) => $params["price_{$id}"]);
672 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::format($id, $params, $field, $lineItem);
673 $totalPrice +
= $lineItem[key($field['options'])]['line_total'];
677 //special case if user select -none-
678 if ($params["price_{$id}"] <= 0) {
681 $params["price_{$id}"] = array($params["price_{$id}"] => 1);
682 $optionValueId = CRM_Utils_Array
::key(1, $params["price_{$id}"]);
683 $optionLabel = CRM_Utils_Array
::value('label', $field['options'][$optionValueId]);
684 $params['amount_priceset_level_radio'] = array();
685 $params['amount_priceset_level_radio'][$optionValueId] = $optionLabel;
686 if (isset($radioLevel)) {
687 $radioLevel = array_merge($radioLevel,
688 array_keys($params['amount_priceset_level_radio'])
692 $radioLevel = array_keys($params['amount_priceset_level_radio']);
694 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::format($id, $params, $field, $lineItem);
695 $totalPrice +
= $lineItem[$optionValueId]['line_total'];
699 $params["price_{$id}"] = array($params["price_{$id}"] => 1);
700 $optionValueId = CRM_Utils_Array
::key(1, $params["price_{$id}"]);
701 $optionLabel = $field['options'][$optionValueId]['label'];
702 $params['amount_priceset_level_select'] = array();
703 $params['amount_priceset_level_select'][CRM_Utils_Array
::key(1, $params["price_{$id}"])] = $optionLabel;
704 if (isset($selectLevel)) {
705 $selectLevel = array_merge($selectLevel, array_keys($params['amount_priceset_level_select']));
708 $selectLevel = array_keys($params['amount_priceset_level_select']);
710 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::format($id, $params, $field, $lineItem);
711 $totalPrice +
= $lineItem[$optionValueId]['line_total'];
715 $params['amount_priceset_level_checkbox'] = $optionIds = array();
716 foreach ($params["price_{$id}"] as $optionId => $option) {
717 $optionIds[] = $optionId;
718 $optionLabel = $field['options'][$optionId]['label'];
719 $params['amount_priceset_level_checkbox']["{$field['options'][$optionId]['id']}"] = $optionLabel;
720 if (isset($checkboxLevel)) {
721 $checkboxLevel = array_unique(array_merge(
723 array_keys($params['amount_priceset_level_checkbox'])
728 $checkboxLevel = array_keys($params['amount_priceset_level_checkbox']);
731 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::format($id, $params, $field, $lineItem);
732 foreach ($optionIds as $optionId) {
733 $totalPrice +
= $lineItem[$optionId]['line_total'];
739 $amount_level = array();
740 $totalParticipant = 0;
741 if (is_array($lineItem)) {
742 foreach ($lineItem as $values) {
743 $totalParticipant +
= $values['participant_count'];
744 if ($values['html_type'] == 'Text') {
745 $amount_level[] = $values['label'] . ' - ' . $values['qty'];
748 $amount_level[] = $values['label'];
752 $displayParticipantCount = '';
753 if ($totalParticipant > 0) {
754 $displayParticipantCount = ' Participant Count -' . $totalParticipant;
757 $params['amount_level'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $amount_level) . $displayParticipantCount . CRM_Core_DAO
::VALUE_SEPARATOR
;
758 $params['amount'] = $totalPrice;
762 * Build the price set form.
764 * @param CRM_Core_Form $form
766 public static function buildPriceSet(&$form) {
767 $priceSetId = $form->get('priceSetId');
768 $userid = $form->getVar('_userID');
773 $validFieldsOnly = TRUE;
774 $className = CRM_Utils_System
::getClassName($form);
775 if (in_array($className, array(
776 'CRM_Contribute_Form_Contribution',
777 'CRM_Member_Form_Membership',
779 $validFieldsOnly = FALSE;
782 $priceSet = self
::getSetDetail($priceSetId, TRUE, $validFieldsOnly);
783 $form->_priceSet
= CRM_Utils_Array
::value($priceSetId, $priceSet);
784 $form->_quickConfig
= $quickConfig = 0;
785 if (CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $priceSetId, 'is_quick_config')) {
789 $form->assign('quickConfig', $quickConfig);
790 if ($className == "CRM_Contribute_Form_Contribution_Main") {
791 $form->_quickConfig
= $quickConfig;
793 $form->assign('priceSet', $form->_priceSet
);
795 $component = 'contribution';
796 if ($className == 'CRM_Member_Form_Membership') {
797 $component = 'membership';
800 if ($className == 'CRM_Contribute_Form_Contribution_Main') {
801 $feeBlock = &$form->_values
['fee'];
802 if (!empty($form->_useForMember
)) {
803 $component = 'membership';
807 $feeBlock = &$form->_priceSet
['fields'];
811 CRM_Utils_Hook
::buildAmount($component, $form, $feeBlock);
813 foreach ($feeBlock as $field) {
814 if (CRM_Utils_Array
::value('visibility', $field) == 'public' ||
817 $options = CRM_Utils_Array
::value('options', $field);
818 if ($className == 'CRM_Contribute_Form_Contribution_Main' && $component = 'membership') {
819 $checklifetime = self
::checkCurrentMembership($options, $userid);
820 if ($checklifetime) {
821 $form->assign('ispricelifetime', TRUE);
824 if (!is_array($options)) {
827 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
::addQuickFormElement($form,
828 'price_' . $field['id'],
831 CRM_Utils_Array
::value('is_required', $field, FALSE),
840 * Check the current Membership.
842 * @param array $options
847 public static function checkCurrentMembership(&$options, $userid) {
848 if (!$userid ||
empty($options)) {
851 static $_contact_memberships = array();
852 $checklifetime = FALSE;
853 foreach ($options as $key => $value) {
854 if (!empty($value['membership_type_id'])) {
855 if (!isset($_contact_memberships[$userid][$value['membership_type_id']])) {
856 $_contact_memberships[$userid][$value['membership_type_id']] = CRM_Member_BAO_Membership
::getContactMembership($userid, $value['membership_type_id'], FALSE);
858 $currentMembership = $_contact_memberships[$userid][$value['membership_type_id']];
859 if (!empty($currentMembership) && empty($currentMembership['end_date'])) {
860 unset($options[$key]);
861 $checklifetime = TRUE;
865 if ($checklifetime) {
874 * Set default the price set fields.
876 * @param CRM_Core_Form $form
877 * @param array $defaults
881 public static function setDefaultPriceSet(&$form, &$defaults) {
882 if (!isset($form->_priceSet
) ||
empty($form->_priceSet
['fields'])) {
886 foreach ($form->_priceSet
['fields'] as $key => $val) {
887 foreach ($val['options'] as $keys => $values) {
888 if ($values['is_default']) {
889 if ($val['html_type'] == 'CheckBox') {
890 $defaults["price_{$key}"][$keys] = 1;
893 $defaults["price_{$key}"] = $keys;
902 * Get field ids of a price set.
904 * @param int $id Price Set id
907 * Array of the field ids
909 public static function getFieldIds($id) {
910 $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field();
911 $priceField->price_set_id
= $id;
913 while ($priceField->fetch()) {
914 $var[] = $priceField->id
;
920 * Copy a price set, including all the fields.
923 * The price set id to copy.
925 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field
927 public static function copy($id) {
928 $maxId = CRM_Core_DAO
::singleValueQuery("SELECT max(id) FROM civicrm_price_set");
930 $title = ts('[Copy id %1]', array(1 => $maxId +
1));
933 'title' => ' ' . $title,
934 'name' => '__Copy_id_' . ($maxId +
1) . '_',
938 $copy = &CRM_Core_DAO
::copyGeneric('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set',
944 //copying all the blocks pertaining to the price set
945 $copyPriceField = &CRM_Core_DAO
::copyGeneric('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field',
946 array('price_set_id' => $id),
947 array('price_set_id' => $copy->id
)
949 if (!empty($copyPriceField)) {
950 $price = array_combine(self
::getFieldIds($id), self
::getFieldIds($copy->id
));
952 //copy option group and values
953 foreach ($price as $originalId => $copyId) {
954 CRM_Core_DAO
::copyGeneric('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue',
955 array('price_field_id' => $originalId),
956 array('price_field_id' => $copyId)
962 CRM_Utils_Hook
::copy('Set', $copy);
967 * Check price set permission.
974 public function checkPermission($sid) {
976 self
::eventPriceSetDomainID()
978 $domain_id = CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $sid, 'domain_id', 'id');
979 if (CRM_Core_Config
::domainID() != $domain_id) {
980 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
987 * Get the sum of participant count for all fields of given price set.
992 * @param bool $onlyActive
994 * @return int|null|string
996 public static function getPricesetCount($sid, $onlyActive = TRUE) {
1004 $where = 'AND value.is_active = 1 AND field.is_active = 1';
1007 static $pricesetFieldCount;
1008 if (!isset($pricesetFieldCount[$sid])) {
1010 SELECT sum(value.count) as totalCount
1011 FROM civicrm_price_field_value value
1012 INNER JOIN civicrm_price_field field ON ( field.id = value.price_field_id )
1013 INNER JOIN civicrm_price_set pset ON ( pset.id = field.price_set_id )
1017 $count = CRM_Core_DAO
::singleValueQuery($sql, array(1 => array($sid, 'Positive')));
1018 $pricesetFieldCount[$sid] = ($count) ?
$count : 0;
1021 return $pricesetFieldCount[$sid];
1025 * Get membership count.
1031 public static function getMembershipCount($ids) {
1033 SELECT count( pfv.id ) AS count, pfv.id AS id
1034 FROM civicrm_price_field_value pfv
1035 INNER JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id
1036 WHERE pfv.id IN ( $ids )
1037 GROUP BY mt.member_of_contact_id";
1039 $crmDAO = CRM_Core_DAO
::executeQuery($queryString);
1042 while ($crmDAO->fetch()) {
1043 $count[$crmDAO->id
] = $crmDAO->count
;
1050 * Function to check if auto renew option should be shown.
1052 * @param int $priceSetId
1056 * $autoRenewOption ( 0:hide, 1:optional 2:required )
1058 public static function checkAutoRenewForPriceSet($priceSetId) {
1059 // auto-renew option should be visible if membership types associated with all the fields has
1060 // been set for auto-renew option
1061 // Auto renew checkbox should be frozen if for all the membership type auto renew is required
1063 // get the membership type auto renew option and check if required or optional
1064 $query = 'SELECT mt.auto_renew, mt.duration_interval, mt.duration_unit
1065 FROM civicrm_price_field_value pfv
1066 INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id
1067 INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id
1068 WHERE pf.price_set_id = %1
1069 AND pf.is_active = 1
1070 AND pfv.is_active = 1';
1072 $params = array(1 => array($priceSetId, 'Integer'));
1074 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1075 $autoRenewOption = 2;
1076 $interval = $unit = array();
1077 while ($dao->fetch()) {
1078 if (!$dao->auto_renew
) {
1079 $autoRenewOption = 0;
1082 if ($dao->auto_renew
== 1) {
1083 $autoRenewOption = 1;
1086 $interval[$dao->duration_interval
] = $dao->duration_interval
;
1087 $unit[$dao->duration_unit
] = $dao->duration_unit
;
1090 if (count($interval) == 1 && count($unit) == 1 && $autoRenewOption > 0) {
1091 return $autoRenewOption;
1099 * Retrieve auto renew frequency and interval.
1101 * @param int $priceSetId
1105 * associate array of frequency interval and unit
1107 public static function getRecurDetails($priceSetId) {
1108 $query = 'SELECT mt.duration_interval, mt.duration_unit
1109 FROM civicrm_price_field_value pfv
1110 INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id
1111 INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id
1112 WHERE pf.price_set_id = %1 LIMIT 1';
1114 $params = array(1 => array($priceSetId, 'Integer'));
1115 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1117 return array($dao->duration_interval
, $dao->duration_unit
);
1121 * Get event price set for domain.
1125 public static function eventPriceSetDomainID() {
1126 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
1127 'event_price_set_domain_id',
1133 * Update the is_quick_config flag in the db.
1136 * Id of the database record.
1137 * @param bool $isQuickConfig we want to set the is_quick_config field.
1138 * Value we want to set the is_quick_config field.
1141 * DAO object on success, null otherwise
1143 public static function setIsQuickConfig($id, $isQuickConfig) {
1144 return CRM_Core_DAO
::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $id, 'is_quick_config', $isQuickConfig);