You are here

pmpapi_push.module in Public Media Platform API Integration 7

Maps drupal entities to MPM profiles, and pushes them to the PMP API.

File

pmpapi_push/pmpapi_push.module
View source
<?php

/**
 * @file
 * Maps drupal entities to MPM profiles, and pushes them to the PMP API.
 */

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

/**
 * Implements hook_menu().
 */
function pmpapi_push_menu() {
  $items = array();
  $items['admin/config/services/pmp/push'] = array(
    'title' => 'Push settings',
    'description' => 'Select which entities (and mapped fields) to push to PMP.',
    'access arguments' => array(
      'administer PMP push',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pmpapi_push_admin_config',
    ),
    'file' => 'pmpapi_push.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Determines if an entity should be pushed to the PMP API.
 *
 * @param $entity object
 *   The drupal entity to be evaluated for push-worthiness
 * @param $type string
 *   The type of entity being evaluated
 *
 * @return boolean
 *   TRUE if entity can be pushed to the PMP API, FALSE if not.
 */
function pmpapi_push_entity_ok_to_push($entity, $type) {
  $uname = pmpapi_push_get_uname($entity, $type);

  // Is global push flag set active?
  $kill_switch = variable_get('pmpapi_push_push_active');

  // Is bundle mapped to a PMP profile?
  $mapped_profile = variable_get('pmpapi_push_' . $uname . '_profile');

  // If an entity is "unpublished" (status = 0), then do not push
  // NB: status property for file entities does not mean publish/unpublish, so we
  // skip $status_on check for these.
  $status_on = !isset($entity->status) || $entity->status || $type == 'file';

  // Absence of a push flag, or a truthy push flag, means "push the entity"
  $push_flag = TRUE;
  $push_flag_field = variable_get('pmpapi_push_' . $uname . '_push_flag');
  if ($push_flag_field) {
    $language = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
    $field_items = field_get_items($type, $entity, $push_flag_field, $language);
    if (!empty($field_items)) {
      foreach ($field_items as $field_item) {
        if (!$field_item['value']) {

          // 1+ falsey value in the field means "don't push"
          $push_flag = FALSE;
          break;
        }
      }
    }
    else {
      $push_flag = FALSE;
    }
  }

  // Modules can implement their own tests on an entity
  $hooks_ok = module_invoke_all('pmpapi_push_entity_ok_to_push', $entity, $type);
  return $kill_switch && $mapped_profile && $status_on && $push_flag && !in_array(FALSE, $hooks_ok, TRUE);
}

/**
 * Gets the field->attribute mapping for an entity.
 *
 * @param $entity object
 *   A drupal entity
 * @param $type string
 *   The type of entity
 *
 * @return array
 *   The field->attribute mapping for an entity.
 */
function pmpapi_push_get_mapping_for_entity($entity, $type) {
  $uname = pmpapi_push_get_uname($entity, $type);
  $mapped_profile = variable_get('pmpapi_push_' . $uname . '_profile');
  $mapping = variable_get('pmpapi_push_mapping_' . $uname . '_' . $mapped_profile);
  return $mapping;
}

/**
 * Pushes an entity to the PMP API.
 *
 * @param object $entity
 *   Any drupal entity object
 * @param string $type
 *   The type of entity (node, file, etc.)
 * @param $profile string
 *   The type of PMP profile that will be created
 * @param array $mapping
 *   A mapping of entity fields to PMP profile attributes (etc.)
 */
function pmpapi_push_push_entity_to_pmp($entity, $type, $profile, $mapping) {
  $pmp = new PMPAPIDrupalPush();
  if ($pmp
    ->pushEntity($entity, $type, $profile, $mapping)) {
    $wrapper = entity_metadata_wrapper($type, $entity);
    $bundle_name = $wrapper
      ->getBundle();
    $label = $wrapper
      ->label();
    drupal_set_message(t('@bundle_name %label was successfully pushed to the PMP API.', array(
      '@bundle_name' => $bundle_name,
      '%label' => $label,
    )));
  }
}

/**
 * Implements hook_entity_presave().
 */
function pmpapi_push_entity_presave($entity, $type) {
  if (pmpapi_push_entity_ok_to_push($entity, $type) && empty($entity->pmpapi_guid)) {
    $entity->pmpapi_guid = pmpapi_guid();
  }
}

/**
 * Implements hook_entity_insert().
 */
function pmpapi_push_entity_insert($entity, $type) {
  if (pmpapi_push_entity_ok_to_push($entity, $type)) {
    $mapping = pmpapi_push_get_mapping_for_entity($entity, $type);
    $uname = pmpapi_push_get_uname($entity, $type);
    $mapped_profile = variable_get('pmpapi_push_' . $uname . '_profile');
    pmpapi_push_push_entity_to_pmp($entity, $type, $mapped_profile, $mapping);
  }
}

/**
 * Implements hook_entity_update().
 */
function pmpapi_push_entity_update($entity, $type) {
  if (pmpapi_push_entity_ok_to_push($entity, $type)) {
    $mapping = pmpapi_push_get_mapping_for_entity($entity, $type);
    $uname = pmpapi_push_get_uname($entity, $type);
    $mapped_profile = variable_get('pmpapi_push_' . $uname . '_profile');
    pmpapi_push_push_entity_to_pmp($entity, $type, $mapped_profile, $mapping);
  }
  elseif (isset($entity->status) && !$entity->status && $type != 'file' && !empty($entity->pmpapi_guid) && empty($entity->pmpapi_do_not_remove)) {
    pmpapi_remove($entity->pmpapi_guid);
  }
}

/**
 * Implements hook_entity_delete().
 */
function pmpapi_push_entity_delete($entity, $type) {
  if (!empty($entity->pmpapi_guid) && empty($entity->pmpapi_do_not_remove)) {
    pmpapi_remove($entity->pmpapi_guid);
  }
}

/**
 * Gets all entity info.
 *
 * Basically a wrapper around entity_get_info(), in case modules want to alter the
 * returned entity info.
 *
 * @return array
 *   (Possibly altered) info on all defined entities.
 */
function pmpapi_push_get_entities() {
  $entities = entity_get_info();
  drupal_alter('pmpapi_push_get_entities', $entities);
  return $entities;
}

/**
 * Create a unique namespace for a given entity.
 *
 * Since bundle names are not unique across entity types, and bundle name cannot
 * always be easily divined from an entity object, we have this modest function.
 *
 * @param object $entity
 *   Any drupal entity object
 * @param string $type
 *   The type of entity (node, file, etc.)
 * @return string
 *   A unique namespace
 */
function pmpapi_push_get_uname($entity, $type) {
  $wrapper = entity_metadata_wrapper($type, $entity);
  $bundle_name = $wrapper
    ->getBundle();
  return $type . '__' . $bundle_name;
}

Functions

Namesort descending Description
pmpapi_push_entity_delete Implements hook_entity_delete().
pmpapi_push_entity_insert Implements hook_entity_insert().
pmpapi_push_entity_ok_to_push Determines if an entity should be pushed to the PMP API.
pmpapi_push_entity_presave Implements hook_entity_presave().
pmpapi_push_entity_update Implements hook_entity_update().
pmpapi_push_get_entities Gets all entity info.
pmpapi_push_get_mapping_for_entity Gets the field->attribute mapping for an entity.
pmpapi_push_get_uname Create a unique namespace for a given entity.
pmpapi_push_menu Implements hook_menu().
pmpapi_push_permission Implements hook_permisssion().
pmpapi_push_push_entity_to_pmp Pushes an entity to the PMP API.