You are here

record_shorten.module in Shorten URLs 6

Same filename and directory in other branches
  1. 7.2 record_shorten.module
  2. 7 record_shorten.module

Records shortened URLs.

File

record_shorten.module
View source
<?php

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

/**
 * Implementation of 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;
}

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

/**
 * Implementation of 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' => time(),
  );
  drupal_write_record('record_shorten', $array);
}

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

/**
 * Clear all records form.
 */
function record_shorten_clear_all(&$form_state) {
  $form = array();
  $form['warning'] = array(
    '#value' => '<p><strong>' . t('Warning: there is no confirmation page. Cleared records are permanently deleted.') . '</strong></p>',
  );
  $form['note'] = array(
    '#value' => '<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();
  $result = pager_query("SELECT original, short, service FROM {record_shorten} ORDER BY sid DESC");
  while ($row = db_fetch_object($result)) {
    $rows[] = array(
      check_plain($row->original),
      check_plain($row->short),
      check_plain($row->service),
    );
  }
  $output = theme('table', $header, $rows);
  $output .= theme('pager');
  return $output;
}

/**
 * Implementation of hook_views_api().
 */
function record_shorten_views_api() {
  return array(
    'api' => 2,
  );
}

Functions

Namesort descending Description
record_shorten_clear_all Clear all records form.
record_shorten_clear_all_submit Submit callback for clear all records form.
record_shorten_menu Implementation of hook_menu().
record_shorten_records_table Builds a list of shortened URLs.
record_shorten_shorten_create Implementation of hook_shorten_create().
record_shorten_theme Implementation of hook_theme().
record_shorten_views_api Implementation of hook_views_api().
theme_record_shorten_records Builds a list of shortened URLs.