/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params * Associative array of property name/value. * pairs to insert in new contact. * @param array $values * The reformatted properties that we can use internally. * * @param array|bool $create Is the formatted Values array going to * be used for CRM_vent_BAO_Participant:create() * * @return array|CRM_Error */ function _civicrm_api3_deprecated_participant_formatted_param($params, &$values, $create = FALSE) { $fields = CRM_Event_DAO_Participant::fields(); _civicrm_api3_store_values($fields, $params, $values); require_once 'CRM/Core/OptionGroup.php'; $customFields = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } // Handling Custom Data if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { $values[$key] = $value; $type = $customFields[$customFieldID]['html_type']; if ($type == 'CheckBox' || $type == 'Multi-Select') { $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); $values[$key] = array(); foreach ($mulValues as $v1) { foreach ($customOption as $customValueID => $customLabel) { $customValue = $customLabel['value']; if (strtolower(trim($customLabel['label'])) == strtolower(trim($v1)) || strtolower(trim($customValue)) == strtolower(trim($v1))) { if ($type == 'CheckBox') { $values[$key][$customValue] = 1; } else { $values[$key][] = $customValue; } } } } } elseif ($type == 'Select' || $type == 'Radio') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); foreach ($customOption as $customFldID => $customValue) { $val = CRM_Utils_Array::value('value', $customValue); $label = CRM_Utils_Array::value('label', $customValue); $label = strtolower($label); $value = strtolower(trim($value)); if ($value == $label || $value == strtolower($val)) { $values[$key] = $val; } } } } switch ($key) { case 'participant_contact_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_api3_create_error("contact_id not valid: {$value}"); } $dao = new CRM_Core_DAO(); $qParams = array(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}", $qParams); if (!$svq) { return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } $values['contact_id'] = $values['participant_contact_id']; unset($values['participant_contact_id']); break; case 'participant_register_date': if (!CRM_Utils_Rule::dateTime($value)) { return civicrm_api3_create_error("{$key} not a valid date: {$value}"); } break; case 'event_title': $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); $values['event_id'] = $id; break; case 'event_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_api3_create_error("Event ID is not valid: {$value}"); } $dao = new CRM_Core_DAO(); $qParams = array(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = {$value}", $qParams); if (!$svq) { return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = {$value}."); } break; case 'participant_status_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_api3_create_error("Event Status ID is not valid: {$value}"); } break; case 'participant_status': $status = CRM_Event_PseudoConstant::participantStatus(); $values['participant_status_id'] = CRM_Utils_Array::key($value, $status); break; case 'participant_role_id': case 'participant_role': $role = CRM_Event_PseudoConstant::participantRole(); $participantRoles = explode(",", $value); foreach ($participantRoles as $k => $v) { $v = trim($v); if ($key == 'participant_role') { $participantRoles[$k] = CRM_Utils_Array::key($v, $role); } else { $participantRoles[$k] = $v; } } require_once 'CRM/Core/DAO.php'; $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); unset($values[$key]); break; default: break; } } if (array_key_exists('participant_note', $params)) { $values['participant_note'] = $params['participant_note']; } if ($create) { // CRM_Event_BAO_Participant::create() handles register_date, // status_id and source. So, if $values contains // participant_register_date, participant_status_id or participant_source, // convert it to register_date, status_id or source $changes = array('participant_register_date' => 'register_date', 'participant_source' => 'source', 'participant_status_id' => 'status_id', 'participant_role_id' => 'role_id', 'participant_fee_level' => 'fee_level', 'participant_fee_amount' => 'fee_amount', 'participant_id' => 'id'); foreach ($changes as $orgVal => $changeVal) { if (isset($values[$orgVal])) { $values[$changeVal] = $values[$orgVal]; unset($values[$orgVal]); } } } return NULL; }
/** * Process the participant. * * @param CRM_Core_Form $form * @param int $contactID * * @return \CRM_Event_BAO_Participant */ public static function addParticipant(&$form, $contactID) { if (empty($form->_params)) { return NULL; } $params = $form->_params; $transaction = new CRM_Core_Transaction(); $groupName = 'participant_role'; $query = "\nSELECT v.label as label ,v.value as value\nFROM civicrm_option_value v,\n civicrm_option_group g\nWHERE v.option_group_id = g.id\n AND g.name = %1\n AND v.is_active = 1\n AND g.is_active = 1\n"; $p = array(1 => array($groupName, 'String')); $dao = CRM_Core_DAO::executeQuery($query, $p); if ($dao->fetch()) { $roleID = $dao->value; } // handle register date CRM-4320 $registerDate = NULL; if (!empty($form->_allowConfirmation) && $form->_participantId) { $registerDate = $params['participant_register_date']; } elseif (!empty($params['participant_register_date']) && is_array($params['participant_register_date']) && !empty($params['participant_register_date'])) { $registerDate = CRM_Utils_Date::format($params['participant_register_date']); } $participantFields = CRM_Event_DAO_Participant::fields(); $participantParams = array('id' => CRM_Utils_Array::value('participant_id', $params), 'contact_id' => $contactID, 'event_id' => $form->_eventId ? $form->_eventId : $params['event_id'], 'status_id' => CRM_Utils_Array::value('participant_status', $params, 1), 'role_id' => CRM_Utils_Array::value('participant_role_id', $params, $roleID), 'register_date' => $registerDate ? $registerDate : date('YmdHis'), 'source' => CRM_Utils_String::ellipsify(isset($params['participant_source']) ? CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params), $participantFields['participant_source']['maxlength']), 'fee_level' => CRM_Utils_Array::value('amount_level', $params), 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), 'fee_amount' => CRM_Utils_Array::value('fee_amount', $params), 'registered_by_id' => CRM_Utils_Array::value('registered_by_id', $params), 'discount_id' => CRM_Utils_Array::value('discount_id', $params), 'fee_currency' => CRM_Utils_Array::value('currencyID', $params), 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params)); if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') { $participantParams['is_test'] = 1; } else { $participantParams['is_test'] = 0; } if (!empty($form->_params['note'])) { $participantParams['note'] = $form->_params['note']; } elseif (!empty($form->_params['participant_note'])) { $participantParams['note'] = $form->_params['participant_note']; } // reuse id if one already exists for this one (can happen // with back button being hit etc) if (!$participantParams['id'] && !empty($params['contributionID'])) { $pID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $params['contributionID'], 'participant_id', 'contribution_id'); $participantParams['id'] = $pID; } $participantParams['discount_id'] = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event'); if (!$participantParams['discount_id']) { $participantParams['discount_id'] = "null"; } $participant = CRM_Event_BAO_Participant::create($participantParams); $transaction->commit(); return $participant; }
/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params Associative array of property name/value * pairs to insert in new contact. * @param array $values The reformatted properties that we can use internally * * @param array $create Is the formatted Values array going to * be used for CRM_Event_BAO_Participant:create() * * @return array|CRM_Error * @access public */ function _civicrm_participant_formatted_param(&$params, &$values, $create = FALSE) { $fields = CRM_Event_DAO_Participant::fields(); _civicrm_store_values($fields, $params, $values); require_once 'CRM/Core/OptionGroup.php'; $customFields = CRM_Core_BAO_CustomField::getFields('Participant'); foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } //Handling Custom Data _civicrm_generic_handle_custom_data($key, $value, $values, $customFields); switch ($key) { case 'participant_contact_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_error("contact_id not valid: {$value}"); } $dao = new CRM_Core_DAO(); $qParams = array(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}", $qParams); if (!$svq) { return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } $values['contact_id'] = $values['participant_contact_id']; unset($values['participant_contact_id']); break; case 'participant_register_date': if (!CRM_Utils_Rule::date($value)) { return civicrm_create_error("{$key} not a valid date: {$value}"); } break; case 'event_title': $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); $values['event_id'] = $id; break; case 'event_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_error("Event ID is not valid: {$value}"); } $dao = new CRM_Core_DAO(); $qParams = array(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = {$value}", $qParams); if (!$svq) { return civicrm_create_error("Invalid Event ID: There is no event record with event_id = {$value}."); } break; case 'participant_status': $values['status_id'] = $values['participant_status_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantStatusType', $value, 'id', 'label'); break; case 'participant_status_id': if ((int) $value) { $values['status_id'] = $values[$key] = $value; } else { $id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantStatusType', $value, 'id', 'label'); $values['status_id'] = $values[$key] = $id; } break; case 'participant_role_id': case 'participant_role': $role = CRM_Event_PseudoConstant::participantRole(); $participantRoles = explode(",", $value); foreach ($participantRoles as $k => $v) { $v = trim($v); if ($key == 'participant_role') { $participantRoles[$k] = CRM_Utils_Array::key($v, $role); } else { $participantRoles[$k] = $v; } } require_once 'CRM/Core/DAO.php'; $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); unset($values[$key]); break; default: break; } } if (array_key_exists('participant_note', $params)) { $values['participant_note'] = $params['participant_note']; } if ($create) { // CRM_Event_BAO_Participant::create() handles register_date, // status_id and source. So, if $values contains // participant_register_date, participant_status_id or participant_source, // convert it to register_date, status_id or source $changes = array('participant_register_date' => 'register_date', 'participant_source' => 'source', 'participant_status_id' => 'status_id', 'participant_role_id' => 'role_id', 'participant_fee_level' => 'fee_level', 'participant_fee_amount' => 'fee_amount', 'participant_id' => 'id'); foreach ($changes as $orgVal => $changeVal) { if (isset($values[$orgVal])) { $values[$changeVal] = $values[$orgVal]; unset($values[$orgVal]); } } } return NULL; }