<?php

/**
 * @file
 *   Allows users to specify custom services for the Shorten URLs module.
 */

/**
 * Implements hook_menu().
 */
function shorten_cs_menu() {
  $items = array();
  $items['admin/config/services/shorten/custom'] = array(
    'title' => 'Custom services',
    'description' => 'Specify information about any service to expose it to Shorten URLs.',
    'page callback' => 'theme',
    'page arguments' => array('shorten_cs_admin'),
    'access arguments' => array('administer Shorten URLs custom services'),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/services/shorten/custom/edit/%shorten_cs'] = array(
    'title' => 'Edit custom service',
    'page callback' => 'shorten_cs_edit_form',
    'page arguments' => array(6),
    'access arguments' => array('administer Shorten URLs custom services'),
    'type' => MENU_CALLBACK,
    'file' => 'shorten_cs.admin.inc',
  );
  $items['admin/config/services/shorten/custom/delete/%shorten_cs'] = array(
    'title' => 'Custom services',
    'page callback' => 'shorten_cs_delete_form',
    'page arguments' => array(6),
    'access arguments' => array('administer Shorten URLs custom services'),
    'type' => MENU_CALLBACK,
    'file' => 'shorten_cs.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_shorten_service().
 */
function shorten_cs_shorten_service() {
  $result = db_query("SELECT * FROM {shorten_cs}")->fetchAll();
  $services = array();
  foreach ($result as $service) {
    $services[$service->name] = array(
      'custom' => $service->type == 'text' ? FALSE : $service->type,
      'url' => $service->url,
    );
    if ($service->type == 'xml') {
      $services[$service->name]['tag'] = $service->tag;
    }
    elseif ($service->type == 'json') {
      $services[$service->name]['json'] = $service->tag;
    }
  }
  return $services;
}

/**
 * Implements hook_theme().
 */
function shorten_cs_theme($existing, $type, $theme, $path) {
  return array(
    'shorten_cs_admin' => array(
      'arguments' => array(),
      'file' => 'shorten_cs.admin.inc',
    ),
  );
}

/**
 * Implements hook_perm().
 */
function shorten_cs_permission() {
  return array(
    // This permission allows tricking users into visiting a malicious site,
    // so it is designated as "restricted access"
    'administer Shorten URLs custom services' => array(
      'title' => t('Administer custom services'),
      'description' => t('Add and remove custom URL shortening services.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Loads a custom service object.
 */
function shorten_cs_load($sid) {
  return db_query("SELECT * FROM {shorten_cs} WHERE sid = :sid", array(':sid' => $sid))
    ->fetchObject();
}
