You are here

function lingotek_admin_profile_form_submit in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.4 lingotek.admin.inc \lingotek_admin_profile_form_submit()
  2. 7.5 lingotek.admin.inc \lingotek_admin_profile_form_submit()
  3. 7.6 lingotek.admin.inc \lingotek_admin_profile_form_submit()

File

./lingotek.admin.inc, line 2229

Code

function lingotek_admin_profile_form_submit($form, &$form_state) {
  $profile_obj = LingotekProfile::loadById($form_state['values']['profile_id']);
  if ($form_state['values']['op'] == 'Save') {
    $profile_obj
      ->setName($form_state['values']['name']);
    foreach (lingotek_get_profile_fields(TRUE, TRUE) as $key) {
      if (isset($form_state['values'][$key])) {
        $profile_obj
          ->setAttribute($key, $form_state['values'][$key]);
      }
    }

    // Set all source-language-specific overrides
    $languages = lingotek_get_target_locales();
    foreach ($languages as $l) {
      if (!empty($form_state['values']['settings_' . $l])) {
        $override_settings = $form_state['values']['settings_' . $l];
        if ($override_settings == LingotekSync::PROFILE_DISABLED) {
          $profile_obj
            ->deleteTargetLocaleOverrides($l);
          $profile_obj
            ->setAttribute('disabled', TRUE, $l);
        }
        elseif ($override_settings === LingotekSync::PROFILE_CUSTOM) {

          // reset for new settings
          $profile_obj
            ->deleteTargetLocaleOverrides($l);

          // set auto-download
          $auto_download = !empty($form_state['values']['auto_download_' . $l]) ? $form_state['values']['auto_download_' . $l] : FALSE;
          $profile_obj
            ->setAutoDownload($auto_download, $l);

          // set workflow
          $workflow_id = !empty($form_state['values']['workflow_id_' . $l]) ? $form_state['values']['workflow_id_' . $l] : $profile_obj
            ->getWorkflow($l);
          $profile_obj
            ->setWorkflow($workflow_id, $l);
        }
        elseif ($override_settings === LingotekSync::PROFILE_INHERIT) {
          $profile_obj
            ->deleteTargetLocaleOverrides($l);
        }
        else {
          throw new LingotekException('Unknown profile selection');
        }
      }
    }

    // If the workflow has changed and if current translations should be changed,
    // then pull all nodes associated with this profile and update the workflow
    // on TMS, including the correct phase.
    //
    // TODO: ACCOUNT FOR THE LANGUAGE-SPECIFIC OVERRIDES BEFORE CHANGING THIS!
    if (isset($form_state['values']['prefill_phases_checkbox']) && $form_state['values']['prefill_phases_checkbox']) {
      $workflow_id = $form_state['values']['workflow_id'];
      $prefill_phase = $form_state['values']['prefill_phase_select'];
      $entities = lingotek_get_all_entities_by_profile($profile_obj
        ->getId());
      $api = LingotekApi::instance();
      $document_ids = array();

      // gather doc IDs for bulk submission
      foreach ($entities as $entity) {
        if (isset($entity['document_id']) && $entity['document_id']) {
          $document_ids[] = $entity['document_id'];
          lingotek_keystore($entity['type'], $entity['id'], 'workflow_id', $workflow_id);
        }
      }
      if (!empty($document_ids)) {
        $api
          ->changeWorkflow($document_ids, $workflow_id, $prefill_phase);
      }
    }
    variable_set('lingotek_profile_changed', $profile_obj
      ->getId());
    $profile_obj
      ->save();
  }
  elseif ($form_state['values']['op'] == 'Delete') {
    $profile_obj
      ->delete();
  }
}