<?php

/**
 * @file
 *   Records shortened URLs.
 */

/**
 * Implements hook_menu().
 */
function record_shorten_menu() {
  $items = array();
  $items['admin/reports/shorten'] = array(
    'title' => 'Shortened URLs',
    'description' => 'Lists shortened URLs.',
    'page callback' => 'theme',
    'page arguments' => array('record_shorten_records'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function record_shorten_theme($existing, $type, $theme, $path) {
  return array(
    'record_shorten_records' => array(
      'variables' => array(),
    ),
  );
}

/**
 * Implements hook_shorten_create().
 */
function record_shorten_shorten_create($old, $new, $service) {
  $array = array(
    'original' => $old,
    'short' => $new,
    'service' => $service,
    'uid' => $GLOBALS['user']->uid,
    'hostname' => ip_address(),
    'created' => REQUEST_TIME,
  );
  drupal_write_record('record_shorten', $array);
}

/**
 * Builds a list of shortened URLs.
 */
function theme_record_shorten_records() {
  $total = db_query("SELECT COUNT(sid) FROM {record_shorten}")->fetchField();
  $output = '<p>' . format_plural($total, '1 shortened URL has been recorded.', '@count shortened URLs have been recorded.');
  $output .= record_shorten_records_table();
  $output .= '<br />';
  $form = drupal_get_form('record_shorten_clear_all');
  $output .= drupal_render($form);
  return $output;
}

/**
 * Clear all records form.
 */
function record_shorten_clear_all($form, $form_state) {
  $form['warning'] = array(
    '#markup' => '<p><strong>' . t('Warning: there is no confirmation page. Cleared records are permanently deleted.') . '</strong></p>',
  );
  $form['note'] = array(
    '#markup' => '<p>' . t('Note: clearing records does not clear the Shorten URLs cache.') . ' ' .
      t('Also, URLs already in the cache are not recorded again.') . '</p>',
  );
  $form['clear'] = array(
    '#type' => 'submit',
    '#value' => t('Clear all records'),
  );
  return $form;
}

/**
 * Submit callback for clear all records form.
 */
function record_shorten_clear_all_submit($form, &$form_state) {
  db_query("TRUNCATE TABLE {record_shorten}");
}

/**
 * Builds a list of shortened URLs.
 */
function record_shorten_records_table() {
  if (module_exists('views')) {
    return views_embed_view('record_shorten', 'default');
  }
  $header = array(t('Original'), t('Short'), t('Service'));
  $rows = array();
  // SELECT original, short, service FROM {record_shorten} ORDER BY sid DESC
  $result = db_select('record_shorten', 'rs')
    ->extend('PagerDefault')
    ->limit(10)
    ->fields('rs', array('original', 'short', 'service'))
    ->orderBy('rs.sid', 'DESC')
    ->execute();
  foreach ($result as $row) {
    // Sigh... DBTNG doesn't have a ->fetchAsNonAssocArray()
    $rows[] = array(check_plain($row->original), check_plain($row->short), check_plain($row->service));
  }
  $output = theme('table', array('header' => $header, 'rows' => $rows));
  // Oddly, theme_pager($vars) has no defaults for $vars.
  $output .= theme('pager', array('tags' => array(), 'element' => 0, 'parameters' => array(), 'quantity' => 9));
  return $output;
}

/**
 * Implements hook_views_api().
 */
function record_shorten_views_api() {
  return array('api' => 3);
}
