/** * Add an Website for a contact * * Allowed @params array keys are: * {@getfields website_create} * @example WebsiteCreate.php * {@example WebsiteCreate.php} * * @return array of newly created website property values. * @access public * @todo convert to using basic create - BAO function non-std */ function civicrm_api3_website_create($params) { $websiteBAO = CRM_Core_BAO_Website::add($params); $values = array(); _civicrm_api3_object_to_array($websiteBAO, $values[$websiteBAO->id]); return civicrm_api3_create_success($values, $params, 'website', 'get'); }
/** * Use this API to create a new group. See the CRM Data Model for uf_group property definitions * * @param $params array Associative array of property name/value pairs to insert in group. * * @return Newly create $ufGroupArray array * {@getfields UFGroup_create} * @example UFGroupCreate.php * @access public */ function civicrm_api3_uf_group_create($params) { $ids = array(); $ids['ufgroup'] = $params['id']; $ufGroup = CRM_Core_BAO_UFGroup::add($params, $ids); _civicrm_api3_object_to_array($ufGroup, $ufGroupArray[$ufGroup->id]); return civicrm_api3_create_success($ufGroupArray, $params); }
/** * Add an Website for a contact. * * @param array $params * * @return array * API result array. * @todo convert to using basic create - BAO function non-std */ function civicrm_api3_website_create($params) { //DO NOT USE THIS FUNCTION AS THE BASIS FOR A NEW API http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards $websiteBAO = CRM_Core_BAO_Website::add($params); $values = array(); _civicrm_api3_object_to_array($websiteBAO, $values[$websiteBAO->id]); return civicrm_api3_create_success($values, $params, 'Website', 'get'); }
/** * @example GroupOrganizationCreate.php * {@getfields GroupOrganization_create} * * @param $params array * * @return array * */ function civicrm_api3_group_organization_create($params) { $groupOrgBAO = CRM_Contact_BAO_GroupOrganization::add($params); if (is_null($groupOrgBAO)) { return civicrm_api3_create_error("group organization not created"); } _civicrm_api3_object_to_array($groupOrgBAO, $values); return civicrm_api3_create_success($values, $params, 'group_organization', 'get', $groupOrgBAO); }
/** * Create payment_processor type. * * @param array $params * Associative array of property name/value pairs to insert in new payment_processor type. * * @return array */ function civicrm_api3_payment_processor_type_create($params) { if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) { return civicrm_api3_create_error('Invalid value for payment_processor type ID'); } $paymentProcessorType = CRM_Financial_BAO_PaymentProcessorType::create($params); $relType = array(); _civicrm_api3_object_to_array($paymentProcessorType, $relType[$paymentProcessorType->id]); return civicrm_api3_create_success($relType, $params, 'PaymentProcessorType', 'create', $paymentProcessorType); }
/** * Create or update a price_field_value * * @param array $params Associative array of property * name/value pairs to insert in new 'price_field_value' * @example PriceFieldValueCreate.php Std Create example * * @return array api result array * {@getfields price_field_value_create} * @access public */ function civicrm_api3_price_field_value_create($params) { $ids = array(); if (!empty($params['id'])) { $ids['id'] = $params['id']; } $bao = CRM_Price_BAO_PriceFieldValue::create($params, $ids); $values = array(); _civicrm_api3_object_to_array($bao, $values[$bao->id]); return civicrm_api3_create_success($values, $params, 'price_field_value', 'create', $bao); }
/** * Create a Event Participant Payment * * This API is used for creating a Participant Payment of Event. * Required parameters : participant_id, contribution_id. * * @param array $params an associative array of name/value property values of civicrm_participant_payment * @example ParticipantPaymentCreate.php * {@example ParticipantPaymentCreate.php 0} * * @return array of newly created payment property values. * {@getfields ParticipantPayment_create} * @access public */ function civicrm_api3_participant_payment_create($params) { $ids = array(); if (!empty($params['id'])) { $ids['id'] = $params['id']; } $participantPayment = CRM_Event_BAO_ParticipantPayment::create($params, $ids); $payment = array(); _civicrm_api3_object_to_array($participantPayment, $payment[$participantPayment->id]); return civicrm_api3_create_success($payment, $params); }
/** * create/update survey * * This API is used to create new survey or update any of the existing * In case of updating existing survey, id of that particular survey must * be in $params array. * * @param array $params (referance) Associative array of property * name/value pairs to insert in new 'survey' * * @return array survey array * * @access public */ function civicrm_api3_option_group_create($params) { $ids = array(); $bao = CRM_Core_BAO_OptionGroup::add($params, $ids); if (is_null($bao)) { return civicrm_api3_create_error('Entity not created'); } else { $values = array(); _civicrm_api3_object_to_array($bao, $values[$bao->id]); return civicrm_api3_create_success($values, $params, 'option_group', 'create', $bao); } }
/** * Create activity type. * * @param array $params * * @return array * created / updated activity type * * @deprecated use the OptionValue api instead */ function civicrm_api3_activity_type_create($params) { $action = 1; $groupParams = array('name' => 'activity_type'); if ($optionValueID = CRM_Utils_Array::value('option_value_id', $params)) { $action = 2; } $activityObject = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $action, $optionValueID); $activityType = array(); _civicrm_api3_object_to_array($activityObject, $activityType[$activityObject->id]); return civicrm_api3_create_success($activityType, $params, 'activity_type', 'create'); }
/** * Creates or updates an Dashlet. * * @param array $params Associative array of property name/value * pairs for the Dashlet. * * @return array Array containing 'is_error' to denote success or failure and details of the created activity * */ function civicrm_api3_dashboard_create($params) { if (empty($params['id'])) { civicrm_api3_verify_one_mandatory($params, NULL, array('name', 'label', 'url', 'fullscreen_url')); } // create dashboard element $dashboardBAO = CRM_Core_BAO_Dashboard::addDashlet($params); if (isset($dashboardBAO->id)) { _civicrm_api3_object_to_array($dashboardBAO, $dashboardArray[$dashboardBAO->id]); return civicrm_api3_create_success($dashboardArray, $params, 'dashboard', 'create', $dashboardBAO); } }
/** * API to Create or update a Membership Type * * @param array $params an associative array of name/value property values of civicrm_membership_type * * @return array $result newly created or updated membership type property values. * @access public * {getfields MembershipType_get} */ function civicrm_api3_membership_type_create($params) { $ids['membershipType'] = CRM_Utils_Array::value('id', $params); $ids['memberOfContact'] = CRM_Utils_Array::value('member_of_contact_id', $params); $ids['contributionType'] = CRM_Utils_Array::value('financial_type_id', $params); $membershipTypeBAO = CRM_Member_BAO_MembershipType::add($params, $ids); $membershipType = array(); _civicrm_api3_object_to_array($membershipTypeBAO, $membershipType[$membershipTypeBAO->id]); CRM_Member_PseudoConstant::membershipType(NULL, TRUE); civicrm_api3('membership', 'getfields', array('cache_clear' => 1, 'fieldname' => 'membership_type_id')); civicrm_api3('profile', 'getfields', array('action' => 'submit', 'cache_clear' => 1)); return civicrm_api3_create_success($membershipType, $params, 'membership_type', 'create', $membershipTypeBAO); }
/** * Create a Event * * This API is used for creating a Event * * @param array $params input parameters * Allowed @params array keys are: * {@getfields event_create} * * @return array API result Array. * @access public */ function civicrm_api3_event_create($params) { _civicrm_api3_event_create_legacy_support_42($params); //format custom fields so they can be added $value = array(); _civicrm_api3_custom_format_params($params, $values, 'Event'); $params = array_merge($values, $params); require_once 'CRM/Event/BAO/Event.php'; $eventBAO = CRM_Event_BAO_Event::create($params); $event = array(); _civicrm_api3_object_to_array($eventBAO, $event[$eventBAO->id]); return civicrm_api3_create_success($event, $params); }
/** * Function to create payment_processor type * * @param array $params Associative array of property name/value pairs to insert in new payment_processor type. * * @return Newly created PaymentProcessor_type object * {@getfields PaymentProcessorType_create} * @access public * {@schema Core/PaymentProcessorType.xml} */ function civicrm_api3_payment_processor_type_create($params) { require_once 'CRM/Utils/Rule.php'; $ids = array(); if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) { return civicrm_api3_create_error('Invalid value for payment_processor type ID'); } $payProcType = new CRM_Core_BAO_PaymentProcessorType(); $payProcType = CRM_Core_BAO_PaymentProcessorType::create($params); $relType = array(); _civicrm_api3_object_to_array($payProcType, $relType[$payProcType->id]); return civicrm_api3_create_success($relType, $params, 'payment_processor_type', 'create', $payProcType); }
/** * Add a Tag. Tags are used to classify CRM entities (including Contacts, Groups and Actions). * * Allowed @params array keys are: * * {@example TagCreate.php} * * @return array of newly created tag property values. * {@getfields tag_create} * @access public */ function civicrm_api3_tag_create($params) { $ids = array('tag' => CRM_Utils_Array::value('tag', $params)); if (CRM_Utils_Array::value('tag', $params)) { $ids['tag'] = $params['tag']; } if (CRM_Utils_Array::value('id', $params)) { $ids['tag'] = $params['id']; } $tagBAO = CRM_Core_BAO_Tag::add($params, $ids); $values = array(); _civicrm_api3_object_to_array($tagBAO, $values[$tagBAO->id]); return civicrm_api3_create_success($values, $params, 'tag', 'create', $tagBAO); }
/** * API to Create or update a Membership Type * * @param array $params an associative array of name/value property values of civicrm_membership_type * * @return array $result newly created or updated membership type property values. * @access public * {getfields MembershipType_get} */ function civicrm_api3_membership_type_create($params) { $values = $params; civicrm_api3_verify_mandatory($values, 'CRM_Member_DAO_MembershipType'); $ids['membershipType'] = CRM_Utils_Array::value('id', $values); $ids['memberOfContact'] = CRM_Utils_Array::value('member_of_contact_id', $values); $ids['contributionType'] = CRM_Utils_Array::value('contribution_type_id', $values); require_once 'CRM/Member/BAO/MembershipType.php'; $membershipTypeBAO = CRM_Member_BAO_MembershipType::add($values, $ids); $membershipType = array(); _civicrm_api3_object_to_array($membershipTypeBAO, $membershipType[$membershipTypeBAO->id]); CRM_Member_PseudoConstant::membershipType(NULL, TRUE); return civicrm_api3_create_success($membershipType, $params, 'membership_type', 'create', $membershipTypeBAO); }
/** * Create Note * * This API is used for creating a note. * Required parameters : entity_id AND note * * @param array $params an associative array of name/value property values of civicrm_note * {@getfields note_create} * * @return array API result array * @access public * @example NoteCreate.php Create example * * */ function civicrm_api3_note_create($params) { $ids = array(); $ids = array('id' => CRM_Utils_Array::value('id', $params)); $noteBAO = CRM_Core_BAO_Note::add($params, $ids); if (is_a($noteBAO, 'CRM_Core_Error')) { $error = civicrm_api3_create_error("Note could not be created"); return $error; } else { $note = array(); _civicrm_api3_object_to_array($noteBAO, $note[$noteBAO->id]); } $result = civicrm_api3_create_success($note, $params); return civicrm_api3_create_success($note, $params); }
/** * Add or update a link between contribution and membership * * @param array $params (reference ) input parameters * * @return array (reference ) membership_payment_id of created or updated record * {@getfields MembershipPayment_create} * @example MembershipPaymentCreate.php * @access public */ function civicrm_api3_membership_payment_create($params) { require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); $mpDAO = new CRM_Member_DAO_MembershipPayment(); $mpDAO->copyValues($params); $result = $mpDAO->save(); if (is_a($result, 'CRM_Core_Error')) { $transaction->rollback(); return civicrm_api3_create_error($result->_errors[0]['message']); } $transaction->commit(); _civicrm_api3_object_to_array($mpDAO, $mpArray[$mpDAO->id]); return civicrm_api3_create_success($mpArray, $params); }
/** * Add or update a plege payment. Pledge Payment API doesn't actually add a pledge * if the request is to 'create' and 'id' is not passed in * the oldest pledge with no associated contribution is updated * * @todo possibly add ability to add payment if there are less payments than pledge installments * @todo possibily add ability to recalc dates if the schedule is changed * * @param array $params input parameters * {@getfields PledgePayment_create} * @example PledgePaymentCreate.php * * @return array API Result * @static void * @access public */ function civicrm_api3_pledge_payment_create($params) { $paymentParams = $params; if (empty($params['id']) && !CRM_Utils_Array::value('option.create_new', $params)) { $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($params['pledge_id']); if (empty($paymentDetails)) { return civicrm_api3_create_error("There are no unmatched payment on this pledge. Pass in the pledge_payment id to specify one or 'option.create_new' to create one"); } elseif (is_array($paymentDetails)) { $paymentParams = array_merge($params, $paymentDetails); } } $dao = CRM_Pledge_BAO_PledgePayment::add($paymentParams); _civicrm_api3_object_to_array($dao, $result[$dao->id]); //update pledge status CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($params['pledge_id']); return civicrm_api3_create_success($result, $params, 'pledge_payment', 'create', $dao); }
/** * Use this API to create a new group. The 'extends' value accepts an array or a comma separated string. * e.g array( 'Individual','Contact') or 'Individual,Contact' * See the CRM Data Model for custom_group property definitions * $params['class_name'] is a required field, class being extended. * * @param $params array Associative array of property name/value pairs to insert in group. * {@getfields CustomGroup_create} * * @return Newly create custom_group object * @todo $params['extends'] is array format - is that std compatible * @access public */ function civicrm_api3_custom_group_create($params) { if (isset($params['extends']) && is_string($params['extends'])) { $extends = explode(",", $params['extends']); unset($params['extends']); $params['extends'] = $extends; } if (!isset($params['extends'][0]) || !trim($params['extends'][0])) { return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact')."); } if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) { // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']); } $customGroup = CRM_Core_BAO_CustomGroup::create($params); _civicrm_api3_object_to_array($customGroup, $values[$customGroup->id]); return civicrm_api3_create_success($values, $params, 'custom_group', $customGroup); }
/** * Create an Event Participant. * * @param array $params * An associative array of name/value property values of civicrm_participant. * * @return array * API result array */ function civicrm_api3_participant_create($params) { // Check that event id is not an template - should be done @ BAO layer. if (!empty($params['event_id'])) { $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); if (!empty($isTemplate)) { return civicrm_api3_create_error(ts('Event templates are not meant to be registered.')); } } $values = $participant = array(); _civicrm_api3_custom_format_params($params, $values, 'Participant'); $params = array_merge($values, $params); $participantBAO = CRM_Event_BAO_Participant::create($params); if (empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])) { _civicrm_api3_participant_createlineitem($params, $participantBAO); } _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]); return civicrm_api3_create_success($participant, $params, 'Participant', 'create', $participantBAO); }
/** * Create a new Action Schedule * * @param array $params * * @return array * * {@getfields action_schedule_create} */ function civicrm_api3_action_schedule_create($params) { if (empty($params['id'])) { // an update does not require any mandatory parameters civicrm_api3_verify_one_mandatory($params, NULL, array('title', 'mapping_id', 'entity_status', 'entity_value')); } $ids = array(); if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) { return civicrm_api3_create_error('Invalid value for ID'); } if (!array_key_exists('name', $params) && !array_key_exists('id', $params)) { $params['name'] = CRM_Utils_String::munge($params['title']); } $actionSchedule = new CRM_Core_BAO_ActionSchedule(); $actionSchedule = CRM_Core_BAO_ActionSchedule::add($params, $ids); $actSchedule = array(); _civicrm_api3_object_to_array($actionSchedule, $actSchedule[$actionSchedule->id]); return civicrm_api3_create_success($actSchedule, $params, 'action_schedule', 'create', $actionSchedule); }
/** * Create relationship type. * * @param array $params * Array per getfields metadata. * * @return array */ function civicrm_api3_relationship_type_create($params) { if (!isset($params['label_a_b'])) { $params['label_a_b'] = $params['name_a_b']; } if (!isset($params['label_b_a'])) { $params['label_b_a'] = $params['name_b_a']; } $ids = array(); if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) { return civicrm_api3_create_error('Invalid value for relationship type ID'); } else { $ids['relationshipType'] = CRM_Utils_Array::value('id', $params); } $relationType = CRM_Contact_BAO_RelationshipType::add($params, $ids); $relType = array(); _civicrm_api3_object_to_array($relationType, $relType[$relationType->id]); return civicrm_api3_create_success($relType, $params, 'RelationshipType', 'create', $relationType); }
/** * create/update grant * * This API is used to create new grant or update any of the existing * In case of updating existing grant, id of that particular grant must * be in $params array. * * @param array $params Associative array of property * name/value pairs to insert in new 'grant' * * @return array grant array * {@getfields grant_create} * @access public */ function civicrm_api3_grant_create($params) { $values = array(); _civicrm_api3_custom_format_params($params, $values, 'Grant'); $params = array_merge($values, $params); // BAO is non standard to we need to construct $ids array. Ideally we would fix BAO to accept $params without // id for standardisation $ids = array(); if (CRM_Utils_Array::value('id', $params)) { $ids['grant'] = $params['id']; } $bao = CRM_GRANT_BAO_GRANT::create($params, $ids); if (is_null($bao)) { return civicrm_api3_create_error('Grant not created '); } else { $values = array(); _civicrm_api3_object_to_array($bao, $values[$bao->id]); return civicrm_api3_create_success($values, $params, $bao, 'create'); } }
/** * Use this API to create a new group. The 'extends' value accepts an array or a comma separated string. * e.g array( 'Individual','Contact') or 'Individual,Contact' * See the CRM Data Model for custom_group property definitions * $params['class_name'] is a required field, class being extended. * * @param $params array Associative array of property name/value pairs to insert in group. * {@getfields CustomGroup_create} * * @return Newly create custom_group object * @todo $params['extends'] is array format - is that std compatible * @todo review custom field create if 'html' approx line 110 * @access public */ function civicrm_api3_custom_group_create($params) { if (is_string($params['extends'])) { $extends = explode(",", $params['extends']); unset($params['extends']); $params['extends'] = $extends; } if (!isset($params['extends'][0]) || !trim($params['extends'][0])) { return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact')."); } $customGroup = CRM_Core_BAO_CustomGroup::create($params); _civicrm_api3_object_to_array($customGroup, $values[$customGroup->id]); if (CRM_Utils_Array::value('html_type', $params)) { $fparams = array('custom_group_id' => $customGroup->id, 'version' => $params['version'], 'label' => 'api created field'); require_once 'api/v3/CustomField.php'; $fieldValues = civicrm_api3_custom_field_create($fparams); $values[$fieldValues['id']] = array_merge($values[$customGroup->id], $fieldValues['values'][$fieldValues['id']]); } return civicrm_api3_create_success($values, $params, 'custom_group', $customGroup); }
/** * Add a OptionValue. OptionValues are used to classify CRM entities (including Contacts, Groups and Actions). * * Allowed @params array keys are: * * {@example OptionValueCreate.php} * * @return array of newly created option_value property values. * {@getfields OptionValue_create} * @access public */ function civicrm_api3_option_value_create($params) { $weight = 0; if (!array_key_exists('label', $params) && array_key_exists('name', $params)) { // no idea why that's a "mandatory" field $params['label'] = $params['name']; } if (!CRM_Utils_Array::value('value', $params) && array_key_exists('option_group_id', $params)) { require_once 'CRM/Utils/Weight.php'; $fieldValues = array('option_group_id' => $params['option_group_id']); // use the next available value /* CONVERT(value, DECIMAL) is used to convert varchar field 'value' to decimal->integer */ $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues, 'CONVERT(value, DECIMAL)'); $weight = $params['value']; } if (!array_key_exists('weight', $params) && array_key_exists('value', $params)) { // no idea why that's a "mandatory" field $params['weight'] = $params['value']; } elseif (array_key_exists('weight', $params) && $params['weight'] == 'next') { // weight is numeric, so it's safe-ish to treat symbol 'next' as magical value $params['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', array('option_group_id' => $params['option_group_id'])); } if (array_key_exists('component', $params)) { if (empty($params['component'])) { $params['component_id'] = ''; } else { $params['component_id'] = array_search($params['component'], CRM_Core_PseudoConstant::component()); } unset($params['component']); } if (CRM_Utils_Array::value('id', $params)) { $ids = array('optionValue' => $params['id']); } $optionValueBAO = CRM_Core_BAO_OptionValue::add($params, $ids); civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1)); $values = array(); _civicrm_api3_object_to_array($optionValueBAO, $values[$optionValueBAO->id]); return civicrm_api3_create_success($values, $params); }
/** * Create an Event Participant * * This API is used for creating a participants in an event. * Required parameters : event_id AND contact_id for new creation * : participant as name/value with participantid for edit * * @param array $params an associative array of name/value property values of civicrm_participant * * @return array apiresult * {@getfields participant_create} * @access public */ function civicrm_api3_participant_create($params) { //check that event id is not an template // note that check duplicate check was removed as it wasn't actually being called. //check contact exists removed as belongs @ wrapper layer if (CRM_Utils_Array::value('event_id', $params)) { $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); if (!empty($isTemplate)) { return civicrm_api3_create_error(ts('Event templates are not meant to be registered')); } } $value = array(); _civicrm_api3_custom_format_params($params, $values, 'Participant'); $params = array_merge($values, $params); require_once 'CRM/Event/BAO/Participant.php'; $participantBAO = CRM_Event_BAO_Participant::create($params); if (empty($params['price_set_id']) && empty($params['id']) && CRM_Utils_Array::value('fee_level', $params)) { _civicrm_api3_participant_createlineitem($params, $participantBAO); } _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]); return civicrm_api3_create_success($participant, $params, 'participant', 'create', $participantBAO); }
/** * Create a Event * * This API is used for creating a Event * * @param array $params input parameters * Allowed @params array keys are: * {@getfields event_create} * * @return array API result Array. * @access public */ function civicrm_api3_event_create($params) { civicrm_api3_verify_one_mandatory($params, NULL, array('event_type_id', 'template_id')); // Clone event from template if (!empty($params['template_id']) && empty($params['id'])) { $copy = CRM_Event_BAO_Event::copy($params['template_id']); $params['id'] = $copy->id; unset($params['template_id']); if (empty($params['is_template'])) { $params['is_template'] = 0; } } _civicrm_api3_event_create_legacy_support_42($params); //format custom fields so they can be added $values = array(); _civicrm_api3_custom_format_params($params, $values, 'Event'); $params = array_merge($values, $params); $eventBAO = CRM_Event_BAO_Event::create($params); $event = array(); _civicrm_api3_object_to_array($eventBAO, $event[$eventBAO->id]); return civicrm_api3_create_success($event, $params); }
/** * Create or update a loc_block * * @param array $params Associative array of property * name/value pairs to insert in new 'loc_block' * @example LocBlockCreate.php Std Create example * * @return array api result array * {@getfields loc_block_create} * @access public */ function civicrm_api3_loc_block_create($params) { $entities = array(); // Call the appropriate api to create entities if any are passed in the params // This is basically chaining but in reverse - we create the sub-entities first // This exists because chainging does not work in reverse, or with keys like 'email_2' $items = array('address', 'email', 'phone', 'im'); foreach ($items as $item) { foreach (array('', '_2') as $suf) { $key = $item . $suf; if (!empty($params[$key]) && is_array($params[$key])) { $info = $params[$key]; // If all we get is an id don't bother calling the api if (count($info) == 1 && !empty($info['id'])) { $params[$key . '_id'] = $info['id']; } else { $info['version'] = $params['version']; $info['contact_id'] = CRM_Utils_Array::value('contact_id', $info, 'null'); $result = civicrm_api($item, 'create', $info); if (!empty($result['is_error'])) { return $result; } $entities[$key] = $result['values'][$result['id']]; $params[$key . '_id'] = $result['id']; } } } } $dao = new CRM_Core_DAO_LocBlock(); $dao->copyValues($params); $dao->save(); if (!empty($dao->id)) { $values = array($dao->id => $entities); _civicrm_api3_object_to_array($dao, $values[$dao->id]); return civicrm_api3_create_success($values, $params, 'loc_block', 'create', $dao); } return civicrm_api3_create_error('Unable to create LocBlock. Please check your params.'); }
/** * Defines 'uf field' within a group. * * @param array $params * Array per getfields metadata. * * @throws API_Exception * * @return array * Newly created $ufFieldArray */ function civicrm_api3_uf_field_create($params) { civicrm_api3_verify_one_mandatory($params, NULL, array('field_name', 'uf_group_id')); $groupId = CRM_Utils_Array::value('uf_group_id', $params); if ((int) $groupId < 1) { throw new API_Exception('Params must be a field_name-carrying array and a positive integer.'); } $field_type = CRM_Utils_Array::value('field_type', $params); $field_name = CRM_Utils_Array::value('field_name', $params); $location_type_id = CRM_Utils_Array::value('location_type_id', $params, CRM_Utils_Array::value('website_type_id', $params)); $phone_type = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params)); if (!CRM_Core_BAO_UFField::isValidFieldName($field_name)) { throw new API_Exception('The field_name is not valid'); } $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type); if (!CRM_Utils_Array::value('group_id', $params)) { $params['group_id'] = $groupId; } $ids = $ufFieldArray = array(); $ids['uf_group'] = $groupId; $fieldId = CRM_Utils_Array::value('id', $params); if (!empty($fieldId)) { $UFField = new CRM_Core_BAO_UFField(); $UFField->id = $fieldId; if ($UFField->find(TRUE)) { $ids['uf_group'] = $UFField->uf_group_id; if (!CRM_Utils_Array::value('group_id', $params)) { // this copied here from previous api function - not sure if required $params['group_id'] = $UFField->uf_group_id; } } else { throw new API_Exception("there is no field for this fieldId"); } $ids['uf_field'] = $fieldId; } if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) { throw new API_Exception("The field was not added. It already exists in this profile."); } //@todo why is this even optional? Surely weight should just be 'managed' ?? if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) { $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params); } $ufField = CRM_Core_BAO_UFField::add($params, $ids); $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE); CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType); _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]); civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE)); return civicrm_api3_create_success($ufFieldArray, $params); }
/** * Internal function to retrieve a case. * * @param int $caseId * * @return array (reference) case object * */ function _civicrm_api3_case_read($caseId, $options) { $return = CRM_Utils_Array::value('return', $options, array()); $dao = new CRM_Case_BAO_Case(); $dao->id = $caseId; if ($dao->find(TRUE)) { $case = array(); _civicrm_api3_object_to_array($dao, $case); // Legacy support for client_id - TODO: in apiv4 remove 'client_id' $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId); //handle multi-value case type $sep = CRM_Core_DAO::VALUE_SEPARATOR; $case['case_type_id'] = trim(str_replace($sep, ',', $case['case_type_id']), ','); if (!empty($return['contacts'])) { //get case contacts $contacts = CRM_Case_BAO_Case::getcontactNames($caseId); $relations = CRM_Case_BAO_Case::getRelatedContacts($caseId); $case['contacts'] = array_merge($contacts, $relations); } if (!empty($return['activities'])) { //get case activities $case['activities'] = array(); $query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = {$caseId}"; $dao = CRM_Core_DAO::executeQuery($query); while ($dao->fetch()) { $case['activities'][] = $dao->activity_id; } } return $case; } }