You are here

shurly_service.module in ShURLy 6

Link ShURLy functionalities to services module. @todo

  • move the block into the user edit form

File

shurly_service/shurly_service.module
View source
<?php

/**
 * @file
 *  Link ShURLy functionalities to services module.
 * @todo
 *  - move the block into the user edit form
 *  
 */

/**
 * Implementation of hook_perm().
 */
function shurly_service_perm() {
  $perms = array(
    'administer short URL services',
    'Expand short URLs',
    'Request custom short URL',
  );
  return $perms;
}

/**
 * Implements hook_menu().
 */
function shurly_service_menu() {
  $items = array();
  $items['shurly/api/shorten'] = array(
    'file' => 'shurly_service.inc',
    'page callback' => 'shurly_service_shorten',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['shurly/api/expand'] = array(
    'file' => 'shurly_service.inc',
    'page callback' => 'shurly_service_expand',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['shurly/api/key'] = array(
    'file' => 'shurly_service.inc',
    'page callback' => 'shurly_service_get_key',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_block().
 */
function shurly_service_block($op = 'list', $delta = 0, $edit = array()) {

  // create a block to add URL
  switch ($op) {
    case 'list':
      $blocks['api_key'] = array(
        'info' => t('Short URL API key'),
      );
      return $blocks;
    case 'view':
      global $user;

      // anon user can't get API key, also check permissions
      if ($delta == 'api_key' && $user->uid > 0 && user_access('Create short URLs')) {
        $block = array(
          'subject' => t('API key'),
          'content' => drupal_get_form('shurly_api_key_form'),
        );
        return $block;
      }
      break;
  }
}

/**
 * Form callback; Display a form with a textfield containing a user's API key.
 */
function shurly_api_key_form($form_state) {
  global $user;
  module_load_include('inc', 'shurly_service', 'shurly_api_keys');
  $key = shurly_get_api_key($user->uid);
  $form = array();
  if ($key) {
    $form['key'] = array(
      '#type' => 'textfield',
      '#title' => t('API key'),
      '#default_value' => $key,
      '#description' => t('You can provide this key to 3rd party apps rather than sharing your password.'),
    );
    $form['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset'),
      '#suffix' => t('If a 3rd party is misusing your API key, you can generate a new one.'),
    );
  }
  else {
    $form['new'] = array(
      '#type' => 'submit',
      '#value' => t('Create'),
      '#suffix' => t('Create an API key to use with 3rd party applications.'),
    );
  }
  return $form;
}

/**
 * Submit handler for shurly_api_key_form(). Regenerate a user API key.
 */
function shurly_api_key_form_submit($form, &$form_state) {

  // we don't need to do anything with the submitted form...
  // just generate a new API key
  global $user;
  shurly_generate_new_api_key($user->uid);
}

/**
 * Implementation of hook_views_api.
 * Notifies the Views module that we're compatible with a particular API revision.
 */
function shurly_service_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'shurly_service') . '/views',
  );
}

Functions

Namesort descending Description
shurly_api_key_form Form callback; Display a form with a textfield containing a user's API key.
shurly_api_key_form_submit Submit handler for shurly_api_key_form(). Regenerate a user API key.
shurly_service_block Implements hook_block().
shurly_service_menu Implements hook_menu().
shurly_service_perm Implementation of hook_perm().
shurly_service_views_api Implementation of hook_views_api. Notifies the Views module that we're compatible with a particular API revision.