You are here

location_cck.module in Location 5.3

Defines location field type.

File

contrib/location_cck/location_cck.module
View source
<?php

/**
 * @file
 * Defines location field type.
 */

/**
 * Implementation of hook_field_info().
 */
function location_cck_field_info() {
  return array(
    'location' => array(
      'label' => t('Location'),
    ),
  );
}

/**
 * Implementation of hook_field_settings().
 */
function location_cck_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $settings = isset($field['location_settings']) ? $field['location_settings'] : FALSE;
      $form['location_settings'] = location_settings($settings);

      // Multiple is handled by CCK.
      unset($form['location_settings']['multiple']);
      return $form;
    case 'save':
      return array(
        'location_settings',
      );
    case 'database columns':
      return array(
        'lid' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => '0',
        ),
      );
    case 'arguments':

      /*      $arguments = content_views_field_arguments($field);
            $alias = "content: $field[field_name]";
            $argument = array(
              'handler' => 'text_views_argument_handler',
              'option' => 'string',
              'help' => t('Set the option to the number of initial characters to filter by. Leave empty for full term; use 1 for an A/B/C style glossary.'),
            );
            $arguments[$alias] = array_merge($arguments[$alias], $argument);
            return $arguments;*/
      return;
    case 'filters':

      /*
            $allowed_values = text_allowed_values($field);
             if (count($allowed_values)) {
               return array(
                 'default' => array(
                  'list' => $allowed_values,
                   'list-type' => 'list',
                   'operator' => 'views_handler_operator_andor',
                   'value-type' => 'array',
                  ),
                );
              }
            else {
              return array(
                'like' => array(
                  'operator' => 'views_handler_operator_like',
                  'handler' => 'views_handler_filter_like',
                ),
              );
            }*/
      break;
  }
}

/*

function location_cck_views_argument_handler($op, &$query, $argtype, $arg = '') {
  $name = explode(':', is_array($argtype) ? $argtype['type'] : $argtype);
  $field_name = trim($name[1]);
  $field      = content_fields($field_name);
  $db_info    = content_database_info($field);
  $value      = $db_info['columns']['value']['column'];
  $table      = 'node_data_'. $field['field_name'];

  switch($op) {
    case 'summary':
      $query->ensure_table($table);
      $query->add_field($value, $table);

      $length = intval($arg);
      $fieldinfo['field'] = $length <= 0 ? "$table.$value" : "LEFT($table.$value, $length)";
      $fieldinfo['fieldname'] = 'letter';
      return $fieldinfo;

    case 'sort':
      break;

    case 'filter':
      $query->ensure_table($table);
      $query->add_field($value, $table);

      $length = intval($argtype['options']);
      $where = $length <= 0 ? "$table.$value = '%s'" : "LEFT($table.$value, $length) = '%s'";
      $query->add_where($where, $arg);
      break;

    case 'link':
      return l(strtoupper($query->letter), "$arg/$query->letter");

    case 'title':
      return check_plain(strtoupper($query));
  }
}
*/

/**
 * Implementation of hook_field().
 */
function location_cck_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':

      /*
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
          $error_field = $field['field_name'] .']['. $delta .'][value';
          if ($item['value'] != '') {
            if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
              form_set_error($error_field, t('Illegal value for %name.', array('%name' => t($field['widget']['label']))));
            }
          }
        }
      }
      */
      break;
    case 'insert':
    case 'update':

      // Store instances of locations by field name and vid.
      $genid = 'cck:' . $field['field_name'] . ':' . $node->vid;
      location_save_locations($items, array(
        'genid' => $genid,
      ));

      // CCK automatically picks up the new lids and stores them in its own tables.
      break;

    /*
        case 'load':
          foreach ($items as $k => $v) {
            if ($v['lid']) {
              $items[$k] = location_load_location($v['lid']);
            }
          }
          break;
    */
    case 'delete':

      // Run through the revisions and clean up all applicable references.
      $result = db_query('SELECT vid FROM {node_revisions} WHERE nid = %d', $node->nid);
      while ($row = db_fetch_object($result)) {
        $genid = 'cck:' . $field['field_name'] . ':' . $row->vid;
        $locs = array();
        location_save_locations($locs, array(
          'genid' => $genid,
        ));
      }
      break;
    case 'delete revision':
      $genid = 'cck:' . $field['field_name'] . ':' . $node->vid;
      $locs = array();
      location_save_locations($locs, array(
        'genid' => $genid,
      ));
      break;
  }
}

/**
 * Implementation of hook_field_formatter_info().
 */
function location_cck_field_formatter_info() {
  return array(
    'default' => array(
      'label' => t('Default'),
      'field types' => array(
        'location',
      ),
    ),
  );
}

/**
 * Implementation of hook_field_formatter().
 *
 * The $node argument is necessary so that filter access can be checked on
 * node preview.
 */
function location_cck_field_formatter($field, $item, $formatter, $node) {
  $hide = array_keys(array_filter($field['location_settings']['display']['hide']));
  if (empty($item)) {
    return '';
  }
  $item = location_load_location($item['lid']);
  return theme('location', $item, $hide);
}

/**
 * Implementation of hook_widget_info().
 */
function location_cck_widget_info() {
  return array(
    'location' => array(
      'label' => t('Location Field'),
      'field types' => array(
        'location',
      ),
    ),
  );
}

/**
 * Implementation of hook_widget_settings().
 */
function location_cck_widget_settings($op, $widget) {
}

/**
 * Implementation of hook_widget().
 */
function location_cck_widget($op, &$node, $field, &$items) {
  switch ($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#description'] = t($field['widget']['description']);
        $delta = 0;
        foreach ($items as $data) {
          if ($data['lid']) {
            $form[$field['field_name']][$delta] = array(
              '#type' => 'location_element',
              '#title' => $delta == 0 ? t($field['widget']['label']) : '',
              '#default_value' => $data,
              '#required' => $delta == 0 ? $field['required'] : FALSE,
              '#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
              '#location_settings' => $field['location_settings'],
              '#weight' => $field['widget']['weight'],
            );
            $delta++;
          }
        }
        foreach (range($delta, $delta + 2) as $delta) {
          $form[$field['field_name']][$delta] = array(
            '#type' => 'location_element',
            '#title' => $delta == 0 ? t($field['widget']['label']) : '',
            '#default_value' => array(),
            '#required' => $delta == 0 ? $field['required'] : FALSE,
            '#weight' => $field['widget']['weight'],
            '#location_settings' => $field['location_settings'],
          );
        }
      }
      else {
        $form[$field['field_name']][0] = array(
          '#type' => 'location_element',
          '#title' => t($field['widget']['label']),
          '#default_value' => isset($items[0]) ? $items[0] : array(),
          '#required' => $field['required'],
          '#description' => t($field['widget']['description']),
          '#weight' => $field['widget']['weight'],
          '#location_settings' => $field['location_settings'],
        );
      }
      return $form;
    case 'process form values':

      // Don't save empty fields except the first value
      foreach ($items as $delta => $item) {

        //        if ($item['value'] == '' && $delta > 0) {
        //        unset($items[$delta]);
        //    }
      }
      break;
    case 'prepare form values':

      // Load locations for use in edit form.
      foreach ($items as $k => $item) {
        $items[$k] = location_load_location($item['lid']);
      }
      break;
  }
}

/**
 * Implementation of hook_locationapi().
 */
function location_cck_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  switch ($op) {
    case 'instance_links':
      foreach ($obj as $k => $v) {
        if (substr($v['genid'], 0, 4) == 'cck:') {
          $data = explode(':', $v['genid']);
          $obj[$k]['href'] = 'node/' . $data[2];
          $obj[$k]['title'] = t('CCK location');
          $obj[$k]['type'] = t('CCK location');
        }
      }
  }
}

Functions