3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * Business object for managing price sets
40 class CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set
extends CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set
{
45 function __construct() {
46 parent
::__construct();
50 * takes an associative array and creates a price set object
52 * @param array $params (reference) an assoc array of name/value pairs
54 * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set object
58 static function create(&$params) {
59 $priceSetBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_Set();
60 $priceSetBAO->copyValues($params);
61 if (self
::eventPriceSetDomainID()) {
62 $priceSetBAO->domain_id
= CRM_Core_Config
::domainID();
64 return $priceSetBAO->save();
68 * Takes a bunch of params that are needed to match certain criteria and
69 * retrieves the relevant objects. Typically the valid params are only
70 * contact_id. We'll tweak this function to be more full featured over a period
71 * of time. This is the inverse function of create. It also stores all the retrieved
72 * values in the default array
74 * @param array $params (reference ) an assoc array of name/value pairs
75 * @param array $defaults (reference ) an assoc array to hold the flattened values
77 * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set object
81 static function retrieve(&$params, &$defaults) {
82 return CRM_Core_DAO
::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $params, $defaults);
86 * update the is_active flag in the db
88 * @param int $id id of the database record
89 * @param boolean $is_active value we want to set the is_active field
91 * @return Object DAO object on sucess, null otherwise
95 static function setIsActive($id, $isActive) {
96 return CRM_Core_DAO
::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $id, 'is_active', $isActive);
100 * Calculate the default price set id
101 * assigned to the contribution/membership etc
103 * @param string $entity
105 * @return id $priceSetID
111 public static function getDefaultPriceSet($entity = 'contribution') {
112 if ($entity == 'contribution') {
113 $entityName = 'default_contribution_amount';
115 else if ($entity == 'membership') {
116 $entityName = 'default_membership_type_amount';
120 SELECT ps.id AS setID, pfv.price_field_id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.name, pfv.label
121 FROM civicrm_price_set ps
122 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
123 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
124 WHERE ps.name = '{$entityName}'
127 $dao = CRM_Core_DAO
::executeQuery($sql);
128 $defaultPriceSet = array();
129 while ($dao->fetch()) {
130 $defaultPriceSet[$dao->priceFieldValueID
]['setID'] = $dao->setID
;
131 $defaultPriceSet[$dao->priceFieldValueID
]['priceFieldID'] = $dao->priceFieldID
;
132 $defaultPriceSet[$dao->priceFieldValueID
]['name'] = $dao->name
;
133 $defaultPriceSet[$dao->priceFieldValueID
]['label'] = $dao->label
;
137 return $defaultPriceSet;
141 * Get the price set title.
143 * @param int $id id of price set
145 * @return string title
151 public static function getTitle($id) {
152 return CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $id, 'title');
156 * Return a list of all forms which use this price set.
158 * @param int $id id of price set
159 * @param str $simpleReturn - get raw data. Possible values: 'entity', 'table'
163 public static function &getUsedBy($id, $simpleReturn = FALSE) {
164 $usedBy = $forms = $tables = array();
166 SELECT entity_table, entity_id
167 FROM civicrm_price_set_entity
168 WHERE price_set_id = %1";
169 $params = array(1 => array($id, 'Integer'));
170 $crmFormDAO = CRM_Core_DAO
::executeQuery($queryString, $params);
172 while ($crmFormDAO->fetch()) {
173 $forms[$crmFormDAO->entity_table
][] = $crmFormDAO->entity_id
;
174 $tables[] = $crmFormDAO->entity_table
;
176 // Return only tables
177 if ($simpleReturn == 'table') {
182 SELECT cli.entity_table, cli.entity_id
183 FROM civicrm_line_item cli
184 LEFT JOIN civicrm_price_field cpf ON cli.price_field_id = cpf.id
185 WHERE cpf.price_set_id = %1";
186 $params = array(1 => array($id, 'Integer'));
187 $crmFormDAO = CRM_Core_DAO
::executeQuery($queryString, $params);
188 while ($crmFormDAO->fetch()) {
189 $forms[$crmFormDAO->entity_table
][] = $crmFormDAO->entity_id
;
190 $tables[] = $crmFormDAO->entity_table
;
196 // Return only entity data
197 if ($simpleReturn == 'entity') {
200 foreach ($forms as $table => $entities) {
202 case 'civicrm_event':
203 $ids = implode(',', $entities);
204 $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
205 FROM civicrm_event ce
206 LEFT JOIN civicrm_option_value ON
207 ( ce.event_type_id = civicrm_option_value.value )
208 LEFT JOIN civicrm_option_group ON
209 ( civicrm_option_group.id = civicrm_option_value.option_group_id )
211 civicrm_option_group.name = 'event_type' AND
212 ( ce.is_template IS NULL OR ce.is_template = 0) AND
215 $crmDAO = CRM_Core_DAO
::executeQuery($queryString);
216 while ($crmDAO->fetch()) {
217 $usedBy[$table][$crmDAO->id
]['title'] = $crmDAO->title
;
218 $usedBy[$table][$crmDAO->id
]['eventType'] = $crmDAO->eventType
;
219 $usedBy[$table][$crmDAO->id
]['startDate'] = $crmDAO->startDate
;
220 $usedBy[$table][$crmDAO->id
]['endDate'] = $crmDAO->endDate
;
221 $usedBy[$table][$crmDAO->id
]['isPublic'] = $crmDAO->isPublic
;
225 case 'civicrm_contribution_page':
226 $ids = implode(',', $entities);
227 $queryString = "SELECT cp.id as id, cp.title as title, cp.start_date as startDate, cp.end_date as endDate,ct.name as type
228 FROM civicrm_contribution_page cp, civicrm_contribution_type ct
229 WHERE ct.id = cp.contribution_type_id AND
232 $crmDAO = CRM_Core_DAO
::executeQuery($queryString);
233 while ($crmDAO->fetch()) {
234 $usedBy[$table][$crmDAO->id
]['title'] = $crmDAO->title
;
235 $usedBy[$table][$crmDAO->id
]['type'] = $crmDAO->type
;
236 $usedBy[$table][$crmDAO->id
]['startDate'] = $crmDAO->startDate
;
237 $usedBy[$table][$crmDAO->id
]['endDate'] = $crmDAO->endDate
;
241 case 'civicrm_contribution':
242 case 'civicrm_membership':
243 case 'civicrm_participant':
248 CRM_Core_Error
::fatal("$table is not supported in PriceSet::usedBy()");
257 * Delete the price set
259 * @param int $id Price Set id
261 * @return boolean false if fields exist for this set, true if the
262 * set could be deleted
267 public static function deleteSet($id) {
268 // remove from all inactive forms
269 $usedBy = self
::getUsedBy($id);
270 if (isset($usedBy['civicrm_event'])) {
271 foreach ($usedBy['civicrm_event'] as $eventId => $unused) {
272 $eventDAO = new CRM_Event_DAO_Event();
273 $eventDAO->id
= $eventId;
275 while ($eventDAO->fetch()) {
276 self
::removeFrom('civicrm_event', $eventDAO->id
);
281 // delete price fields
282 $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field();
283 $priceField->price_set_id
= $id;
285 while ($priceField->fetch()) {
286 // delete options first
287 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
::deleteField($priceField->id
);
290 $set = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set();
292 return $set->delete();
296 * Link the price set with the specified table and id
298 * @param string $entityTable
299 * @param integer $entityId
300 * @param integer $priceSetId
304 public static function addTo($entityTable, $entityId, $priceSetId) {
305 // verify that the price set exists
306 $dao = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set();
307 $dao->id
= $priceSetId;
313 $dao = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_SetEntity();
314 // find if this already exists
315 $dao->entity_id
= $entityId;
316 $dao->entity_table
= $entityTable;
319 // add or update price_set_id
320 $dao->price_set_id
= $priceSetId;
325 * Delete price set for the given entity and id
327 * @param string $entityTable
328 * @param integer $entityId
330 public static function removeFrom($entityTable, $entityId) {
331 $dao = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_SetEntity();
332 $dao->entity_table
= $entityTable;
333 $dao->entity_id
= $entityId;
334 return $dao->delete();
338 * Find a price_set_id associatied with the given table, id and usedFor
339 * Used For value for events:1, contribution:2, membership:3
341 * @param string $entityTable
342 * @param int $entityId
343 * @param int $usedFor ( price set that extends/used for particular component )
345 * @return integer|false price_set_id, or false if none found
347 public static function getFor($entityTable, $entityId, $usedFor = NULL, $isQuickConfig = NULL, &$setName = NULL) {
348 if (!$entityTable ||
!$entityId) {
352 $sql = 'SELECT ps.id as price_set_id, ps.name as price_set_name
353 FROM civicrm_price_set ps
354 INNER JOIN civicrm_price_set_entity pse ON ps.id = pse.price_set_id
355 WHERE pse.entity_table = %1 AND pse.entity_id = %2 ';
356 if ($isQuickConfig) {
357 $sql .= " AND ps.is_quick_config = 0 ";
359 $params = array(1 => array($entityTable, 'String'),
360 2 => array($entityId, 'Integer'),
363 $sql .= " AND ps.extends LIKE '%%3%' ";
364 $params[3] = array($usedFor, 'Integer');
367 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
369 $setName = (isset($dao->price_set_name
)) ?
$dao->price_set_name
: FALSE;
370 return (isset($dao->price_set_id
)) ?
$dao->price_set_id
: FALSE;
374 * Find a price_set_id associatied with the given option value or field ID
376 * @param array $params (reference) an assoc array of name/value pairs
377 * array may contain either option id or
380 * @return price set id on success, null otherwise
384 public static function getSetId(&$params) {
387 if ($oid = CRM_Utils_Array
::value('oid', $params)) {
388 $fieldValue = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
389 $fieldValue->id
= $oid;
390 if ($fieldValue->find(TRUE)) {
391 $fid = $fieldValue->price_field_id
;
395 $fid = CRM_Utils_Array
::value('fid', $params);
399 return CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $fid, 'price_set_id');
406 * Return an associative array of all price sets
408 * @param bool $withInactive whether or not to include inactive entries
409 * @param string $extendComponentName name of the component like 'CiviEvent','CiviContribute'
411 * @return array associative array of id => name
413 public static function getAssoc($withInactive = FALSE, $extendComponentName = FALSE) {
416 DISTINCT ( price_set_id ) as id, title
421 civicrm_price_set.id = civicrm_price_field.price_set_id AND is_quick_config = 0 ";
423 if (!$withInactive) {
424 $query .= " AND civicrm_price_set.is_active = 1 ";
427 if (self
::eventPriceSetDomainID()) {
428 $query .= " AND civicrm_price_set.domain_id = " . CRM_Core_Config
::domainID();
431 $priceSets = array();
433 if ($extendComponentName) {
434 $componentId = CRM_Core_Component
::getComponentID($extendComponentName);
438 $query .= " AND civicrm_price_set.extends LIKE '%$componentId%' ";
441 $dao = CRM_Core_DAO
::executeQuery($query);
442 while ($dao->fetch()) {
443 $priceSets[$dao->id
] = $dao->title
;
449 * Get price set details
451 * An array containing price set details (including price fields) is returned
453 * @param int $setId - price set id whose details are needed
455 * @return array $setTree - array consisting of field details
457 public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE) {
460 $select = $from = $where = $orderBy = '';
462 $priceFields = array(
471 'is_display_amounts',
480 if ($required == TRUE) {
481 $priceFields[] = 'is_required';
485 $select = 'SELECT ' . implode(',', $priceFields);
486 $from = ' FROM civicrm_price_field';
489 $params[1] = array($setID, 'Integer');
491 WHERE price_set_id = %1
496 $currentTime = date('YmdHis');
498 AND ( active_on IS NULL OR active_on <= {$currentTime} )
499 AND ( expire_on IS NULL OR expire_on >= {$currentTime} )
503 $orderBy = ' ORDER BY weight';
505 $sql = $select . $from . $where . $dateSelect . $orderBy;
507 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
509 $visibility = CRM_Core_PseudoConstant
::visibility('name');
510 while ($dao->fetch()) {
513 $setTree[$setID]['fields'][$fieldID] = array();
514 $setTree[$setID]['fields'][$fieldID]['id'] = $fieldID;
516 foreach ($priceFields as $field) {
517 if ($field == 'id' ||
is_null($dao->$field)) {
521 if ($field == 'visibility_id') {
522 $setTree[$setID]['fields'][$fieldID]['visibility'] = $visibility[$dao->$field];
524 $setTree[$setID]['fields'][$fieldID][$field] = $dao->$field;
526 $setTree[$setID]['fields'][$fieldID]['options'] = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
::getOptions($fieldID, FALSE);
529 // also get the pre and post help from this price set
531 SELECT extends, contribution_type_id, help_pre, help_post, is_quick_config
532 FROM civicrm_price_set
534 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
536 $setTree[$setID]['extends'] = $dao->extends;
537 $setTree[$setID]['contribution_type_id'] = $dao->contribution_type_id
;
538 $setTree[$setID]['help_pre'] = $dao->help_pre
;
539 $setTree[$setID]['help_post'] = $dao->help_post
;
540 $setTree[$setID]['is_quick_config'] = $dao->is_quick_config
;
545 static function initSet(&$form, $id, $entityTable = 'civicrm_event', $validOnly = FALSE, $priceSetId = NULL) {
547 $priceSetId = self
::getFor($entityTable, $id);
550 //check if priceset is is_config
551 if (is_numeric($priceSetId)) {
552 if (CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $priceSetId, 'is_quick_config') && $form->getVar('_name') != 'Participant') {
553 $form->assign('quickConfig', 1);
558 if ($form->_action
& CRM_Core_Action
::UPDATE
) {
559 $entityId = $entity = NULL;
561 switch ($entityTable) {
562 case 'civicrm_event':
563 $entity = 'participant';
564 if (CRM_Utils_System
::getClassName($form) == 'CRM_Event_Form_Participant') {
565 $entityId = $form->_id
;
568 $entityId = $form->_participantId
;
572 case 'civicrm_contribution_page':
573 case 'civicrm_contribution':
574 $entity = 'contribution';
575 $entityId = $form->_id
;
579 if ($entityId && $entity) {
580 $form->_values
['line_items'] = CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::getLineItems($entityId, $entity);
588 $form->_priceSetId
= $priceSetId;
589 $priceSet = self
::getSetDetail($priceSetId, $required, $validOnly);
590 $form->_priceSet
= CRM_Utils_Array
::value($priceSetId, $priceSet);
591 $form->_values
['fee'] = CRM_Utils_Array
::value('fields', $form->_priceSet
);
593 //get the price set fields participant count.
594 if ($entityTable == 'civicrm_event') {
595 //get option count info.
596 $form->_priceSet
['optionsCountTotal'] = self
::getPricesetCount($priceSetId);
597 if ($form->_priceSet
['optionsCountTotal']) {
598 $optionsCountDeails = array();
599 if (!empty($form->_priceSet
['fields'])) {
600 foreach ($form->_priceSet
['fields'] as $field) {
601 foreach ($field['options'] as $option) {
602 $count = CRM_Utils_Array
::value('count', $option, 0);
603 $optionsCountDeails['fields'][$field['id']]['options'][$option['id']] = $count;
607 $form->_priceSet
['optionsCountDetails'] = $optionsCountDeails;
610 //get option max value info.
611 $optionsMaxValueTotal = 0;
612 $optionsMaxValueDetails = array();
614 if (!empty($form->_priceSet
['fields'])) {
615 foreach ($form->_priceSet
['fields'] as $field) {
616 foreach ($field['options'] as $option) {
617 $maxVal = CRM_Utils_Array
::value('max_value', $option, 0);
618 $optionsMaxValueDetails['fields'][$field['id']]['options'][$option['id']] = $maxVal;
619 $optionsMaxValueTotal +
= $maxVal;
624 $form->_priceSet
['optionsMaxValueTotal'] = $optionsMaxValueTotal;
625 if ($optionsMaxValueTotal) {
626 $form->_priceSet
['optionsMaxValueDetails'] = $optionsMaxValueDetails;
629 $form->set('priceSetId', $form->_priceSetId
);
630 $form->set('priceSet', $form->_priceSet
);
637 static function processAmount(&$fields, &$params, &$lineItem) {
640 $radioLevel = $checkboxLevel = $selectLevel = $textLevel = array();
642 foreach ($fields as $id => $field) {
643 if (!CRM_Utils_Array
::value("price_{$id}", $params) ||
644 (empty($params["price_{$id}"]) && $params["price_{$id}"] == NULL)
646 // skip if nothing was submitted for this field
650 switch ($field['html_type']) {
652 $params["price_{$id}"] = array(key($field['options']) => $params["price_{$id}"]);
653 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::format($id, $params, $field, $lineItem);
654 $totalPrice +
= $lineItem[key($field['options'])]['line_total'];
658 //special case if user select -none-
659 if ($params["price_{$id}"] <= 0) {
662 $params["price_{$id}"] = array($params["price_{$id}"] => 1);
663 $optionValueId = CRM_Utils_Array
::key(1, $params["price_{$id}"]);
664 $optionLabel = CRM_Utils_Array
::value('label', $field['options'][$optionValueId]);
665 $params['amount_priceset_level_radio'] = array();
666 $params['amount_priceset_level_radio'][$optionValueId] = $optionLabel;
667 if (isset($radioLevel)) {
668 $radioLevel = array_merge($radioLevel,
669 array_keys($params['amount_priceset_level_radio'])
673 $radioLevel = array_keys($params['amount_priceset_level_radio']);
675 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::format($id, $params, $field, $lineItem);
676 $totalPrice +
= $lineItem[$optionValueId]['line_total'];
680 $params["price_{$id}"] = array($params["price_{$id}"] => 1);
681 $optionValueId = CRM_Utils_Array
::key(1, $params["price_{$id}"]);
682 $optionLabel = $field['options'][$optionValueId]['label'];
683 $params['amount_priceset_level_select'] = array();
684 $params['amount_priceset_level_select'][CRM_Utils_Array
::key(1, $params["price_{$id}"])] = $optionLabel;
685 if (isset($selectLevel)) {
686 $selectLevel = array_merge($selectLevel, array_keys($params['amount_priceset_level_select']));
689 $selectLevel = array_keys($params['amount_priceset_level_select']);
691 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::format($id, $params, $field, $lineItem);
692 $totalPrice +
= $lineItem[$optionValueId]['line_total'];
695 $params['amount_priceset_level_checkbox'] = $optionIds = array();
696 foreach ($params["price_{$id}"] as $optionId => $option) {
697 $optionIds[] = $optionId;
698 $optionLabel = $field['options'][$optionId]['label'];
699 $params['amount_priceset_level_checkbox']["{$field['options'][$optionId]['id']}"] = $optionLabel;
700 if (isset($checkboxLevel)) {
701 $checkboxLevel = array_unique(array_merge(
703 array_keys($params['amount_priceset_level_checkbox'])
708 $checkboxLevel = array_keys($params['amount_priceset_level_checkbox']);
711 CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem
::format($id, $params, $field, $lineItem);
712 foreach ($optionIds as $optionId) {
713 $totalPrice +
= $lineItem[$optionId]['line_total'];
719 $amount_level = array();
720 $totalParticipant = 0;
721 if (is_array($lineItem)) {
722 foreach ($lineItem as $values) {
723 $totalParticipant +
= $values['participant_count'];
724 if ($values['html_type'] == 'Text') {
725 $amount_level[] = $values['label'] . ' - ' . $values['qty'];
728 $amount_level[] = $values['label'];
732 $displayParticipantCount = '';
733 if ($totalParticipant > 0) {
734 $displayParticipantCount = ' Participant Count -' . $totalParticipant;
737 $params['amount_level'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $amount_level) . $displayParticipantCount . CRM_Core_DAO
::VALUE_SEPARATOR
;
738 $params['amount'] = $totalPrice;
742 * Function to build the price set form.
747 static function buildPriceSet(&$form) {
748 $priceSetId = $form->get('priceSetId');
749 $userid = $form->getVar('_userID');
754 $validFieldsOnly = TRUE;
755 $className = CRM_Utils_System
::getClassName($form);
756 if (in_array($className, array(
757 'CRM_Contribute_Form_Contribution', 'CRM_Member_Form_Membership'))) {
758 $validFieldsOnly = FALSE;
761 $priceSet = self
::getSetDetail($priceSetId, TRUE, $validFieldsOnly);
762 $form->_priceSet
= CRM_Utils_Array
::value($priceSetId, $priceSet);
763 $form->_quickConfig
= $quickConfig = 0;
764 if (CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $priceSetId, 'is_quick_config')) {
768 $form->assign('quickConfig', $quickConfig);
769 if ($className == "CRM_Contribute_Form_Contribution_Main") {
770 $form->_quickConfig
= $quickConfig;
772 $form->assign('priceSet', $form->_priceSet
);
774 $component = 'contribution';
775 if ($className == 'CRM_Member_Form_Membership') {
776 $component = 'membership';
779 if ($className == 'CRM_Contribute_Form_Contribution_Main') {
780 $feeBlock = &$form->_values
['fee'];
781 if (!empty($form->_useForMember
)) {
782 $component = 'membership';
786 $feeBlock = &$form->_priceSet
['fields'];
790 CRM_Utils_Hook
::buildAmount($component, $form, $feeBlock);
792 foreach ($feeBlock as $field) {
793 if (CRM_Utils_Array
::value('visibility', $field) == 'public' ||
796 $options = CRM_Utils_Array
::value('options', $field);
797 if ($className == 'CRM_Contribute_Form_Contribution_Main' && $component = 'membership') {
798 $checklifetime = self
::checkCurrentMembership($options, $userid);
799 if ($checklifetime) {
800 $form->assign('ispricelifetime', TRUE);
803 if (!is_array($options)) {
806 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field
::addQuickFormElement($form,
807 'price_' . $field['id'],
810 CRM_Utils_Array
::value('is_required', $field, FALSE),
819 * Function to check the current Membership
820 * having end date null.
822 static function checkCurrentMembership(&$options, $userid) {
823 if (!$userid ||
empty($options)) {
826 static $_contact_memberships = array();
827 $checklifetime = FALSE;
828 foreach ($options as $key => $value) {
829 if (CRM_Utils_Array
::value('membership_type_id', $value)) {
830 if (!isset($_contact_memberships[$userid][$value['membership_type_id']])) {
831 $_contact_memberships[$userid][$value['membership_type_id']] = CRM_Member_BAO_Membership
::getContactMembership($userid, $value['membership_type_id'], FALSE);
833 $currentMembership = $_contact_memberships[$userid][$value['membership_type_id']];
834 if (!empty($currentMembership) && !CRM_Utils_Array
::value('end_date', $currentMembership)) {
835 unset($options[$key]);
836 $checklifetime = TRUE;
840 if ($checklifetime) {
849 * Function to set daefult the price set fields.
851 * @return array $defaults
854 static function setDefaultPriceSet(&$form, &$defaults) {
855 if (!isset($form->_priceSet
) ||
empty($form->_priceSet
['fields'])) {
859 foreach ($form->_priceSet
['fields'] as $key => $val) {
860 foreach ($val['options'] as $keys => $values) {
861 if ($values['is_default']) {
862 if ($val['html_type'] == 'CheckBox') {
863 $defaults["price_{$key}"][$keys] = 1;
866 $defaults["price_{$key}"] = $keys;
875 * Get field ids of a price set
877 * @param int id Price Set id
879 * @return array of the field ids
884 public static function getFieldIds($id) {
885 $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field();
886 $priceField->price_set_id
= $id;
888 while ($priceField->fetch()) {
889 $var[] = $priceField->id
;
895 * This function is to make a copy of a price set, including
898 * @param int $id the price set id to copy
900 * @return the copy object
904 static function copy($id) {
905 $maxId = CRM_Core_DAO
::singleValueQuery("SELECT max(id) FROM civicrm_price_set");
907 $title = ts('[Copy id %1]', array(1 => $maxId +
1));
909 'suffix' => array('title' => ' ' . $title,
910 'name' => '__Copy_id_' . ($maxId +
1) . '_',
914 $copy = &CRM_Core_DAO
::copyGeneric('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set',
920 //copying all the blocks pertaining to the price set
921 $copyPriceField = &CRM_Core_DAO
::copyGeneric('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field',
922 array('price_set_id' => $id),
923 array('price_set_id' => $copy->id
)
925 if (!empty($copyPriceField)) {
926 $price = array_combine(self
::getFieldIds($id), self
::getFieldIds($copy->id
));
928 //copy option group and values
929 foreach ($price as $originalId => $copyId) {
930 CRM_Core_DAO
::copyGeneric('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue',
931 array('price_field_id' => $originalId),
932 array('price_field_id' => $copyId)
938 CRM_Utils_Hook
::copy('Set', $copy);
943 * This function is to check price set permission
945 * @param int $sid the price set id
947 function checkPermission($sid) {
949 self
::eventPriceSetDomainID()
951 $domain_id = CRM_Core_DAO
::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $sid, 'domain_id', 'id');
952 if (CRM_Core_Config
::domainID() != $domain_id) {
953 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
960 * Get the sum of participant count
961 * for all fields of given price set.
963 * @param int $sid the price set id
968 public static function getPricesetCount($sid, $onlyActive = TRUE) {
976 $where = 'AND value.is_active = 1 AND field.is_active = 1';
979 static $pricesetFieldCount;
980 if (!isset($pricesetFieldCount[$sid])) {
982 SELECT sum(value.count) as totalCount
983 FROM civicrm_price_field_value value
984 INNER JOIN civicrm_price_field field ON ( field.id = value.price_field_id )
985 INNER JOIN civicrm_price_set pset ON ( pset.id = field.price_set_id )
989 $count = CRM_Core_DAO
::singleValueQuery($sql, array(1 => array($sid, 'Positive')));
990 $pricesetFieldCount[$sid] = ($count) ?
$count : 0;
993 return $pricesetFieldCount[$sid];
996 public static function getMembershipCount($ids) {
998 SELECT count( pfv.id ) AS count, pfv.id AS id
999 FROM civicrm_price_field_value pfv
1000 INNER JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id
1001 WHERE pfv.id IN ( $ids )
1002 GROUP BY mt.member_of_contact_id";
1004 $crmDAO = CRM_Core_DAO
::executeQuery($queryString);
1007 while ($crmDAO->fetch()) {
1008 $count[$crmDAO->id
] = $crmDAO->count
;
1015 * Function to check if auto renew option should be shown
1017 * @param int $priceSetId price set id
1019 * @return int $autoRenewOption ( 0:hide, 1:optional 2:required )
1021 public static function checkAutoRenewForPriceSet($priceSetId) {
1022 // auto-renew option should be visible if membership types associated with all the fields has
1023 // been set for auto-renew option
1024 // Auto renew checkbox should be frozen if for all the membership type auto renew is required
1026 // get the membership type auto renew option and check if required or optional
1027 $query = 'SELECT mt.auto_renew, mt.duration_interval, mt.duration_unit
1028 FROM civicrm_price_field_value pfv
1029 INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id
1030 INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id
1031 WHERE pf.price_set_id = %1
1032 AND pf.is_active = 1
1033 AND pfv.is_active = 1';
1035 $params = array(1 => array($priceSetId, 'Integer'));
1037 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1038 $autoRenewOption = 2;
1039 $interval = $unit = array();
1040 while ($dao->fetch()) {
1041 if (!$dao->auto_renew
) {
1042 $autoRenewOption = 0;
1045 if ($dao->auto_renew
== 1) {
1046 $autoRenewOption = 1;
1049 $interval[$dao->duration_interval
] = $dao->duration_interval
;
1050 $unit[$dao->duration_unit
] = $dao->duration_unit
;
1053 if (count($interval) == 1 && count($unit) == 1 && $autoRenewOption > 0) {
1054 return $autoRenewOption;
1062 * Function to retrieve auto renew frequency and interval
1064 * @param int $priceSetId price set id
1066 * @return array associate array of frequency interval and unit
1070 public static function getRecurDetails($priceSetId) {
1071 $query = 'SELECT mt.duration_interval, mt.duration_unit
1072 FROM civicrm_price_field_value pfv
1073 INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id
1074 INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id
1075 WHERE pf.price_set_id = %1 LIMIT 1';
1077 $params = array(1 => array($priceSetId, 'Integer'));
1078 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1080 return array($dao->duration_interval
, $dao->duration_unit
);
1083 static function eventPriceSetDomainID() {
1084 return CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MULTISITE_PREFERENCES_NAME
,
1085 'event_price_set_domain_id',
1091 * update the is_quick_config flag in the db
1093 * @param int $id id of the database record
1094 * @param boolean $isQuickConfig value we want to set the is_quick_config field
1096 * @return Object DAO object on sucess, null otherwise
1100 static function setIsQuickConfig($id, $isQuickConfig) {
1101 return CRM_Core_DAO
::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $id, 'is_quick_config', $isQuickConfig);