You are here

function lingotek_dashboard_command_ajax in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.2 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 136
Lingotek Dashboard.

Code

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

      // Add/Insert Target Language
      $message = 'POST: Insert a new Target Language';
      $lingotek_locale = check_plain($_POST['code']);
      $parameters = array();
      foreach ($_POST as $key => $value) {
        $parameters[$key] = check_plain($value);
      }
      if (strlen($lingotek_locale) > 1) {

        // Adds the language locale to the local list of languages and to the Lingotek project (via the API).
        $include_all_docs_in_project = variable_get('lingotek_enable_language_specific_profiles', FALSE) ? FALSE : TRUE;
        $add = lingotek_add_target_language($lingotek_locale, $include_all_docs_in_project);
        $response = lingotek_get_language_details($lingotek_locale);
        $response['tms_added'] = $add;
      }
      else {
        $response['error'] = "locale code required";
      }
      break;
    case 'DELETE':

      // Remove/Delete Target Language
      // 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}
      break;
    case 'GET':

    // 4. Retrieve the Target Languages and Status
    default:

      // Retrieve the Target Languages and Status
      $message = 'GET: retrieve the details of the locale(s)';
      $parameters = $_GET;
      $temp = array();
      foreach ($_GET as $key => $value) {
        $temp[$key] = check_plain($value);
      }
      $response = isset($temp['code']) ? lingotek_get_language_details($temp['code']) : lingotek_get_language_details();
      break;
  }

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

    //skip logging GETS (since they occur frequently)
    LingotekLog::info('<h2>Target - @method</h21>
      <h3>Parameters:</h3>
      <div>!parameters</div>
      <h3>Response:</h3>
      <div>!response</div>
      ', array(
      '@method' => $method,
      '!parameters' => $parameters,
      '!response' => $response,
    ), 'endpoint');
  }
  return lingotek_json_output_cors($full_response);
}