You are here

function lingotek_dashboard_command_ajax in Lingotek Translation 7.2

Same name and namespace in other branches
  1. 7.7 lingotek.dashboard.inc \lingotek_dashboard_command_ajax()
  2. 7.3 lingotek.dashboard.inc \lingotek_dashboard_command_ajax()
  3. 7.4 lingotek.dashboard.inc \lingotek_dashboard_command_ajax()
  4. 7.5 lingotek.dashboard.inc \lingotek_dashboard_command_ajax()
  5. 7.6 lingotek.dashboard.inc \lingotek_dashboard_command_ajax()

Ajax Command Processing for the Lingotek dashboard.

1 string reference to 'lingotek_dashboard_command_ajax'
lingotek_menu in ./lingotek.module
Implements hook_menu().

File

./lingotek.dashboard.inc, line 156
Lingotek Dashboard.

Code

function lingotek_dashboard_command_ajax() {
  $parameters = array();
  $message = 'Not doing anything.';
  $method = $_SERVER['REQUEST_METHOD'];
  $response = array();

  // 1. Update a Target Language (Initiate a Machine Translation Batch)
  if ($method == 'PUT') {

    // Initiate Machine Translation
    $message = 'PUT: Initiate Machine Translation';
    parse_str(file_get_contents("php://input"), $parameters);
    $type = $parameters['type'];
    $lingotek_locale = $parameters['code'];
    $response = array(
      'code' => $lingotek_locale,
      'active' => TRUE,
      'docs' => '0',
      'pending' => '0',
      'current' => '0',
    );

    // Launch the machine translation process!
  }

  // END:  PUT
  // 2. Add/Insert Target Language
  if ($_POST) {
    $message = 'POST: Insert a new Target Language';
    $lingotek_locale = $_POST['code'];
    $parameters = $_POST;
    if (strlen($lingotek_locale) > 1) {

      // Adds the language locale to the local list of languages and to the Lingotek project (via the API).
      $add = lingotek_add_target_language($lingotek_locale);
      $response = lingotek_get_target_status($lingotek_locale);
      $response['success'] = $add;
    }
  }

  // END:  POST
  // 3. Remove/Delete Target Language
  if ($method == 'DELETE') {

    // Initiate Language Delete
    parse_str(file_get_contents("php://input"), $parameters);
    $lingotek_locale = $parameters['code'];
    $message = t('DELETE: Remove language (@code)', array(
      '@code' => $lingotek_locale,
    ));

    // Removes the language to the local list of languages we do translation for, and also removes the target language from the Lingotek project.
    $delete = lingotek_delete_target_language($lingotek_locale);

    // Mark this language as Active => False
    $response = array(
      'code' => $lingotek_locale,
      'active' => FALSE,
      'success' => $delete,
    );

    // Format:  {'code':'es','active':'FALSE','success':TRUE}
  }

  // END:  DELETE
  // 4. Retrieve the Target Languages and Status
  if ($method == 'GET') {

    // Retrieve the Target Languages and Status
    $message = 'GET: Retrieve the Target Language(s) and Status';
    $parameters = $_GET;
    $response = isset($_GET['code']) ? lingotek_get_target_status($_GET['code']) : lingotek_get_target_status();
  }

  // END:  GET
  if ($method == 'PUT') {
    $commands = array();
    ajax_deliver(array(
      '#type' => 'ajax',
      '#commands' => $commands,
    ));
  }
  else {

    //lingotek_json_output_cors(array('response' => $response, 'message' => $message, 'data' => $data, 'method' => $method  ));
    $general = array(
      'message' => $message,
    );
    $full_response = array_merge($general, $response);
    if ($method !== 'GET') {

      //skip GETS (since they occur lots)
      watchdog('endpoint_api', '
      <h1>@method</h1>
      <h2>Parameters:</h2>
      <div>!parameters</div>
      <h2>Response:</h2>
      <div>!response</div>
      ', array(
        '@method' => $method,
        '!parameters' => watchdog_format_object($parameters),
        '!response' => watchdog_format_object($response),
      ), WATCHDOG_NOTICE);
    }
    return lingotek_json_output_cors($full_response);
  }
}