<?php
/**
 * @file
 * Provide workflow actions for VBO.
 * Split out from workflow_actions.
 */

/**
 * Implements hook_action_info().
 */
function workflow_vbo_action_info() {
  return array(
    'workflow_vbo_next_state_action' => array(
      'type' => 'node',
      'label' => t('Change workflow state of post to next state'),
      'configurable' => FALSE,
      'triggers' => array('any'),
      ),

    'workflow_vbo_given_state_action' => array(
      'type' => 'node',
      'label' => t('Change workflow state of post to new state'),
      'configurable' => TRUE,
      'triggers' => array('any'),
      ),
    );
}

/**
 * Implements a Drupal action. Move a node to the next state in the workfow.
 */
function workflow_vbo_next_state_action($node, $context) {
  // If this action is being fired because it's attached to a workflow transition
  // then the node's new state (now its current state) should be in $node->workflow
  // because that is where the value from the workflow form field is stored;
  // otherwise the current state is placed in $node->workflow by our nodeapi load.
  if (!isset($node->nid)) {
    watchdog('workflow_vbo', 'Unable to get current node id state of node - node is not yet saved.');
    return;
  }
  if (!isset($node->workflow)) {
    watchdog('workflow_vbo', 'Unable to get current workflow state of node %nid.',
      array('%nid' => $node->nid));
    return;
  }

  $current_state = $node->workflow;
  $new_state = $current_state;

  // Get the node's new state.
  $choices = workflow_field_choices($node);
  foreach ($choices as $sid => $name) {
    if (isset($flag)) {
      $new_state = $sid;
      $new_state_name = $name;
      break;
    }
    if ($sid == $current_state) {
      $flag = TRUE;
    }
  }

  // Fire the transition.
  workflow_execute_transition($node, $new_state);
}

/**
 * Implements a Drupal action. Move a node to a specified state.
 */
function workflow_vbo_given_state_action($node, $context) {
  global $user;
  if (!isset($node->nid)) {
    watchdog('workflow_vbo', 'Unable to get current node id state of node - node is not yet saved.');
    return;
  }

  $comment = t($context['workflow_comment'], array(
      '%title' => check_plain($node->title), 
      '%state' => check_plain($context['state_name']),
      '%user' => theme('username', array('account' => $user)),
      ));

  workflow_execute_transition($node, $context['target_sid'], $comment, $context['force']);
}

/**
 * Configuration form for "Change workflow state of post to new state" action.
 *
 * @see workflow_vbo_given_state_action()
 */
function workflow_vbo_given_state_action_form($context) {
  $previous_workflow = '';
  $options = array();

  // Get all states, only where active.
  foreach (workflow_get_workflow_states(array('status' => 1)) as $data) {
    $options[$data->name][$data->sid] = check_plain($data->state);
  }

  $form['target_sid'] = array(
    '#type' => 'select',
    '#title' => t('Target state'),
    '#description' => t('Please select that state that should be assigned when this action runs.'),
    '#default_value' => isset($context['target_sid']) ? $context['target_sid'] : '',
    '#options' => $options,
    );

  $form['force'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force transition'),
    '#description' => t('If this box is checked, the new state will be assigned even if workflow ' .
      'permissions disallow it.'),
    '#default_value' => isset($context['force']) ? $context['force'] : '',
    );

  $form['workflow_comment'] = array(
    '#type' => 'textfield',
    '#title' => t('Message'),
    '#description' => t('This message will be written into the workflow history log when the action ' .
      'runs. You may include the following variables: %state, %title, %user'),
    '#default_value' => isset($context['workflow_history']) ? $context['workflow_history'] : t('Action set %title to %state by %user.'),
    );

  return $form;
}

/**
 * Submit handler for "Change workflow state of post to new state" action
 * configuration form.
 *
 * @see workflow_vbo_given_state_action_form()
 */
function workflow_vbo_given_state_action_submit($form_id, $form_state) {
  if ($state = workflow_get_workflow_states_by_sid($form_state['values']['target_sid'])) {
    return array(
      'target_sid' => $form_state['values']['target_sid'],
      'state_name' => check_plain($state->state),
      'force' => $form_state['values']['force'],
      'workflow_comment' => $form_state['values']['workflow_comment'],
      );
  }
}
