<?php
/**
 * @file
 * Provides a context condition based on a list of entities UUIDs.
 */

/**
 * Implements hook_ctools_plugin_api().
 *
 * Make module compatible with context 3.x.
 */
function context_uuid_ctools_plugin_api($module, $api) {
  if ($module == 'context' && $api == 'plugins') {
    return array('version' => 3);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function context_uuid_form_ctools_export_ui_edit_item_form_alter(&$entire_form, &$form_state, $form_id) {
  // Make sure this run only on context form.
  if (empty($entire_form['#base']) || $entire_form['#base'] != 'context_ui_form') {
    return;
  }

  $form = &$entire_form['conditions']['plugins']['context_uuid']['values'];
  $context = $form['#context_uuid_context'];
  $context_condition = $form['#context_uuid_context_condition'];
  $current_values = $context_condition->fetch_from_context($context, 'values');

  // If just loading from context, we need to transform the $current_values
  // array into a multi-dimensional one for using as form default values.
  if (!empty($current_values) && !isset($current_values['add_item'])) {
    $current_items = array();
    foreach ($current_values as $item) {
      list($entity_type, $uuid) = explode(':', $item);
      $current_items[$entity_type][] = $uuid;
    }
    $current_values = array(
      'current_items' => $current_items,
    );
    // Set storage current items so they can be deleted.
    $form_state['storage']['current_items'] = $current_items;
  }

  // Add current items.
  if (!empty($form_state['storage']['current_items'])) {
    $current_values['current_items'] = $form_state['storage']['current_items'];
  }

  // Embbed autocomplete multiple form.
  $form += uuid_entity_autocomplete_form_multiple($current_values);

  // Add a submit hook for hacking there.
  array_unshift($entire_form['#submit'], 'context_uuid_form_context_form_submit');
}

/**
 * Submit callback. Add a submitted flag to form values when the context form
 * is submitted for later check.
 */
function context_uuid_form_context_form_submit($form, &$form_state) {
  if (!empty($form_state['values']['conditions']['plugins']['context_uuid']['values'])) {
    $form_state['values']['conditions']['plugins']['context_uuid']['values']['submitted'] = TRUE;
  }
}

/**
 * Implements hook_context_plugins().
 */
function context_uuid_context_plugins() {
  $path = drupal_get_path('module', 'context_uuid');

  $plugins = array();

  $plugins['context_uuid_condition'] = array(
    'handler' => array(
      'path' => $path,
      'file' => 'context_uuid_condition.inc',
      'class' => 'context_uuid_condition',
      'parent' => 'context_condition',
    ),
  );

  if (module_exists('taxonomy')) {
    $plugins['context_condition_node_taxonomy_uuid'] = array(
      'handler' => array(
        'path' => $path,
        'file' => 'context_condition_node_taxonomy_uuid.inc',
        'class' => 'context_condition_node_taxonomy_uuid',
        'parent' => 'context_condition_node',
      ),
    );
  }

  return $plugins;
}

/**
 * Implements hook_context_registry().
 */
function context_uuid_context_registry() {
  $conditions = array();

  $conditions['context_uuid'] = array(
    'title' => t('Content (UUID)'),
    'plugin' => 'context_uuid_condition',
  );

  if (module_exists('taxonomy')) {
    $conditions['node_taxonomy_uuid'] = array(
      'title' => t('Taxonomy (UUID)'),
      'plugin' => 'context_condition_node_taxonomy_uuid',
    );
  }

  return array('conditions' => $conditions);
}

/**
 * Implements hook_context_page_condition().
 */
function context_uuid_context_page_condition() {
  if ($plugin = context_get_plugin('condition', 'context_uuid')) {
    $plugin->execute();
  }
}

/**
 * Implements hook_context_node_condition_alter().
 */
function context_uuid_context_node_condition_alter(&$node, $op) {
  if ($plugin = context_get_plugin('condition', 'node_taxonomy_uuid')) {
    $plugin->execute($node, $op);
  }
}
