Skip to content
Snippets Groups Projects
mm.module 1.92 KiB
<?php
/**
 * @file
 * Main module file for Media Magnet
 */

define('MM_VERSION', '7.x-1.0');

define('MM_API_URL_DEFAULT', 'https://mediamagnet.osu.edu/api/v1/');


/**
 * Implements hook_menu().
 */
function mm_menu() {
  $items = array();

  $items['admin/config/content/mm'] = array(
    'title' => 'Media Magnet',
    'description' => 'Media Magnet integration settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mm_settings_form'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/config/content/mm/core'] = array(
    'title' => 'Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  return $items;
}

/**
 * Menu callback: Media Magnet settings
 *
 * @see mm_menu()
 */
function mm_settings_form($form, &$form_state) {
  $form['mm_api_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Media Magnet API URL'),
    '#default_value' => variable_get('mm_api_url', MM_API_URL_DEFAULT),
    '#description' => t('The base URL of the Media Magnet API. Ex: https://mediamagnet.osu.edu/api/v1/'),
    '#required' => TRUE,
  );

  return system_settings_form($form);
}


/**
 * Builds a fully qualified Media Magnet API query URL
 *
 * @param string
 */
function mm_query_url($query) {
  return variable_get('mm_api_url', MM_API_URL_DEFAULT) . $query;
}

/**
 * Adds URL query parameters to a string
 *
 * @param string $url
 *   The URL fragment
 * @param array $params
 *   Array of URL parameters, keyed by parameter name
 *
 * @return string
 *   The URL with parameters added
 */
function _mm_add_query_params($url, $params=array()) {
  $params = array_filter($params);
  if (empty($params)) {
    return $url;
  }

  $has_query = parse_url($url, PHP_URL_QUERY);
  $separator = $has_query ? '&' : '?';
  $add_query = preg_replace('/(%5B|\[)[0-9]+(%5D|\])/s', '[]', http_build_query($params, '', '&'));

  return $url . $separator . $add_query;
}