| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Business objects for managing price fields. |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field extends CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field { |
| 41 | |
| 42 | protected $_options; |
| 43 | |
| 44 | /** |
| 45 | * Takes an associative array and creates a price field object |
| 46 | * |
| 47 | * the function extract all the params it needs to initialize the create a |
| 48 | * price field object. the params array could contain additional unused name/value |
| 49 | * pairs |
| 50 | * |
| 51 | * @param array $params |
| 52 | * (reference ) an assoc array of name/value pairs. |
| 53 | * |
| 54 | * @return CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field |
| 55 | */ |
| 56 | public static function &add(&$params) { |
| 57 | $priceFieldBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field(); |
| 58 | |
| 59 | $priceFieldBAO->copyValues($params); |
| 60 | |
| 61 | if ($id = CRM_Utils_Array::value('id', $params)) { |
| 62 | $priceFieldBAO->id = $id; |
| 63 | } |
| 64 | |
| 65 | $priceFieldBAO->save(); |
| 66 | return $priceFieldBAO; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Takes an associative array and creates a price field object |
| 71 | * |
| 72 | * This function is invoked from within the web form layer and also from the api layer |
| 73 | * |
| 74 | * @param array $params |
| 75 | * (reference) an assoc array of name/value pairs. |
| 76 | * |
| 77 | * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field |
| 78 | */ |
| 79 | public static function create(&$params) { |
| 80 | |
| 81 | $transaction = new CRM_Core_Transaction(); |
| 82 | |
| 83 | $priceField = self::add($params); |
| 84 | |
| 85 | if (is_a($priceField, 'CRM_Core_Error')) { |
| 86 | $transaction->rollback(); |
| 87 | return $priceField; |
| 88 | } |
| 89 | |
| 90 | $options = $optionsIds = array(); |
| 91 | $maxIndex = CRM_Price_Form_Field::NUM_OPTION; |
| 92 | |
| 93 | if ($priceField->html_type == 'Text') { |
| 94 | $maxIndex = 1; |
| 95 | |
| 96 | $fieldValue = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue(); |
| 97 | $fieldValue->price_field_id = $priceField->id; |
| 98 | |
| 99 | // update previous field values( if any ) |
| 100 | if ($fieldValue->find(TRUE)) { |
| 101 | $optionsIds['id'] = $fieldValue->id; |
| 102 | } |
| 103 | } |
| 104 | $defaultArray = array(); |
| 105 | if ($params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) { |
| 106 | $tempArray = array_keys($params['default_checkbox_option']); |
| 107 | foreach ($tempArray as $v) { |
| 108 | if ($params['option_amount'][$v]) { |
| 109 | $defaultArray[$v] = 1; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | else { |
| 114 | if (!empty($params['default_option'])) { |
| 115 | $defaultArray[$params['default_option']] = 1; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | for ($index = 1; $index <= $maxIndex; $index++) { |
| 120 | |
| 121 | if (array_key_exists('option_amount', $params) && |
| 122 | array_key_exists($index, $params['option_amount']) && |
| 123 | (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_label', $params)) || !empty($params['is_quick_config'])) && |
| 124 | !CRM_Utils_System::isNull($params['option_amount'][$index]) |
| 125 | ) { |
| 126 | $options = array( |
| 127 | 'price_field_id' => $priceField->id, |
| 128 | 'label' => trim($params['option_label'][$index]), |
| 129 | 'name' => CRM_Utils_String::munge($params['option_label'][$index], '_', 64), |
| 130 | 'amount' => CRM_Utils_Rule::cleanMoney(trim($params['option_amount'][$index])), |
| 131 | 'count' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_count', $params), NULL), |
| 132 | 'max_value' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_max_value', $params), NULL), |
| 133 | 'description' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_description', $params), NULL), |
| 134 | 'membership_type_id' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_type_id', $params), NULL), |
| 135 | 'weight' => $params['option_weight'][$index], |
| 136 | 'is_active' => 1, |
| 137 | 'is_default' => CRM_Utils_Array::value($params['option_weight'][$index], $defaultArray) ? $defaultArray[$params['option_weight'][$index]] : 0, |
| 138 | ); |
| 139 | |
| 140 | if ($opIds = CRM_Utils_Array::value('option_id', $params)) { |
| 141 | if ($opId = CRM_Utils_Array::value($index, $opIds)) { |
| 142 | $optionsIds['id'] = $opId; |
| 143 | } |
| 144 | else { |
| 145 | $optionsIds['id'] = NULL; |
| 146 | } |
| 147 | } |
| 148 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::create($options, $optionsIds); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | $transaction->commit(); |
| 153 | return $priceField; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Fetch object based on array of properties |
| 158 | * |
| 159 | * @param array $params |
| 160 | * (reference ) an assoc array of name/value pairs. |
| 161 | * @param array $defaults |
| 162 | * (reference ) an assoc array to hold the flattened values. |
| 163 | * |
| 164 | * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field |
| 165 | */ |
| 166 | public static function retrieve(&$params, &$defaults) { |
| 167 | return CRM_Core_DAO::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $params, $defaults); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Update the is_active flag in the db |
| 172 | * |
| 173 | * @param int $id |
| 174 | * Id of the database record. |
| 175 | * @param bool $is_active |
| 176 | * Value we want to set the is_active field. |
| 177 | * |
| 178 | * @return Object |
| 179 | * DAO object on sucess, null otherwise |
| 180 | * |
| 181 | */ |
| 182 | public static function setIsActive($id, $is_active) { |
| 183 | return CRM_Core_DAO::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $id, 'is_active', $is_active); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Get the field title. |
| 188 | * |
| 189 | * @param int $id |
| 190 | * Id of field. |
| 191 | * |
| 192 | * @return string |
| 193 | * name |
| 194 | * |
| 195 | */ |
| 196 | public static function getTitle($id) { |
| 197 | return CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $id, 'label'); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * This function for building custom fields |
| 202 | * |
| 203 | * @param CRM_Core_Form $qf |
| 204 | * Form object (reference). |
| 205 | * @param string $elementName |
| 206 | * Name of the custom field. |
| 207 | * @param int $fieldId |
| 208 | * @param bool $inactiveNeeded |
| 209 | * @param bool $useRequired |
| 210 | * True if required else false. |
| 211 | * @param string $label |
| 212 | * Label for custom field. |
| 213 | * @param null $fieldOptions |
| 214 | * @param array $freezeOptions |
| 215 | * |
| 216 | * @return null |
| 217 | */ |
| 218 | public static function addQuickFormElement( |
| 219 | &$qf, |
| 220 | $elementName, |
| 221 | $fieldId, |
| 222 | $inactiveNeeded, |
| 223 | $useRequired = TRUE, |
| 224 | $label = NULL, |
| 225 | $fieldOptions = NULL, |
| 226 | $freezeOptions = array() |
| 227 | ) { |
| 228 | |
| 229 | $field = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); |
| 230 | $field->id = $fieldId; |
| 231 | if (!$field->find(TRUE)) { |
| 232 | /* FIXME: failure! */ |
| 233 | |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | $otherAmount = $qf->get('values'); |
| 238 | $config = CRM_Core_Config::singleton(); |
| 239 | $qf->assign('currencySymbol', CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Currency', $config->defaultCurrency, 'symbol', 'name')); |
| 240 | // get currency name for price field and option attributes |
| 241 | $currencyName = $config->defaultCurrency; |
| 242 | |
| 243 | if (!isset($label)) { |
| 244 | $label = (property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock['is_separate_payment']) && $field->name == 'contribution_amount' && empty($otherAmount['is_allow_other_amount'])) ? ts('Additional Contribution') : $field->label; |
| 245 | } |
| 246 | |
| 247 | if ($field->name == 'contribution_amount') { |
| 248 | $qf->_contributionAmount = 1; |
| 249 | } |
| 250 | |
| 251 | if (isset($qf->_online) && $qf->_online) { |
| 252 | $useRequired = FALSE; |
| 253 | } |
| 254 | |
| 255 | $customOption = $fieldOptions; |
| 256 | if (!is_array($customOption)) { |
| 257 | $customOption = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::getOptions($field->id, $inactiveNeeded); |
| 258 | } |
| 259 | |
| 260 | //use value field. |
| 261 | $valueFieldName = 'amount'; |
| 262 | $seperator = '|'; |
| 263 | switch ($field->html_type) { |
| 264 | case 'Text': |
| 265 | $optionKey = key($customOption); |
| 266 | $count = CRM_Utils_Array::value('count', $customOption[$optionKey], ''); |
| 267 | $max_value = CRM_Utils_Array::value('max_value', $customOption[$optionKey], ''); |
| 268 | $priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName], $count, $max_value)); |
| 269 | |
| 270 | $extra = array(); |
| 271 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && property_exists($qf, '_contributionAmount') && $qf->_contributionAmount) { |
| 272 | $qf->assign('priceset', $elementName); |
| 273 | $extra = array('onclick' => 'useAmountOther();'); |
| 274 | } |
| 275 | |
| 276 | // if seperate membership payment is used with quick config priceset then change the other amount label |
| 277 | if (property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock['is_separate_payment']) && $qf->_quickConfig && $field->name == 'other_amount' && !property_exists($qf, '_contributionAmount')) { |
| 278 | $label = ts('Additional Contribution'); |
| 279 | $useRequired = 0; |
| 280 | } |
| 281 | elseif (!empty($fieldOptions[$optionKey]['label'])) { |
| 282 | $label = $fieldOptions[$optionKey]['label']; |
| 283 | } |
| 284 | |
| 285 | if ($field->is_display_amounts) { |
| 286 | $label .= ' - '; |
| 287 | $label .= CRM_Utils_Money::format(CRM_Utils_Array::value($valueFieldName, $customOption[$optionKey])); |
| 288 | } |
| 289 | |
| 290 | $element = &$qf->add('text', $elementName, $label, |
| 291 | array_merge($extra, |
| 292 | array( |
| 293 | 'price' => json_encode(array($optionKey, $priceVal)), |
| 294 | 'size' => '4', |
| 295 | ) |
| 296 | ), |
| 297 | $useRequired && $field->is_required |
| 298 | ); |
| 299 | |
| 300 | // CRM-6902 |
| 301 | if (in_array($optionKey, $freezeOptions)) { |
| 302 | $element->freeze(); |
| 303 | } |
| 304 | |
| 305 | //CRM-10117 |
| 306 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig) { |
| 307 | $message = ts("Please enter a valid amount."); |
| 308 | $type = "money"; |
| 309 | } |
| 310 | else { |
| 311 | $message = ts('%1 must be an integer (whole number).', array(1 => $label)); |
| 312 | $type = "positiveInteger"; |
| 313 | } |
| 314 | // integers will have numeric rule applied to them. |
| 315 | $qf->addRule($elementName, $message, $type); |
| 316 | break; |
| 317 | |
| 318 | case 'Radio': |
| 319 | $choice = array(); |
| 320 | |
| 321 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && property_exists($qf, '_contributionAmount') && $qf->_contributionAmount) { |
| 322 | $qf->assign('contriPriceset', $elementName); |
| 323 | } |
| 324 | |
| 325 | foreach ($customOption as $opId => $opt) { |
| 326 | if ($field->is_display_amounts) { |
| 327 | $opt['label'] = !empty($opt['label']) ? $opt['label'] . ' - ' : ''; |
| 328 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); |
| 329 | } |
| 330 | $count = CRM_Utils_Array::value('count', $opt, ''); |
| 331 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
| 332 | $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); |
| 333 | $extra = array( |
| 334 | 'price' => json_encode(array($elementName, $priceVal)), |
| 335 | 'data-amount' => $opt[$valueFieldName], |
| 336 | 'data-currency' => $currencyName, |
| 337 | ); |
| 338 | if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && $field->name == 'contribution_amount') { |
| 339 | $extra += array('onclick' => 'clearAmountOther();'); |
| 340 | } |
| 341 | elseif (property_exists($qf, '_quickConfig') && $qf->_quickConfig && $field->name == 'membership_amount') { |
| 342 | $extra += array( |
| 343 | 'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});", |
| 344 | 'membership-type' => $opt['membership_type_id'], |
| 345 | ); |
| 346 | $qf->assign('membershipFieldID', $field->id); |
| 347 | } |
| 348 | $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra); |
| 349 | |
| 350 | // CRM-6902 |
| 351 | if (in_array($opId, $freezeOptions)) { |
| 352 | $choice[$opId]->freeze(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock['is_separate_payment']) && $field->name == 'contribution_amount') { |
| 357 | $choice[] = $qf->createElement('radio', NULL, '', 'No thank you', '-1', |
| 358 | array( |
| 359 | 'onclick' => 'clearAmountOther();', |
| 360 | ) |
| 361 | ); |
| 362 | } |
| 363 | |
| 364 | if (!$field->is_required) { |
| 365 | // add "none" option |
| 366 | if (!empty($otherAmount['is_allow_other_amount']) && $field->name == 'contribution_amount') { |
| 367 | $none = ts('Other Amount'); |
| 368 | } |
| 369 | elseif (property_exists($qf, '_membershipBlock') && empty($qf->_membershipBlock['is_required']) && $field->name == 'membership_amount') { |
| 370 | $none = ts('No thank you'); |
| 371 | } |
| 372 | else { |
| 373 | $none = ts('-none-'); |
| 374 | } |
| 375 | |
| 376 | $choice[] = $qf->createElement('radio', NULL, '', $none, '0', |
| 377 | array('price' => json_encode(array($elementName, "0"))) |
| 378 | ); |
| 379 | } |
| 380 | |
| 381 | $element = &$qf->addGroup($choice, $elementName, $label); |
| 382 | |
| 383 | // make contribution field required for quick config when membership block is enabled |
| 384 | if (($field->name == 'contribution_amount' || $field->name == 'membership_amount') && property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock) && !$field->is_required) { |
| 385 | $useRequired = $field->is_required = TRUE; |
| 386 | } |
| 387 | |
| 388 | if ($useRequired && $field->is_required) { |
| 389 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); |
| 390 | } |
| 391 | break; |
| 392 | |
| 393 | case 'Select': |
| 394 | $selectOption = $allowedOptions = $priceVal = array(); |
| 395 | |
| 396 | foreach ($customOption as $opt) { |
| 397 | $count = CRM_Utils_Array::value('count', $opt, ''); |
| 398 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
| 399 | $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); |
| 400 | |
| 401 | if ($field->is_display_amounts) { |
| 402 | $opt['label'] .= ' - '; |
| 403 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); |
| 404 | } |
| 405 | $selectOption[$opt['id']] = $opt['label']; |
| 406 | |
| 407 | if (!in_array($opt['id'], $freezeOptions)) { |
| 408 | $allowedOptions[] = $opt['id']; |
| 409 | } |
| 410 | } |
| 411 | $element = &$qf->add('select', $elementName, $label, |
| 412 | array('' => ts('- select -')) + $selectOption, |
| 413 | $useRequired && $field->is_required, |
| 414 | array('price' => json_encode($priceVal)) |
| 415 | ); |
| 416 | |
| 417 | // CRM-6902 |
| 418 | $button = substr($qf->controller->getButtonName(), -4); |
| 419 | if (!empty($freezeOptions) && $button != 'skip') { |
| 420 | $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/"); |
| 421 | } |
| 422 | break; |
| 423 | |
| 424 | case 'CheckBox': |
| 425 | |
| 426 | $check = array(); |
| 427 | foreach ($customOption as $opId => $opt) { |
| 428 | $count = CRM_Utils_Array::value('count', $opt, ''); |
| 429 | $max_value = CRM_Utils_Array::value('max_value', $opt, ''); |
| 430 | $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value)); |
| 431 | |
| 432 | if ($field->is_display_amounts) { |
| 433 | $opt['label'] .= ' - '; |
| 434 | $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]); |
| 435 | } |
| 436 | $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'], |
| 437 | array( |
| 438 | 'price' => json_encode(array($opt['id'], $priceVal)), |
| 439 | 'data-amount' => $opt[$valueFieldName], |
| 440 | 'data-currency' => $currencyName, |
| 441 | ) |
| 442 | ); |
| 443 | |
| 444 | // CRM-6902 |
| 445 | if (in_array($opId, $freezeOptions)) { |
| 446 | $check[$opId]->freeze(); |
| 447 | } |
| 448 | } |
| 449 | $element = &$qf->addGroup($check, $elementName, $label); |
| 450 | if ($useRequired && $field->is_required) { |
| 451 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); |
| 452 | } |
| 453 | break; |
| 454 | } |
| 455 | if (isset($qf->_online) && $qf->_online) { |
| 456 | $element->freeze(); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Retrieve a list of options for the specified field |
| 462 | * |
| 463 | * @param int $fieldId |
| 464 | * Price field ID. |
| 465 | * @param bool $inactiveNeeded |
| 466 | * Include inactive options. |
| 467 | * @param bool $reset |
| 468 | * Ignore stored values\. |
| 469 | * |
| 470 | * @return array |
| 471 | * array of options |
| 472 | */ |
| 473 | public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) { |
| 474 | static $options = array(); |
| 475 | |
| 476 | if ($reset || empty($options[$fieldId])) { |
| 477 | $values = array(); |
| 478 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded); |
| 479 | $options[$fieldId] = $values; |
| 480 | } |
| 481 | |
| 482 | return $options[$fieldId]; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * @param $optionLabel |
| 487 | * @param int $fid |
| 488 | * |
| 489 | * @return mixed |
| 490 | */ |
| 491 | public static function getOptionId($optionLabel, $fid) { |
| 492 | if (!$optionLabel || !$fid) { |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | $optionGroupName = "civicrm_price_field.amount.{$fid}"; |
| 497 | |
| 498 | $query = " |
| 499 | SELECT |
| 500 | option_value.id as id |
| 501 | FROM |
| 502 | civicrm_option_value option_value, |
| 503 | civicrm_option_group option_group |
| 504 | WHERE |
| 505 | option_group.name = %1 |
| 506 | AND option_group.id = option_value.option_group_id |
| 507 | AND option_value.label = %2"; |
| 508 | |
| 509 | $dao = CRM_Core_DAO::executeQuery($query, array( |
| 510 | 1 => array($optionGroupName, 'String'), |
| 511 | 2 => array($optionLabel, 'String'), |
| 512 | )); |
| 513 | |
| 514 | while ($dao->fetch()) { |
| 515 | return $dao->id; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Delete the price set field. |
| 521 | * |
| 522 | * @param int $id |
| 523 | * Field Id. |
| 524 | * |
| 525 | * @return mixed |
| 526 | * Boolean-ish |
| 527 | */ |
| 528 | public static function deleteField($id) { |
| 529 | $field = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); |
| 530 | $field->id = $id; |
| 531 | |
| 532 | if ($field->find(TRUE)) { |
| 533 | // delete the options for this field |
| 534 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::deleteValues($id); |
| 535 | |
| 536 | // reorder the weight before delete |
| 537 | $fieldValues = array('price_set_id' => $field->price_set_id); |
| 538 | |
| 539 | CRM_Utils_Weight::delWeight('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $field->id, $fieldValues); |
| 540 | |
| 541 | // now delete the field |
| 542 | return $field->delete(); |
| 543 | } |
| 544 | |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * @return array |
| 550 | */ |
| 551 | public static function &htmlTypes() { |
| 552 | static $htmlTypes = NULL; |
| 553 | if (!$htmlTypes) { |
| 554 | $htmlTypes = array( |
| 555 | 'Text' => ts('Text / Numeric Quantity'), |
| 556 | 'Select' => ts('Select'), |
| 557 | 'Radio' => ts('Radio'), |
| 558 | 'CheckBox' => ts('CheckBox'), |
| 559 | ); |
| 560 | } |
| 561 | return $htmlTypes; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Validate the priceset |
| 566 | * |
| 567 | * @param int $priceSetId |
| 568 | * @param array $fields |
| 569 | * @param array $error |
| 570 | */ |
| 571 | public static function priceSetValidation($priceSetId, $fields, &$error) { |
| 572 | // check for at least one positive |
| 573 | // amount price field should be selected. |
| 574 | $priceField = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field(); |
| 575 | $priceField->price_set_id = $priceSetId; |
| 576 | $priceField->find(); |
| 577 | |
| 578 | $priceFields = array(); |
| 579 | |
| 580 | while ($priceField->fetch()) { |
| 581 | $key = "price_{$priceField->id}"; |
| 582 | if (!empty($fields[$key])) { |
| 583 | $priceFields[$priceField->id] = $fields[$key]; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if (!empty($priceFields)) { |
| 588 | // we should has to have positive amount. |
| 589 | $sql = " |
| 590 | SELECT id, html_type |
| 591 | FROM civicrm_price_field |
| 592 | WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; |
| 593 | $fieldDAO = CRM_Core_DAO::executeQuery($sql); |
| 594 | $htmlTypes = array(); |
| 595 | while ($fieldDAO->fetch()) { |
| 596 | $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type; |
| 597 | } |
| 598 | |
| 599 | $selectedAmounts = array(); |
| 600 | |
| 601 | foreach ($htmlTypes as $fieldId => $type) { |
| 602 | $options = array(); |
| 603 | CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue::getValues($fieldId, $options); |
| 604 | |
| 605 | if (empty($options)) { |
| 606 | |
| 607 | continue; |
| 608 | |
| 609 | } |
| 610 | |
| 611 | if ($type == 'Text') { |
| 612 | foreach ($options as $opId => $option) { |
| 613 | $selectedAmounts[$opId] = $priceFields[$fieldId] * $option['amount']; |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | elseif (is_array($fields["price_{$fieldId}"])) { |
| 618 | foreach (array_keys($fields["price_{$fieldId}"]) as $opId) { |
| 619 | $selectedAmounts[$opId] = $options[$opId]['amount']; |
| 620 | } |
| 621 | } |
| 622 | elseif (in_array($fields["price_{$fieldId}"], array_keys($options))) { |
| 623 | $selectedAmounts[$fields["price_{$fieldId}"]] = $options[$fields["price_{$fieldId}"]]['amount']; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | list($componentName) = explode(':', $fields['_qf_default']); |
| 628 | // now we have all selected amount in hand. |
| 629 | $totalAmount = array_sum($selectedAmounts); |
| 630 | if ($totalAmount < 0) { |
| 631 | $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName)); |
| 632 | } |
| 633 | } |
| 634 | else { |
| 635 | $error['_qf_default'] = ts("Please select at least one option from price set."); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | } |