You are here

pmpapi_query_tools.module in Public Media Platform API Integration 7

Allows admins to build, save, and execute PMP queries

File

pmpapi_query_tools/pmpapi_query_tools.module
View source
<?php

/**
 * @file
 * Allows admins to build, save, and execute PMP queries
 */

/**
 * Implements hook_permission().
 */
function pmpapi_query_tools_permission() {
  return array(
    'administer PMP queries' => array(
      'title' => t('Administer PMP API queries'),
      'description' => t('Perform administration tasks for queries to the PMP API.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function pmpapi_query_tools_menu() {
  $items = array();
  $items['admin/config/services/pmp/queries'] = array(
    'title' => 'Queries',
    'description' => 'Save, edit and delete PMP queries.',
    'access arguments' => array(
      'administer PMP queries',
    ),
    'page callback' => 'pmpapi_query_tools_list_queries',
    'file' => 'pmpapi_query_tools.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/services/pmp/queries/add'] = array(
    'title' => 'Add query',
    'description' => 'Add a PMP query.',
    'access arguments' => array(
      'administer PMP queries',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pmpapi_query_tools_admin_config',
    ),
    'file' => 'pmpapi_query_tools.admin.inc',
    'type' => MENU_LOCAL_ACTION,
  );
  $items['admin/config/services/pmp/queries/edit/%'] = array(
    'title' => 'Edit query',
    'description' => 'Edit a PMP query.',
    'access arguments' => array(
      'administer PMP queries',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pmpapi_query_tools_admin_config',
      6,
    ),
    'file' => 'pmpapi_query_tools.admin.inc',
  );
  $items['admin/config/services/pmp/queries/delete/%'] = array(
    'title' => 'Delete query',
    'description' => 'Delete a PMP query.',
    'access arguments' => array(
      'administer PMP queries',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pmpapi_query_tools_remove_query_form',
      6,
    ),
    'file' => 'pmpapi_query_tools.admin.inc',
  );
  return $items;
}

/**
 * Gets data for a given query.
 *
 * @param string $query
 *   Name of a saved PMP query.
 *
 * @return array
 *   The query parameters for the query, or NULL, if the query does not exist.
 */
function pmpapi_query_tools_get_query($query) {
  $result = db_query('SELECT options FROM pmpapi_query_tools_queries WHERE name=:name', array(
    ':name' => $query,
  ))
    ->fetchField(0);
  if ($result) {
    return unserialize($result);
  }
}

/**
 * Gets data for all queries.
 *
 * @return array
 *   The query parameters for all the queries, or NULL, if there are are no queries.
 */
function pmpapi_query_tools_get_queries() {
  $queries = array();
  $results = db_select('pmpapi_query_tools_queries', 'pqtq')
    ->fields('pqtq', array(
    'name',
    'options',
  ))
    ->execute()
    ->fetchAllKeyed();
  if ($results) {
    $queries = array_map('unserialize', $results);
  }
  return $queries;
}

/**
 * Adds/updates query data.
 *
 * @param string $query
 *   Name of a PMP query.
 *
 * @param string $options
 *   New/initial query parameters to be saved.
 */
function pmpapi_query_tools_merge_query($query, $options) {
  db_merge('pmpapi_query_tools_queries')
    ->key(array(
    'name' => $query,
  ))
    ->fields(array(
    'options' => serialize($options),
  ))
    ->execute();
}

/**
 * Deletes all data for a given query.
 *
 * @param string $query
 *   Name of a saved PMP query.
 */
function pmpapi_query_tools_delete_query($query) {
  db_delete('pmpapi_query_tools_queries')
    ->condition('name', $query)
    ->execute();
  module_invoke_all('pmpapi_query_tools_query_delete', $query);
}

/**
 * Implements hook_cron().
 */
function pmpapi_query_tools_cron() {
  if (variable_get('pmpapi_pull_pull_active')) {
    pmpapi_query_tools_execute_queries();
  }
}

/**
 * Executes all saved queries.
 */
function pmpapi_query_tools_execute_queries() {
  foreach (pmpapi_query_tools_get_queries() as $query => $options) {

    // Prep options as PMP query parameters
    // See https://github.com/publicmediaplatform/pmpdocs/wiki/Querying-the-API
    // ; = boolean OR
    // : = boolean AND
    // creator
    $creators = array();
    foreach ($options['creator'] as $i => $creator) {
      if ($creator) {
        $creators[] = $i;
      }
    }
    if ($creators) {
      $options['creator'] = implode(';', $creators);
    }

    // profile
    $profiles = array();
    foreach ($options['profile'] as $i => $profile) {
      if ($profile) {
        $profiles[] = $i;
      }
    }
    if ($profiles) {
      $options['profile'] = implode(';', $profiles);
    }

    // tag
    if (!empty($options['tag'])) {
      $tags = explode(' ', $options['tag']);
      $options['tag'] = implode(';', $tags);
    }

    // clear out empty values
    foreach ($options as $i => $value) {
      if (!$value) {
        unset($options[$i]);
      }
    }
    drupal_alter('pmpapi_query_tools_query_pre_execute', $options, $query);
    $context = array(
      'pmpapi_query_tools_query' => $query,
    );
    $pmp = new PMPAPIDrupalPull(FALSE);
    $pmp
      ->pullDocs($options, $context);
  }
}

Functions

Namesort descending Description
pmpapi_query_tools_cron Implements hook_cron().
pmpapi_query_tools_delete_query Deletes all data for a given query.
pmpapi_query_tools_execute_queries Executes all saved queries.
pmpapi_query_tools_get_queries Gets data for all queries.
pmpapi_query_tools_get_query Gets data for a given query.
pmpapi_query_tools_menu Implements hook_menu().
pmpapi_query_tools_merge_query Adds/updates query data.
pmpapi_query_tools_permission Implements hook_permission().