<?php

/**
 * @defgroup pathauto Pathauto: Automatically generates aliases for content
 *
 * The Pathauto module automatically generates path aliases for various kinds of
 * content (nodes, categories, users) without requiring the user to manually
 * specify the path alias. This allows you to get aliases like
 * /category/my-node-title.html instead of /node/123. The aliases are based upon
 * a "pattern" system which the administrator can control.
 */

/**
 * @file
 * Main file for the Pathauto module, which automatically generates aliases for content.
 *
 * @ingroup pathauto
 */

/**
 * Implements hook_help().
 */
function pathauto_help($path, $arg) {
  switch ($path) {
    case 'admin/help#pathauto':
      $output = t('<p>Provides a mechanism for modules to automatically generate aliases for the content they manage.</p>
                  <h2>Settings</h2>
                  <p>The <strong>Maximum Alias Length</strong> and <strong>Maximum component length</strong> values
                  default to 100 and have a limit of 128 from pathauto. This length is limited by the length of the dst
                  column of the url_alias database table. The default database schema for this column is 128. If you
                  set a length that is equal to that of the one set in the dst column it will cause problems in situations
                  where the system needs to append additional words to the aliased URL. For example... URLs generated
                  for feeds will have "/feed" added to the end. You should enter a value that is the length of the dst
                  column minus the length of any strings that might get added to the end of the URL. The length of
                  strings that might get added to the end of your URLs depends on which modules you have enabled and
                  on your Pathauto settings. The recommended and default value is 100.</p>
                  <p><strong>Raw Tokens</strong> In Pathauto it is appropriate to use the -raw form of tokens. Paths are
                  sent through a filtering system which ensures that raw user content is filtered. Failure to use -raw
                  tokens can cause problems with the Pathauto punctuation filtering system.</p>');
      return $output;
  }
}

/**
 * Implements hook_perm().
 */
function pathauto_perm() {
  return array(
    'administer pathauto',
    'notify of path changes',
  );
}

/**
 * Implements hook_menu().
 */
function pathauto_menu() {
  $items['admin/build/path/pathauto'] = array(
    'title' => 'Automated alias settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pathauto_admin_settings'),
    'access arguments' => array('administer pathauto'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'file' => 'pathauto.admin.inc',
  );

  $items['admin/build/path/delete_bulk'] = array(
    'title' => 'Delete aliases',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pathauto_admin_delete'),
    'access arguments' => array('administer url aliases'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 30,
    'file' => 'pathauto.admin.inc',
  );

  return $items;
}

/**
 * Include all Pathauto include files.
 */
function _pathauto_include() {
  module_load_include('inc', 'pathauto');
  module_load_include('inc', 'pathauto', 'pathauto_node');
  module_load_include('inc', 'pathauto', 'pathauto_taxonomy');
  module_load_include('inc', 'pathauto', 'pathauto_user');
}

/**
 * Implements hook_token_list().
 */
function pathauto_token_list($type = 'all') {
  $tokens = array();
  if (module_exists('taxonomy')) {
    if ($type == 'taxonomy' || $type == 'all') {
      $tokens['taxonomy']['catpath'] = t('As [cat], but including its supercategories separated by /.');
      $tokens['taxonomy']['catpath-raw'] = t('As [cat-raw], but including its supercategories separated by /.');
      $tokens['taxonomy']['catalias'] = t('The URL alias of the taxonomy term.');
      $tokens['taxonomy']['catalias-raw'] = t('The URL alias of the taxonomy term.');
    }
    if ($type == 'node' || $type == 'all') {
      $tokens['node']['termpath'] = t('As [term], but including its supercategories separated by /.');
      $tokens['node']['termpath-raw'] = t('As [term-raw], but including its supercategories separated by /.');
      $tokens['node']['termalias'] = t('The URL alias of the taxonomy term.');
      $tokens['node']['termalias-raw'] = t('The URL alias of the taxonomy term.');
    }
  }
  if (module_exists('book')) {
    if ($type == 'node' || $type == 'all') {
      $tokens['node']['bookpathalias'] = t('The URL alias of the parent book of the node.');
      $tokens['node']['bookpathalias-raw'] = t('The URL alias of the parent book of the node.');
    }
  }
  return $tokens;
}

/**
 * Implements hook_token_values().
 */
function pathauto_token_values($type, $object = NULL, $options = array(), $label = NULL) {
  $values = array();

  switch ($type) {
    case 'node':
      // Token [bookpathalias].
      if (module_exists('book')) {
        $values['bookpathalias'] = '';
        $values['bookpathalias-raw'] = '';
        if (!empty($object->book['plid']) && $parent = book_link_load($object->book['plid'])) {
          $values['bookpathalias-raw'] = drupal_get_path_alias($parent['href']);
          $values['bookpathalias']     = check_plain($values['bookpathalias-raw']);
        }
      }

      // Tokens [termpath], [termpath-raw], and [termalias].
      if (module_exists('taxonomy')) {
        // Get the lowest-weighted term from the lowest-weighted vocabulary.
        // This query is copied from @taxonomy_node_get_terms()
        $term = db_fetch_object(db_query_range('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.vid = %d ORDER BY v.weight, t.weight, t.name', $object->vid, 0, 1));
        if ($term) {
          $values = array_merge($values, pathauto_token_values('taxonomy', $term, $options, 'term'));
        }
        else {
          $values['termpath'] = $values['termpath-raw'] = $values['termalias'] = '';
        }
      }
      break;

    case 'taxonomy':
      // In the realm of nodes these are 'terms', in the realm of taxonomy, 'cats'.
      if (!isset($label)) {
        $label = 'cat';
      }

      $values[$label . 'path'] = '';
      $values[$label . 'path-raw'] = '';
      $values[$label . 'alias'] = '';
      $values[$label . 'alias-raw'] = '';

      // Tokens [catpath] and [catpath-raw].
      if (isset($object->tid)) {
        $parents = taxonomy_get_parents_all($object->tid);
        $catpath = $catpath_raw = array();
        foreach ($parents as $parent) {
          array_unshift($catpath, check_plain($parent->name));
          array_unshift($catpath_raw, $parent->name);
        }
        $values[$label . 'path'] = !empty($options['pathauto']) ? $catpath : implode('/', $catpath);
        $values[$label . 'path-raw'] = !empty($options['pathauto']) ? $catpath_raw : implode('/', $catpath_raw);

        // Token [catalias-raw] and [catalias].
        $values[$label . 'alias-raw'] = drupal_get_path_alias(taxonomy_term_path($object));
        $values[$label . 'alias']     = check_plain($values[$label . 'alias-raw']);
      }
      break;
  }

  return $values;
}

/**
 * Implementation of hook_path_alias_types().
 *
 * Used primarily by the bulk delete form.
 */
function pathauto_path_alias_types() {
  $objects['user/'] = t('Users');
  $objects['node/'] = t('Content');
  if (module_exists('blog')) {
    $objects['blog/'] = t('User blogs');
  }
  if (module_exists('taxonomy')) {
    $objects['taxonomy/term/'] = t('Taxonomy terms');
  }
  if (module_exists('forum')) {
    $objects['forum/'] = t('Forums');
  }
  if (module_exists('contact')) {
    $objects['user/%/contact'] = t('User contact forms');
  }
  if (module_exists('tracker')) {
    $objects['user/%/track'] = t('User trackers');
  }
  return $objects;
}

/**
 * Return the proper SQL to perform cross-db and field-type concatenation.
 *
 * @return
 *   A string of SQL with the concatenation.
 */
function _pathauto_sql_concat() {
  $args = func_get_args();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      return 'CONCAT(' . implode(', ', $args) . ')';
    default:
      // The ANSI standard of concatentation uses the double-pipe.
      return '(' . implode(' || ', $args) . ')';
  }
}

/**
 * Implements hook_field_attach_rename_bundle().
 *
 * Respond to machine name changes for pattern variables.
 */
function pathauto_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  $result = db_query("SELECT name FROM {variable} WHERE name LIKE '%s%%'", "pathauto_{$entity_type}_{$bundle_old}_");
  while ($variable = db_result($result)) {
    $value = variable_get($variable, '');
    variable_del($variable);
    $variable = strtr($variable, array("{$entity_type}_{$bundle_old}" => "{$entity_type}_{$bundle_new}"));
    variable_set($variable, $value);
  }
}

/**
 * Implements hook_field_attach_delete_bundle().
 *
 * Respond to sub-types being deleted, their patterns can be removed.
 */
function pathauto_field_attach_delete_bundle($entity_type, $bundle) {
  $result = db_query("SELECT name FROM {variable} WHERE name LIKE '%s%%'", "pathauto_{$entity_type}_{$bundle}_");
  while ($variable = db_result($result)) {
    variable_del($variable);
  }
}

//==============================================================================
// Some node related functions.

/**
 * Implements hook_nodeapi().
 */
function pathauto_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'presave':
      // About to be saved (before insert/update)
      if (!empty($node->pathauto_perform_alias) && isset($node->old_alias)
          && $node->path == '' && $node->old_alias != '') {
        /**
         * There was an old alias, but when pathauto_perform_alias was checked
         * the javascript disabled the textbox which led to an empty value being
         * submitted. Restoring the old path-value here prevents the Path module
         * from deleting any old alias before Pathauto gets control.
         */
        $node->path = $node->old_alias;
      }
      break;
    case 'insert':
    case 'update':
      // Skip processing if the user has disabled pathauto for the node.
      if (isset($node->pathauto_perform_alias) && empty($node->pathauto_perform_alias)) {
        return;
      }

      _pathauto_include();
      // Get the specific pattern or the default
      if (variable_get('language_content_type_'. $node->type, 0)) {
        $pattern = trim(variable_get('pathauto_node_'. $node->type .'_'. $node->language .'_pattern', FALSE));
      }
      if (empty($pattern)) {
        $pattern = trim(variable_get('pathauto_node_'. $node->type .'_pattern', FALSE));
        if (empty($pattern)) {
          $pattern = trim(variable_get('pathauto_node_pattern', FALSE));
        }
      }
      // Only do work if there's a pattern
      if ($pattern) {
        $placeholders = pathauto_get_placeholders('node', $node);
        $src = "node/$node->nid";
        if ($alias = pathauto_create_alias('node', $op, $placeholders, $src, $node->nid, $node->type, $node->language)) {
          $node->path = $alias;
        }
      }
      break;
    case 'delete':
      path_set_alias('node/'. $node->nid);
      path_set_alias('node/'. $node->nid .'/feed');
      break;
  }
}

/**
 * Implements hook_node_type().
 */
function pathauto_node_type($op, $info) {
  switch ($op) {
    case 'update':
      if (!empty($info->old_type) && $info->old_type != $info->type) {
        pathauto_field_attach_rename_bundle('node', $info->old_type, $info->type);
      }
      break;
    case 'delete':
      pathauto_field_attach_delete_bundle('node', $info->type);
      break;
  }
}

/**
 * Implements hook_form_alter().
 *
 * This allows alias creators to override Pathauto and specify their
 * own aliases (Pathauto will be invisible to other users). Inserted
 * into the path module's fieldset in the node form.
 */
function pathauto_form_alter(&$form, $form_state, $form_id) {
  // Process only node forms.
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $node = $form['#node'];
    $pattern = FALSE;

    // Find if there is an automatic alias pattern for this node type.
    if (isset($form['language'])) {
      $language = isset($form['language']['#value']) ? $form['language']['#value'] : $form['language']['#default_value'];
      $pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_'. $language .'_pattern', ''));
    }
    if (!$pattern) {
      $pattern = trim(variable_get('pathauto_node_'. $form['type']['#value'] .'_pattern', ''));
      if (!$pattern) {
        $pattern = trim(variable_get('pathauto_node_pattern', ''));
      }
    }

    // If there is a pattern, show the automatic alias checkbox.
    if ($pattern) {
      if (!isset($node->pathauto_perform_alias)) {
        if (!empty($node->nid)) {
          // If this is not a new node, compare it's current alias to the
          // alias that would be genereted by pathauto. If they are the same,
          // then keep the automatic alias enabled.
          _pathauto_include();
          $placeholders = pathauto_get_placeholders('node', $node);
          $pathauto_alias = pathauto_create_alias('node', 'return', $placeholders, "node/{$node->nid}", $node->nid, $node->type, $node->language);
          $node->pathauto_perform_alias = isset($node->path) && $node->path == $pathauto_alias;
        }
        else {
          // If this is a new node, enable the automatic alias.
          $node->pathauto_perform_alias = TRUE;
        }
      }

      // Add JavaScript that will disable the path textfield when the automatic
      // alias checkbox is checked.
      drupal_add_js(drupal_get_path('module', 'pathauto') .'/pathauto.js');

      // Override path.module's vertical tabs summary.
      $form['path']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'pathauto') . '/pathauto.js';

      $form['path']['pathauto_perform_alias'] = array(
        '#type' => 'checkbox',
        '#title' => t('Automatic alias'),
        '#default_value' => $node->pathauto_perform_alias,
        '#description' => t('An alias will be generated for you. If you wish to create your own alias below, uncheck this option.'),
        '#weight' => -1,
      );

      if (user_access('administer pathauto')) {
        $form['path']['pathauto_perform_alias']['#description'] .= ' '. t('To control the format of the generated aliases, see the <a href="@pathauto">automated alias settings</a>.', array('@pathauto' => url('admin/build/path/pathauto')));
      }

      if ($node->pathauto_perform_alias && !empty($node->old_alias) && empty($node->path)) {
        $form['path']['path']['#default_value'] = $node->old_alias;
        $node->path = $node->old_alias;
      }

      // For Pathauto to remember the old alias and prevent the Path-module from deleteing it when Pathauto wants to preserve it
      if (isset($node->path)) {
        $form['path']['old_alias'] = array(
          '#type' => 'value',
          '#value' => $node->path,
        );
      }
    }
  }
}

/**
 * Implements hook_node_operations().
 */
function pathauto_node_operations() {
  $operations['pathauto_update_alias'] = array(
    'label' => t('Update URL alias'),
    'callback' => 'pathauto_node_update_alias_multiple',
    'callback arguments' => array('bulkupdate', TRUE),
  );
  return $operations;
}

/**
 * Update the URL aliases for an individual node.
 *
 * @param $node
 *   A node object.
 * @param $op
 *   Operation being performed on the node ('insert', 'update' or 'bulkupdate').
 */
function pathauto_node_update_alias($node, $op) {
  module_load_include('inc', 'pathauto');
  $placeholders = pathauto_get_placeholders('node', $node);
  if ($alias = pathauto_create_alias('node', $op, $placeholders, "node/{$node->nid}", $node->nid, $node->type, $node->language)) {
    $node->path = $alias;
  }
}

/**
 * Update the URL aliases for multiple nodes.
 *
 * @param $nids
 *   An array of node IDs.
 * @param $op
 *   Operation being performed on the nodes ('insert', 'update' or
 *   'bulkupdate').
 * @param $message
 *   A boolean if TRUE will display a message about how many nodes were
 *   updated.
 */
function pathauto_node_update_alias_multiple($nids, $op, $message = FALSE) {
  foreach ($nids as $nid) {
    if ($node = node_load($nid, NULL, TRUE)) {
      pathauto_node_update_alias($node, $op);
    }
  }
  if ($message) {
    drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
  }
}

/**
 * Wrapper function backwards compatibility. Should be avoided.
 *
 * @param $nodes
 *   An array of node IDs.
 *
 * @see pathauto_node_update_alias_multiple().
 */
function pathauto_node_operations_update($nodes) {
  return pathauto_node_update_alias_multiple($nodes, 'bulkupdate');
}

//==============================================================================
// Taxonomy related functions.

/**
 * Implements hook_taxonomy().
 */
function pathauto_taxonomy($op, $type, $object = NULL) {
  switch ($type) {
    case 'term':
      switch ($op) {
        case 'insert':
        case 'update':
          $term = (object) $object;

          // Skip processing if the user has disabled pathauto for the term.
          if (isset($term->pathauto_perform_alias) && empty($term->pathauto_perform_alias)) {
            return;
          }

          // Clear the taxonomy term's static cache.
          if ($op == 'update') {
            taxonomy_get_term($term->tid, TRUE);
          }

          // Use the category info to automatically create an alias
          _pathauto_include();
          if ($term->name) {
            $count = _taxonomy_pathauto_alias($term, $op);
          }

          // For all children generate new alias (important if [catpath] used)
          foreach (taxonomy_get_tree($term->vid, $term->tid) as $subcategory) {
            $count = _taxonomy_pathauto_alias($subcategory, $op);
          }

          break;

        case 'delete':
          // If the category is deleted, remove the path aliases
          $term = (object) $object;
          path_set_alias('taxonomy/term/'. $term->tid);
          path_set_alias(taxonomy_term_path($term));
          path_set_alias('forum/'. $term->tid);
          path_set_alias('taxonomy/term/'. $term->tid .'/0/feed');
          break;
      }
      break;

    case 'vocabulary':
      $vocabulary = (object) $object;
      switch ($op) {
        case 'delete':
          pathauto_field_attach_delete_bundle('taxonomy', $vocabulary->vid);
          break;
      }
      break;
  }
}

//==============================================================================
// User related functions.

/**
 * Implements hook_user().
 */
function pathauto_user($op, &$edit, &$user, $category = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      // Build the user object.
      $pathauto_user = (object) array_merge((array) $user, $edit);

      // Skip processing if the user has disabled pathauto for the account.
      if (isset($pathauto_user->pathauto_perform_alias) && empty($pathauto_user->pathauto_perform_alias)) {
        return;
      }

      // Use the username to automatically create an alias
      _pathauto_include();
      if ($user->name) {
        $placeholders = pathauto_get_placeholders('user', $pathauto_user);
        $src = 'user/'. $user->uid;
        $alias = pathauto_create_alias('user', $op, $placeholders, $src, $user->uid);

        if (module_exists('blog')) {
          $new_user = drupal_clone($user);
          if ($category == 'account') {
            $new_user->roles = isset($edit['roles']) ? $edit['roles'] : array();
            $new_user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user'; // Add this back
          }
          if (node_access('create', 'blog', $new_user)) {
            $src = 'blog/'. $user->uid;
            $alias = pathauto_create_alias('blog', $op, $placeholders, $src, $user->uid);
          }
          else {
            path_set_alias('blog/'. $user->uid);
            path_set_alias('blog/'. $user->uid .'/feed');
          }
        }
        if (module_exists('tracker')) {
          $src = 'user/'. $user->uid .'/track';
          $alias = pathauto_create_alias('tracker', $op, $placeholders, $src, $user->uid);
        }
        if (module_exists('contact')) {
          $src = 'user/'. $user->uid .'/contact';
          $alias = pathauto_create_alias('contact', $op, $placeholders, $src, $user->uid);
        }
      }
      break;
    case 'delete':
      // If the user is deleted, remove the path aliases
      $user = (object) $user;
      path_set_alias('user/'. $user->uid);

      // They may have enabled these modules and/or feeds when the user was created, so let's try to delete all of them
      path_set_alias('blog/'. $user->uid);
      path_set_alias('blog/'. $user->uid .'/feed');
      path_set_alias('user/'. $user->uid .'/track');
      path_set_alias('user/'. $user->uid .'/track/feed');
      path_set_alias('user/'. $user->uid .'/contact');
      break;
  }
}

/**
 * Implements hook_user_operations().
 */
function pathauto_user_operations() {
  $operations['pathauto_update_alias'] = array(
    'label' => t('Update URL alias'),
    'callback' => 'pathauto_user_update_alias_multiple',
    'callback arguments' => array('bulkupdate', TRUE),
  );
  return $operations;
}

/**
 * Update the URL aliases for an individual user account.
 *
 * @param $account
 *   A user account object.
 * @param $op
 *   Operation being performed on the account ('insert', 'update' or
 *   'bulkupdate').
 *
 * @todo Remove support for any sub-path aliases.
 */
function pathauto_user_update_alias($account, $op) {
  module_load_include('inc', 'pathauto');
  $placeholders = pathauto_get_placeholders('user', $account);
  pathauto_create_alias('user', $op, $placeholders, "user/{$account->uid}", $account->uid);

  if (module_exists('blog')) {
    if (node_access('create', 'blog', $account)) {
      pathauto_create_alias('blog', $op, $placeholders, "blog/{$account->uid}", $account->uid);
    }
    else {
      path_set_alias('blog/'. $user->uid);
      path_set_alias('blog/'. $user->uid .'/feed');
    }
  }
  if (module_exists('tracker')) {
    $alias = pathauto_create_alias('tracker', $op, $placeholders, "user/{$account->uid}/track", $user->uid);
  }
  if (module_exists('contact')) {
    $alias = pathauto_create_alias('contact', $op, $placeholders, "user/{$account->uid}/contact", $user->uid);
  }
}

/**
 * Update the URL aliases for multiple user accounts.
 *
 * @param $uids
 *   An array of user account IDs.
 * @param $op
 *   Operation being performed on the accounts ('insert', 'update' or
 *   'bulkupdate').
 * @param $message
 *   A boolean if TRUE will display a message about how many accounts were
 *   updated.
 */
function pathauto_user_update_alias_multiple($uids, $op, $message = FALSE) {
  foreach ($uids as $uid) {
    if ($account = user_load($uid)) {
      pathauto_user_update_alias($account, $op);
    }
  }
  if ($message) {
    drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.'));
  }
}
