_ajax_execute_rest_call

Versions
mediamosa-21
_ajax_execute_rest_call()

Insert the output of the REST call into a text area.

Code

sites/all/modules/mediamosa_development/mediamosa_development.admin.inc, line 34

<?php
function _ajax_execute_rest_call() {

  // Ignore compare check key listing.
  $ignore_list = array(
    'asset' => array(
      'GET' => array('locked')
    ),
    'asset/$asset_id' => array(
      'GET' => array('locked')
    ),
  );

  // Load the form based upon the $_POST data sent via the ajax call.
  list($form, $form_state) = ajax_get_form();
  // Fix drupal notice.
  $form_state['values'] = isset($form_state['values']) ? $form_state['values'] : array();

  // Get the select REST call.
  $uri_method = isset($_SESSION['mediamosa_development_params']['uri']) ? $_SESSION['mediamosa_development_params']['uri'] : '';

  if (empty($uri_method)) {
    return array('#type' => 'ajax', '#commands' => array(), '#header' => FALSE);
  }

  list($uri, $method) = explode('|', $uri_method);

  $var_setup = mediamosa_rest::get_var_setup($_SESSION['mediamosa_development_params']['rest_call']);
  $data = $uri_rebuild = $uri_vars = $errors = array();

  // fill in the uri with the values (when needed).
  foreach (explode('/', $uri) as $uri_var) {
    $uri_var = trim($uri_var, '# ');
    if ($uri_var{0} == '$') {
      if (!isset($form_state['input']['_' . substr($uri_var, 1)]) || $form_state['input']['_' . substr($uri_var, 1)] == '') {
        $errors[] = t('Missing value for @name', array('@name' => substr($uri_var, 1)));
        continue;
      }

      // So we know the urivars.
      $uri_vars[] = substr($uri_var, 1);

      // To rebuild.
      $uri_rebuild[] = $form_state['input']['_' . substr($uri_var, 1)];
    }
    else {
      $uri_rebuild[] = $uri_var;
    }
  }

  // Rebuild uri.
  $uri = implode('/', $uri_rebuild);

  // Get the vars.
  foreach ($var_setup['vars'] as $name => $var) {
    if (!isset($form_state['input']['_' . $name]) || $form_state['input']['_' . $name] == '') {
      continue;
    }

    if (in_array($name, $uri_vars)) {
      continue;
    }

    // Store the value.
    $data[$name] = $form_state['input']['_' . $name];
  }

  // Get the selected app.
  $app_id = $form_state['input']['client_application'];
  $username = NULL;
  $password = NULL;

  if ($app_id) {
    $app = mediamosa_app::get_by_appid($app_id);

    $username = $app[mediamosa_app_db::APP_NAME];
    $password = $app[mediamosa_app_db::SHARED_KEY];
  }

  $commands = array();

  // Run same on mediamosa 1.7?
  $run_on_mm_17 = ($form_state['input']['run_on_mm_17'] == 1);

  $response1x = $response2x = FALSE;

  if (empty($errors)) {
    // Create mediamosa_connector.
    $mediamosa_connector = new mediamosa_connector($username, $password);

    // Get response.
    $response_mm2 = $mediamosa_connector->request($uri, array('method' => $method, 'data' => $data));

    if (empty($response_mm2) || empty($response_mm2->data)) {
      $form['rest_result']['output']['#value'] = t('Check mediamosa connector settings, no output received.');
    }
    else {
      $form['rest_result']['output']['#value'] = $response_mm2->data;
      $response2x = TRUE;
    }

    if ($run_on_mm_17) {
      $mediamosa_development_mediamosa_17_rest_url = variable_get('mediamosa_development_mediamosa_17_rest_url', '');

      if (!empty($mediamosa_development_mediamosa_17_rest_url)) {
        // Create mediamosa_connector.
        $mediamosa_connector = new mediamosa_connector($username, $password, $mediamosa_development_mediamosa_17_rest_url);

        // Get response.
        $response_mm1 = $mediamosa_connector->request($uri, array('method' => $method, 'data' => $data));

        if (empty($response_mm1) || empty($response_mm1->data)) {
          $form['rest_result']['output_mm17']['#value'] = t('Check MediaMosa 1.7 settings, unable to retrieve data');
        }
        else {
          $form['rest_result']['output_mm17']['#value'] = $response_mm1->data;
          $response1x = TRUE;
        }

        $form['rest_result']['output_mm17']['#type'] = 'textarea';
      }
      else {
        drupal_set_message('No REST interface URL set, check settings.');
      }
    }
  }
  else {
    $form['rest_result']['output']['#value'] = implode("\n", $errors);
  }

  if ($response1x && $response2x) {
    $pass = $failure = 0;

    $response_mm1_array = mediamosa_lib::simplexml2array($response_mm1->xml->items);
    $response_mm2_array = mediamosa_lib::simplexml2array($response_mm2->xml->items);

    $compares = array(t('Result compare MediaMosa2.x with MediaMosa 1.x response.'));

    if ((int)$response_mm1->xml->header->item_count != (int)$response_mm2->xml->header->item_count) {
      $compares[] = t('Item count does not match.');
    }

    if ((int)$response_mm1->xml->header->item_count_total != (int)$response_mm2->xml->header->item_count_total) {
      $compares[] = t('Item count total does not match.');
    }

    // Find possible ignore key listing for this uri/method.
    $ignore = empty($ignore_list[$uri][$method]) ? array() : $ignore_list[$uri][$method];

    $compares = _mediamosa_development_compare_array($response_mm1_array['item'], $response_mm2_array['item'], 'MediaMosa 1.7.x', 'MediaMosa2.x', $compares, $ignore);

    $form['rest_result']['output_compare']['#type'] = 'textarea';
    $form['rest_result']['output_compare']['#value'] = implode("\n", $compares);
  }


  $form = form_builder('', $form, $form_state);
  $commands[] = ajax_command_replace('', drupal_render($form['rest_result']));

  return array('#type' => 'ajax', '#commands' => $commands, '#header' => FALSE);
}
?>