/**
 * Creates a new household according to the specs
 */
function civicrm_api3_contact_create_household($params)
{
    if (empty($params['mode'])) {
        $params['mode'] = CRM_Householdmerge_Logic_Configuration::getHouseholdMode();
    }
    $head_id = NULL;
    switch ($params['mode']) {
        case 'hierarchy':
            $head_id = (int) $params['head_id'];
        case 'link':
            // get member IDs
            $member_ids = $params['member_ids'];
            if (is_string($member_ids)) {
                $member_ids = explode(',', $member_ids);
            }
            // sanitise member IDs
            $sanitised_member_ids = array();
            foreach ($member_ids as $member_id) {
                $sanitised_member_id = (int) $member_id;
                if ($sanitised_member_id) {
                    $sanitised_member_ids[] = $sanitised_member_id;
                }
            }
            // now pass the work on the the worker
            $worker = new CRM_Householdmerge_Logic_Worker();
            $household_id = $worker->createLinkedHousehold($params['household_name'], $sanitised_member_ids, $params['address'], $head_id);
            return civicrm_api3_create_success();
        default:
            return civicrm_api3_create_error("Contact.create_household cannot process mode '{$params['mode']}'.");
    }
}
 /**
  * see if this could be a household
  *
  * @return array see findNewHouseholds()
  */
 protected function investigateCandidate($contact_id, $signature)
 {
     // signature last name should at least have two letters
     if (!preg_match("/\\w.*\\w/", $signature['last_name'])) {
         return NULL;
     }
     // compile members
     $contact_ids = explode('||', $signature['contact_ids']);
     $display_names = explode('||', $signature['display_names']);
     $gender_ids = explode('||', $signature['gender_ids']);
     $members = array();
     for ($i = 0; $i < count($contact_ids); $i++) {
         $contact_id = $contact_ids[$i];
         $members[$contact_id] = array('id' => $contact_id, 'display_name' => $display_names[$i], 'gender_id' => $gender_ids[$i], 'last_name' => $signature['last_name'], 'street_address' => $signature['street_address'], 'supplemental_address_1' => $signature['supplemental_address_1'], 'supplemental_address_2' => $signature['supplemental_address_2'], 'postal_code' => $signature['postal_code'], 'city' => $signature['city'], 'country_id' => $signature['country_id']);
     }
     // stop here if there's not enough
     if (count($members) < CRM_Householdmerge_Logic_Configuration::getMinimumMemberCount()) {
         return NULL;
     }
     $candidate = array('id' => $this->createID(), 'household_id' => 0, 'head_id' => 0, 'household_name' => '', 'member_ids' => array(), 'contacts' => array(), 'address' => array('street_address' => $signature['street_address'], 'supplemental_address_1' => $signature['supplemental_address_1'], 'supplemental_address_2' => $signature['supplemental_address_2'], 'postal_code' => $signature['postal_code'], 'city' => $signature['city'], 'country_id' => $signature['country_id']));
     foreach ($members as $member_id => $member) {
         $candidate['member_ids'][] = $member['id'];
         $candidate['contacts'][$member['id']] = $member;
         $candidate['household_name'] = $member['last_name'];
     }
     if ('hierarchy' == CRM_Householdmerge_Logic_Configuration::getHouseholdMode()) {
         // we need to identify the HEAD
         $head_id = $this->identifyHead($members);
         $candidate['head_id'] = $head_id;
         // remeove head from member_ids
         $index = array_search($head_id, $candidate['member_ids']);
         unset($candidate['member_ids'][$index]);
     }
     return $candidate;
 }
 function preProcess()
 {
     $this->setDefaults(array('hh_mode' => CRM_Householdmerge_Logic_Configuration::getHouseholdMode(), 'hh_head_mode' => CRM_Householdmerge_Logic_Configuration::getHouseholdHeadMode(), 'hh_member_relation' => CRM_Householdmerge_Logic_Configuration::getHeadRelationID(), 'hh_head_relation' => CRM_Householdmerge_Logic_Configuration::getMemberRelationID()));
 }
 /**
  * investigates if the given household still complies
  * with all the requirements for a proper household entity
  *
  */
 function checkHousehold($household_id)
 {
     $problems_identified = array();
     // load household
     $household = civicrm_api3('Contact', 'getsingle', array('id' => $household_id));
     // load members
     $members = $this->getMembers($household_id);
     // CHECK 1: number of members
     if (count($members) < CRM_Householdmerge_Logic_Configuration::getMinimumMemberCount()) {
         if (count($members) == 0) {
             $problems_identified[] = CRM_Householdmerge_Logic_Problem::createProblem('HOM0', $household_id);
         } else {
             $problems_identified[] = CRM_Householdmerge_Logic_Problem::createProblem('HOMX', $household_id, array('count' => count($members)));
         }
     }
     // HEAD related checks
     if ('hierarchy' == CRM_Householdmerge_Logic_Configuration::getHouseholdMode()) {
         $heads = array();
         foreach ($members as $member) {
             if ($member['hh_relation'] == 'head') {
                 $heads[] = $member;
             }
         }
         // CHECK 2: is there still a head?
         if (empty($heads)) {
             $problems_identified[] = CRM_Householdmerge_Logic_Problem::createProblem('HHN0', $household_id);
         }
         // CHECK 3: is there more than one head?
         if (count($heads) > 1) {
             $problems_identified[] = CRM_Householdmerge_Logic_Problem::createProblem('HHN2', $household_id);
         }
         // CHECK 4: does the head have a DO NOT mail/phone/sms/email
         $donts = CRM_Householdmerge_Logic_Configuration::getDontXXXChecks();
         foreach ($heads as $head) {
             foreach ($donts as $field_name) {
                 if (!empty($head[$field_name])) {
                     $problems_identified[] = CRM_Householdmerge_Logic_Problem::createProblem('HHNC', $household_id);
                     break;
                 }
             }
         }
         // CHECK 5: does the head have certain tags
         $bad_tags = CRM_Householdmerge_Logic_Configuration::getBadHeadTags();
         foreach ($heads as $head) {
             $tags = CRM_Core_BAO_EntityTag::getContactTags($head['id']);
             foreach ($tags as $tag) {
                 if (in_array($tag, $bad_tags)) {
                     $problems_identified[] = CRM_Householdmerge_Logic_Problem::createProblem('HHTG', $household_id, array('tag' => $tag));
                 }
             }
         }
         // CHECK 6: is the head also head of another household?
         foreach ($heads as $head) {
             $head_relation_id = CRM_Householdmerge_Logic_Configuration::getHeadRelationID();
             $relationships_a = civicrm_api3('Relationship', 'get', array('contact_id_a' => $head['id'], 'relationship_type_id' => $head_relation_id, 'is_active' => 1));
             $relationships_b = civicrm_api3('Relationship', 'get', array('contact_id_b' => $head['id'], 'relationship_type_id' => $head_relation_id, 'is_active' => 1));
             if ($relationships_a['count'] + $relationships_b['count'] > 1) {
                 $problems_identified[] = CRM_Householdmerge_Logic_Problem::createProblem('HHMM', $household_id);
             }
         }
     }
     // all member checks
     // CHECK 7: does one of the members not have the household address any more?
     $this->checkAddresses($household, $members, $problems_identified);
     // CHECK 8: Is there a potential new member for this household?
     $this->findNewMembers($household, $members, $problems_identified);
     // if (!empty($problems_identified)) {
     //   $this->createActivity($household, $problems_identified, $members);
     // }
     foreach ($problems_identified as $problem) {
         $problem->createActivity();
     }
 }
/**
 * merge hook for 'merge' mode households
 */
function householdmerge_civicrm_merge($type, &$data, $mainId = NULL, $otherId = NULL, $tables = NULL)
{
    if ('merge' == CRM_Householdmerge_Logic_Configuration::getHouseholdMode()) {
        // if in 'merge' mode, pass this hook to the househould merge controller
        $hhmerge_controller = new CRM_Householdmerge_MergeController();
        $hhmerge_controller->resolveConflicts($type, $data, $mainId, $otherId);
    }
}