Browse Source

finished showroom migration batch function

Bachir Soussi Chiadmi 8 years ago
parent
commit
1ec80dc60e

+ 1 - 1
sites/all/modules/gui/materiobasemod/materio_showroom.install

@@ -24,7 +24,7 @@
 function materio_showroom_field_schema($field) {
   $columns = array(
     'showroom_tid' => array('type' => 'int', 'not null' => FALSE),
-    'location' => array('type' => 'varchar', 'length' => 10, 'not null' => TRUE),
+    'location' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
   );
   $indexes = array(
     'location' => array('location'),

+ 328 - 3
sites/all/modules/gui/materiobasemod/materio_showroom.module

@@ -115,10 +115,11 @@ function materio_showroom_field_formatter_view($entity_type, $entity, $field, $i
     // This formatter simply outputs the field as text and with a color.
     case 'materio_showroom_location_simple_text':
       foreach ($items as $delta => $item) {
+        $term = taxonomy_term_load($item['showroom_tid']);
         $element[$delta] = array(
           '#type' => 'html_tag',
           '#tag' => 'p',
-          '#value' => t('@loc', array('@loc' => $item['location'])),
+          '#value' => t('%showroom : @loc', array('%showroom' => $term->name, '@loc' => $item['location'])),
         );
       }
       break;
@@ -196,7 +197,7 @@ function materio_showroom_field_widget_form(&$form, &$form_state, $field, $insta
         // Allow a slightly larger size that the field length to allow for some
         // configurations where all characters won't fit in input field.
         '#size' => 10,
-        '#maxlength' => 10,
+        '#maxlength' => 255,
       );
 
       $widget['location'] = array(
@@ -206,7 +207,7 @@ function materio_showroom_field_widget_form(&$form, &$form_state, $field, $insta
         // Allow a slightly larger size that the field length to allow for some
         // configurations where all characters won't fit in input field.
         '#size' => 10,
-        '#maxlength' => 10,
+        '#maxlength' => 255,
       );
 
       break;
@@ -371,3 +372,327 @@ function _materio_showroom_alter_location_field_form(&$form, &$form_state, $form
 }
 
 // TODO: migrate old location field to new one
+
+/**
+ * Implements hook_permission().
+ */
+function materio_showroom_permission() {
+  return array(
+    'materio showroom migrate fields' =>  array(
+      'title' => t('Migrate materio showroom location fields'),
+      'description' => t('Migrate materio showroom location fields'),
+    ),
+  );
+}
+
+//     __  ____                  __  _
+//    /  |/  (_)___ __________ _/ /_(_)___  ____
+//   / /|_/ / / __ `/ ___/ __ `/ __/ / __ \/ __ \
+//  / /  / / / /_/ / /  / /_/ / /_/ / /_/ / / / /
+// /_/  /_/_/\__, /_/   \__,_/\__/_/\____/_/ /_/
+//          /____/
+/**
+ * Implements hook_menu().
+ */
+function materio_showroom_menu() {
+
+  $items['admin/config/content/materio_showroom'] = array(
+    'title' => 'Showrooms location fields migration settings',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('materio_showroom_migrate_location_fields_settings_form'),
+    'access arguments' => array('materio showroom migrate fields'),
+    'type' => MENU_LOCAL_ACTION,
+    // 'file' => ,
+  );
+  // $items['admin/config/content/materio_showroom/migrate'] = array(
+  //   'title' => 'Migrate showrooms location fields',
+  //   'page callback' => 'materio_showroom_migrate_location_fields',
+  //   // 'page arguments' => array('materio_showroom_migrate_location_fields'),
+  //   'access arguments' => array('materio showroom migrate fields'),
+  //   'type' => MENU_LOCAL_TASK,
+  //   // 'file' => ,
+  // );
+
+  return $items;
+}
+
+function materio_showroom_migrate_location_fields_settings_form(){
+  drupal_set_title('Showroom Migration settings', PASS_THROUGH);
+
+  $field_type = 'field_materio_showroom_location';
+
+  $types = node_type_get_types();
+  // dsm($types, 'types');
+  $nt_options = array();
+  foreach ($types as $mn => $type) {
+    $nt_options[$mn] = $type->name;
+  }
+
+  $node_type = variable_get('materio_showroom_migrate_node_type', null);
+  // source field (must be a text field)
+  $form['node_type'] = array(
+    '#type'=>'select',
+    '#options'=>$nt_options,
+    '#default_value' => $node_type,
+    '#title' => t('source field (must be a text field)'),
+    '#multiple' => false,
+  );
+
+  if($node_type){
+
+    $fieldsmap = field_info_field_map();
+    $src_options = array();
+    $target_options = array();
+    foreach ($fieldsmap as $field_name => $field) {
+      // dsm($field, $field_name);
+      if ($field['type'] == 'text'
+          && isset($field['bundles']['node'])
+          && in_array($node_type, $field['bundles']['node'])) {
+        $src_options[$field_name] = $field_name;
+      }
+      if ($field['type'] == $field_type
+          && isset($field['bundles']['node'])
+          && in_array($node_type, $field['bundles']['node'])) {
+        $target_options[$field_name] = $field_name;
+      }
+    }
+
+    // source field (must be a text field)
+    $form['source_field'] = array(
+      '#type'=>'select',
+      '#options'=>$src_options,
+      '#default_value' => variable_get('materio_showroom_migrate_source_field', null),
+      '#title' => t('source field (must be a text field)'),
+      '#multiple' => false,
+    );
+
+    // target field (must be a showroom location field)
+    $form['target_field'] = array(
+      '#type'=>'select',
+      '#options'=>$target_options,
+      '#default_value' => variable_get('materio_showroom_migrate_target_field', null),
+      '#title' => t('target field (must be a showroom location field)'),
+      '#multiple' => false,
+    );
+
+
+    $vocs = taxonomy_get_vocabularies();
+    $voc_options = array();
+    foreach ($vocs as $vid => $voc) {
+      $voc_options[$vid] = $voc->name;
+    }
+
+    // vocabulary for showrooms
+    $vid = variable_get('materio_showroom_migrate_vocabulary', null);
+    $form['vocabulary'] = array(
+      '#type'=>'select',
+      '#options'=>$voc_options,
+      '#default_value' => $vid,
+      '#title' => t('vocabulary for showrooms'),
+      '#multiple' => false,
+    );
+
+    // default taxonomy term
+    if($vid){
+      $tree = taxonomy_get_tree($vid);
+      foreach ($tree as $key => $term) {
+        $terms_options[$term->tid] = $term->name;
+      }
+
+      $term = variable_get('materio_showroom_migrate_default_term', null);
+      $form['default_term'] = array(
+        '#type'=>'select',
+        '#options'=>$terms_options,
+        '#default_value' => $term,
+        '#title' => t('default taxonomy term'),
+        '#multiple' => false,
+      );
+
+      if($term){
+        $form['migrate'] = array(
+          '#type' => 'submit',
+          '#value' => 'Migrate',
+          // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
+        );
+      }
+    }
+
+  }
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Save',
+    // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
+  );
+
+  return $form;
+}
+
+function materio_showroom_migrate_location_fields_settings_form_submit($form, &$form_state){
+  // dsm($form_state, 'form_state');
+
+  $node_type = $form_state['values']['node_type'];
+  $src_field = $form_state['values']['source_field'];
+  $target_field = $form_state['values']['target_field'];
+  $vid = $form_state['values']['vocabulary'];
+  $default_term = $form_state['values']['default_term'];
+
+  variable_set('materio_showroom_migrate_node_type', $node_type);
+  variable_set('materio_showroom_migrate_source_field', $src_field);
+  variable_set('materio_showroom_migrate_target_field', $target_field);
+  variable_set('materio_showroom_migrate_vocabulary', $vid);
+  variable_set('materio_showroom_migrate_default_term', $default_term);
+
+  // dsm($node_type, 'node_type');
+  // dsm($src_field,"src_field");
+  // dsm($target_field,"target_field");
+  // dsm($vid,"vid");
+  // dsm($default_term,"default_term");
+
+  if ($form_state['values']['op'] == 'Migrate'){
+    _materio_showroom_batch_migration($node_type, $src_field, $target_field, $vid, $default_term);
+  }
+
+  //   $query = "INSERT INTO field_data_field_screenshot SELECT a.* FROM field_data_field_image a LEFT JOIN field_data_field_screenshot b ON a.entity_id = b.entity_id AND a.entity_type = b.entity_type WHERE b.entity_id IS NULL";
+  //
+  //   $query2 = "INSERT INTO field_revision_field_screenshot SELECT a.* FROM field_revision_field_image a LEFT JOIN field_revision_field_screenshot b ON a.entity_id = b.entity_id AND a.entity_type = b.entity_type WHERE b.entity_id IS NULL";
+}
+
+function _materio_showroom_batch_migration($node_type, $src_field, $target_field, $vid, $tid){
+  // Reset counter for debug information.
+  $_SESSION['http_request_count'] = 0;
+
+  $batch = array(
+    'title' => t('Migrating Showroom fields ...'),
+    'operations' => array(),
+    'init_message' => t('Commencing'),
+    'progress_message' => t('Processed @current out of @total.'),
+    'error_message' => t('An error occurred during processing'),
+    'finished' => '_materio_showroom_batch_migration_finished',
+  );
+
+  $query = new EntityFieldQuery();
+  $query->entityCondition('entity_type', 'node')
+    ->entityCondition('bundle', $node_type)
+    ->fieldCondition($src_field, 'value', '', '!=');
+
+  $result = $query->execute();
+  // dsm($result, 'result');
+
+  if (isset($result['node'])) {
+
+    $voc = taxonomy_vocabulary_load($vid);
+    $tree = taxonomy_get_tree($vid);
+    foreach ($tree as $key => $term) {
+      $terms[$term->tid] = $term->name;
+    }
+
+    if(!empty($terms)){
+      foreach ($result['node'] as $nid => $value) {
+        $batch['operations'][] = array(
+          '_materio_showroom_batch_migration_op',
+          array(
+            $nid,
+            $src_field, $target_field,
+            $terms, $tid,
+          )
+        );
+      }
+    }
+  }
+
+  batch_set($batch);
+}
+
+function _materio_showroom_batch_migration_op($nid, $src_field, $target_field, $terms, $default_tid, &$context){
+  $context['results']['field_migrated']++;
+
+  $node = node_load($nid);
+
+  $src_items = field_get_items('node', $node, $src_field);
+  $src_value = $src_items ? $src_items[0]['value'] : '';
+
+  // $src_value = str_replace('/', '-', $src_value);
+  //
+  // build new item list
+  $items = array(LANGUAGE_NONE => array());
+  foreach ($terms as $tid => $name) {
+    $items[LANGUAGE_NONE][] = array(
+      'showroom_tid' => $tid,
+      'location' => $default_tid == $tid ? $src_value : '',
+    );
+  }
+
+  $node->$target_field = $items;
+
+  node_save($node);
+
+
+  //Simply show the import row count.
+  $context['message'] = t('Migrating node !c : %title (%nid)', array(
+    '!c' => $context['results']['field_migrated'],
+    '%title'=>$node->title,
+    '%nid'=>$nid ));
+
+  // In order to slow importing and debug better,
+  // we can uncomment this line to make each import slightly slower.
+  // usleep(2500);
+
+  if ( false ) {
+    $context['results']['failed_nodes'][] = $nid ;
+  }
+
+  _materio_showroom_update_http_requests();
+}
+
+
+function _materio_showroom_batch_migration_finished($success, $results, $operations){
+  // dsm($success, 'success');
+  // dsm($results, 'results');
+  // dsm($operations, 'operations');
+  if ($success) {
+    // Here we could do something meaningful with the results.
+    // We just display the number of nodes we processed...
+    drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _materio_showroom_get_http_requests())));
+    drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
+  }
+  else {
+    // An error occurred.
+    // $operations contains the operations that remained unprocessed.
+    drupal_set_message(
+      t('operations : @args',
+        array(
+          '@args' => print_r(current($operations), TRUE),
+        )
+      ),
+      'error'
+    );
+    $error_operation = reset($operations);
+    drupal_set_message(
+      t('An error occurred while processing @operation with arguments : @args',
+        array(
+          '@operation' => $error_operation[0],
+          '@args' => print_r($error_operation[0], TRUE),
+        )
+      ),
+      'error'
+    );
+  }
+}
+
+
+/**
+ * Utility function to increment HTTP requests in a session variable.
+ */
+function _materio_showroom_update_http_requests() {
+  $_SESSION['http_request_count']++;
+}
+
+/**
+ * Utility function to count the HTTP requests in a session variable.
+ *
+ * @return int
+ *   Number of requests.
+ */
+function _materio_showroom_get_http_requests() {
+  return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
+}