You are here

function lingotek_config_start_query in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.config.inc \lingotek_config_start_query()
1 call to lingotek_config_start_query()
lingotek_config_get_rows in ./lingotek.config.inc

File

./lingotek.config.inc, line 90
Config grid form elements

Code

function lingotek_config_start_query($form_state, $union_part_desc) {
  $query = db_select('locales_source', 's');

  // Gets sync statuses for each language.
  $query
    ->addExpression("(SELECT COUNT(*) FROM {locales_target} target LEFT JOIN {lingotek_config_map} map ON map.lid = target.lid WHERE map.lid = s.lid AND map.current = 1 AND target.i18n_status = 0)", 'current_target_count');
  $query
    ->addExpression("(SELECT GROUP_CONCAT(SUBSTRING(config_key, 20, 10)) FROM {lingotek_config_metadata} WHERE id = (SELECT set_id FROM {lingotek_config_map} WHERE lid = s.lid) AND config_key LIKE 'target_sync_status_%' AND value='PENDING')", 'pending');
  $query
    ->addExpression("(SELECT GROUP_CONCAT(SUBSTRING(config_key, 20, 10)) FROM {lingotek_config_metadata} WHERE id = (SELECT set_id FROM {lingotek_config_map} WHERE lid = s.lid) AND config_key LIKE 'target_sync_status_%' AND value='READY')", 'ready');
  $query
    ->addExpression("(SELECT GROUP_CONCAT(SUBSTRING(config_key, 20, 10)) FROM {lingotek_config_metadata} WHERE id = (SELECT set_id FROM {lingotek_config_map} WHERE lid = s.lid) AND config_key LIKE 'target_sync_status_%' AND value='CURRENT')", 'current');
  $query
    ->addExpression("(SELECT GROUP_CONCAT(SUBSTRING(config_key, 20, 10)) FROM {lingotek_config_metadata} WHERE id = (SELECT set_id FROM {lingotek_config_map} WHERE lid = s.lid) AND config_key LIKE 'target_sync_status_%' AND value='EDITED')", 'edited');
  $query
    ->addExpression("(SELECT GROUP_CONCAT(SUBSTRING(config_key, 20, 10)) FROM {lingotek_config_metadata} WHERE id = (SELECT set_id FROM {lingotek_config_map} WHERE lid = s.lid) AND config_key LIKE 'target_sync_status_%' AND value='UNTRACKED')", 'untracked');
  $query
    ->addExpression("(SELECT GROUP_CONCAT(language) FROM {locales_target} WHERE lid = s.lid AND i18n_status = 1)", 'source_edited');
  $query
    ->addExpression("(SELECT set_id FROM {lingotek_config_map} lcm WHERE lcm.lid = s.lid)", 'set_id');

  // Lingotek Document ID
  $query
    ->leftJoin('lingotek_config_metadata', 'lingo_document_id', 'lingo_document_id.id = (SELECT set_id FROM lingotek_config_map WHERE lid = s.lid) AND lingo_document_id.config_key = \'document_id\'');
  $query
    ->addField('lingo_document_id', 'value', 'document_id');

  // Entity Upload Status
  $query
    ->leftJoin('lingotek_config_metadata', 'lingo_upload_status', 'lingo_upload_status.id = (SELECT set_id FROM lingotek_config_map WHERE lid = s.lid) AND lingo_upload_status.config_key = \'upload_status\' AND lingo_upload_status.value <> \'' . LingotekSync::STATUS_TARGET . '\'');
  $query
    ->addField('lingo_upload_status', 'value', 'upload_status');
  $query
    ->fields('s', array(
    'source',
    'location',
    'context',
    'lid',
    'textgroup',
  ));

  // Only show items in textgroups that are selected for translation on Config Settings page.
  $translatable_textgroups = LingotekConfigSet::getTextgroupsForTranslation() ?: array(
    'no textgroups selected',
  );
  $query
    ->condition('s.textgroup', $translatable_textgroups, 'IN');

  // Hide taxonomy terms that use translate for i18n_mode.
  $translate_terms_subquery = db_select('taxonomy_vocabulary', 'tv');
  $translate_terms_subquery
    ->fields('tv', array(
    'vid',
  ));
  $translate_terms_subquery
    ->condition('tv.i18n_mode', LINGOTEK_TAXONOMY_TRANSLATE_VALUE);
  $translate_ids = $translate_terms_subquery
    ->execute()
    ->fetchCol();

  // Hide taxonomy terms that have custom fields.
  $custom_fields_subquery = db_select('taxonomy_vocabulary', 'tv');
  $custom_fields_subquery
    ->fields('tv', array(
    'vid',
  ));
  $custom_fields_subquery
    ->join('field_config_instance', 'fci', 'tv.machine_name = fci.bundle');
  if ($union_part_desc == 'taxonomy name') {
    $query
      ->leftJoin('taxonomy_term_data', 'term_name', 'term_name.name = s.source AND term_name.vid NOT IN (' . implode(',', array_merge(array(
      -1,
    ), $translate_ids)) . ') AND term_name.vid NOT IN (' . $custom_fields_subquery . ')');
    $query
      ->addField('term_name', 'tid', 'tid_from_name');
  }
  else {
    $query
      ->addExpression('NULL', 'tid_from_name');
  }
  if ($union_part_desc == 'taxonomy desc') {
    $query
      ->leftJoin('taxonomy_term_data', 'term_description', 'term_description.description = s.source AND term_description.vid NOT IN (' . implode(',', array_merge(array(
      -1,
    ), $translate_ids)) . ') AND term_description.vid NOT IN (' . $custom_fields_subquery . ')');
    $query
      ->addField('term_description', 'tid', 'tid_from_description');
  }
  else {
    $query
      ->addExpression('NULL', 'tid_from_description');
  }

  // For all taxonomy terms, only show if it has a name or description.
  if ($union_part_desc == 'not taxonomy') {
    $query
      ->condition('s.textgroup', 'taxonomy', '<>');
  }
  elseif ($union_part_desc == 'taxonomy name') {
    $query
      ->isNotNull('term_name.tid');
  }
  elseif ($union_part_desc = 'taxonomy desc') {
    $query
      ->isNotNull('term_description.tid');
  }
  return $query;
}