<commit_before><?php

/**
 * Export the intern records into the social graph denormalized table.
 */

/* Constants */

// Object types.
define('OBJECT_TYPE_LEAD', 'Lead');
define('OBJECT_TYPE_CONTACT', 'Contact');

// Setting for function get_picklist_field_info().
define('GET_TID_FIELD_NAMES', TRUE);

// Vocabulary id's for the picklists.
define('SOURCE_VID', 361);
define('INTERN_LENGTH_VID', 362);
define('INTERN_TYPE_VID', 363);
define('CITIZEN_VID', 364);
define('POS_PREF_VID', 365);
define('SPECIAL_SKILLS_VID', 366);
define('WORK_ENVIRON_VID', 367);
define('WORK_POP_PREF_VID', 368);
define('CVC_DEGREE_PROG_VID', 369);

/* Main script. */

// Go down a directory to include Drupal framework, for DB layer functions.
chdir('..');
require_once './includes/bootstrap.inc'; // Drupal (for database & SF functions)

// Drupal bootstrap - to use database functions and Salesforce module 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Switch to the social graph database.
db_set_active('techmi5_socgraph');

// Execute the intern sync cron.
run_cron();

/* Function definitions. */

// Main cron function
function run_cron() {
  $sf = salesforce_api_connect();

  if(!is_object($sf)) {
    exit_connection_failed();
  }
  
  $cv_objects = query_sf_objects($sf);
  print_r($cv_objects);
  
  if((isset($objects[OBJECT_TYPE_LEAD]) && count($objects[OBJECT_TYPE_LEAD]) > 0) || (isset($objects[OBJECT_TYPE_CONTACT]) && count($objects[OBJECT_TYPE_CONTACT]) > 0)) {
    $result = write_cv_objects($cv_objects);
  }
}

// Queries for the Salesforce intern leads to be imported
function query_sf_objects($sf) {
  // Function requires an active Salesforce connection.
  if(!is_object($sf)) {
    return NULL;
  }  
  $query_results = array();
  $sf_object_ids = array();
  $object_types = get_object_types();
  
  foreach($object_types as $object_type) {
    $object_fields = get_object_fields($object_type);
    $soql_fields = implode(", ", $object_fields);

    $soql = build_soql_query($soql_fields, $object_type); 	
    try {
      $result = $sf->client->query($soql);
    }
    catch (Exception $e) {
      salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception in sf_intern_sync ' . $object_type . ' queries: ' . $e->getMessage(), array(), WATCHDOG_ALERT);
    }

    if(isset($result->records)) {
      if($result->size >= 1) {
        foreach($result->records as $index => $sf_object) {
          $sf_object_ids[] = $sf_object->Id;
        }
      }
      $cv_object_records = array();
      foreach($result->records as $key => $sf_object) {
        $cv_object = normalize_record($sf_object, $object_type);
        $cv_object = populate_record($cv_object, $object_type);
        $cv_object_records[$cv_object->Id] = $cv_object;
      }
      // Free up some memory by unsetting the result array.
      unset($result);
    }
    $query_results += array($object_type => $cv_object_records);
  }
  if(count($sf_object_ids) > 0) {
    $sf_object_ids_text = implode_quoted($sf_object_ids);
    $soql = "SELECT Id, ParentId, Name, Body FROM Attachment WHERE ParentId IN (" . $sf_object_ids_text . ")"; 
    try {
      $result_attachments = $sf->client->query($soql);
    }
    catch (Exception $e) {
      salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception in sf_intern_sync attachment queries: ' . $e->getMessage(), array(), WATCHDOG_ALERT);
    }
    if(isset($result_attachments->records)) { 
      $attachment_records = array();
      foreach($result_attachments->records as $key => $attachment) {
        $attachment_records[$attachment->ParentId] = $attachment;
      }
      // Free up some memory by unsetting the result array.
      unset($result_attachments);
      $query_results['Attachment'] = $attachment_records;
    }
  }
  return $query_results;
}

// Normalize a lead record to have all the fields.
function normalize_record($sf_object, $object_type) {
  // Exit early if the "bare minimum" of fields is not present.
  if(!is_object($sf_object) || empty($sf_object->Id)) {
    return NULL;
  }
  
  $object_fields = get_object_fields($object_type);
  foreach($object_fields as $cv_fieldname => $sf_fieldname) {
    $sf_object->{$cv_fieldname} = !empty($sf_object->{$sf_fieldname}) ? $sf_object->{$sf_fieldname} : '';
    // Remove the duplicate fields.
    if($sf_fieldname != 'Id') {
      unset($sf_object->{$sf_fieldname});
    }
  }
  
  return $sf_object;
}

// Populate the fields that are not directly in the SF object.
function populate_record($cv_object, $object_type) {
  // Exit early if the "bare minimum" of fields is not present.
  if(!is_object($cv_object) || empty($cv_object->Id)) {
    return NULL;
  }
  
  // Set the title field.
  $cv_object->title = $cv_object->applic_first_name . ' ' . $cv_object->applic_last_name;
  
  // Populate the picklist fields by tid.
  $picklist_field_info = get_picklist_field_info();
  foreach($picklist_field_info as $tid_field => $field_info) {
    $field_values = array();
    $tid_field_values = array();
    if($field_info['name'] == 'intern_type') {
      $cv_object->{$field_info['name']} = explode(';', $cv_object->{$field_info['name']});
    }
    else {
      $field_values = array($cv_object->{$field_info['name']});
    }
    foreach($field_values as $key => $field_value) {
      $field_values[$key] = get_picklist_name_by_synonym($field_value);<commit_msg>Fixed the intern_type picklist multi-select<commit_after><?php

/**
 * Export the intern records into the social graph denormalized table.
 */

/* Constants */

// Object types.
define('OBJECT_TYPE_LEAD', 'Lead');
define('OBJECT_TYPE_CONTACT', 'Contact');

// Setting for function get_picklist_field_info().
define('GET_TID_FIELD_NAMES', TRUE);

// Vocabulary id's for the picklists.
define('SOURCE_VID', 361);
define('INTERN_LENGTH_VID', 362);
define('INTERN_TYPE_VID', 363);
define('CITIZEN_VID', 364);
define('POS_PREF_VID', 365);
define('SPECIAL_SKILLS_VID', 366);
define('WORK_ENVIRON_VID', 367);
define('WORK_POP_PREF_VID', 368);
define('CVC_DEGREE_PROG_VID', 369);

/* Main script. */

// Go down a directory to include Drupal framework, for DB layer functions.
chdir('..');
require_once './includes/bootstrap.inc'; // Drupal (for database & SF functions)

// Drupal bootstrap - to use database functions and Salesforce module 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Switch to the social graph database.
db_set_active('techmi5_socgraph');

// Execute the intern sync cron.
run_cron();

/* Function definitions. */

// Main cron function
function run_cron() {
  $sf = salesforce_api_connect();

  if(!is_object($sf)) {
    exit_connection_failed();
  }
  
  $cv_objects = query_sf_objects($sf);
  print_r($cv_objects);
  
  if((isset($objects[OBJECT_TYPE_LEAD]) && count($objects[OBJECT_TYPE_LEAD]) > 0) || (isset($objects[OBJECT_TYPE_CONTACT]) && count($objects[OBJECT_TYPE_CONTACT]) > 0)) {
    $result = write_cv_objects($cv_objects);
  }
}

// Queries for the Salesforce intern leads to be imported
function query_sf_objects($sf) {
  // Function requires an active Salesforce connection.
  if(!is_object($sf)) {
    return NULL;
  }  
  $query_results = array();
  $sf_object_ids = array();
  $object_types = get_object_types();
  
  foreach($object_types as $object_type) {
    $object_fields = get_object_fields($object_type);
    $soql_fields = implode(", ", $object_fields);

    $soql = build_soql_query($soql_fields, $object_type); 	
    try {
      $result = $sf->client->query($soql);
    }
    catch (Exception $e) {
      salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception in sf_intern_sync ' . $object_type . ' queries: ' . $e->getMessage(), array(), WATCHDOG_ALERT);
    }

    if(isset($result->records)) {
      if($result->size >= 1) {
        foreach($result->records as $index => $sf_object) {
          $sf_object_ids[] = $sf_object->Id;
        }
      }
      $cv_object_records = array();
      foreach($result->records as $key => $sf_object) {
        $cv_object = normalize_record($sf_object, $object_type);
        $cv_object = populate_record($cv_object, $object_type);
        $cv_object_records[$cv_object->Id] = $cv_object;
      }
      // Free up some memory by unsetting the result array.
      unset($result);
    }
    $query_results += array($object_type => $cv_object_records);
  }
  if(count($sf_object_ids) > 0) {
    $sf_object_ids_text = implode_quoted($sf_object_ids);
    $soql = "SELECT Id, ParentId, Name, Body FROM Attachment WHERE ParentId IN (" . $sf_object_ids_text . ")"; 
    try {
      $result_attachments = $sf->client->query($soql);
    }
    catch (Exception $e) {
      salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception in sf_intern_sync attachment queries: ' . $e->getMessage(), array(), WATCHDOG_ALERT);
    }
    if(isset($result_attachments->records)) { 
      $attachment_records = array();
      foreach($result_attachments->records as $key => $attachment) {
        $attachment_records[$attachment->ParentId] = $attachment;
      }
      // Free up some memory by unsetting the result array.
      unset($result_attachments);
      $query_results['Attachment'] = $attachment_records;
    }
  }
  return $query_results;
}

// Normalize a lead record to have all the fields.
function normalize_record($sf_object, $object_type) {
  // Exit early if the "bare minimum" of fields is not present.
  if(!is_object($sf_object) || empty($sf_object->Id)) {
    return NULL;
  }
  
  $object_fields = get_object_fields($object_type);
  foreach($object_fields as $cv_fieldname => $sf_fieldname) {
    $sf_object->{$cv_fieldname} = !empty($sf_object->{$sf_fieldname}) ? $sf_object->{$sf_fieldname} : '';
    // Remove the duplicate fields.
    if($sf_fieldname != 'Id') {
      unset($sf_object->{$sf_fieldname});
    }
  }
  
  return $sf_object;
}

// Populate the fields that are not directly in the SF object.
function populate_record($cv_object, $object_type) {
  // Exit early if the "bare minimum" of fields is not present.
  if(!is_object($cv_object) || empty($cv_object->Id)) {
    return NULL;
  }
  
  // Set the title field.
  $cv_object->title = $cv_object->applic_first_name . ' ' . $cv_object->applic_last_name;
  
  // Populate the picklist fields by tid.
  $picklist_field_info = get_picklist_field_info();
  foreach($picklist_field_info as $tid_field => $field_info) {
    $field_values = array();
    $tid_field_values = array();
    if($field_info['name'] == 'intern_type') {
      $field_values = explode(';', $cv_object->{$field_info['name']});
    }
    else {
      $field_values = array($cv_object->{$field_info['name']});
    }
    foreach($field_values as $key => $field_value) {
      $field_values[$key] = get_picklist_name_by_synonym($field_value);
{'fraction_non_alphanumeric': 0.14721858025847537, 'fraction_numerical': 0.005993631766248361, 'mean_word_length': 4.025411764705883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 78, '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': '1638462', 'n_tokens_mistral': 3651, 'n_tokens_neox': 3584, 'n_words': 737}