<?php

/**
 * This module gives you control over the page title. It gives you the chance
 * to provide templates for how the title should be structured, and on node
 * pages, gives you the chance to specify the page title rather than defaulting
 * to the node title.
 */

function page_title_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      $output = t("Enhanced control over the page title (in the &lt;head> tag).");
      break;
    case 'admin/help#page_title':
      $output = t('<p>Adds an extra form element to node creation forms that allows you to specify the page title.</p>
        <p>Allows for better global configuration of the page title.</p>');
      break;
    case 'admin/settings/page_title':
      $output = t("<p>The default page title follows one of two patterns:
        <ol><li><em>page title</em> | <em>site name</em></li>
            <li><em>site name</em> | <em>site slogan</em></li></ol>
        <p>#1 is the common case where the visitor is viewing a specific page whereas #2 is used at least for the default front page (<em>node</em>).</p>
        <p>The page_title.module lets you change these defaults in two ways. First, you can adjust the templates below using the placeholders given. This will change the way the default page titles are created. Second, on every node creation form, you can have the option of specifying a title that is different than the title of the node. If a value is provided, this will be used to generate the <code>%page_title</code> placeholder. If left blank, <code>%page_title</code> will be the node's title, as is now the case.</p>");
      break;
  }
  return $output;
}

/**
 * Implementation of hook_perm().
 */
function page_title_perm() {
  return array('set page title');
}

function page_title_settings() {
  $form['page_title'] = array(
    '#type' => 'fieldset',
    '#title' => t('Page title templates'),
  );
  $form['page_title']['page_title_individual'] = array(
    '#type' => 'textfield',
    '#title' => t('Pattern for individual pages'),
    '#default_value' => variable_get('page_title_individual', '%page_title | %site_name'),
    '#size' => 128,
    '#maxlength' => 128,
    '#description' => t('Available placeholders: <code>%page_title</code>, <code>%site_name</code>, <code>%site_slogan</code>.'),
  );
  $form['page_title']['page_title_front'] = array(
    '#type' => 'textfield',
    '#title' => t('Pattern for front page'),
    '#default_value' => variable_get('page_title_front', '%site_name | %site_slogan'),
    '#size' => 128,
    '#maxlength' => 128,
    '#description' => t('Available placeholders: <code>%site_name</code>, <code>%site_slogan</code>.'),
  );
  return $form;
}

function page_title_form_alter($form_id, &$form) {
  if ($form['#id'] == 'node-form' && user_access('set page title')) {
    $form['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page title'),
      '#default_value' => $form['#node']->page_title,
      '#size' => 60,
      '#maxlength' => 127,
      '#weight' => -4,
    );
  }
}

function page_title_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'update':
      db_query("DELETE FROM {page_title} WHERE nid = %d", $node->nid);
      // fallthrough to insert intentional!
    case 'insert':
      if (strlen(trim($node->page_title)) > 0) {
        db_query("INSERT INTO {page_title} VALUES (%d, '%s')", $node->nid, $node->page_title);
      }
      break;

    case 'delete':
      db_query('DELETE FROM {page_title} WHERE nid = %d', $node->nid);
      break;

    case 'load':
      $object = new stdClass();
      $object->page_title = page_title_node_get_title($node->nid);
      return array('page_title' => $object->page_title);

    case 'view':
      if ($page) {
        page_title_set_title($node->page_title);
      }
      break;
  }
}


/**
 * Set the page title of the current page, for display on the page and in the title bar.
 */
function page_title_set_title($title = NULL) {
  static $stored_title;

  if (isset($title)) {
    $stored_title = $title;
  }
  return $stored_title;
}

// Public API (every module's gotta have one =)

/**
 * Gets the page title for a node id
 *
 * @param the node's id
 * @return the node's page title.
 */
function page_title_node_get_title($nid) {
  $row = db_fetch_object(db_query('SELECT page_title FROM {page_title} WHERE nid = %d', $nid));
  return $row->page_title;
}

/**
 * Does the logic to see what title should be send to the page template. Call this function from
 * the page hook of function _phptemplate_variables in template.php.
 */
function page_title_page_get_title() {
  $page_title_individual = variable_get('page_title_individual', '%page_title | %site_name');
  $page_title_front = variable_get('page_title_front', '%site_name | %site_slogan');

  // this is the normal title. For node pages it is $node->title
  $drupal_title = strip_tags(drupal_get_title());

  // this is the custom page title. For node pages it is $node->page_title.
  $page_title = (page_title_set_title()) ? strip_tags(page_title_set_title()) : $drupal_title;

  $site_name = variable_get('site_name', 'drupal');
  $site_slogan = variable_get('site_slogan', '');

  if (drupal_is_front_page() || !$drupal_title) {
    $title = t($page_title_front, array('%site_name' => $site_name, '%site_slogan' => $site_slogan));
  }
  else {
    $title = t($page_title_individual, array('%page_title' => $page_title, '%site_name' => $site_name, '%site_slogan' => $site_slogan));
  }
  
  return $title;
}
