/** * takes an associative array and creates a price field object * * This function is invoked from within the web form layer and also from the api layer * * @param array $params (reference) an assoc array of name/value pairs * * @return object CRM_Price_DAO_Field object * @access public * @static */ static function create(&$params, $ids) { require_once 'CRM/Core/Transaction.php'; require_once 'CRM/Price/BAO/FieldValue.php'; $transaction = new CRM_Core_Transaction(); $priceField =& self::add($params, $ids); if (is_a($priceField, 'CRM_Core_Error')) { $transaction->rollback(); return $priceField; } $options = $optionsIds = array(); require_once 'CRM/Price/Form/Field.php'; $maxIndex = CRM_Price_Form_Field::NUM_OPTION; if ($priceField->html_type == 'Text') { $maxIndex = 1; require_once 'CRM/Price/BAO/FieldValue.php'; $fieldValue = new CRM_Price_DAO_FieldValue(); $fieldValue->price_field_id = $priceField->id; // update previous field values( if any ) if ($fieldValue->find(true)) { $optionsIds['id'] = $fieldValue->id; } } $defaultArray = array(); if ($params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) { $tempArray = array_keys($params['default_checkbox_option']); foreach ($tempArray as $v) { if ($params['option_amount'][$v]) { $defaultArray[$v] = 1; } } } else { if (CRM_Utils_Array::value('default_option', $params) && isset($params['option_amount'][$params['default_option']])) { $defaultArray[$params['default_option']] = 1; } } for ($index = 1; $index <= $maxIndex; $index++) { if (CRM_Utils_Array::value($index, $params['option_label']) && !CRM_Utils_System::isNull($params['option_amount'][$index])) { $options = array('price_field_id' => $priceField->id, 'label' => trim($params['option_label'][$index]), 'name' => CRM_Utils_String::munge($params['option_label'][$index], '_', 64), 'amount' => CRM_Utils_Rule::cleanMoney(trim($params['option_amount'][$index])), 'count' => CRM_Utils_Array::value($index, $params['option_count'], null), 'max_value' => CRM_Utils_Array::value($index, $params['option_max_value'], null), 'description' => CRM_Utils_Array::value($index, $params['option_description'], null), 'weight' => $params['option_weight'][$index], 'is_active' => 1, 'is_default' => CRM_Utils_Array::value($index, $defaultArray)); CRM_Price_BAO_FieldValue::add($options, $optionsIds); } } $transaction->commit(); return $priceField; }
/** * Find a price_set_id associatied with the given option value or field ID * * @param array $params (reference) an assoc array of name/value pairs * array may contain either option id or * price field id * * @return price set id on success, null otherwise * @static * @access public */ public static function getSetId(&$params) { $fid = NULL; if ($oid = CRM_Utils_Array::value('oid', $params)) { $fieldValue = new CRM_Price_DAO_FieldValue(); $fieldValue->id = $oid; if ($fieldValue->find(TRUE)) { $fid = $fieldValue->price_field_id; } } else { $fid = CRM_Utils_Array::value('fid', $params); } if (isset($fid)) { return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $fid, 'price_set_id'); } return NULL; }
/** * Find a price_set_id associatied with the given option value or field ID * @param array $params (reference) an assoc array of name/value pairs * array may contain either option id or * price field id * * @return price set id on success, null otherwise * @static * @access public */ public static function getSetId(&$params) { $fid = null; require_once 'CRM/Utils/Array.php'; if ($oid = CRM_Utils_Array::value('oid', $params)) { require_once 'CRM/Price/DAO/FieldValue.php'; $fieldValue = new CRM_Price_DAO_FieldValue(); $fieldValue->id = $oid; if ($fieldValue->find(true)) { $fid = $fieldValue->price_field_id; } } else { $fid = CRM_Utils_Array::value('fid', $params); } if (isset($fid)) { return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $fid, 'price_set_id'); } return null; }
/** * takes an associative array and creates a price field object * * This function is invoked from within the web form layer and also from the api layer * * @param array $params (reference) an assoc array of name/value pairs * * @return object CRM_Price_DAO_Field object * @access public * @static */ static function create(&$params) { $transaction = new CRM_Core_Transaction(); $priceField = self::add($params); if (is_a($priceField, 'CRM_Core_Error')) { $transaction->rollback(); return $priceField; } $options = $optionsIds = array(); $maxIndex = CRM_Price_Form_Field::NUM_OPTION; if ($priceField->html_type == 'Text') { $maxIndex = 1; $fieldValue = new CRM_Price_DAO_FieldValue(); $fieldValue->price_field_id = $priceField->id; // update previous field values( if any ) if ($fieldValue->find(TRUE)) { $optionsIds['id'] = $fieldValue->id; } } $defaultArray = array(); if ($params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) { $tempArray = array_keys($params['default_checkbox_option']); foreach ($tempArray as $v) { if ($params['option_amount'][$v]) { $defaultArray[$v] = 1; } } } else { if (CRM_Utils_Array::value('default_option', $params)) { $defaultArray[$params['default_option']] = 1; } } for ($index = 1; $index <= $maxIndex; $index++) { if (array_key_exists('option_amount', $params) && array_key_exists($index, $params['option_amount']) && (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_label', $params)) || CRM_Utils_Array::value('is_quick_config', $params)) && !CRM_Utils_System::isNull($params['option_amount'][$index])) { $options = array('price_field_id' => $priceField->id, 'label' => trim($params['option_label'][$index]), 'name' => CRM_Utils_String::munge($params['option_label'][$index], '_', 64), 'amount' => CRM_Utils_Rule::cleanMoney(trim($params['option_amount'][$index])), 'count' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_count', $params), NULL), 'max_value' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_max_value', $params), NULL), 'description' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_description', $params), NULL), 'membership_type_id' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_type_id', $params), NULL), 'weight' => $params['option_weight'][$index], 'is_active' => 1, 'is_default' => CRM_Utils_Array::value($params['option_weight'][$index], $defaultArray) ? $defaultArray[$params['option_weight'][$index]] : 0); if ($opIds = CRM_Utils_Array::value('option_id', $params)) { if ($opId = CRM_Utils_Array::value($index, $opIds)) { $optionsIds['id'] = $opId; } else { $optionsIds['id'] = NULL; } } CRM_Price_BAO_FieldValue::create($options, $optionsIds); } } $transaction->commit(); return $priceField; }
/** * Retrive the all values for given field id * * @param int $fieldId price_field_id * @param array $values (reference ) to hold the values * @param string $orderBy for order by, default weight * @param int $isActive is_active, default false * * @return array $values * * @access public * @static */ static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) { $fieldValueDAO = new CRM_Price_DAO_FieldValue(); $fieldValueDAO->price_field_id = $fieldId; $fieldValueDAO->orderBy($orderBy, 'label'); if ($isActive) { $fieldValueDAO->is_active = 1; } $fieldValueDAO->find(); while ($fieldValueDAO->fetch()) { CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]); } return $values; }