<?php

/**
 * @file
 * Entity translation actions module.
 */

// Skip any actions if translation for language exists.
define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_SKIP', 0);

// Clear values from language. Add all values from source language.
define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_REPLACE', 1);

// Keep existed values for language. Add new values from source language.
define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_MERGE', 2);


define('ENTITY_TRANSLATION_ACTIONS_RESULT_CREATED', 'SUCCESS CREATED');
define('ENTITY_TRANSLATION_ACTIONS_RESULT_REPLACED', 'SUCCESS REPLACED');
define('ENTITY_TRANSLATION_ACTIONS_RESULT_DELETED', 'SUCCESS DELETED');
define('ENTITY_TRANSLATION_ACTIONS_RESULT_EXISTS', 'WARNING EXISTS');
define('ENTITY_TRANSLATION_ACTIONS_RESULT_EQUAL', 'WARNING EQUAL');
define('ENTITY_TRANSLATION_ACTIONS_RESULT_NO_SOURCE', 'WARNING NO SOURCE');

/**
 * Implements hook_views_api().
 */
function entity_translation_actions_views_api($module = NULL, $api = NULL) {
  return array('api' => '3.0');
}

/**
 * Implements hook_menu().
 */
function entity_translation_actions_menu() {

  // Page with list of available views.
  $items['admin/content/entity-translations'] = array(
    'title' => 'Entity translations',
    'description' => 'Manage entity translations.',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array('administer entity translation'),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );

  return $items;
}

/**
 * Implements hook_action_info().
 *
 * Registers custom VBO translation actions as Drupal actions.
 */
function entity_translation_actions_action_info() {

  $actions['entity_translation_actions_modify_action'] = array(
    'type' => 'entity',
    'label' => t('Modify entity translations'),
    'behavior' => array('changes_property'),
    'configurable' => TRUE,
    'vbo_configurable' => TRUE,
    'triggers' => array('any'),
  );

  return $actions;
}

/**
 * Implements hook_ctools_plugin_type().
 *
 * Define ctools plugin type.
 */
function entity_translation_actions_ctools_plugin_type() {
  return array(
    'et_action' => array(
      'use hooks' => FALSE,
      'classes' => array('handler'),
    ),
  );
}

/**
 * Implements hook_ctools_plugin_directory().
 *
 * Define plugins with basic translation actions.
 */
function entity_translation_actions_ctools_plugin_directory($owner, $plugin_type) {

  switch ($owner) {
    case 'entity_translation_actions':
      if (in_array($plugin_type, array('et_action'))) {
        return 'plugins/' . $plugin_type;
      }
      break;
  }
}

/**
 * Function returns plugin instance by it id.
 */
function entity_translation_actions_plugin($id) {
  ctools_include('plugins');
  $plugin = ctools_get_plugins('entity_translation_actions', 'et_action', $id);
  return $plugin;
}

/**
 * Function returns all plugin instances.
 */
function entity_translation_actions_plugins() {
  ctools_include('plugins');
  $plugins = ctools_get_plugins('entity_translation_actions', 'et_action');

  // Order list of plugins according to propertie "weight".
  uasort($plugins, '_user_sort');
  return $plugins;
}

/**
 * Action callback function.
 *
 * Goes through list of entities and apply action of selected handler.
 */
function entity_translation_actions_modify_action($entity, $context) {
  $context['action_handler']->action($entity, $context);
}

/**
 * Action form function.
 *
 * Form provide list of available actions. Form rebuilds according to selected
 * plugin.
 */
function entity_translation_actions_modify_action_form($context, &$form_state) {

  $form = array();
  $form_state['entity_type'] = $entity_type = $context['entity_type'];

  $settings = isset($form_state['values']['settings']) ? $form_state['values']['settings'] : array(
    'action' => FALSE,
  );

  $form['settings'] = array(
    '#tree' => 1,
  );

  $action_options = array(
    '' => t('None'),
  );

  foreach (entity_translation_actions_plugins() as $plugin_name => $plugin) {
    $class = ctools_plugin_get_class($plugin, 'handler');
    
    if (!$class) {
      continue;
    }
    
    $handler = new $class($entity_type, array());
    if ($handler->available()) {
      $action_options[$plugin_name] = $plugin['title'];
    }
  }

  $form['settings']['action'] = array(
    '#type' => 'radios',
    '#title' => t('Select action'),
    '#options' => $action_options,
    '#default_value' => $settings['action'],
    '#required' => TRUE,
    '#multiple' => FALSE,
    '#ajax' => array(
      'callback' => 'entity_translation_actions_modify_action_ajax_callback',
    ),
  );

  // Check if any plugin selected.
  if (!empty($settings['action'])) {
    $settings += array($settings['action'] => array());

    // Build plugin handler.
    $plugin = entity_translation_actions_plugin($settings['action']);
    $class = ctools_plugin_get_class($plugin, 'handler');
    $handler = new $class($entity_type, $settings[$settings['action']]);

    // Keep handler object during whole process,
    $form_state['action_handler'] = $handler;

    // Generate custom form for selected plugin.
    $form['settings'][$settings['action']] = array(
      '#type' => 'fieldset',
      '#title' => $action_options[$settings['action']],
      '#description' => $plugin['description'],
    );
    $handler->formBuild($form['settings'][$settings['action']], $form_state);
  }

  return $form;
}

/**
 * Ajax callback for entity_translation_actions_modify_action_form.
 *
 * Function rebuilds form and returns replace command.
 */
function entity_translation_actions_modify_action_ajax_callback($form, &$form_state) {

  $commands[] = ajax_command_replace('form:has(input[value="' . $form['#build_id'] . '"])', render($form));
  return array('#type' => 'ajax', '#commands' => $commands);
}

/**
 * Validate callback for entity_translation_actions_modify_action_form.
 *
 * Allow plugin to execute validate function.
 */
function entity_translation_actions_modify_action_validate($form, &$form_state) {
  if (isset($form_state['action_handler'])) {
    $form_state['action_handler']->formValidate($form, $form_state);
  }
}

/**
 * Submit callback for entity_translation_actions_modify_action_form.
 *
 * Allow plugin to execute submit function. Return context for VBO action.
 */
function entity_translation_actions_modify_action_submit($form, $form_state) {

  if (isset($form_state['action_handler'])) {

    $settings = &$form_state['values']['settings'];

    // Name of selected action.
    $action = $settings['action'];

    // Options for selected action.
    $options = isset($settings[$action]) ? $settings[$action] : array();

    // Execute plugin specific actions on submit.
    $form_state['action_handler']->formSubmit($form, $form_state, $options);

    // Return handler object as context.
    return array(
      'action_handler' => $form_state['action_handler'],
    );
  }
}
