<commit_before><?php

require_once 'bipdata.civix.php';

/**
 * See if a CiviCRM custom field exists
 * @param string $field_label
 *   custom field name to look for, corresponds to field civicrm_custom_field.label
 * @return custom id
 *   custom field id if it exists, else zero
 */
function custom_field_get_id($field_label) {
  $result = 0;
  $response = civicrm_api3('CustomField', 'get', array(
    'sequential' => 1,
    'label' => $field_label,
  ));
  foreach($response['values'] as $key => $value) {
    $result = $value['id'];
  }
  return $result;
}

function bipdata_civicrm_pre($op, $objectName, $id, &$params) {
  // This hook fires all the time, only run this code if we're creating/editing an address
  if ($objectName == 'Address' && $op == 'edit') {
    $custom_id = custom_field_get_id('BBL');
    $custom_field = 'custom_' . $custom_id;

    // 1) the custom_153 field as part of the params is
    // returned as custom_153_id_from_db (like custom_153_1218)
    // why is that?
    $result = civicrm_api3('Address', 'get', array(
      'sequential' => 1,
      'return' => $custom_field,
      'id' => $id,
    ));
    foreach($result['values'] as $key => $value) {
      $bbl = $value[$custom_field];
    }

    if ($bbl !== null) {
      // Write some code to get the BIP values from someplace else.  Could be an API call or a SQL call or whatever.
      // Example using API (assumes BIP data is its own object)
      $response = civicrm_api3('BipData', 'get', array(
        'sequential' => 1,
        'bip_bbl' => $bbl,
      ));<commit_msg>removes commentg<commit_after><?php

require_once 'bipdata.civix.php';

/**
 * See if a CiviCRM custom field exists
 * @param string $field_label
 *   custom field name to look for, corresponds to field civicrm_custom_field.label
 * @return custom id
 *   custom field id if it exists, else zero
 */
function custom_field_get_id($field_label) {
  $result = 0;
  $response = civicrm_api3('CustomField', 'get', array(
    'sequential' => 1,
    'label' => $field_label,
  ));
  foreach($response['values'] as $key => $value) {
    $result = $value['id'];
  }
  return $result;
}

function bipdata_civicrm_pre($op, $objectName, $id, &$params) {
  // This hook fires all the time, only run this code if we're creating/editing an address
  if ($objectName == 'Address' && $op == 'edit') {
    $custom_id = custom_field_get_id('BBL');
    $custom_field = 'custom_' . $custom_id;

    // 1) the custom_153 field as part of the params is
    // returned as custom_153_id_from_db (like custom_153_1218)
    // why is that?
    $result = civicrm_api3('Address', 'get', array(
      'sequential' => 1,
      'return' => $custom_field,
      'id' => $id,
    ));
    foreach($result['values'] as $key => $value) {
      $bbl = $value[$custom_field];
    }

    if ($bbl !== null) {
      $response = civicrm_api3('BipData', 'get', array(
        'sequential' => 1,
        'bip_bbl' => $bbl,
      ));
{'fraction_non_alphanumeric': 0.14952638700947227, 'fraction_numerical': 0.014208389715832206, 'mean_word_length': 3.2182596291012837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '284862', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1006, 'n_words': 291}