You are here

function lingotek_form_bulk_sync in Lingotek Translation 7.3

Same name and namespace in other branches
  1. 7.4 lingotek.sync.inc \lingotek_form_bulk_sync()
  2. 7.5 lingotek.sync.inc \lingotek_form_bulk_sync()
1 string reference to 'lingotek_form_bulk_sync'
lingotek_menu in ./lingotek.module
Implements hook_menu().

File

./lingotek.sync.inc, line 234
Sync and management

Code

function lingotek_form_bulk_sync($form, $form_state) {
  global $base_url;
  $submit_functions = array(
    'lingotek_form_bulk_sync_submit',
  );
  $report = LingotekSync::getReport();
  $upload_count_total = $report['upload_nids_count'] + (isset($report['upload_nids_et_count']) ? $report['upload_nids_et_count'] : 0) + (isset($report['upload_config_count']) ? $report['upload_config_count'] : 0);

  // Upload
  $form['upload'] = array(
    '#type' => 'fieldset',
    '#title' => t('Upload content for translation'),
    //'#description' => t('Translation management defaults used when creating new nodes. At the node level, these settings can be adjusted.'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#group' => 'administrative_settings',
    '#prefix' => '<div id="upload_wrapper">',
    '#suffix' => '</div>',
  );
  if ($upload_count_total > 0) {
    if (module_exists('workbench_moderation')) {

      // Get nodes that correspond to the upload state in $form['upload']['upload_workbench_moderation']
      lingotek_workbench_moderation_bulk_select($form, $form_state, $report);
      $options_upload = array();
      foreach ($report['workbench_moderation_states'] as $state) {
        if (isset($report['upload_nids_wb_count_' . $state]) && $report['upload_nids_wb_count_' . $state] > 0) {
          $form['upload']['upload_nids_wb_json_' . $state] = array(
            '#type' => 'hidden',
            '#value' => json_encode($report['upload_nids_wb_' . $state]),
          );
          $options_upload[$state] = array(
            array(
              'data' => $state,
              'width' => '20%',
            ),
            array(
              'data' => $report['upload_nids_wb_count_' . $state],
              'width' => '80%',
            ),
          );
        }
      }
      if (!empty($options_upload)) {
        $form['upload']['upload_wb'] = array(
          '#type' => 'tableselect',
          '#prefix' => t('Select nodes to upload based on their Workbench Moderation state'),
          '#header' => array(
            t('State'),
            t('New / Modified Nodes'),
          ),
          '#options' => $options_upload,
          '#default_value' => array_fill_keys(array_keys($options_upload), 0),
        );
      }
      $form['upload']['upload_nids_nowb_json'] = array(
        '#type' => 'hidden',
        '#value' => json_encode($report['upload_nids_nowb']),
      );
      if ($report['upload_nids_nowb_count'] > 0) {
        $form['upload']['upload_nowb'] = array(
          '#type' => 'checkbox',
          '#title' => t('Include new/modified nodes not using Workbench Moderation (<span id="upload-total-nowb">@count</span>)', array(
            '@count' => $report['upload_nids_nowb_count'],
          )),
          '#default_value' => $report['upload_nids_nowb_count'] == 0 ? 0 : 1,
          '#disabled' => $report['upload_nids_nowb_count'] == 0,
        );
      }
    }
    else {
      $form['upload']['upload_nids_json'] = array(
        '#type' => 'hidden',
        '#value' => json_encode($report['upload_nids']),
      );
      if ($report['upload_nids_count'] > 0) {
        $form['upload']['upload'] = array(
          '#type' => 'checkbox',
          '#title' => t('Include new/modified nodes (<span id="upload-total">@count</span>)', array(
            '@count' => $report['upload_nids_count'],
          )),
          '#default_value' => $report['upload_nids_count'] == 0 ? 0 : 1,
          '#disabled' => $report['upload_nids_count'] == 0,
        );
      }
    }
    if (module_exists('entity_translation')) {
      $form['upload']['upload_nids_et_json'] = array(
        '#type' => 'hidden',
        '#value' => json_encode($report['upload_nids_et']),
      );
      if ($report['upload_nids_et_count'] > 0) {
        $form['upload']['upload_et'] = array(
          '#type' => 'checkbox',
          '#title' => t('Include nodes managed by Entity Translation (<span id="upload-et-total">@count</span>)', array(
            '@count' => $report['upload_nids_et_count'],
          )),
          '#default_value' => 0,
          '#disabled' => $report['upload_nids_et_count'] == 0,
          '#description' => t('These nodes will now be managed by Lingotek.'),
        );
      }
    }
    if (variable_get('lingotek_translate_config')) {

      // pre-build include/exclude messages based on enabled config types
      $config_types = array(
        'lingotek_translate_config' => t('Include built-in interface data'),
        'lingotek_translate_config_blocks' => t('blocks'),
        'lingotek_translate_config_taxonomies' => t('taxonomies'),
        'lingotek_translate_config_menus' => t('menus'),
        'lingotek_translate_config_views' => t('views'),
      );
      $config_types_included = array();
      $config_types_excluded = array();
      foreach ($config_types as $k => $v) {
        if (variable_get($k, 0)) {
          $config_types_included[$k] = $v;
        }
        else {
          $config_types_excluded[$k] = $v;
        }
      }
      $cf_included_str = implode(', ', $config_types_included);
      if (count($config_types_included) > 1) {
        $cf_included_str = substr_replace($cf_included_str, ' ' . t('and') . ' ' . substr($cf_included_str, strrpos($cf_included_str, ', ') + 2), strrpos($cf_included_str, ', '));
      }
      $cf_excluded_str = implode(', ', $config_types_excluded);
      if (count($config_types_excluded) > 1) {
        $cf_excluded_str = substr_replace($cf_excluded_str, ' ' . t('and') . ' ' . substr($cf_excluded_str, strrpos($cf_excluded_str, ', ') + 2), strrpos($cf_excluded_str, ', '));
      }
      $form['upload']['upload_config_json'] = array(
        '#type' => 'hidden',
        '#value' => json_encode($report['upload_config']),
      );
      $form['upload']['upload_config'] = array(
        '#type' => 'checkbox',
        '#title' => $cf_included_str . ' (<span id="upload-config-total">' . $report['upload_config_count'] . ' ' . format_plural($report['upload_config_count'], 'set', 'sets') . ' ' . t('of additions/changes') . '</span>)',
        '#default_value' => $report['upload_config_count'] == 0 ? 0 : 1,
        '#disabled' => $report['upload_config_count'] == 0,
      );
      if (count($config_types_excluded)) {
        $form['upload']['upload_config']['#description'] = t('(You can also enable translation for') . ' ' . $cf_excluded_str . ' ' . t('in the Additional Translation tab of the') . ' <a href="' . $base_url . '/admin/settings/lingotek/settings">' . t('Lingotek Settings') . '</a>)';
      }
    }
  }
  else {
    $form['upload']['none'] = array(
      '#markup' => t('There are currently no new/modified items to be uploaded.'),
    );
  }

  // Download
  $form['download'] = array(
    '#type' => 'fieldset',
    '#title' => t('Download translations'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#group' => 'vertical_tab',
  );
  $form['download']['download_targets_workflow_complete_json'] = array(
    '#type' => 'hidden',
    '#value' => json_encode($report['download_targets_workflow_complete']),
  );
  $form['download']['download_targets_workflow_incomplete_json'] = array(
    '#type' => 'hidden',
    '#value' => json_encode($report['download_targets_workflow_incomplete']),
  );
  $download_count_total = $report['download_targets_workflow_complete_count'] + (isset($report['download_targets_workflow_incomplete_count']) ? $report['download_targets_workflow_incomplete_count'] : 0);
  $available_targets = Lingotek::availableLanguageTargets();
  $options_complete = array();

  // locale => label
  $options_incomplete = array();
  foreach ($available_targets as $target) {
    $label = $target->name . ' / ' . $target->native . ' (' . $target->lingotek_locale . ')';
    $locale_complete_count = 0;
    foreach ($report['download_targets_workflow_complete'] as $download_target) {
      if ($target->lingotek_locale == $download_target['locale']) {
        $locale_complete_count++;
      }
    }
    $locale_incomplete_count = 0;
    foreach ($report['download_targets_workflow_incomplete'] as $download_target) {
      if ($target->lingotek_locale == $download_target['locale']) {
        $locale_incomplete_count++;
      }
    }
    if ($locale_complete_count > 0) {
      $row = array(
        array(
          'data' => $label,
          'width' => '20%',
        ),
        array(
          'data' => $locale_complete_count,
          'width' => '80%',
        ),
      );
      $options_complete[$target->lingotek_locale] = $row;
    }
    if ($locale_incomplete_count > 0) {
      $row = array(
        array(
          'data' => $label,
          'width' => '20%',
        ),
        array(
          'data' => $locale_incomplete_count,
          'width' => '80%',
        ),
      );
      $options_incomplete[$target->lingotek_locale] = $row;
    }
  }
  if (empty($options_complete) && empty($options_incomplete)) {
    $form['download']['none'] = array(
      '#markup' => t('There are currently no pending translations to be downloaded.'),
    );
  }
  if (!empty($options_complete)) {
    $form['download']['download_locales_complete'] = array(
      '#type' => 'tableselect',
      '#prefix' => t('Completed Translations'),
      '#header' => array(
        t('Language'),
        t('Translations'),
      ),
      '#options' => $options_complete,
      '#default_value' => array_fill_keys(array_keys($options_complete), 1),
    );
  }
  if (!empty($options_incomplete)) {

    /* $form['download']['download_locales_incomplete_check'] = array(
       '#type' => 'checkbox',
       '#title' => t('Download Incomplete Translations'),
       '#default_value' => 0,
       ); */
    $form['download']['download_locales_incomplete'] = array(
      '#type' => 'tableselect',
      '#prefix' => t('Incomplete Translations'),
      '#header' => array(
        t('Language'),
        t('Translations'),
      ),
      '#options' => $options_incomplete,
      '#default_value' => array_fill_keys(array_keys($options_complete), 0),
    );
  }
  if (!empty($options_complete) || !empty($options_incomplete)) {
    if (module_exists('workbench_moderation')) {
      $transition_select = lingotek_workbench_moderation_get_mult_transitions();
      $form['download']['download_locales_wb_options'] = array(
        '#type' => 'select',
        '#title' => 'WORKBENCH MODERATION',
        '#field_suffix' => 'after translations have downloaded.',
        '#options' => lingotek_get_workbench_moderation_options(),
        '#default_value' => variable_get('lingotek_sync_workbench_moderation', 'no_moderation'),
        '#description' => t("Transitions will not occur until <i>all</i> of a node's translations have downloaded."),
      );
      if (!empty($transition_select)) {

        // select options for multiple possible transitions
        $form['download']['download_locales_wb'] = array(
          '#type' => 'container',
          '#states' => array(
            'visible' => array(
              'select[name="download_locales_wb_options"]' => array(
                'value' => 'increment',
              ),
            ),
          ),
        );
        $default_override = variable_get('lingotek_sync', 0) ? 0 : 1;
        $form['download']['download_locales_wb']['wb_override_defaults'] = array(
          '#type' => 'checkbox',
          '#prefix' => t('<b>Your configuration of the Workbench Moderation module has multiple transition paths.</b>'),
          '#title' => 'Override defaults',
          '#description' => t('Recommended if defaults have not been set in admin->lingotek->settings->content defaults'),
          '#default_value' => $default_override,
        );
        $form['download']['download_locales_wb']['wb_select'] = array(
          '#type' => 'container',
          '#states' => array(
            'invisible' => array(
              ':input[name="wb_override_defaults"]' => array(
                'checked' => FALSE,
              ),
            ),
          ),
        );
        $header = array(
          'INITIAL STATE',
          'TRANSITIONS TO',
        );
        $form['download']['download_locales_wb']['wb_select']['wb_table'] = array(
          '#input' => TRUE,
          '#prefix' => t('Choose the state that you would like the translations to transition to below.'),
          '#type' => 'container',
          '#theme' => 'table',
          '#header' => $header,
          '#rows' => array(),
        );
        foreach ($transition_select as $from_state => $to_states) {
          $selected = variable_get('lingotek_sync_wb_select_' . $from_state);

          // get default
          $select_str = '<div class="form-item form-type-select form-item-download-locales-wb-' . $from_state . '">';
          $select_str .= '<select id="edit_download_locales_wb_' . $from_state . '" name="download_locales_wb_' . $from_state . '"';
          $select_str .= ' class="form-select">';
          foreach ($to_states as $state) {
            $select_str .= '<option ';
            $select_str .= 'value="' . $state . '"';
            if ($selected && $selected == $state) {

              // select default
              $select_str .= ' selected="selected"';
            }
            $select_str .= '>' . $state . '</option>';
          }

          // foreach option add to string
          $select_str .= '</select></div>';
          $form['download']['download_locales_wb']['wb_select']['wb_table']['#rows'][] = array(
            array(
              'data' => $from_state,
              'width' => '20%',
            ),
            array(
              'data' => $select_str,
              'width' => '80%',
            ),
          );

          // Get out of $form_state['input']
        }
      }
    }

    // end workbench_moderation form elements
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Sync'),
    '#disabled' => $upload_count_total == 0 && $download_count_total == 0,
    '#submit' => $submit_functions,
  );
  return $form;
}