/** * create/update group * * This API is used to create new group or update any of the existing * In case of updating existing group, id of that particular grop must * be in $params array. Either id or name is required field in the * $params array * * @param array $params (referance) Associative array of property * name/value pairs to insert in new 'group' * * @return array returns id of the group created if success, * error message otherwise *@example GroupCreate.php *{@getfields group_create} * @access public */ function civicrm_api3_group_create($params) { $group = CRM_Contact_BAO_Group::create($params); if (is_null($group)) { return civicrm_api3_create_error('Group not created'); } else { $values = array(); _civicrm_api3_object_to_array_unique_fields($group, $values[$group->id]); return civicrm_api3_create_success($values, $params, 'group', 'create', $group); } }
/** * Create a 'custom field' within a custom field group. * We also empty the static var in the getfields * function after deletion so that the field is available for us (getfields manages date conversion * among other things * * @param $params array Associative array of property name/value pairs to create new custom field. * * @return Newly API success object * * @access public * * @example CustomFieldCreate.php * {@getfields CustomField_create} * {@example CustomFieldCreate.php 0} * */ function civicrm_api3_custom_field_create($params) { // Array created for passing options in params if (isset($params['option_values']) && is_array($params['option_values'])) { foreach ($params['option_values'] as $key => $value) { $params['option_label'][$key] = $value['label']; $params['option_value'][$key] = $value['value']; $params['option_status'][$key] = $value['is_active']; $params['option_weight'][$key] = $value['weight']; } } $values = array(); $customField = CRM_Core_BAO_CustomField::create($params); _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); _civicrm_api3_custom_field_flush_static_caches(); return civicrm_api3_create_success($values, $params, 'custom_field', $customField); }
/** * File for the CiviCRM APIv3 group functions * * @package CiviCRM_APIv3 * @subpackage API_pcpteams * @copyright CiviCRM LLC (c) 2004-2014 */ function civicrm_api3_pcpteams_create($params) { // since we are allowing html input from the user // we also need to purify it, so lets clean it up // $params['pcp_title'] = $pcp['title']; // $params['pcp_contact_id'] = $pcp['contact_id']; $htmlFields = array('intro_text', 'page_text', 'title'); foreach ($htmlFields as $field) { if (!empty($params[$field])) { $params[$field] = CRM_Utils_String::purifyHTML($params[$field]); } } $entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($params['page_type']); $pcpBlock = new CRM_PCP_DAO_PCPBlock(); $pcpBlock->entity_table = $entity_table; $pcpBlock->entity_id = $params['page_id']; $pcpBlock->find(TRUE); $params['pcp_block_id'] = $pcpBlock->id; $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']); // 1 -> waiting review // 2 -> active / approved (default for now) $params['status_id'] = CRM_Utils_Array::value('status_id', $params, 2); // active by default for now $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 1); $pcp = CRM_Pcpteams_BAO_PCP::create($params, FALSE); //Custom Set $customFields = CRM_Core_BAO_CustomField::getFields('PCP', FALSE, FALSE, NULL, NULL, TRUE); $isCustomValueSet = FALSE; foreach ($customFields as $fieldID => $fieldValue) { list($tableName, $columnName, $cgId) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID); if (!empty($params[$columnName]) || !empty($params["custom_{$fieldID}"])) { $isCustomValueSet = TRUE; //FIXME: to find out the custom value exists, set -1 as default now $params["custom_{$fieldID}_-1"] = !empty($params[$columnName]) ? $params[$columnName] : $params["custom_{$fieldID}"]; } } if ($isCustomValueSet) { $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $pcp->id, 'PCP'); CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_pcp', $pcp->id); } //end custom set $values = array(); @_civicrm_api3_object_to_array_unique_fields($pcp, $values[$pcp->id]); return civicrm_api3_create_success($values, $params, 'Pcpteams', 'create'); }
/** * Create or update a Contact. * * @param array $params * Input parameters. * * @throws API_Exception * * @return array * API Result Array */ function civicrm_api3_contact_create($params) { $contactID = CRM_Utils_Array::value('contact_id', $params, CRM_Utils_Array::value('id', $params)); $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE); $values = _civicrm_api3_contact_check_params($params, $dupeCheck); if ($values) { return $values; } if (!$contactID) { // If we get here, we're ready to create a new contact if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { $defLocType = CRM_Core_BAO_LocationType::getDefault(); $params['email'] = array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => $defLocType->id ? $defLocType->id : 1)); } } if (!empty($params['home_url'])) { $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); $params['website'] = array(1 => array('website_type_id' => key($websiteTypes), 'url' => $params['home_url'])); } _civicrm_api3_greeting_format_params($params); $values = array(); if (empty($params['contact_type']) && $contactID) { $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($contactID); } if (!isset($params['contact_sub_type']) && $contactID) { $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($contactID); } _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $contactID); $params = array_merge($params, $values); //@todo we should just call basic_create here - but need to make contact:create accept 'id' on the bao $contact = _civicrm_api3_contact_update($params, $contactID); if (is_a($contact, 'CRM_Core_Error')) { throw new API_Exception($contact->_errors[0]['message']); } else { $values = array(); _civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]); } return civicrm_api3_create_success($values, $params, 'Contact', 'create'); }
/** * Create a 'custom field' within a custom field group. * * We also empty the static var in the getfields * function after deletion so that the field is available for us (getfields manages date conversion * among other things * * @param array $params * Array per getfields metadata. * * @return array * API success array */ function civicrm_api3_custom_field_create($params) { // Array created for passing options in params. if (isset($params['option_values']) && is_array($params['option_values'])) { $weight = 0; foreach ($params['option_values'] as $key => $value) { // Translate simple key/value pairs into full-blown option values if (!is_array($value)) { $value = array('label' => $value, 'value' => $key, 'is_active' => 1, 'weight' => $weight); $key = $weight++; } $params['option_label'][$key] = $value['label']; $params['option_value'][$key] = $value['value']; $params['option_status'][$key] = $value['is_active']; $params['option_weight'][$key] = $value['weight']; } } $values = array(); $customField = CRM_Core_BAO_CustomField::create($params); _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); _civicrm_api3_custom_field_flush_static_caches(); return civicrm_api3_create_success($values, $params, 'CustomField', $customField); }
/** * Add a payment for a Contribution. * * @param array $params * Input parameters. * * @throws API_Exception * @return array * Api result array */ function civicrm_api3_payment_create(&$params) { // Check if it is an update if (CRM_Utils_Array::value('id', $params)) { $amount = $params['total_amount']; civicrm_api3('Payment', 'cancel', $params); $params['total_amount'] = $amount; } // Get contribution $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $params['contribution_id'])); $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus($contribution['contribution_status_id'], 'name'); if ($contributionStatus != 'Partially paid' && !($contributionStatus == 'Pending' && $contribution['is_pay_later'] == TRUE)) { throw new API_Exception('Please select a contribution which has a partial or pending payment'); } else { // Check if pending contribution $fullyPaidPayLater = FALSE; if ($contributionStatus == 'Pending') { $cmp = bccomp($contribution['total_amount'], $params['total_amount'], 5); // Total payment amount is the whole amount paid against pending contribution if ($cmp == 0 || $cmp == -1) { civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id'])); // Get the trxn $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); $ftParams = array('id' => $trxnId['financialTrxnId']); $trxn = CRM_Core_BAO_FinancialTrxn::retrieve($ftParams, CRM_Core_DAO::$_nullArray); $fullyPaidPayLater = TRUE; } else { civicrm_api3('Contribution', 'create', array('id' => $contribution['id'], 'contribution_status_id' => 'Partially paid')); } } if (!$fullyPaidPayLater) { $trxn = CRM_Core_BAO_FinancialTrxn::getPartialPaymentTrxn($contribution, $params); if (CRM_Utils_Array::value('line_item', $params) && !empty($trxn)) { foreach ($params['line_item'] as $values) { foreach ($values as $id => $amount) { $p = array('id' => $id); $check = CRM_Price_BAO_LineItem::retrieve($p, $defaults); if (empty($check)) { throw new API_Exception('Please specify a valid Line Item.'); } // get financial item $sql = "SELECT fi.id\n FROM civicrm_financial_item fi\n INNER JOIN civicrm_line_item li ON li.id = fi.entity_id and fi.entity_table = 'civicrm_line_item'\n WHERE li.contribution_id = %1 AND li.id = %2"; $sqlParams = array(1 => array($params['contribution_id'], 'Integer'), 2 => array($id, 'Integer')); $fid = CRM_Core_DAO::singleValueQuery($sql, $sqlParams); // Record Entity Financial Trxn $eftParams = array('entity_table' => 'civicrm_financial_item', 'financial_trxn_id' => $trxn->id, 'amount' => $amount, 'entity_id' => $fid); civicrm_api3('EntityFinancialTrxn', 'create', $eftParams); } } } elseif (!empty($trxn)) { // Assign the lineitems proportionally CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn, $contribution); } } } $values = array(); _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]); return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn); }
/** * Create or update a Contact. * * @param array $params * Input parameters. * * @throws API_Exception * * @return array * API Result Array */ function civicrm_api3_contact_create($params) { $contactID = CRM_Utils_Array::value('contact_id', $params, CRM_Utils_Array::value('id', $params)); if ($contactID && !empty($params['check_permissions']) && !CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) { throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify contact record'); } $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE); $values = _civicrm_api3_contact_check_params($params, $dupeCheck); if ($values) { return $values; } if (array_key_exists('api_key', $params) && !empty($params['check_permissions'])) { if (CRM_Core_Permission::check('edit api keys') || CRM_Core_Permission::check('administer CiviCRM')) { // OK } elseif ($contactID && CRM_Core_Permission::check('edit own api keys') && CRM_Core_Session::singleton()->get('userID') == $contactID) { // OK } else { throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify api key'); } } if (!$contactID) { // If we get here, we're ready to create a new contact if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { $defLocType = CRM_Core_BAO_LocationType::getDefault(); $params['email'] = array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => $defLocType->id ? $defLocType->id : 1)); } } if (!empty($params['home_url'])) { $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); $params['website'] = array(1 => array('website_type_id' => key($websiteTypes), 'url' => $params['home_url'])); } _civicrm_api3_greeting_format_params($params); $values = array(); if (empty($params['contact_type']) && $contactID) { $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($contactID); } if (!isset($params['contact_sub_type']) && $contactID) { $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($contactID); } _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $contactID); $params = array_merge($params, $values); //@todo we should just call basic_create here - but need to make contact:create accept 'id' on the bao $contact = _civicrm_api3_contact_update($params, $contactID); if (is_a($contact, 'CRM_Core_Error')) { throw new API_Exception($contact->_errors[0]['message']); } else { $values = array(); _civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]); } return civicrm_api3_create_success($values, $params, 'Contact', 'create'); }
/** * Create a 'custom field' within a custom field group. * We also empty the static var in the getfields * function after deletion so that the field is available for us (getfields manages date conversion * among other things * * @param $params array Associative array of property name/value pairs to create new custom field. * * @return Newly API success object * * @access public * * @example CustomFieldCreate.php * {@getfields CustomField_create} * {@example CustomFieldCreate.php 0} * */ function civicrm_api3_custom_field_create($params) { if (!CRM_Utils_Array::value('option_type', $params)) { if (CRM_Utils_Array::value('id', $params)) { $params['option_type'] = 2; } else { $params['option_type'] = 1; } } // Array created for passing options in params if (isset($params['option_values']) && is_array($params['option_values'])) { foreach ($params['option_values'] as $key => $value) { $params['option_label'][$key] = $value['label']; $params['option_value'][$key] = $value['value']; $params['option_status'][$key] = $value['is_active']; $params['option_weight'][$key] = $value['weight']; } } $customField = CRM_Core_BAO_CustomField::create($params); civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1)); _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); return civicrm_api3_create_success($values, $params, 'custom_field', $customField); }
/** * Create or update a contact (note you should always call this via civicrm_api() & never directly) * * @param array $params input parameters * * Allowed @params array keys are: * {@getfields contact_create} * * * @example ContactCreate.php Example of Create Call * * @return array API Result Array * * @static void * @access public */ function civicrm_api3_contact_create($params) { $contactID = CRM_Utils_Array::value('contact_id', $params, CRM_Utils_Array::value('id', $params)); $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE); $values = _civicrm_api3_contact_check_params($params, $dupeCheck); if ($values) { return $values; } if (empty($contactID)) { // If we get here, we're ready to create a new contact if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { require_once 'CRM/Core/BAO/LocationType.php'; $defLocType = CRM_Core_BAO_LocationType::getDefault(); $params['email'] = array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => $defLocType->id ? $defLocType->id : 1)); } } if (CRM_Utils_Array::value('home_url', $params)) { require_once 'CRM/Core/PseudoConstant.php'; $websiteTypes = CRM_Core_PseudoConstant::websiteType(); $params['website'] = array(1 => array('website_type_id' => key($websiteTypes), 'url' => $params['home_url'])); } if (isset($params['suffix_id']) && !is_numeric($params['suffix_id'])) { $params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::individualSuffix()); } if (isset($params['prefix_id']) && !is_numeric($params['prefix_id'])) { $params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::individualPrefix()); } if (isset($params['gender_id']) && !is_numeric($params['gender_id'])) { $params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender()); } $error = _civicrm_api3_greeting_format_params($params); if (civicrm_error($error)) { return $error; } $values = array(); $entityId = $contactID; if (!CRM_Utils_Array::value('contact_type', $params) && $entityId) { $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($entityId); } if (!isset($params['contact_sub_type']) && $entityId) { require_once 'CRM/Contact/BAO/Contact.php'; $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($entityId); } _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $entityId); $params = array_merge($params, $values); $contact = _civicrm_api3_contact_update($params, $contactID); if (is_a($contact, 'CRM_Core_Error')) { return civicrm_api3_create_error($contact->_errors[0]['message']); } else { $values = array(); _civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]); } return civicrm_api3_create_success($values, $params, 'Contact', 'create'); }
/** * Add a payment for a Contribution. * * @param array $params * Input parameters. * * @throws API_Exception * @return array * Api result array */ function civicrm_api3_payment_create(&$params) { // Check if it is an update if (CRM_Utils_Array::value('id', $params)) { $amount = $params['total_amount']; civicrm_api3('Payment', 'cancel', $params); $params['total_amount'] = $amount; } // Get contribution $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $params['contribution_id'])); if ($contribution['contribution_status'] != 'Partially paid') { throw new API_Exception('Please select a contribution which has a partial payment'); } else { $trxn = CRM_Contribute_BAO_Contribution::recordPartialPayment($contribution, $params); $paid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($params['contribution_id']); $total = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution_id'], 'total_amount'); $cmp = bccomp($total, $paid, 5); if ($cmp == 0 || $cmp == -1) { // If paid amount is greater or equal to total amount civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id'])); } } if (CRM_Utils_Array::value('line_item', $params) && !empty($trxn)) { foreach ($params['line_item'] as $values) { foreach ($values as $id => $amount) { $p = array('id' => $id); $check = CRM_Price_BAO_LineItem::retrieve($p, $defaults); if (empty($check)) { throw new API_Exception('Please specify a valid Line Item.'); } // get financial item $sql = "SELECT fi.id\n FROM civicrm_financial_item fi\n INNER JOIN civicrm_line_item li ON li.id = fi.entity_id\n WHERE li.contribution_id = %1 AND li.id = %2"; $sqlParams = array(1 => array($params['contribution_id'], 'Integer'), 2 => array($id, 'Integer')); $fid = CRM_Core_DAO::singleValueQuery($sql, $sqlParams); // Record Entity Financial Trxn $eftParams = array('entity_table' => 'civicrm_financial_item', 'financial_trxn_id' => $trxn->id, 'amount' => $amount, 'entity_id' => $fid); civicrm_api3('EntityFinancialTrxn', 'create', $eftParams); } } } elseif (!empty($trxn)) { // Assign the lineitems proportionally CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn, $contribution); } $values = array(); _civicrm_api3_object_to_array_unique_fields($trxn, $values[$trxn->id]); return civicrm_api3_create_success($values, $params, 'Payment', 'create', $trxn); }