From b860b4246419e6ed021b8212c8e98cc455a7868f Mon Sep 17 00:00:00 2001
From: jucallme <jucallme@gmail.com>
Date: Wed, 21 Nov 2012 00:52:39 -0300
Subject: [PATCH] add translations and make the path attribute on the entity a
 field so that it can have multipal values per translation

---
 fieldable_panels_panes.info                        |   1 +
 fieldable_panels_panes.install                     | 141 ++++++++++++++++++++-
 fieldable_panels_panes.module                      |  95 ++++++++++++--
 .../translation.handler.fieldable_panels_panes.inc |  30 +++++
 4 files changed, 250 insertions(+), 17 deletions(-)
 create mode 100644 includes/translation.handler.fieldable_panels_panes.inc

diff --git a/fieldable_panels_panes.info b/fieldable_panels_panes.info
index 836d6b1..0587875 100644
--- a/fieldable_panels_panes.info
+++ b/fieldable_panels_panes.info
@@ -7,6 +7,7 @@ dependencies[] = "views"
 dependencies[] = "field_ui"
 
 files[] = includes/PanelsPaneController.class.php
+files[] = includes/translation.handler.fieldable_panels_panes.inc
 files[] = plugins/views/fieldable_panels_panes_handler_argument_bundle.inc
 files[] = plugins/views/fieldable_panels_panes_handler_field_bundle.inc
 files[] = plugins/views/fieldable_panels_panes_handler_field_delete_entity.inc
diff --git a/fieldable_panels_panes.install b/fieldable_panels_panes.install
index e5f8747..0c7663f 100644
--- a/fieldable_panels_panes.install
+++ b/fieldable_panels_panes.install
@@ -41,11 +41,6 @@ function fieldable_panels_panes_schema() {
         'type' => 'int',
         'size' => 'tiny',
       ),
-      'path' => array(
-        'description' => 'The path the title should link to.',
-        'type' => 'varchar',
-        'length' => 255,
-      ),
       'reusable' => array(
         'description' => 'Whether or not this entity will appear in the Add Content dialog.',
         'type' => 'int',
@@ -64,6 +59,13 @@ function fieldable_panels_panes_schema() {
         'description' => 'The category it will appear in the Add Content dialog under.',
         'type' => 'text',
       ),
+      'language' => array(
+        'description' => 'The language of this entity.',
+        'type' => 'varchar',
+        'length' => 32,
+        'not null' => TRUE,
+        'default' => '',
+      ),
       'view_access' => array(
         'type' => 'text',
         'size' => 'big',
@@ -210,3 +212,132 @@ function fieldable_panels_panes_update_7105() {
   db_add_field('fieldable_panels_panes', 'link', array('type' => 'int', 'size' => 'tiny', 'description' => 'Whether or not this entity title will link to another page.'));
   db_add_field('fieldable_panels_panes', 'path', array('type' => 'varchar', 'length' => 255, 'description' => 'The path the title should link to.'));
 }
+
+/**
+ * Add language column.
+ */
+function fieldable_panels_panes_update_7107(&$sandbox) {
+  $table = 'fieldable_panels_panes';
+  $field = 'language';
+
+  if (!db_field_exists($table, $field)) {
+    $schema = drupal_get_schema($table, TRUE);
+    db_add_field($table, $field, $schema['fields'][$field]);
+
+    db_update($table)
+      ->fields(array($field => LANGUAGE_NONE))
+      ->execute();
+
+    return t("Added $table.language field.");
+  }
+
+  return t("$table.language field already exists, updated skipped.");
+}
+
+/**
+ * Add new path field instance.
+ */
+function fieldable_panels_panes_update_7108(&$sandbox) {
+  // make sure the new field is registered with drupal
+  drupal_flush_all_caches();
+
+  // add the new path field.
+  fieldable_panels_panes_install();
+}
+
+/**
+ * Migrate the path data into a field instance.
+ */
+function fieldable_panels_panes_update_7109(&$sandbox) {
+  if (!isset($sandbox['progress'])) {
+    $sandbox['progress'] = 0;
+    $sandbox['current_id'] = 0;
+    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT fpid) FROM {fieldable_panels_panes}')->fetchField() - 1;
+  }
+  // miragte all path data into the field.
+  $query = db_select('fieldable_panels_panes', 'f');
+
+  $query
+    ->condition('f.fpid', $sandbox['current_id'], '>')
+    ->condition('f.path', '', '<>')
+    ->fields('f', array('fpid', 'path'))
+    ->orderBy('f.fpid', 'ASC')
+    ->range(0, 50);
+
+  $results = $query->execute();
+
+  if ($results->rowCount() > 0) {
+    foreach($results as $result) {
+      $entity = fieldable_panels_panes_load($result->fpid);
+      // The default is to enable this field but NOT to make it translatable
+      // As not every one will use that. So we can only set the default value
+      // Of UND.
+      if (isset($entity->bundle)) {
+        $entity->fieldable_panels_panes_path[LANGUAGE_NONE][0]['value'] = $result->path;
+        fieldable_panels_panes_save($entity);
+      }
+      $sandbox['current_id'] = $entity->fpid;
+      $sandbox['progress']++;
+    }
+    $sandbox['#finished'] = empty($sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
+  }
+  else{
+    $sandbox['#finished'] = TRUE;
+  }
+}
+
+/**
+ * Drop the old path off the entity.
+ */
+function fieldable_panels_panes_update_7110(&$sandbox) {
+  // remove the old path field.
+  db_drop_field('fieldable_panels_panes', 'path');
+}
+
+/**
+ * Implements hook_install().
+ */
+function fieldable_panels_panes_install() {
+  $field = array(
+    'field_name' => 'fieldable_panels_panes_path',
+    'cadinality' => 1,
+    'type' => 'text',
+    'locked' => TRUE,
+  );
+
+  // During installation, the t() function is unavailable, so we use get_t()
+  // to store the name of the translation function.
+  $t = get_t();
+
+  field_create_field($field);
+  $field_instances = array(
+    'field_fieldable_panels_panes_path' => array(
+       'field_name' => 'fieldable_panels_panes_path',
+       'label' => $t('Path'),
+       'description' => $t('The path the title should link to (only internal links are accepted).'),
+       'required' => FALSE,
+       'widget' => array(
+         'type' => 'fieldable_panels_panes_path',
+       ),
+       'display' => array(
+         'default' => array(
+           'label' => 'hidden',
+           'type' => 'hidden',
+         ),
+       ),
+       'settings' => array(
+         'text_processing' => 0,
+       ),
+     ),
+  );
+
+  //create the instance
+  $entity_info = entity_get_info('fieldable_panels_pane');
+  foreach ($field_instances as $field_instance) {
+    foreach ($entity_info['bundles'] as $bundle => $info) {
+      $field_instance['entity_type'] = 'fieldable_panels_pane';
+      $field_instance['bundle'] = $bundle;
+      field_create_instance($field_instance);
+    }
+  }
+}
diff --git a/fieldable_panels_panes.module b/fieldable_panels_panes.module
index ca14b2a..e5b9776 100644
--- a/fieldable_panels_panes.module
+++ b/fieldable_panels_panes.module
@@ -50,7 +50,34 @@ function fieldable_panels_panes_entity_info() {
           'custom settings' => FALSE,
         ),
       ),
-
+      // Add title replacement support for translations.
+      'field replacement' => array(
+        'title' => array(
+          'field' => array(
+            'type' => 'text',
+            'cardinality' => 1,
+            'translatable' => TRUE,
+          ),
+          'instance' => array(
+            'label' => t('Title'),
+            'required' => TRUE,
+            'settings' => array(
+              'text_processing' => 0,
+            ),
+            'widget' => array(
+              'weight' => -5,
+            ),
+          ),
+        ),
+      ),
+      'translation' => array(
+        'entity_translation' => array(
+          'class' => 'EntityTranslationFieldablePanelsPanesHandler',
+          'base path' =>  'admin/structure/fieldable-panels-panes/view/%fieldable_panels_panes',
+          'edit path' =>  'admin/structure/fieldable-panels-panes/view/%fieldable_panels_panes/edit',
+          'path wildcard' => '%fieldable_panels_panes',
+        ),
+      ),
       // entity module callbacks
       'view callback' => 'entity_metadata_view_single',
       'creation callback' => 'fieldable_panels_panes_create',
@@ -577,10 +604,12 @@ function template_preprocess_fieldable_panels_pane(&$vars) {
  * Add a class for our bundle to the normal panels pane theme.
  */
 function fieldable_panels_panes_preprocess_panels_pane(&$vars) {
+  global $language;
   if ($vars['pane']->type == 'fieldable_panels_pane') {
     $entity = fieldable_panels_panes_load_entity($vars['pane']->subtype);
+    $items = field_get_items('fieldable_panels_pane', $entity, 'fieldable_panels_panes_path', $language->language);
     if ($entity->link && !empty($vars['title'])) {
-      $vars['title'] = l($vars['title'], $entity->path);
+      $vars['title'] = l($vars['title'], $items[0]['value']);
     }
     $vars['classes_array'][] = 'pane-bundle-' . ctools_cleanstring($entity->bundle, array('lower case' => TRUE));
   }
@@ -760,6 +789,12 @@ function fieldable_panels_panes_get_base_context($entity = NULL) {
  */
 function fieldable_panels_panes_entity_edit_form($form, &$form_state) {
   $entity = $form_state['entity'];
+  // Map these properties for entity translations.
+  $form['#entity_type'] = array(
+    '#type' => 'hidden',
+    '#value' => $entity->bundle,
+  );
+  $form_state['fieldable_panels_pane'] = $form_state['entity'];
 
   // fixes a bug in wysiwyg
   // oh wysiwyg. We need a better way to address this.
@@ -788,12 +823,15 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) {
     '#id' => 'edit-link',
   );
 
-  $form['link']['path'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Path'),
-    '#description' => t('The path for this link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')),
-    '#dependency' => array('edit-link' => array(1)),
-    '#default_value' => $entity->path,
+  $langcode = fieldable_panels_panes_get_lang_code($entity);
+  field_attach_form('fieldable_panels_pane', $entity, $form, $form_state, $langcode);
+
+  // Shift the fieldable_panels_panes_path into the link fold and
+  // Make it dependent on link.
+  $form['fieldable_panels_panes_path']['#states'] = array(
+    'visible' => array(
+      ':input#edit-link' => array('checked' => TRUE),
+    ),
   );
 
   $form['reusable'] = array(
@@ -859,8 +897,6 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) {
     '#default_value' => '',
   );
 
-  field_attach_form('fieldable_panels_pane', $entity, $form, $form_state);
-
   if (!empty($form_state['add submit'])) {
     $form['actions'] = array(
       '#type' => 'actions',
@@ -879,7 +915,18 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) {
  * Validate callback for the pane entity.
  */
 function fieldable_panels_panes_entity_edit_form_validate($form, &$form_state) {
-  field_attach_form_validate('fieldable_panels_pane', $form_state['entity'], $form, $form_state);
+  $entity = $form_state['entity'];
+
+  field_attach_form_validate('fieldable_panels_pane', $entity, $form, $form_state);
+  if (isset($form_state['values']['fieldable_panels_panes_path'])) {
+    $langcode = fieldable_panels_panes_get_lang_code($entity);
+    // Check that the link exists
+    $normal_path = drupal_get_normal_path($form_state['values']['fieldable_panels_panes_path'][$langcode][0]['value']);
+    $source = drupal_valid_path($normal_path);
+    if (!$source) {
+      form_set_error('fieldable_panels_panes_path', t('The path dose not exist.'));
+    }
+  }
 }
 
 /**
@@ -891,11 +938,11 @@ function fieldable_panels_panes_entity_edit_form_submit($form, &$form_state) {
   // Copy hardcoded fields.
   $entity->title = $form_state['values']['title'];
   $entity->link = $form_state['values']['link'];
-  $entity->path = $form_state['values']['path'];
   $entity->reusable = $form_state['values']['reusable'];
   $entity->category = $form_state['values']['category'];
   $entity->admin_title = $form_state['values']['admin_title'];
   $entity->admin_description = $form_state['values']['admin_description'];
+  $entity->fieldable_panels_panes_path = $form_state['values']['fieldable_panels_panes_path'];
 
   $entity->revision = $form_state['values']['revision'];
   // Only set a log message if there was a new revision. This prevents
@@ -911,3 +958,27 @@ function fieldable_panels_panes_entity_edit_form_submit($form, &$form_state) {
     drupal_set_message(t('The entity has been saved.'));
   }
 }
+
+/**
+ * Determine what language to use.
+ */
+function fieldable_panels_panes_get_lang_code($entity, $form = TRUE) {
+  // Provide support for translations
+  if (module_exists('entity_translation')
+      && entity_translation_enabled('fieldable_panels_pane')) {
+    $handler = entity_translation_get_handler('fieldable_panels_pane', $entity);
+    if ($form) {
+      $langcode = $handler->getFormLanguage();
+    }
+    else {
+      $langcode = $handler->getLanguage();
+    }
+    
+  }
+  else {
+    $langcode = entity_language('fieldable_panels_pane');
+  }
+
+  return $langcode;
+}
+
diff --git a/includes/translation.handler.fieldable_panels_panes.inc b/includes/translation.handler.fieldable_panels_panes.inc
new file mode 100644
index 0000000..ab29e39
--- /dev/null
+++ b/includes/translation.handler.fieldable_panels_panes.inc
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * @file
+ * Fieldable panels panes translation handler for the translation module.
+ */
+
+
+/**
+ * Bean translation handler.
+ *
+ * Overrides default behaviors for Fieldable panels panes properties.
+ */
+class EntityTranslationFieldablePanelsPanesHandler extends EntityTranslationDefaultHandler {
+
+  public function __construct($entity_type, $entity_info, $entity) {
+    parent::__construct('fieldable_panels_pane', $entity_info, $entity);
+  }
+
+  public function getAccess($op) {
+    return fieldable_panels_panes_access($op, $this->entity);
+  }
+
+  protected function getEntityId() {
+    if (isset($this->entity->fpid)) {
+      return $this->entity->fpid;
+    }
+    return parent::getEntityId();
+  }
+}
-- 
1.7.11.3

