/** * This function ensures that we have the right input parameters * * We also need to make sure we run all the form rules on the params list * to ensure that the params are valid * * @param array $params Associative array of property name/value * pairs to insert in new history. * * @return bool|CRM_Utils_Error * @access public */ function _crm_check_required_fields(&$params, $daoName) { $extends = CRM_Utils_Array::value('extends', $params); if (($extends == 'Activity' || $extends == 'Phonecall' || $extends == 'Meeting' || $extends == 'Group' || $extends == 'Contribution') && $params['style'] == 'Tab') { return _crm_error(ts("Can not create Custom Group in Tab for " . $extends)); } require_once str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php"; $dao =& new $daoName(); $fields = $dao->fields(); //eval('$dao =& new CRM_Core_DAO_' . $type . 'History();'); $missing = array(); foreach ($fields as $k => $v) { if ($k == 'id' || $k == 'domain_id') { continue; } if (CRM_Utils_Array::value('required', $v) && !isset($params[$k])) { $missing[] = $k; } } if (!empty($missing)) { return _crm_error(ts("Required fields " . implode(',', $missing) . " for {$daoName} are not found")); } return true; }
/** * Defines 'custom value' within a field for a specific entity table/id combination. * * @param $entity_table String Name of the table that this value is attached to * * @param $entity_id int ID of the object in the relevant table * * @param $custom_field object field type of the value * * @param $data Array data appropriate value for the above custom field * * @param $separator String separator for values for ckeckbox. * * @return newly created custom_value object * * @access public * * */ function crm_create_custom_value($entity_table, $entity_id, &$custom_field, &$data, $separator = null) { _crm_initialize(); if (!isset($entity_table)) { return _crm_error("parameter entity_table is not set "); } if (!isset($entity_id)) { return _crm_error("parameter entity_id is not set "); } if (!isset($custom_field->id) && !isset($custom_field->type)) { return _crm_error("field id ot type is not set in custom_field object"); } if ($separator) { $values = explode($separator, $data['value']); require_once 'CRM/Core/BAO/CustomOption.php'; $data['value'] = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $values); } $data['type'] = $custom_field->data_type; $data['custom_field_id'] = $custom_field->id; $data['entity_table'] = $entity_table; $data['entity_id'] = $entity_id; require_once 'CRM/Core/BAO/CustomValue.php'; return CRM_Core_BAO_CustomValue::create($data); }
/** * confirm membership to a group * * @param CRM_Contact $group A valid group object (passed by reference). * @param array $contacts An array of one or more valid Contact objects (passed by reference). * * * @return null if success or CRM_Error (db error or contact was not valid) * * @access public */ function crm_confirm_group_contacts(&$group, $contacts) { _crm_initialize(); if (!is_a($group, 'CRM_Contact_BAO_Group') && !is_a($group, 'CRM_Contact_DAO_Group')) { return _crm_error('Invalid group object passed in'); } if (!is_array($contacts)) { return _crm_error('$contacts is not Array '); } foreach ($contacts as $contact) { if (!isset($contact->id)) { return _crm_error('Invalid contact object passed in'); } $member = CRM_Contact_BAO_GroupContact::getMembershipDetail($contact->id, $group->id); if ($member->status != 'Pending') { return _crm_error('Can not confirm subscription. Current group status is NOT Pending.'); } CRM_Contact_BAO_GroupContact::updateGroupMembershipStatus($contact->id, $group->id); } return null; }
/** * Delete uf field * * @param $ufField Object Valid uf_field object that to be deleted * * @return true on successful delete or return error * * @access public * */ function crm_delete_uf_field($ufField) { _crm_initialize(); $fieldId = $ufField->id; if (!isset($fieldId)) { return _crm_error("parameter {$fieldId} is not set "); } require_once 'CRM/Core/BAO/UFField.php'; return CRM_Core_BAO_UFField::del($fieldId); }
/** * Derives the Membership Status of a given Membership Reocrd * * This API is used for deriving Membership Status of a given Membership * record using the rules encoded in the membership_status table. * * @param Int $membershipID Id of a membership * @param String $statusDate * * @return Array Array of status id and status name * @public */ function crm_calc_membership_status($membershipID) { if (empty($membershipID)) { return _crm_error('Invalid value for membershipID'); } $query = "\nSELECT start_date, end_date, join_date\n FROM civicrm_membership\n WHERE id = %1\n"; $params = array(1 => array($membershipID, 'Integer')); $dao =& CRM_Core_DAO::executeQuery($query, $params); if ($dao->fetch()) { require_once 'CRM/Member/BAO/MembershipStatus.php'; $result =& CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dao->start_date, $dao->end_date, $dao->join_date); } else { $result = null; } $dao->free(); return $result; }
/** * Given an assoc list of params, find if there is a record * for this set of params and return the group id * * @param array $params (reference) an assoc array of name/value pairs * * @return int or null * @access public * */ function crm_find_uf_join_UFGroupId(&$params) { if (!is_array($params) || empty($params)) { return _crm_error("{$params} is not valid array"); } if (!isset($params['entity_table']) && !isset($params['entity_id']) && !isset($params['weight'])) { return _crm_error("{$param} should have atleast entity_table or entiy_id or weight"); } return CRM_Core_BAO_UFJoin::findUFGroupId($params); }
/** * Deletes an existing entity file assignment. * Required parameters : 1. id of an entity-file * 2. entity_id and entity_table of an entity-file * * @param array $params an associative array of name/value property values of civicrm_entity_file. * * @return null if successfull, object of CRM_Core_Error otherwise * @access public */ function crm_delete_entity_file(&$params) { require_once 'CRM/Core/DAO/EntityFile.php'; //if ( ! isset($params['id']) && ( !isset($params['entity_id']) || !isset($params['entity_file']) ) ) { if (!isset($params['id']) && (!isset($params['entity_id']) || !isset($params['entity_table']))) { return _crm_error('Required parameters missing'); } $entityFileDAO =& new CRM_Core_DAO_EntityFile(); $properties = array('id', 'entity_id', 'entity_table', 'file_id'); foreach ($properties as $name) { if (array_key_exists($name, $params)) { $entityFileDAO->{$name} = $params[$name]; } } return $entityFileDAO->delete() ? null : _crm_error('Error while deleting'); }
/** * Check if object is valid and has an id * * @param CRM_Core_DAO_ActivityHistory $historyDAO Activity History object to be checked * * @param boolean $checkForId - check if id is set * * @return true|CRM_Core_Error An error if 'contact' is invalid, * permissions are insufficient, etc. * * @access public * */ function _crm_check_activity_history_object(&$historyDAO, $checkForId = false) { _crm_initialize(); // check if valid DAO if (!is_a($historyDAO, 'CRM_Core_DAO_ActivityHistory')) { return _crm_error(ts('Invalid history object passed in')); } if ($checkForId && !isset($historyDAO->id)) { return _crm_error(ts('History object does not contain a primary key - it is needed for update operation')); } return true; }
/** * This function adds the activity history variable in $values to the * parameter list $params. For most cases, $values should have length 1. * * @param array $values The variable(s) to be added * @param array $params The structured parameter list * * @return bool|CRM_Utils_Error * @access public */ function _crm_add_formatted_history_param(&$values, &$params) { /* Cache the various object fields */ static $fields = null; if ($fields == null) { $fields = array(); } //print_r($values); //print_r($params); if (isset($values['activity_type'])) { $params['activity_type'] = $values['activity_type']; return true; } if (isset($values['activity_date'])) { $params['activity_date'] = $values['activity_date']; return true; } if (isset($values['activity_id'])) { $params['activity_id'] = $values['activity_id']; return true; } /* Check for custom field values */ if ($fields['custom'] == null) { $fields['custom'] =& CRM_Core_BAO_CustomField::getFields('Contribution'); } foreach ($values as $key => $value) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { /* check if it's a valid custom field id */ if (!array_key_exists($customFieldID, $fields['custom'])) { return _crm_error('Invalid custom field ID'); } if (!isset($params['custom'])) { $params['custom'] = array(); } // fixed for Import $newMulValues = array(); if ($fields['custom'][$customFieldID][3] == 'CheckBox' || $fields['custom'][$customFieldID][3] == 'Multi-Select') { $value = str_replace("|", ",", $value); $mulValues = explode(',', $value); $custuomOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($mulValues as $v1) { foreach ($custuomOption as $v2) { if (strtolower($v2['label']) == strtolower(trim($v1))) { $newMulValues[] = $v2['value']; } } } $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $newMulValues); } else { if ($fields['custom'][$customFieldID][3] == 'Select' || $fields['custom'][$customFieldID][3] == 'Radio') { $custuomOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($custuomOption as $v2) { if (strtolower($v2['label']) == strtolower(trim($value))) { $value = $v2['value']; break; } } } } $customBlock = count($params['custom']) + 1; $params['custom'][$customBlock] = array('custom_field_id' => $customFieldID, 'value' => $value, 'type' => $fields['custom'][$customFieldID][2], 'name' => $fields['custom'][$customFieldID][0]); } } /* Finally, check for contribution fields */ if (!isset($fields['History'])) { $fields['History'] =& CRM_Core_DAO_ActivityHistory::fields(); } _crm_store_values($fields['History'], $values, $params); }
/** * Deletes an existing entity tag assignment. * * @param $entity_tag object Valid entity_tag object. * @access public */ function crm_delete_entity_tag(&$entity_tag) { require_once 'CRM/Core/BAO/EntityTag.php'; if (!isset($entity_tag->id)) { return _crm_error('Required parameters missing'); } $params = array('id' => $entity_tag->id); return CRM_Core_BAO_EntityTag::del($params); }
/** * Function to update relationship * * @param object $relationship A valid Relationship object. * @param array $params Associative array of property name/value pairs to be updated. See Data Model for available properties. * * @return updated relationship object * * @access public * */ function crm_update_relationship(&$relationship, $params) { $ids = array(); if (!isset($relationship->id) && !isset($relationship->contact_id_a) && !isset($relationship->contact_id_b)) { return _crm_error('$relationship is not valid relationship type object'); } $conactId = $relationship->contact_id_b; $params['relationship_type_id'] = $relationship->relationship_type_id . '_a_b'; $ids['contact'] = $relationship->contact_id_a; $ids['relationship'] = $relationship->id; $ids['contactTarget'] = $relationship->contact_id_b; $relationship = CRM_Contact_BAO_Relationship::add($params, $ids, $conactId); if (CRM_Core_Permission::access('CiviMember')) { $params['contact_check'] = array($relationship->contact_id_b => 1); CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a, $params, $ids, CRM_Core_Action::ADD); } return $relationship; }
/** * function to retrieve and update geocoding (lat/long) values for a specified 'address object' and 'contact object' using the configured geo-coding method. * * @param object $object valid address/contact object * * @return null * * $access public */ function crm_fix_address($object) { require_once 'CRM/Utils/Geocode/Yahoo.php'; if (is_a($object, 'CRM_Core_BAO_Address')) { $temp = array(); foreach ($object as $name => $value) { $temp[$name] = $value; } $found = CRM_Utils_Geocode_Yahoo::format($temp); $object->copyValues($temp); // code for saving the changes in database $params = array(); $ids = array(); foreach ($object as $name => $value) { $params['location'][1]['address'][$name] = $value; } $ids['location'][1]['id'] = $object->location_id; $ids['location'][1]['address'] = $object->id; CRM_Core_BAO_Address::add($params, $ids, 1); } else { if (is_a($object, 'CRM_Contact_BAO_Contact')) { $params = $ids = $temp = array(); $locations =& crm_get_locations($object); $locNo = 1; foreach ($locations as $loc => $value) { $addObject =& $locations[$locNo]->address; foreach ($addObject as $name => $value) { $temp[$name] = $value; } if (CRM_Utils_Geocode_Yahoo::format($temp)) { $params['location'][$locNo]['address']['geo_code_1'] = $temp['geo_code_1']; $params['location'][$locNo]['address']['geo_code_2'] = $temp['geo_code_2']; $ids['location'][$locNo]['id'] = $object->location[$locNo]->id; $ids['location'][$locNo]['address'] = $object->location[$locNo]->address->id; $locationId = $locNo; CRM_Core_BAO_Address::add($params, $ids, $locationId); } $locNo++; } } else { return _crm_error('Please pass valid contact / address object.'); } } }
/** * Returns array of location(s) for a contact * * @param object $contact A valid Contact object (passed by reference). * @param Array $location_type Valid location_type label Array. If NULL, all locations are returned. * * * @return An array of Location objects. 'location_id' and 'location_type' are always returned. * * @acces public * */ function crm_get_locations(&$contact, $location_types = null) { _crm_initialize(); if (!isset($contact->id)) { return _crm_error('$contact is not valid contact datatype'); } $params = array(); $params['contact_id'] = $contact->id; $params['entity_id'] = $contact->id; $locationDAO =& new CRM_Core_DAO_Location(); $locationDAO->entity_table = 'civicrm_contact'; $locationDAO->entity_id = $contact->id; $locationCount = $locationDAO->count(); $values = array(); $locations = CRM_Core_BAO_Location::getValues($params, $values, $ids, $locationCount); if (is_array($location_types) && count($location_types) > 0) { $newLocations = array(); foreach ($location_types as $locationName) { $LocationTypeDAO =& new CRM_Core_DAO_LocationType(); $LocationTypeDAO->name = $locationName; $LocationTypeDAO->find(); $LocationTypeDAO->fetch(); foreach ($locations as $location) { if ($location->location_type_id == $LocationTypeDAO->id) { $newLocations[] = $location; } } } if (empty($newLocations)) { return _crm_error('Location information is empty'); } return $newLocations; } return $locations; }
/** * Deletes an existing event * * This API is used for deleting a event * * @param Int $eventID ID of event to be deleted * * @return null if successfull, object of CRM_Core_Error otherwise * @access public */ function &crm_delete_event($eventID) { if (!$eventID) { return _crm_error('Invalid value for eventID'); } require_once 'CRM/Event/BAO/Event.php'; return CRM_Event_BAO_Event::del($eventID); }
/** * Delete a specified contribution. * * @param CRM_Contribution $contribution Contribution object to be deleted * * @return void|CRM_Core_Error An error if 'contribution' is invalid, * permissions are insufficient, etc. * * @access public * */ function crm_delete_contribution(&$contribution) { _crm_initialize(); if (!isset($contribution->id)) { return _crm_error('Invalid contribution object passed in'); } CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id); }
/** * Function to update relationship * * @param object $relationship A valid Relationship object. * @param array $params Associative array of property name/value pairs to be updated. See Data Model for available properties. * * @return updated relationship object * * @access public * */ function crm_update_relationship(&$relationship, $params) { $ids = array(); if (!isset($relationship->id) && !isset($relationship->contact_id_a) && !isset($relationship->contact_id_b)) { return _crm_error("{$relationship} is not valid relationship type object"); } $conactId = $relationship->contact_id_b; $params['relationship_type_id'] = $relationship->relationship_type_id . '_a_b'; $ids['contact'] = $relationship->contact_id_a; $ids['relationship'] = $relationship->id; $ids['contactTarget'] = $relationship->contact_id_b; return CRM_Contact_BAO_Relationship::add($params, $ids, $conactId); }
/** * Updates a note record. * * This api is used to update an existing note record. * 'id' of the note-record to be updated is the required parameter. * * @param array $params Associative array of property name/value pairs with new values to be updated with. * * @return Array of all Note property values (updated). * * @access public */ function &crm_update_note(&$params) { if (!is_array($params)) { return _crm_error('Params is not an array'); } if (!isset($params['id'])) { return _crm_error('Required parameter missing'); } $noteBAO =& new CRM_Core_BAO_Note(); $noteBAO->id = $params['id']; if ($noteBAO->find(true)) { $noteBAO->copyValues($params); if (!$params['modified_date'] && !$noteBAO->modified_date) { $noteBAO->modified_date = date("Ymd"); } } $noteBAO->save(); $note = array(); _crm_object_to_array($noteBAO, $note); return $note; }