/** * Returns array of contacts who are members of the specified group. * * @param CRM_Contact $group A valid group object (passed by reference) * @param array $returnProperties Which properties * should be included in the returned Contact object(s). If NULL, * the default set of contact properties will be * included. group_contact properties (such as 'status', * ' in_date', etc.) are included automatically.Note:Do not inclue * Id releted properties. * @param text $status A valid status value ('Added', 'Pending', 'Removed'). * @param text $sort Associative array of * one or more "property_name"=>"sort direction" * pairs which will control order of Contact objects returned. * @param Int $offset Starting row index. * @param Int $row_count Maximum number of rows to returns. * * * @return $contactArray Array of contacts who are members of the specified group * * @access public */ function getGroupContacts(&$group, $returnProperties = null, $status = 'Added', $sort = null, $offset = null, $row_count = null) { $query = "SELECT * FROM civicrm_group WHERE id = " . CRM_Utils_Type::escape($group->id, 'Integer'); $groupDAO =& new CRM_Contact_DAO_Group(); $groupDAO->id = $group->id; if (!$groupDAO->find(true)) { return CRM_Core_Error::createError("Could not locate group with id: {$id}"); } // make sure user has got permission to view this group if (!CRM_Contact_BAO_Group::checkPermission($groupDAO->id, $groupDAO->title)) { return CRM_Core_Error::createError("You do not have permission to access group with id: {$id}"); } $query = ''; if (empty($returnProperties)) { $query = "SELECT civicrm_contact.id as contact_id,\n civicrm_email.email as email"; //$query = "SELECT *,civicrm_contact.id as contact_id, (talk to lobo before re-enabling this) //civicrm_email.email as email"; } else { $query = "SELECT civicrm_contact.id as contact_id ,"; $query .= implode(',', $returnProperties); } $fv = array('group' => array($group->id => true)); if ($status) { $fv['group_contact_status'] = array($status => true); } else { $fv['group_contact_status'] = array('Added' => true, 'Removed' => true, 'Pending' => true); } $tables = array(CRM_Contact_BAO_GroupContact::getTableName() => true, CRM_Core_BAO_Email::getTableName() => true, CRM_Contact_BAO_Contact::getTableName() => true, CRM_Contact_BAO_Group::getTableName() => true); $inner = array(); $whereTables = array(); $where = CRM_Contact_BAO_Query::getWhereClause($fv, null, $tables, $whereTables); $permission = CRM_Core_Permission::whereClause(CRM_CORE_PERMISSION_VIEW, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($tables, $inner); $query .= " {$from} WHERE {$permission} AND {$where} "; if ($sort != null) { $order = array(); foreach ($sort as $key => $direction) { $order[] = " {$key} {$direction} "; } $query .= " ORDER BY " . implode(',', $order); } if ($offset != null && $row_count != null) { $query .= " LIMIT {$offset}, {$row_count}"; } // CRM_Core_Error::debug( 'q', $query ); $dao =& new CRM_Contact_DAO_Contact(); $dao->query($query); // this is quite inefficient, we need to change the return // values in docs $contactArray = array(); while ($dao->fetch()) { $contactArray[] = clone $dao; } return $contactArray; }
/** * Returns array of contacts who are members of the specified group. * * @param CRM_Contact $group A valid group object (passed by reference) * @param array $returnProperties Which properties * should be included in the returned Contact object(s). If NULL, * the default set of contact properties will be * included. group_contact properties (such as 'status', * ' in_date', etc.) are included automatically.Note:Do not inclue * Id releted properties. * @param text $status A valid status value ('Added', 'Pending', 'Removed'). * @param text $sort Associative array of * one or more "property_name"=>"sort direction" * pairs which will control order of Contact objects returned. * @param Int $offset Starting row index. * @param Int $row_count Maximum number of rows to returns. * * * @return $contactArray Array of contacts who are members of the specified group * * @access public */ static function getGroupContacts(&$group, $returnProperties = null, $status = 'Added', $sort = null, $offset = null, $row_count = null, $includeChildGroups = false) { $groupDAO =& new CRM_Contact_DAO_Group(); $groupDAO->id = $group->id; if (!$groupDAO->find(true)) { return CRM_Core_Error::createError("Could not locate group with id: {$id}"); } // make sure user has got permission to view this group require_once 'CRM/Contact/BAO/Group.php'; if (!CRM_Contact_BAO_Group::checkPermission($groupDAO->id, $groupDAO->title)) { return CRM_Core_Error::createError("You do not have permission to access group with id: {$id}"); } $query = ''; if (empty($returnProperties)) { $query = "SELECT contact_a.id as contact_id,\n civicrm_email.email as email"; } else { $query = "SELECT contact_a.id as contact_id , {$grpStatus} as status,"; $query .= implode(',', $returnProperties); } $params = array(); if ($includeChildGroups) { require_once 'CRM/Contact/BAO/GroupNesting.php'; $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds(array($group->id)); } else { $groupIds = array($group->id); } foreach ($groupIds as $groupId) { $params[] = array('group', 'IN', array($group->id => true), 0, 0); } require_once 'CRM/Core/BAO/Email.php'; require_once 'CRM/Contact/BAO/Contact.php'; $tables = array(CRM_Core_BAO_Email::getTableName() => true, CRM_Contact_BAO_Contact::getTableName() => true); $inner = array(); $whereTables = array(); $where = CRM_Contact_BAO_Query::getWhereClause($params, null, $tables, $whereTables); $permission = CRM_Core_Permission::whereClause(CRM_Core_Permission::VIEW, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($tables, $inner); $query .= " {$from} WHERE {$permission} AND {$where} "; if ($sort != null) { $order = array(); foreach ($sort as $key => $direction) { $order[] = " {$key} {$direction} "; } $query .= " ORDER BY " . implode(',', $order); } if (!is_null($offset) && !is_null($row_count)) { $query .= " LIMIT {$offset}, {$row_count}"; } $dao =& new CRM_Contact_DAO_Contact(); $dao->query($query); // this is quite inefficient, we need to change the return // values in docs $contactArray = array(); while ($dao->fetch()) { $contactArray[] = clone $dao; } return $contactArray; }
/** * Ensure that we have the right input parameters * * @todo 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 contact. * @param boolean $dupeCheck Should we check for duplicate contacts * @param boolean $dupeErrorArray Should we return values of error * object in array foramt * @param boolean $requiredCheck Should we check if required params * are present in params array * @param int $dedupeRuleGroupID - the dedupe rule ID to use if present * * @return null on success, error message otherwise * @access public */ function civicrm_contact_check_params(&$params, $dupeCheck = TRUE, $dupeErrorArray = FALSE, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) { if ($requiredCheck) { $required = array('Individual' => array(array('first_name', 'last_name'), 'email'), 'Household' => array('household_name'), 'Organization' => array('organization_name')); // cannot create a contact with empty params if (empty($params)) { return civicrm_create_error('Input Parameters empty'); } if (!array_key_exists('contact_type', $params)) { return civicrm_create_error('Contact Type not specified'); } // contact_type has a limited number of valid values $fields = CRM_Utils_Array::value($params['contact_type'], $required); if ($fields == NULL) { return civicrm_create_error("Invalid Contact Type: {$params['contact_type']}"); } if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) { if (!CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type'])) { return civicrm_create_error("Invalid or Mismatched Contact SubType: " . implode(', ', (array) $csType)); } } if (!CRM_Utils_Array::value('contact_id', $params)) { $valid = FALSE; $error = ''; foreach ($fields as $field) { if (is_array($field)) { $valid = TRUE; foreach ($field as $element) { if (!CRM_Utils_Array::value($element, $params)) { $valid = FALSE; $error .= $element; break; } } } else { if (CRM_Utils_Array::value($field, $params)) { $valid = TRUE; } } if ($valid) { break; } } if (!$valid) { return civicrm_create_error("Required fields not found for {$params['contact_type']} : {$error}"); } } } if ($dupeCheck) { // check for record already existing require_once 'CRM/Dedupe/Finder.php'; $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); // CRM-6431 // setting 'check_permission' here means that the dedupe checking will be carried out even if the // person does not have permission to carry out de-dupes // this is similar to the front end form if (isset($params['check_permission'])) { $dedupeParams['check_permission'] = $fields['check_permission']; } $ids = implode(',', CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Strict', array(), $dedupeRuleGroupID)); if ($ids != NULL) { if ($dupeErrorArray) { $error = CRM_Core_Error::createError("Found matching contacts: {$ids}", CRM_Core_Error::DUPLICATE_CONTACT, 'Fatal', $ids); return civicrm_create_error($error->pop()); } return civicrm_create_error("Found matching contacts: {$ids}", array($ids)); } } //check for organisations with same name if (CRM_Utils_Array::value('current_employer', $params)) { $organizationParams = array(); $organizationParams['organization_name'] = $params['current_employer']; require_once 'CRM/Dedupe/Finder.php'; $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); $dedupParams['check_permission'] = FALSE; $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Fuzzy'); // check for mismatch employer name and id if (CRM_Utils_Array::value('employer_id', $params) && !in_array($params['employer_id'], $dupeIds)) { return civicrm_create_error('Employer name and Employer id Mismatch'); } // show error if multiple organisation with same name exist if (!CRM_Utils_Array::value('employer_id', $params) && count($dupeIds) > 1) { return civicrm_create_error('Found more than one Organisation with same Name.'); } } return NULL; }
/** * @param array $params * @param bool $dupeCheck * @param bool $dupeErrorArray * @param bool $requiredCheck * @param int $dedupeRuleGroupID * * @return array|null */ function _civicrm_api3_deprecated_contact_check_params(&$params, $dupeCheck = TRUE, $dupeErrorArray = FALSE, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) { if (isset($params['id']) && is_numeric($params['id'])) { $requiredCheck = FALSE; } if ($requiredCheck) { if (isset($params['id'])) { $required = array('Individual', 'Household', 'Organization'); } $required = array('Individual' => array(array('first_name', 'last_name'), 'email'), 'Household' => array('household_name'), 'Organization' => array('organization_name')); // contact_type has a limited number of valid values if (empty($params['contact_type'])) { return civicrm_api3_create_error("No Contact Type"); } $fields = CRM_Utils_Array::value($params['contact_type'], $required); if ($fields == NULL) { return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}"); } if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) { if (!CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type'])) { return civicrm_api3_create_error("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType)); } } if (empty($params['contact_id']) && !empty($params['id'])) { $valid = FALSE; $error = ''; foreach ($fields as $field) { if (is_array($field)) { $valid = TRUE; foreach ($field as $element) { if (empty($params[$element])) { $valid = FALSE; $error .= $element; break; } } } else { if (!empty($params[$field])) { $valid = TRUE; } } if ($valid) { break; } } if (!$valid) { return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : {$error}"); } } } if ($dupeCheck) { // @todo switch to using api version by uncommenting these lines & removing following 11. // Any change here is too scary for a stable release. // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID))); // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL; // check for record already existing require_once 'CRM/Dedupe/Finder.php'; $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); // CRM-6431 // setting 'check_permission' here means that the dedupe checking will be carried out even if the // person does not have permission to carry out de-dupes // this is similar to the front end form if (isset($params['check_permission'])) { $dedupeParams['check_permission'] = $params['check_permission']; } $ids = implode(',', CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array(), $dedupeRuleGroupID)); if ($ids != NULL) { if ($dupeErrorArray) { $error = CRM_Core_Error::createError("Found matching contacts: {$ids}", CRM_Core_Error::DUPLICATE_CONTACT, 'Fatal', $ids); return civicrm_api3_create_error($error->pop()); } return civicrm_api3_create_error("Found matching contacts: {$ids}"); } } // check for organisations with same name if (!empty($params['current_employer'])) { $organizationParams = array(); $organizationParams['organization_name'] = $params['current_employer']; require_once 'CRM/Dedupe/Finder.php'; $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); $dedupParams['check_permission'] = FALSE; $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); // check for mismatch employer name and id if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)) { return civicrm_api3_create_error('Employer name and Employer id Mismatch'); } // show error if multiple organisation with same name exist if (empty($params['employer_id']) && count($dupeIds) > 1) { return civicrm_api3_create_error('Found more than one Organisation with same Name.'); } } return NULL; }
/** * Ensure that we have the right input parameters * * @todo 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 contact. * @param boolean $dupeCheck Should we check for duplicate contacts * @param boolean $dupeErrorArray Should we return values of error * object in array foramt * @param boolean $requiredCHeck Should we check if required params * are present in params array * * @return null on success, error message otherwise * @access public */ function civicrm_contact_check_params(&$params, $dupeCheck = true, $dupeErrorArray = false, $requiredCheck = true) { if ($requiredCheck) { $required = array('Individual' => array(array('first_name', 'last_name'), 'email'), 'Household' => array('household_name'), 'Organization' => array('organization_name')); // cannot create a contact with empty params if (empty($params)) { return civicrm_create_error('Input Parameters empty'); } if (!array_key_exists('contact_type', $params)) { return civicrm_create_error('Contact Type not specified'); } // contact_type has a limited number of valid values $fields = CRM_Utils_Array::value($params['contact_type'], $required); if ($fields == null) { return civicrm_create_error("Invalid Contact Type: {$params['contact_type']}"); } $valid = false; $error = ''; foreach ($fields as $field) { if (is_array($field)) { $valid = true; foreach ($field as $element) { if (!CRM_Utils_Array::value($element, $params)) { $valid = false; $error .= $element; break; } } } else { if (CRM_Utils_Array::value($field, $params)) { $valid = true; } } if ($valid) { break; } } if (!$valid) { return civicrm_create_error("Required fields not found for {$params['contact_type']} : {$error}"); } } if ($dupeCheck) { // check for record already existing require_once 'CRM/Dedupe/Finder.php'; $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); $ids = implode(',', CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'])); if ($ids != null) { if ($dupeErrorArray) { $error = CRM_Core_Error::createError("Found matching contacts: {$ids}", CRM_Core_Error::DUPLICATE_CONTACT, 'Fatal', $ids); return civicrm_create_error($error->pop()); } return civicrm_create_error("Found matching contacts: {$ids}", $ids); } } return null; }
function _crm_error($message, $code = 8000, $level = 'Fatal', $params = null) { return CRM_Core_Error::createError($message, $code, $level, $params); }
/** * * @param <type> $params * @return <type> */ function _civicrm_duplicate_formatted_contact(&$params) { $id = CRM_Utils_Array::value('id', $params); $externalId = CRM_Utils_Array::value('external_identifier', $params); if ($id || $externalId) { $contact = new CRM_Contact_DAO_Contact(); $contact->id = $id; $contact->external_identifier = $externalId; if ($contact->find(true)) { if ($params['contact_type'] != $contact->contact_type) { return civicrm_create_error("Mismatched contact IDs OR Mismatched contact Types"); } $error = CRM_Core_Error::createError("Found matching contacts: {$contact->id}", CRM_Core_Error::DUPLICATE_CONTACT, 'Fatal', $contact->id); return civicrm_create_error($error->pop()); } } else { require_once 'CRM/Dedupe/Finder.php'; $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Strict'); if (!empty($ids)) { $ids = implode(',', $ids); $error = CRM_Core_Error::createError("Found matching contacts: {$ids}", CRM_Core_Error::DUPLICATE_CONTACT, 'Fatal', $ids); return civicrm_create_error($error->pop()); } } return civicrm_create_success(true); }
/** * Check if there is a contribution with the same trxn_id or invoice_id * * @param array $params Associative array of property name/value * pairs to insert in new contribution. * * @return array|CRM_Error * @access private */ function _civicrm_contribute_duplicate_check(&$params) { require_once 'CRM/Contribute/BAO/Contribution.php'; $duplicates = array(); $result = CRM_Contribute_BAO_Contribution::checkDuplicate($params, $duplicates); if ($result) { $d = implode(', ', $duplicates); $error = CRM_Core_Error::createError("Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: {$d}", CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', $d); return civicrm_create_error($error->pop(), $d); } else { return array(); } }
/** * This function collects all the information and * "simulates" a payment processor by creating an incomplete mandate, * that will later be connected with the results of the rest of the * payment process * * @param array $params assoc array of input parameters for this transaction * * @return array the result in an nice formatted array (or an error object) */ function doDirectPayment(&$params) { $original_parameters = $params; // prepare the creation of an incomplete mandate $params['creditor_id'] = $this->_creditorId; $params['contact_id'] = $this->getVar('_contactID'); $params['source'] = $params['description']; $params['iban'] = $params['bank_account_number']; $params['bic'] = $params['bank_identification_number']; $params['creation_date'] = date('YmdHis'); $params['status'] = 'PARTIAL'; if (empty($params['is_recur'])) { $params['type'] = 'OOFF'; $params['entity_table'] = 'civicrm_contribution'; } else { $params['type'] = 'RCUR'; $params['entity_table'] = 'civicrm_contribution_recur'; } // we don't have the associated entity id yet // so we set MAX_INT as a dummy value // remark: setting this to 0/NULL does not work // due to complications with the api $params['entity_id'] = pow(2, 32) - 1; // Allow further manipulation of the arguments via custom hooks .. CRM_Utils_Hook::alterPaymentProcessorParams($this, $original_parameters, $params); // create the mandate $params['version'] = 3; $mandate = civicrm_api('SepaMandate', 'create', $params); if (!empty($mandate['is_error'])) { return CRM_Core_Error::createError(ts("Couldn't create SEPA mandate. Error was: ") . $mandate['error_message']); } $params['trxn_id'] = $mandate['values'][$mandate['id']]['reference']; $params['sepa_start_date'] = empty($params['start_date']) ? date('YmdHis') : date('YmdHis', strtotime($params['start_date'])); // update the contribution, if existing (RCUR case) if (!empty($params['contributionID'])) { $contribution = civicrm_api3('Contribution', 'getsingle', array('id' => $params['contributionID'])); civicrm_api3('Contribution', 'create', array('id' => $params['contributionID'], 'contact_id' => $contribution['contact_id'], 'receive_date' => $params['sepa_start_date'], 'trxn_id' => $params['trxn_id'])); } if (!empty($params['contributionRecurID'])) { civicrm_api3('ContributionRecur', 'create', array('id' => $params['contributionRecurID'], 'start_date' => $params['sepa_start_date'], 'cycle_day' => $params['cycle_day'], 'trxn_id' => $params['trxn_id'])); } return $params; }
/** * * @param <type> $params * @return <type> */ function civicrm_participant_check_params(&$params) { require_once 'CRM/Event/BAO/Participant.php'; $result = array(); if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) { $participantID = array_pop($result); $error = CRM_Core_Error::createError("Found matching participant record.", CRM_Core_Error::DUPLICATE_PARTICIPANT, 'Fatal', $participantID); return civicrm_create_error($error->pop(), array('contactID' => $params['contact_id'], 'participantID' => $participantID)); } return true; }
/** * * @param <type> $params * * @return <type> */ function civicrm_participant_check_params(&$params, $checkDuplicate = FALSE) { require_once 'CRM/Event/BAO/Participant.php'; //check if participant id is valid or not if (CRM_Utils_Array::value('id', $params)) { $participant = new CRM_Event_BAO_Participant(); $participant->id = $params['id']; if (!$participant->find(TRUE)) { return civicrm_create_error(ts('Participant id is not valid')); } } require_once 'CRM/Contact/BAO/Contact.php'; //check if contact id is valid or not if (CRM_Utils_Array::value('contact_id', $params)) { $contact = new CRM_Contact_BAO_Contact(); $contact->id = $params['contact_id']; if (!$contact->find(TRUE)) { return civicrm_create_error(ts('Contact id is not valid')); } } //check that event id is not an template 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_create_error(ts('Event templates are not meant to be registered')); } } $result = array(); if ($checkDuplicate) { if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) { $participantID = array_pop($result); $error = CRM_Core_Error::createError("Found matching participant record.", CRM_Core_Error::DUPLICATE_PARTICIPANT, 'Fatal', $participantID); return civicrm_create_error($error->pop(), array('contactID' => $params['contact_id'], 'participantID' => $participantID)); } } return TRUE; }