public static function singelton()
 {
     if (!self::$singleton) {
         self::$singleton = new CRM_Chapters_AutomatchConfig();
     }
     return self::$singleton;
 }
 public function run()
 {
     $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE, 0);
     $config = CRM_Chapters_AutomatchConfig::singelton();
     $sql = "SELECT id, `" . $config->getMatchTypeField('column_name') . "` AS `type`, `" . $config->getCountryField('column_name') . "` AS `country`, `" . $config->getZipCodeRangeFromField('column_name') . "` AS `zipcode_from`, `" . $config->getZipCodeRangeToField('column_name') . "` AS `zipcode_to` FROM `" . $config->getCustomGroup('table_name') . "` WHERE entity_id = %1";
     $params[1] = array($cid, 'Integer');
     $rows = array();
     $dao = CRM_Core_DAO::executeQuery($sql, $params);
     $types = CRM_Core_OptionGroup::values('chapter_match_type');
     $countries = CRM_Core_OptionGroup::values('chapter_match_country');
     while ($dao->fetch()) {
         $country = '';
         if (!empty($countries[$dao->country])) {
             $country = $countries[$dao->country];
         }
         $row = array();
         $row['type'] = $types[$dao->type];
         $row['type_value'] = $dao->type;
         $row['country'] = $country;
         $row['zipcode_from'] = $dao->zipcode_from;
         $row['zipcode_to'] = $dao->zipcode_to;
         $row['id'] = $dao->id;
         $rows[] = $row;
     }
     $this->assign('rows', $rows);
     $this->assign('cid', $cid);
     parent::run();
 }
 public function postProcess()
 {
     $config = CRM_Chapters_AutomatchConfig::singelton();
     if ($this->_action & CRM_Core_Action::DELETE) {
         CRM_Core_BAO_CustomValue::deleteCustomValue($this->id, $config->getCustomGroup('id'));
         $this->updateAllContacts();
         return;
     }
     $cg = 'custom_';
     $id = ':-1';
     if ($this->id) {
         $id = ':' . $this->id;
     }
     $data['entity_id'] = $this->contact_id;
     $data[$cg . $config->getMatchTypeField('id') . $id] = $this->_submitValues['type'];
     $data[$cg . $config->getCountryField('id') . $id] = $this->_submitValues['country'];
     $data[$cg . $config->getZipCodeRangeFromField('id') . $id] = $this->_submitValues['zipcode_from'];
     $data[$cg . $config->getZipCodeRangeToField('id') . $id] = $this->_submitValues['zipcode_to'];
     civicrm_api3('CustomValue', 'create', $data);
     $this->updateAllContacts();
 }
/**
 * Implements hook_civicrm_tabs().
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC44/hook_civicrm_tabs
 */
function chapters_civicrm_tabs(&$tabs, $contactID)
{
    $config = CRM_Chapters_AutomatchConfig::singelton();
    //unset the tab automatch
    $tab_id = 'custom_' . $config->getCustomGroup('id');
    $tabExists = false;
    $weight = 0;
    $count = 0;
    foreach ($tabs as $key => $tab) {
        if ($tab['id'] == $tab_id) {
            unset($tabs[$key]);
            $weight = $tab['weight'];
            $count = $tab['count'];
            $tabExists = true;
        }
    }
    if ($tabExists) {
        $url = CRM_Utils_System::url('civicrm/contact/automatch_chapters', "reset=1&cid={$contactID}&snippet=1");
        //Count rules
        $tabs[] = array('id' => 'automatch_chapters', 'url' => $url, 'count' => $count, 'title' => $config->getCustomGroup('title'), 'weight' => $weight);
    }
}
 public function findChapterForContact($contact_id)
 {
     $config = CRM_Chapters_AutomatchConfig::singelton();
     try {
         $primary_address = civicrm_api3('Address', 'getsingle', array('contact_id' => $contact_id, 'is_primary' => '1'));
         $country_iso_codes = CRM_Core_PseudoConstant::countryIsoCode();
         $us_country_id = false;
         foreach ($country_iso_codes as $country_id => $iso_code) {
             if ($iso_code == 'US') {
                 $us_country_id = $country_id;
             }
         }
         if (!empty($primary_address['country_id'])) {
             $country_sql = "SELECT entity_id FROM `" . $config->getCustomGroup('table_name') . "` WHERE `" . $config->getMatchTypeField('column_name') . "` = 'country' AND `" . $config->getCountryField('column_name') . "` = %1";
             $country_params[1] = array($primary_address['country_id'], 'Integer');
             $dao = CRM_Core_DAO::executeQuery($country_sql, $country_params);
             if ($dao->fetch()) {
                 return $dao->entity_id;
             }
         }
         if (!empty($primary_address['country_id']) && $us_country_id && $primary_address['country_id'] == $us_country_id && !empty($primary_address['postal_code'])) {
             $zipcode = substr($primary_address['postal_code'], 0, 5);
             if (is_numeric($zipcode)) {
                 $zipcode_sql = "SELECT entity_id FROM `" . $config->getCustomGroup('table_name') . "` WHERE `" . $config->getMatchTypeField('column_name') . "` = 'zipcode' AND `" . $config->getZipCodeRangeFromField('column_name') . "` <=  %1 AND `" . $config->getZipCodeRangeToField('column_name') . "` >= %1";
                 $zipcode_params[1] = array($zipcode, 'Integer');
                 $dao = CRM_Core_DAO::executeQuery($zipcode_sql, $zipcode_params);
                 if ($dao->fetch()) {
                     return $dao->entity_id;
                 }
             }
         }
     } catch (Exception $e) {
         //do nothing
     }
     return false;
 }