You are here

location_addanother.module in Location 5.3

"Add location from node view" functionality. Split from main location.module in version 3.

File

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

/**
 * @file
 * "Add location from node view" functionality.
 * Split from main location.module in version 3.
 */

/**
 * Implementation of hook_nodeapi().
 */
function location_addanother_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'view':
      if (variable_get('location_addanother_' . $node->type, 0) && count($node->locations) < variable_get('location_maxnum_' . $node->type, 0) && !$teaser && node_access('update', $node)) {
        $node->content['location_addanother'] = array(
          '#type' => 'markup',
          '#value' => drupal_get_form('location_addanother_form', $node),
        );
      }
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function location_addanother_form_alter($form_id, &$form) {
  if ($form_id == 'node_type_form') {
    $type = $form['#node_type']->type;
    $form['location']['multiple_locations']['location_addanother'] = array(
      '#type' => 'checkbox',
      '#title' => t('Add another location from node view page'),
      '#default_value' => variable_get('location_addanother_' . $type, 0),
      '#description' => t('Display the "Add another location" option on the node view page.'),
    );
  }
}

/**
 * Form to display directly on a node view for "quick location add" functionality.
 */
function location_addanother_form(&$node) {
  $settings = variable_get('location_fields_' . $node->type, array());
  $form['location'] = array(
    '#type' => 'location_element',
    '#title' => t('Add another location'),
    '#default_value' => NULL,
    '#location_settings' => $settings,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['location']['nid'] = array(
    '#type' => 'hidden',
    // @@@ See if we can get away with value-ing this.
    '#value' => $node->nid,
  );
  $form['location']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add location'),
    '#weight' => 50,
  );
  return $form;
}

/**
 * Validation function for "add another location" form.
 */
function location_addanother_form_validate($form_id, &$form_values) {
  $location = $form_values['location'];
  $node = node_load($location['nid']);
  unset($location['nid']);
  if (!(variable_get('location_addanother_' . $node->type, 0) && count($node->locations) < variable_get('location_maxnum_' . $node->type, 0) && node_access('update', $node))) {
    form_set_error('location', t("You don't have permission to add a location to this node, or the node has the maximum number of locations already."));
  }
}

/**
 * Submission function for "add another location" form.
 */
function location_addanother_form_submit($form_id, &$form_values) {
  $location = $form_values['location'];
  $node = node_load($location['nid']);
  unset($location['nid']);

  // As this is always a new location, no need to delete anything first...
  $lid = location_save($location, TRUE);
  if ($lid) {
    db_query('INSERT INTO {location_instance} (nid, vid, lid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $lid);
    drupal_set_message(t('Your new location has been added. (See below)'));
  }
  return 'node/' . $node->nid;
}

/**
 * Implementation of hook_node_type().
 * Synchronize our settings.
 */
function location_addanother_node_type($op, $info) {
  switch ($op) {
    case 'delete':
      variable_del('location_addanother_' . $info->type);
      break;
    case 'update':
      if (!empty($info->old_type) && $info->old_type != $info->type) {
        $setting = variable_get('location_addanother_' . $info->old_type, 0);
        variable_del('location_addanother_' . $info->old_type);
        variable_set('location_addanother_' . $info->type, $setting);
      }
      break;
  }
}

Functions

Namesort descending Description
location_addanother_form Form to display directly on a node view for "quick location add" functionality.
location_addanother_form_alter Implementation of hook_form_alter().
location_addanother_form_submit Submission function for "add another location" form.
location_addanother_form_validate Validation function for "add another location" form.
location_addanother_nodeapi Implementation of hook_nodeapi().
location_addanother_node_type Implementation of hook_node_type(). Synchronize our settings.