<?php
// $Id: location_generate.module,v 1.4 2008/12/05 21:10:57 bdragon Exp $

/**
 * @file
 * Generate random locations.
 */

/**
 * Implementation of hook_nodeapi().
 */
function location_generate_nodeapi(&$node, $op) {
  if ($op == 'presave') {
    // Devel.module bulk node generation.
    if (isset($node->devel_generate)) {
      // Argh, we inherit a broken rand() on windows because devel_generate
      // calls array_rand()...
      // http://bugs.php.net/bug.php?id=45301
      srand((double)microtime()*1000000);
      $sources = array(LOCATION_LATLON_USER_SUBMITTED, LOCATION_LATLON_GEOCODED_APPROX, LOCATION_LATLON_GEOCODED_EXACT);
      $results = $node->devel_generate;
      if ($results['add_locations']) {
        $numlocs = rand(0, (int)variable_get('location_maxnum_'. $node->type, 0));
        $node->locations = array();
        for ($i = 0; $i < $numlocs; $i++) {
          $location = array(
            'lid' => NULL,
            //'name' => '',
            //'street' => '',
            //'additional' => '',
            //'city' => '',
            //'province' => '',
            //'postal_code' => '',
            //'country' => '',
            'latitude' => (float)((mt_rand(0, 120000) - 60000)/1000),
            'longitude' => (float)((mt_rand(0, 360000) - 180000)/1000),
            'source' => $sources[rand(0, 2)],
            'inhibit_geocode' => TRUE,
          );
          $settings = variable_get('location_settings_node_'. $node->type, array());
          $location['location_settings'] = $settings;
          $node->locations[] = $location;
        }
      }
    }
  }
}

function location_generate_form_devel_generate_content_form_alter(&$form, &$form_state) {
  $form['add_locations'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add locations to each node.'),
    '#description' => t('Added by location_generate.module'),
    '#default_value' => FALSE,
  );

  if (!isset($form['submit']['#weight'])) {
    $form['submit']['#weight'] = 1;
  }
}
