You are here

function i18n_commerce_product_product_overview in Internationalization for commerce product 7

Page callback for "admin/commerce/products/%commerce_product/translate".

1 string reference to 'i18n_commerce_product_product_overview'
i18n_commerce_product_menu in ./i18n_commerce_product.module
Implements hook_menu().

File

./i18n_commerce_product.module, line 47

Code

function i18n_commerce_product_product_overview($commerce_product) {
  require_once DRUPAL_ROOT . '/includes/language.inc';
  if (!empty($commerce_product->tproduct_id)) {

    // Already part of a set, grab that set.
    $tproduct_id = $commerce_product->tproduct_id;
    $translations = array(
      entity_language('commerce_product', $commerce_product) => $commerce_product,
    );
    $translations += i18n_commerce_product_product_get_translations($commerce_product->tproduct_id, TRUE);
  }
  else {

    // We have no translation source nid, this could be a new set, emulate that.
    $tproduct_id = $commerce_product->product_id;
    $translations = array(
      entity_language('commerce_product', $commerce_product) => $commerce_product,
    );
    $translations += i18n_commerce_product_product_get_translations($commerce_product->product_id);
  }
  $type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
  $header = array(
    t('Language'),
    t('Title'),
    t('Status'),
    t('Operations'),
  );
  foreach (language_list() as $langcode => $language) {
    if (empty($language->enabled)) {
      continue;
    }
    $options = array();
    $language_name = $language->name;
    if (isset($translations[$langcode])) {

      // Existing translation in the translation set: display status.
      // We load the full commerce product to check whether the user can edit it.
      $translation_commerce_product = commerce_product_load($translations[$langcode]->product_id);
      $path = 'admin/commerce/products/' . $translation_commerce_product->product_id;
      $links = language_negotiation_get_switch_links($type, $path);
      $title = empty($links->links[$langcode]['href']) ? l($translation_commerce_product->title, $path) : l($translation_commerce_product->title, $links->links[$langcode]['href'], $links->links[$langcode]);
      if (commerce_product_access('update', $translation_commerce_product)) {
        $text = t('edit');
        $path = 'admin/commerce/products/' . $translation_commerce_product->product_id;
        $links = language_negotiation_get_switch_links($type, $path);
        $options[] = empty($links->links[$langcode]['href']) ? l($text, $path) : l($text, $links->links[$langcode]['href'], $links->links[$langcode]);
      }
      $status = $translation_commerce_product->status ? t('Active') : t('Disabled');
      if ($translation_commerce_product->product_id == $tproduct_id) {
        $language_name = t('<strong>@language_name</strong> (source)', array(
          '@language_name' => $language_name,
        ));
      }
    }
    else {

      // No such translation in the set yet: help user to create it.
      $title = t('n/a');
      if (commerce_product_access('create', $commerce_product)) {
        $text = t('add translation');
        $path = 'admin/commerce/products/add/' . str_replace('_', '-', $commerce_product->type);
        $links = language_negotiation_get_switch_links($type, $path);
        $query = array(
          'query' => array(
            'translation' => $commerce_product->product_id,
            'target' => $langcode,
          ),
        );
        $options[] = empty($links->links[$langcode]['href']) ? l($text, $path, $query) : l($text, $links->links[$langcode]['href'], array_merge_recursive($links->links[$langcode], $query));
      }
      $status = t('Not translated');
    }
    $rows[] = array(
      $language_name,
      $title,
      $status,
      implode(" | ", $options),
    );
  }
  drupal_set_title(t('Translations of %title', array(
    '%title' => $commerce_product->title,
  )), PASS_THROUGH);
  $build['translation_commerce_product_overview'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  );
  return $build;
}