function smart_ip_store_location_csv in Smart IP 7        
                          
                  
                        Same name and namespace in other branches
- 6.2 includes/smart_ip.utility.inc \smart_ip_store_location_csv()
 - 6 includes/smart_ip.utility.inc \smart_ip_store_location_csv()
 - 7.2 includes/smart_ip.utility.inc \smart_ip_store_location_csv()
 
 
1 string reference to 'smart_ip_store_location_csv'
  - smart_ip_update_db_batch in includes/smart_ip.utility.inc
 
  - Prepare a batch definition
 
 
File
 
   - includes/smart_ip.utility.inc, line 160
 
  - Utility routines to load the Smart IP database.
 
Code
function smart_ip_store_location_csv(&$context) {
  $last_cache = variable_get('smart_ip_store_location_csv_done', FALSE);
  $cache_intact = cache_get('smart_ip:' . $last_cache, 'cache_smart_ip');
  if ($cache_intact) {
    
    $context['finished'] = TRUE;
    $context['message'] = t('Using previous stored locations (recovery mode)');
    return;
  }
  variable_set('smart_ip_store_location_csv_done', FALSE);
  
  if (isset($context['results']['#abort'])) {
    return;
  }
  $fp = @fopen($context['results']['#location_csv_file'], 'r');
  if ($fp === FALSE) {
    
    $message = t('Opening CSV file %file failed.', array(
      '%file' => $context['results']['#location_csv_file'],
    ));
    $context['results']['#abort'] = $message;
    $context['message'] = $message;
    unset($context['results']['#location_csv_file']);
    variable_set('smart_ip_extract_zip_done', FALSE);
    variable_set('smart_ip_get_zip_done', FALSE);
  }
  else {
    if (!isset($context['sandbox']['#location_csv_pointer'])) {
      $location_csv_pointer = variable_get('smart_ip_location_csv_pointer', 0);
      if ($location_csv_pointer) {
        
        @fseek($fp, $location_csv_pointer);
      }
      else {
        
        cache_clear_all('smart_ip:', 'cache_smart_ip', TRUE);
        
        $fp_check = @fopen($context['results']['#location_csv_file'], 'r');
        @fseek($fp_check, -1, SEEK_END);
        variable_set('smart_ip_location_csv_last_pointer', @ftell($fp_check));
      }
    }
    else {
      @fseek($fp, $context['sandbox']['#location_csv_pointer']);
    }
    $data = @fgetcsv($fp);
    $context['sandbox']['#location_csv_pointer'] = @ftell($fp);
    if (feof($fp)) {
      fclose($fp);
      
      $context['finished'] = TRUE;
      $context['message'] = t('Processing %location done', array(
        '%location' => basename($context['results']['#location_csv_file']),
      ));
      unset($context['results']['#location_csv_file']);
      
      variable_set('smart_ip_store_location_csv_done', (int) variable_get('smart_ip_indicator_last_ip'));
      
      variable_set('smart_ip_indicator_last_ip', FALSE);
      variable_set('smart_ip_location_csv_pointer', 0);
      return;
    }
    else {
      $current_pointer = $context['sandbox']['#location_csv_pointer'];
      $last_pointer = variable_get('smart_ip_location_csv_last_pointer', 0);
      $estimated_progress = floor(100 * ($current_pointer / $last_pointer));
      
      $context['finished'] = $estimated_progress / 100;
      $context['message'] = t('Parsing %location at line number: @value of @end', array(
        '%location' => basename($context['results']['#location_csv_file']),
        '@value' => $current_pointer,
        '@end' => $last_pointer,
      ));
    }
    if (count($data) == 9 && is_numeric($data[0])) {
      $data_location = array(
        'country_code' => $data[1],
        'region' => $data[2],
        'city' => $data[3],
        'zip' => $data[4],
        'latitude' => $data[5],
        'longitude' => $data[6],
      );
      variable_set('smart_ip_location_csv_pointer', $context['sandbox']['#location_csv_pointer']);
      variable_set('smart_ip_indicator_last_ip', $data[0]);
      cache_set('smart_ip:' . $data[0], $data_location, 'cache_smart_ip');
    }
  }
}