You are here

function lingotek_admin_entity_bundle_profiles_form in Lingotek Translation 7.5

Same name and namespace in other branches
  1. 7.7 lingotek.admin.inc \lingotek_admin_entity_bundle_profiles_form()
  2. 7.6 lingotek.admin.inc \lingotek_admin_entity_bundle_profiles_form()

Content translation form

2 calls to lingotek_admin_entity_bundle_profiles_form()
lingotek_setup_comment_translation_settings_form in ./lingotek.setup.inc
lingotek_setup_node_translation_settings_form in ./lingotek.setup.inc
Entity Translation Settings - Form Layout Select the Content Types and Fields to be Translated.
1 string reference to 'lingotek_admin_entity_bundle_profiles_form'
lingotek_admin_configuration_view in ./lingotek.admin.inc

File

./lingotek.admin.inc, line 78

Code

function lingotek_admin_entity_bundle_profiles_form($form, &$form_state, $entity_type, $show_fieldset = FALSE) {

  //$setup_complete = variable_get('lingotek_setup_complete', 0);
  $setup_complete = !lingotek_is_config_missing();
  $entity_type_info = entity_get_info($entity_type);
  $bundles = $entity_type_info['bundles'];

  // Add the default bundle related to message types, if applicable.
  if ($entity_type == 'message_type' && module_exists('message')) {
    $bundles['message_type'] = array(
      'label' => 'message_type',
      'rdf_mapping' => array(),
    );
  }
  $types = array();
  $translate = variable_get('lingotek_enabled_fields', array());
  $translate = isset($translate[$entity_type]) ? $translate[$entity_type] : array();
  $profiles_options = lingotek_get_profiles_by_name();

  // these will come translated via the t-function
  if ($entity_type == 'field_collection_item') {
    $profiles_options = array(
      'ENABLED' => t('Enabled'),
      LingotekSync::PROFILE_DISABLED => t('Disabled'),
    );
    $entity_type_info['label'] = 'Field Collection';
  }
  $entity_profiles = variable_get('lingotek_entity_profiles');

  // What types of fields DO we translate?
  $translatable_field_types = lingotek_get_translatable_field_types();
  $form['entity_type'] = array(
    '#type' => 'hidden',
    '#value' => $entity_type,
  );
  $selector = '.lingotek-content-settings-table.' . $entity_type;
  $enable_all = l(t('(set all to automatic)'), '#', array(
    'attributes' => array(
      'onclick' => "lingotekSetAll('" . $selector . "', '0'); return false;",
    ),
  ));
  $enable_all_manual = l(t('(set all to manual)'), '#', array(
    'attributes' => array(
      'onclick' => "lingotekSetAll('" . $selector . "', '1'); return false;",
    ),
  ));
  $disable_all = l(t('(disable all)'), '#', array(
    'attributes' => array(
      'onclick' => "lingotekSetAll('" . $selector . "', 'DISABLED'); return false;",
    ),
  ));
  $title_str = $setup_complete ? t('Translate @types', array(
    '@type' => $entity_type_info['label'],
  )) : t('Which content types do you want translated?');
  $links_str = t('') . ' ' . $enable_all . ' ' . $enable_all_manual . ' ' . $disable_all;
  $form['translation_' . $entity_type] = array(
    '#type' => $show_fieldset ? 'fieldset' : 'item',
    '#title' => check_plain($title_str),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'administrative_settings',
    'actions' => array(
      '#type' => 'actions',
    ),
    '#submit' => array(
      'lingotek_admin_entity_bundle_profiles_form_submit',
    ),
  );
  $form['translation_' . $entity_type][] = array(
    '#type' => 'item',
    '#description' => $links_str,
  );
  $rows = array();
  foreach ($bundles as $bundle_name => $bundle) {
    $fr = array();
    foreach (field_info_instances($entity_type, $bundle_name) as $field) {
      $field_label = $field['label'];
      $field_machine_name = $field['field_name'];
      $field_type = $field['widget']['type'];
      if (array_search($field_type, $translatable_field_types)) {
        $fr[$field_machine_name] = array(
          '#type' => 'checkbox',
          '#title' => check_plain($field_label),
          '#attributes' => array(
            'id' => array(
              'edit-form-item-' . $bundle_name . '-seperator-' . $field_machine_name,
            ),
            'name' => $bundle_name . '_SEPERATOR_' . $field_machine_name,
            'class' => array(
              'field',
            ),
          ),
          '#id' => 'edit-form-item-' . $bundle_name . '-seperator-' . $field_machine_name,
          '#states' => array(
            'invisible' => array(
              ':input[name="profile_' . $bundle_name . '"]' => array(
                'value' => 'DISABLED',
              ),
            ),
          ),
        );
        $is_enabled = !empty($translate[$bundle_name]) && array_search($field_machine_name, $translate[$bundle_name]) !== FALSE;
        if (!$setup_complete || $is_enabled) {
          $fr[$field_machine_name]['#attributes']['checked'] = 'checked';
        }
      }
    }
    $missing_title = !isset($fr['title_field']) && $entity_type == 'node';
    $missing_subject = !isset($fr['subject_field']) && $entity_type == 'comment';
    if ($missing_title || $missing_subject) {
      $message = $entity_type == 'node' ? t('Title (Note: field will be created.)') : t('Subject (Note: field will be created.)');
      $fr['title_field'] = array(
        '#type' => 'checkbox',
        '#title' => check_plain($message),
        '#attributes' => array(
          'id' => array(
            'edit-form-item-' . $bundle_name . '-seperator-title',
          ),
          'name' => 'title_swap_' . $bundle_name,
          'class' => array(
            'field',
          ),
        ),
        '#id' => 'edit-form-item-' . $bundle_name . '-seperator-title',
        '#states' => array(
          'invisible' => array(
            ':input[name="profile_' . $bundle_name . '"]' => array(
              'value' => 'DISABLED',
            ),
          ),
        ),
      );
      if (!$setup_complete) {
        $fr['title_field']['#attributes']['checked'] = 'checked';
      }
    }
    $default_profile = $setup_complete ? LingotekSync::PROFILE_DISABLED : 0;
    $fr2 = array();
    $fr2['profile_' . $bundle_name] = array(
      '#type' => 'select',
      '#options' => $profiles_options,
      '#value' => isset($entity_profiles[$entity_type][$bundle_name]) ? $entity_profiles[$entity_type][$bundle_name] : $default_profile,
      '#attributes' => array(
        'id' => array(
          'edit-form-item-profile-' . $bundle_name,
        ),
        'name' => 'profile_' . $bundle_name,
        'class' => array(
          'field',
        ),
      ),
    );
    if ($entity_type == 'node') {
      $target_ids = db_select('lingotek_entity_metadata', 'lem')
        ->fields('lem', array(
        'entity_id',
      ))
        ->condition('entity_key', 'node_sync_status')
        ->condition('entity_type', 'node')
        ->condition('value', 'TARGET')
        ->execute()
        ->fetchCol();
      $query = db_select('lingotek_entity_metadata', 'lem')
        ->fields('lem')
        ->condition('lem.entity_key', 'profile')
        ->condition('lem.entity_type', 'node');
      if (!empty($target_ids)) {
        $query
          ->condition('lem.entity_id', $target_ids, 'NOT IN');
      }
      $query
        ->join('node', 'n', 'lem.entity_id = n.nid AND n.type = \'' . $bundle_name . '\'');
      $exception_num = $query
        ->execute()
        ->rowCount();
      $options = array(
        'attributes' => array(
          'class' => array(
            'ctools-use-modal ctools-modal-lingotek-small lingotek-center',
          ),
          'title' => 'Remove all profile exceptions for this content type',
        ),
      );
      drupal_add_js(array(
        'lingotek-small' => array(
          'modalSize' => array(
            'type' => 'fixed',
            'width' => 450,
            'height' => 400,
          ),
          'closeImage' => theme('image', array(
            'path' => drupal_get_path('module', 'lingotek') . '/images/close.png',
            'alt' => t('Close window'),
            'title' => t('Close window'),
          )),
          'animation' => 'fadeIn',
        ),
      ), 'setting');
      $title = $exception_num > 0 ? l($exception_num, LINGOTEK_MENU_MAIN_BASE_URL . '/clearexceptions/' . $bundle_name, $options) : $exception_num;
      $fr3 = array(
        '#type' => 'item',
        '#markup' => $title,
      );
      $rows[$bundle_name] = array(
        array(
          'data' => $bundle['label'],
          'width' => '20%',
        ),
        array(
          'data' => drupal_render($fr2),
        ),
        array(
          'data' => drupal_render($fr3),
          'style' => 'text-align:center',
        ),
        array(
          'data' => drupal_render($fr),
          'width' => '65%',
        ),
      );
    }
    else {
      $rows[$bundle_name] = array(
        array(
          'data' => $bundle['label'],
          'width' => '20%',
        ),
        array(
          'data' => drupal_render($fr2),
        ),
        array(
          'data' => drupal_render($fr),
          'width' => '65%',
        ),
      );
    }
  }
  $header = array(
    t('Content Type'),
    t('Translation Profile *'),
    t('Fields'),
  );
  if ($entity_type == 'node') {
    $header = array(
      t('Content Type'),
      t('Translation Profile *'),
      t('Exceptions'),
      t('Fields'),
    );
  }
  elseif ($entity_type == 'field_collection_item') {
    $header = array(
      t('Collection Name'),
      t('Status *'),
      t('Fields'),
    );
  }
  $variables = array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'class' => array(
        'lingotek-content-settings-table',
        $entity_type,
      ),
    ),
  );
  $form['translation_' . $entity_type]['types'] = array(
    '#type' => 'markup',
    '#markup' => theme('table', $variables),
  );
  if ($setup_complete) {
    $form['translation_' . $entity_type]['types']['#suffix'] = t('Note: changing the profile will update all settings for existing @entity_types except for the project, workflow, vault, and storage method (e.g. node/field)', array(
      '@entity_type' => strtolower($entity_type_info['label']),
    ));
  }
  lingotek_admin_add_entity_specific_changes($form, $form_state, $entity_type);
  $form['translation_' . $entity_type]['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}