From 173bbaafdba0c6b49ef7a3f25da63f525b49dfa0 Mon Sep 17 00:00:00 2001
From: Pedro Cambra <pedro.cambra@gmail.com>
Date: Wed, 23 Mar 2011 16:24:03 +0100
Subject: [PATCH 1/2] Issue #1032302 by das-peter, rfay,  bojanz, pcambra: Integrate translation handler for commerce products.

---
 modules/product/commerce_product.info              |    3 +
 modules/product/commerce_product_ui.module         |   65 ++++++++++++++++++++
 .../includes/commerce_product.controller.inc       |    2 +
 .../product/includes/commerce_product.forms.inc    |    9 +++-
 .../translation.handler.commerce_product.inc       |   55 +++++++++++++++++
 5 files changed, 133 insertions(+), 1 deletions(-)
 create mode 100644 modules/product/includes/translation.handler.commerce_product.inc

diff --git a/modules/product/commerce_product.info b/modules/product/commerce_product.info
index 6a20cd0..036fdf0 100644
--- a/modules/product/commerce_product.info
+++ b/modules/product/commerce_product.info
@@ -20,3 +20,6 @@ files[] = includes/views/handlers/commerce_product_handler_filter_product_type.i
 
 ; Simple tests
 files[] = tests/commerce_product.test
+
+; Translation handler
+files[] = includes/translation.handler.commerce_product.inc
diff --git a/modules/product/commerce_product_ui.module b/modules/product/commerce_product_ui.module
index d961dde..648ec15 100644
--- a/modules/product/commerce_product_ui.module
+++ b/modules/product/commerce_product_ui.module
@@ -452,6 +452,20 @@ function commerce_product_ui_form_commerce_product_ui_product_form_alter(&$form,
   // Add a submit handler to the save button to add a redirect.
   $form['actions']['submit']['#submit'][] = 'commerce_product_ui_product_form_submit';
 
+  // If translation support is enabled - provide the suitable languages
+  if (module_exists('entity_translation') && function_exists('entity_translation_enabled') && entity_translation_enabled('commerce_product')) {
+  	$form['language'] = array(
+      '#type' => 'select',
+      '#title' => t('Language'),
+      '#default_value' => (isset($form_state['commerce_product']->language) ? $form_state['commerce_product']->language : ''),
+      '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
+      '#weight' => -10,
+    );
+    // Since this function may changes the language of the submitted form
+    // values it has to be the first called.
+    array_unshift($form['actions']['submit']['#submit'], 'commerce_product_ui_product_form_translation_processing');
+  }
+
   // Add the save and continue button for new products.
   if (empty($form_state['commerce_product']->product_id)) {
     $form['actions']['save_continue'] = array(
@@ -483,6 +497,34 @@ function commerce_product_ui_product_form_submit($form, &$form_state) {
 }
 
 /**
+ * Special submit callback for commerce_product_ui_product_form().
+ *
+ * Checks if translation is enabled and handle possible language changes.
+ * Since this handler may changes the language of submitted form values it
+ * should be invoked as the first submit handler.
+ */
+function commerce_product_ui_product_form_translation_processing($form, &$form_state) {
+  // If translation support is enabled - make sure language changes are handled.
+  if (module_exists('translation') && function_exists('entity_translation_enabled') && entity_translation_enabled('commerce_product')) {
+    $available_languages = field_content_languages();
+    list(, , $bundle) = entity_extract_ids('commerce_product', $form_state['commerce_product']);
+
+    foreach (field_info_instances('commerce_product', $bundle) as $instance) {
+      $field_name = $instance['field_name'];
+      $field = field_info_field($field_name);
+      $previous_language = $form[$field_name]['#language'];
+
+      // Handle a possible language change: new language values are inserted,
+      // previous ones are deleted.
+      if ($field['translatable'] && $previous_language != $form_state['values']['language']) {
+        $form_state['values'][$field_name][$form_state['values']['language']] = $form_state['commerce_product']->{$field_name}[$previous_language];
+        $form_state['values'][$field_name][$previous_language] = array();
+      }
+    }
+  }
+}
+
+/**
  * Implements hook_form_FORM_ID_alter().
  *
  * The Product UI module instantiates the Product delete form at a particular
@@ -536,3 +578,26 @@ function commerce_product_ui_set_breadcrumb($product_types = FALSE) {
 
   drupal_set_breadcrumb($breadcrumb);
 }
+
+/**
+ * Implements hook_translation_info().
+ *
+ * Translation handler is already registered in the commerce_product module.
+ * @see includes/translation.handler.commerce_product.inc
+ */
+function commerce_product_ui_translation_info($types = NULL) {
+  $info = array();
+
+  $info['commerce_product'] = array(
+    'translation' => array(
+      'entity_translation' => array(
+        'class' => 'EntityTranslationCommerceProductHandler',
+        'base path' => 'admin/commerce/products/%commerce_product',
+        'access callback' => 'commerce_product_access',
+        'access arguments' => array('update', 3),
+        'edit form' => TRUE,
+      ),
+    ),
+  );
+  return $info;
+}
diff --git a/modules/product/includes/commerce_product.controller.inc b/modules/product/includes/commerce_product.controller.inc
index 6b80cff..c18ae60 100644
--- a/modules/product/includes/commerce_product.controller.inc
+++ b/modules/product/includes/commerce_product.controller.inc
@@ -50,7 +50,9 @@ class CommerceProductEntityController extends DrupalDefaultEntityController {
       $product->changed = REQUEST_TIME;
 
       // Give modules the opportunity to prepare field data for saving.
+      module_invoke_all('entity_presave', $product, 'commerce_product');
       rules_invoke_all('commerce_product_presave', $product);
+
       field_attach_presave('commerce_product', $product);
 
       // If this is a new product...
diff --git a/modules/product/includes/commerce_product.forms.inc b/modules/product/includes/commerce_product.forms.inc
index 5eb6ce8..4513629 100644
--- a/modules/product/includes/commerce_product.forms.inc
+++ b/modules/product/includes/commerce_product.forms.inc
@@ -40,7 +40,7 @@ function commerce_product_product_form($form, &$form_state, $product) {
 
   // Add the field related form elements.
   $form_state['commerce_product'] = $product;
-  field_attach_form('commerce_product', $product, $form, $form_state);
+  field_attach_form('commerce_product', $product, $form, $form_state, $product->language);
 
   $form['status'] = array(
     '#type' => 'radios',
@@ -61,6 +61,12 @@ function commerce_product_product_form($form, &$form_state, $product) {
     '#weight' => 400,
   );
 
+  // Simply use default language
+  $form['language'] = array(
+    '#type' => 'value',
+    '#value' => $product->language,
+  );
+
   // We add the form's #submit array to this button along with the actual submit
   // handler to preserve any submit handlers added by a form callback_wrapper.
   $submit = array();
@@ -122,6 +128,7 @@ function commerce_product_product_form_submit($form, &$form_state) {
   $product->sku = $form_state['values']['sku'];
   $product->title = $form_state['values']['title'];
   $product->status = $form_state['values']['status'];
+  $product->language = $form_state['values']['language'];
 
   // Set the product's uid if it's being created at this time.
   if (empty($product->product_id)) {
diff --git a/modules/product/includes/translation.handler.commerce_product.inc b/modules/product/includes/translation.handler.commerce_product.inc
new file mode 100644
index 0000000..e8ee130
--- /dev/null
+++ b/modules/product/includes/translation.handler.commerce_product.inc
@@ -0,0 +1,55 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Commerce product translation handler for the translation module.
+ */
+
+
+/**
+ * Commerce product translation handler.
+ *
+ * Override the default behaviours to provide the needed commerce product
+ * properties.
+ *
+ * This class is registered in the commerce_product_ui.module. If you want to
+ * use it without the commerce_product_ui.module make sure you register it
+ * on your own by using hook_translation_info().
+ * @see hook_translation_info()
+ * @see commerce_product_ui.module
+ */
+class EntityTranslationCommerceProductHandler extends EntityTranslationDefaultHandler {
+
+  public function __construct($entity_type, $entity_info, $entity, $entity_id) {
+    parent::__construct('commerce_product', $entity_info, $entity, $entity_id);
+  }
+
+  /**
+   * Indicates whether this commerce product is a revision or not.
+   */
+  public function isRevision() {
+    return !empty($this->entity->revision);
+  }
+
+  /**
+   * Returns the original language of the commerce product.
+   */
+  public function getLanguage() {
+    return $this->entity->language;
+  }
+
+  /**
+   * Checks whether the current user has access to this commerce product.
+   */
+  public function getAccess($op) {
+    return commerce_product_access($op, $this->entity);
+  }
+
+  /**
+   * Returns whether the commerce product is active (TRUE) or disabled (FALSE).
+   */
+  protected function getStatus() {
+    return (boolean) $this->entity->status;
+  }
+}
-- 
1.7.3.3


From 93d4405c2fa0161530b50940c4b77903bee83f1b Mon Sep 17 00:00:00 2001
From: Pedro Cambra <pedro.cambra@gmail.com>
Date: Thu, 24 Mar 2011 17:50:46 +0100
Subject: [PATCH 2/2] Issue #1032302 by das-peter, rfay, bojanz, pcambra: Integrate translation handler for commerce products.

---
 modules/product/commerce_product_ui.module         |   36 ++++++++++++++++++-
 .../product/includes/commerce_product_ui.forms.inc |   15 ++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/modules/product/commerce_product_ui.module b/modules/product/commerce_product_ui.module
index 648ec15..89422b5 100644
--- a/modules/product/commerce_product_ui.module
+++ b/modules/product/commerce_product_ui.module
@@ -593,11 +593,43 @@ function commerce_product_ui_translation_info($types = NULL) {
       'entity_translation' => array(
         'class' => 'EntityTranslationCommerceProductHandler',
         'base path' => 'admin/commerce/products/%commerce_product',
-        'access callback' => 'commerce_product_access',
-        'access arguments' => array('update', 3),
+        'access callback' => 'commerce_product_ui_entity_translation_tab_access',
+        'access arguments' => array(3),
         'edit form' => TRUE,
       ),
     ),
   );
   return $info;
 }
+
+/**
+ * Check if the given entity has product translation enabled.
+ */
+function commerce_product_ui_entity_translation_product($entity_type, $product) {
+  return $entity_type == 'commerce_product' && function_exists('translation_supported_type') && translation_supported_type($product->type);
+}
+
+/**
+ * Product translate tab specific access callback.
+ */
+function commerce_product_ui_entity_translation_tab_access($product) {
+  if ($product->language != LANGUAGE_NONE) {
+    if (commerce_product_ui_entity_translation_supported_type($product->type)) {
+      return entity_translation_tab_access('commerce_product');
+    }
+    elseif (commerce_product_ui_entity_translation_product('commerce_product', $product)) {
+      return _translation_tab_access($product);
+    }
+  }
+  return FALSE;
+}
+
+/**
+ * Returns whether the given product type has support for translations.
+ *
+ * @return
+ *   Boolean value.
+ */
+function commerce_product_ui_entity_translation_supported_type($type) {
+  return variable_get('language_content_type_' . $type, 0) == ENTITY_TRANSLATION_ENABLED;
+}
diff --git a/modules/product/includes/commerce_product_ui.forms.inc b/modules/product/includes/commerce_product_ui.forms.inc
index 60e69fd..b6bd3cc 100644
--- a/modules/product/includes/commerce_product_ui.forms.inc
+++ b/modules/product/includes/commerce_product_ui.forms.inc
@@ -65,6 +65,16 @@ function commerce_product_ui_product_type_form($form, &$form_state, $product_typ
     '#rows' => 3,
   );
 
+  if (module_exists('entity_translation')) {
+    $form['product_type']['language_content_type'] = array(
+      '#type' => 'radios',
+      '#title' => t('Multilingual support'),
+      '#default_value' => variable_get('language_content_type_' . $product_type['type'], 0),
+      '#options' => array(0 => t('Disabled'), ENTITY_TRANSLATION_ENABLED => t('Enabled, with entity translation')),
+      '#description' => t('Enable multilingual support for this content type. If <em>entity translation</em> is enabled it will be possible to provide a different version of the same content for each available language. Existing content will not be affected by changing this option.'),
+    );
+  }
+
   $form['actions'] = array(
     '#type' => 'container',
     '#attributes' => array('class' => array('form-actions')),
@@ -138,6 +148,11 @@ function commerce_product_ui_product_type_form_submit($form, &$form_state) {
   $product_type['is_new'] = !$updated;
   commerce_product_ui_product_type_save($product_type);
 
+  // Save
+  if (module_exists('entity_translation')) {
+  	variable_set('language_content_type_' . $product_type['type'], $form_state['values']['product_type']['language_content_type']);
+  }
+
   // Redirect based on the button clicked.
   drupal_set_message(t('Product type saved.'));
 
-- 
1.7.3.3

