/** * Validate the priceset. * * @param int $priceSetId * @param array $fields * @param array $error */ public static function priceSetValidation($priceSetId, $fields, &$error) { // check for at least one positive // amount price field should be selected. $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); $priceField->price_set_id = $priceSetId; $priceField->find(); $priceFields = array(); while ($priceField->fetch()) { $key = "price_{$priceField->id}"; if (!empty($fields[$key])) { $priceFields[$priceField->id] = $fields[$key]; } } if (!empty($priceFields)) { // we should has to have positive amount. $sql = "\nSELECT id, html_type\nFROM civicrm_price_field\nWHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; $fieldDAO = CRM_Core_DAO::executeQuery($sql); $htmlTypes = array(); while ($fieldDAO->fetch()) { $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type; } $selectedAmounts = array(); foreach ($htmlTypes as $fieldId => $type) { $options = array(); CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fieldId, $options); if (empty($options)) { continue; } if ($type == 'Text') { foreach ($options as $opId => $option) { $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount']; break; } } elseif (is_array($fields["price_{$fieldId}"])) { foreach (array_keys($fields["price_{$fieldId}"]) as $opId) { $selectedAmounts[$opId] = $options[$opId]['amount']; } } elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) { $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount']; } } list($componentName) = explode(':', $fields['_qf_default']); // now we have all selected amount in hand. $totalAmount = array_sum($selectedAmounts); if ($totalAmount < 0) { $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName)); } } else { $error['_qf_default'] = ts("Please select at least one option from price set."); } }
/** * Get field ids of a price set. * * @param int $id Price Set id * * @return array * Array of the field ids * */ public static function getFieldIds($id) { $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); $priceField->price_set_id = $id; $priceField->find(); while ($priceField->fetch()) { $var[] = $priceField->id; } return $var; }