باز هم مهاجرت از دروپال 6 به دروپال 7

مدتی بعد از مهاجرت وبلاگ خودم به دروپال 7 ، این بار سایت www.jahizieh.net را نیز به دروپال 7 منتقل کردم این بار کار به مراتب دشوار تر بود چون سایت فوق دارای تعداد زیادی محصول بود و در ضمن من میخواستم کلا طراحی سایت رو عوض کنم و محصولات رو به ماژول commerce منقل کنم کد زیر حاصل 24 ساعت تلاش مداوم من است که می تواند برای دیگران هم جنبه‌ی آموزشی داشته باشد.

مجددا عرض می کنم که کد ذیل، فقط برای سایت jahizieh.net کاربرد داشته و برای استفاده برای سایت های دیگر، لازم است که در آن تغییرات لازم انجام گیرد. این کد در واقع تغییر یافته‌ی کدهایی است که برای وبلاگ خودم نوشته بودم.

   1<?php
   2/**
   3 * @file Import data from d6 to d7
   4 * @author Ahmad Hejazee <info@hejazee.com?-->
   5 * @url <a href="http://www.jahizieh.net/">http://www.jahizieh.net/</a> @endurl
   6 */
   7
   8if (!function_exists('drush_print')) {
   9  function drush_print() {
  10
  11  }
  12}
  13
  14/**
  15 * hook_menu()
  16 */
  17 function myimport_menu() {
  18    $items['myimport'] = array(
  19    'title' => 'My import',
  20    'description' => 'Import data to drupal 7',
  21    'page callback' => 'drupal_get_form',
  22    'page arguments' => array('myimport_imort_form'),
  23    'access callback' => TRUE,
  24    'type' => MENU_NORMAL_ITEM,
  25    );
  26    return $items;
  27 }
  28
  29/**
  30 * Form builder
  31 */
  32 function myimport_imort_form() {
  33    $form = array();
  34    return confirm_form($form, t('Are you sure?'), 'myimport', t('Import?'), t('Import'), t('Cancel'));
  35 }
  36
  37/**
  38 * Submit handler
  39 */
  40  function myimport_imort_form_submit() {
  41    global $my_arr;
  42    require_once (dirname(__FILE__) . '/nodes.php');
  43
  44  $imported = false;
  45
  46  foreach ($my_arr as $key => $node) {
  47    drush_print("Importing node #{$node['nid']} ({$node['type']})");
  48    switch ($node['type']) {
  49      case 'news':
  50        $new_node = myimport_imort_news($node);
  51        $imported = true;
  52        break;
  53      case 'faq':
  54        //$new_node = myimport_imort_faq($node);
  55        $imported = false;
  56        break;
  57      case 'page':
  58        $new_node = myimport_imort_page($node);
  59        $imported = true;
  60        break;
  61      case 'homepage':
  62        //$new_node = myimport_imort_homepage($node);
  63        $imported = false;
  64        break;
  65      case 'webform':
  66        //$new_node = myimport_imort_webform($node);
  67        $imported = false;
  68        break;
  69      case 'jah_product':
  70        $new_node = myimport_imort_jah_product($node);
  71        $imported = true;
  72        break;
  73      case 'story':
  74        //$new_node = myimport_imort_story($node);
  75        $imported = false;
  76        break;
  77      case 'poll':
  78        //$new_node = myimport_imort_poll($node);
  79        $imported = false;
  80        break;
  81      case 'profile':
  82        //$new_node = myimport_imort_profile($node);
  83        $imported = false;
  84        break;
  85      default:
  86        $imported = false;
  87        //break;
  88    }
  89
  90    if (isset($new_node->nid) && ($imported == TRUE)) {
  91      drush_print("Imported node #{$node['nid']} ({$node['type']}) to node #{$new_node->nid} ({$new_node->type})");
  92      myimport_import_node_comments($node['nid'], $new_node->nid);
  93    }
  94  }
  95}
  96
  97function myimport_imort_news($node) { /////////////////////// WORKS WELL
  98  //node->field_news_images => node->field_image
  99  $n = new stdClass();
 100  $n->uid = $node['uid'];
 101  $n->title = $node['title'];
 102  $n->status = $node['status'];
 103  $n->comment = $node['comment'];
 104  $n->promote = $node['promote'];
 105  $n->sticky = $node['sticky'];
 106  $n->type = $node['type'];
 107  $n->language = $node['language'];
 108  $n->created = $node['created'];
 109  $n->changed = $node['changed'];
 110  $n->tnid = $node['tnid'];
 111  $n->translate = $node['translate'];
 112  $n->revision_timestamp = $node['revision_timestamp'];
 113  $n->revision_uid = $node['revision_uid'];
 114  $n->body = array(
 115    'und' => array(
 116      0 => array(
 117        'value' => $node['body'],
 118        'summary' => $node['teaser'],
 119        'format' => myimport_convert_format($node['format']),
 120        //'safe_value' => '',
 121        //'safe_summary' => '',
 122      ),
 123    ),
 124  );
 125
 126  $node['field_news_images'] = _myimport_trim_field($node['field_news_images']);
 127  if (!empty($node['field_news_images'])) {
 128    $field_image = array();
 129
 130    $field_image_und = myimport_file_multiple($node['field_news_images']);
 131    if (!empty($field_image_und)) {
 132      $field_image['und'] = $field_image_und;
 133    }
 134    
 135    foreach ($node['field_news_images'] as $c => $file_array) {
 136      $description = $alt = $title = '';
 137      if (isset($file_array['data']['description'])) {
 138        $description = $file_array['data']['description'];
 139      }
 140      if (isset($file_array['data']['alt'])) {
 141        $alt = $file_array['data']['alt'];
 142      }
 143      if (isset($file_array['data']['title'])) {
 144        $title = $file_array['data']['title'];
 145      }
 146    
 147      $field_image['und'][$c]['description'] = $description;
 148      $field_image['und'][$c]['alt'] = $alt;
 149      $field_image['und'][$c]['title'] = $title;
 150      $field_image['und'][$c]['display'] = 1;
 151      //remove empty files
 152      if ($field_image['und'][$c]['uri'] === 'public://') {
 153        unset($field_image['und'][$c]);
 154      }
 155    }
 156    $n->field_image = $field_image;
 157  }
 158
 159  $n->last_comment_timestamp = $node['last_comment_timestamp'];
 160  $n->last_comment_name = $node['last_comment_name'];
 161  $n->comment_count = $node['comment_count'];
 162  $n->name = $node['name'];
 163  $n->picture = $node['picture'];
 164  $n->data = $node['data'];
 165
 166  node_save($n);
 167  return $n;
 168}
 169
 170function myimport_imort_faq($node) {  /////NOT IMPORTING
 171  //I decided not to import this content type (only two nodes are created)
 172}
 173
 174function myimport_imort_page($node) {   /////////////////////// WORKS WELL
 175  //No extra fields
 176  //node->field_news_images => node->field_image
 177  $n = new stdClass();
 178  $n->uid = $node['uid'];
 179  $n->title = $node['title'];
 180  $n->status = $node['status'];
 181  $n->comment = $node['comment'];
 182  $n->promote = $node['promote'];
 183  $n->sticky = $node['sticky'];
 184  $n->type = $node['type'];
 185  $n->language = $node['language'];
 186  $n->created = $node['created'];
 187  $n->changed = $node['changed'];
 188  $n->tnid = $node['tnid'];
 189  $n->translate = $node['translate'];
 190  $n->revision_timestamp = $node['revision_timestamp'];
 191  $n->revision_uid = $node['revision_uid'];
 192  $n->body = array(
 193    'und' => array(
 194      0 => array(
 195        'value' => $node['body'],
 196        'summary' => $node['teaser'],
 197        'format' => myimport_convert_format($node['format']),
 198        //'safe_value' => '',
 199        //'safe_summary' => '',
 200      ),
 201    ),
 202  );
 203
 204  $n->last_comment_timestamp = $node['last_comment_timestamp'];
 205  $n->last_comment_name = $node['last_comment_name'];
 206  $n->comment_count = $node['comment_count'];
 207  $n->name = $node['name'];
 208  $n->picture = $node['picture'];
 209  $n->data = $node['data'];
 210
 211  node_save($n);
 212  return $n;
 213}
 214
 215function myimport_imort_homepage($node) {  /////NOT IMPORTING
 216  //I decided not to import this content type (only two nodes are created)
 217}
 218
 219function myimport_imort_webform($node) {  /////NOT IMPORTING
 220  //Import manually
 221}
 222
 223function myimport_imort_jah_product($node) {
 224  /**
 225   * #############################
 226   * Mapping D6 fields to D7 fields:
 227      *
 228      *# node->title ==> node->title ; product->title ; product->sku
 229      *# node->body ==> node->body
 230      *
 231   * node->field_jah_product_price ==> product->price (If not specified, set 0, admin must edit)
 232      *
 233   * (These are taxonomy fields:)
 234   * node->field_jah_product_noe_parche ==> product->field_fabric_type (term refernce)
 235   * node->field_jah_product_color ==> product->field_color (term refernce)
 236   * node->field_jah_product_jesnse_rookesh ==> product->field_veneer (term refernce)
 237      *
 238   * node->field_jah_product_little_image ==> node->field_image
 239   * node->field_jah_product_large_images ==> node->field_large_images
 240      *
 241   * node->field_jah_product_files ==> node->field_files
 242      *
 243   * ///IGNORE THESE FIELDS (They have duplicate data that is not needed any more)
 244   * /// I'll replace them by on of the d7 fields later.
 245   * node->field_pager_item_text
 246   * node->field_slide_text
 247   * node->field_image
 248      *
 249      *
 250   * #################################
 251   * Taxonomies: from d6 to d7
 252      *
 253   * Vid 1 => albums (d7_node->field_albums)
 254   * Vid 2 => tags (d7_node->field_tags)
 255   * Vid 3 -> product_type (d7_node->field_product_type)
 256   * Vid 4 => company (d7_node->field_company)
 257   * Vid 5 => //////////////////////Not Implemented (tariqeye sefaresh dadan)
 258   * Vid 6 => price_range (d7_node->field_price_range)
 259   * Vid 7 => lifetime (d7_node->field_lifetime)
 260   * Vid 8 => vogue (d7_node->field_vogue)
 261   * Vid 9 => recommendation (d7_node->field_recommendation)
 262      *
 263      *
 264      *
 265   * #################################
 266   * D7's Extra fields:
 267      *
 268   * node->field_products  (Will be automatically filled by a new product)
 269   * product->status = 1
 270      *
 271   * (These are taxonomy fields:)
 272   * product->field_power (SET EMTY VALUE) (term refernce)
 273   * product->field_voltage (SET EMTY VALUE) (term refernce)
 274   * product->field_noise_level (SET EMTY VALUE) (term refernce)
 275      */
 276
 277  $n = new stdClass(); //New node
 278  $p = commerce_product_new('product'); //New commerce_product
 279
 280  $n->uid = $node['uid'];
 281  $n->title = $node['title'];
 282
 283  $p->title = $node['title'];
 284  $p->uid = $node['uid'];
 285  $p->sku = $node['title'] . "-" . $node['nid'];
 286  $p->status = 1;
 287
 288  $n->status = $node['status'];
 289  $n->comment = $node['comment'];
 290  $n->promote = $node['promote'];
 291  $n->sticky = $node['sticky'];
 292  $n->type = 'product';
 293  $n->language = $node['language'];
 294  $n->created = $node['created'];
 295  $n->changed = $node['changed'];
 296  $n->tnid = $node['tnid'];
 297  $n->translate = $node['translate'];
 298  $n->revision_timestamp = $node['revision_timestamp'];
 299  $n->revision_uid = $node['revision_uid'];
 300
 301  $n->last_comment_timestamp = $node['last_comment_timestamp'];
 302  $n->last_comment_name = $node['last_comment_name'];
 303  $n->comment_count = $node['comment_count'];
 304  $n->name = $node['name'];
 305  $n->picture = $node['picture'];
 306  $n->data = $node['data'];
 307
 308  //Body
 309  $n->body = array(
 310    'und' => array(
 311      0 => array(
 312        'value' => $node['body'],
 313        'summary' => $node['teaser'],
 314        'format' => myimport_convert_format($node['format']),
 315        //'safe_value' => '',
 316        //'safe_summary' => '',
 317      ),
 318    ),
 319  );
 320
 321  //Price
 322  if (!empty($node['field_jah_product_price']) && isset($node['field_jah_product_price'][0]['value'])) {
 323    $price = $node['field_jah_product_price'][0]['value'];
 324    //remove strings and make a number. Then multiply by 10 to make RIALS unit
 325    $price_corrected = (int)(preg_replace("/[^0-9]/", "", $price)) * 10;
 326
 327    $p->commerce_price = array(
 328      'und' => array(
 329        0 => array(
 330          'amount' => $price_corrected,
 331          'currency_code' => 'IRR',
 332        ),
 333      ),
 334    );
 335  }
 336
 337  //field_jah_product_little_image
 338  $node['field_jah_product_little_image'] = _myimport_trim_field($node['field_jah_product_little_image']);
 339  if (!empty($node['field_jah_product_little_image'])) {
 340    $field_image = array();
 341
 342    $field_image_und = myimport_file_multiple($node['field_jah_product_little_image']);
 343    if (!empty($field_image_und)) {
 344      $field_image['und'] = $field_image_und;
 345    }
 346    
 347    foreach ($node['field_jah_product_little_image'] as $c => $file_array) {
 348      $description = $alt = $title = '';
 349      if (isset($file_array['data']['description'])) {
 350        $description = $file_array['data']['description'];
 351      }
 352      if (isset($file_array['data']['alt'])) {
 353        $alt = $file_array['data']['alt'];
 354      }
 355      if (isset($file_array['data']['title'])) {
 356        $title = $file_array['data']['title'];
 357      }
 358    
 359      $field_image['und'][$c]['description'] = $description;
 360      $field_image['und'][$c]['alt'] = $alt;
 361      $field_image['und'][$c]['title'] = $title;
 362      $field_image['und'][$c]['display'] = 1;
 363      //remove empty files
 364      if ($field_image['und'][$c]['uri'] === 'public://') {
 365        unset($field_image['und'][$c]);
 366      }
 367    }
 368    $n->field_image = $field_image;
 369  }
 370
 371  //field_jah_product_large_images
 372  $node['field_jah_product_large_images'] = _myimport_trim_field($node['field_jah_product_large_images']);
 373  if (!empty($node['field_jah_product_large_images'])){
 374    $field_large_images = array();
 375
 376    $field_large_images_und = myimport_file_multiple($node['field_jah_product_large_images']);
 377    if (!empty($field_large_images_und)) {
 378      $field_large_images['und'] = $field_large_images_und;
 379    }
 380    
 381    foreach ($node['field_jah_product_large_images'] as $c => $file_array) {
 382      $description = $alt = $title = '';
 383      if (isset($file_array['data']['description'])) {
 384        $description = $file_array['data']['description'];
 385      }
 386      if (isset($file_array['data']['alt'])) {
 387        $alt = $file_array['data']['alt'];
 388      }
 389      if (isset($file_array['data']['title'])) {
 390        $title = $file_array['data']['title'];
 391      }
 392    
 393      $field_large_images['und'][$c]['description'] = $description;
 394      $field_large_images['und'][$c]['alt'] = $alt;
 395      $field_large_images['und'][$c]['title'] = $title;
 396      $field_large_images['und'][$c]['display'] = 1;
 397      //remove empty files
 398      if ($field_large_images['und'][$c]['uri'] === 'public://') {
 399        unset($field_large_images['und'][$c]);
 400      }
 401    }
 402    $n->field_large_images = $field_large_images;
 403  }
 404
 405  //field_jah_product_files
 406  $node['field_jah_product_files'] = _myimport_trim_field($node['field_jah_product_files']);
 407  if (!empty($node['field_jah_product_files'])){
 408    $field_files = array();
 409
 410    $field_files_und = myimport_file_multiple($node['field_jah_product_files']);
 411    if (!empty($field_files_und)) {
 412      $field_files['und'] = $field_files_und;
 413    }
 414    
 415    foreach ($node['field_jah_product_files'] as $c => $file_array) {
 416      $description = $alt = $title = '';
 417      if (isset($file_array['data']['description'])) {
 418        $description = $file_array['data']['description'];
 419      }
 420      if (isset($file_array['data']['alt'])) {
 421        $alt = $file_array['data']['alt'];
 422      }
 423      if (isset($file_array['data']['title'])) {
 424        $title = $file_array['data']['title'];
 425      }
 426    
 427      $field_files['und'][$c]['description'] = $description;
 428      $field_files['und'][$c]['alt'] = $alt;
 429      $field_files['und'][$c]['title'] = $title;
 430      $field_files['und'][$c]['display'] = 1;
 431      //remove empty files
 432      if ($field_files['und'][$c]['uri'] === 'public://') {
 433        unset($field_files['und'][$c]);
 434      }
 435    }
 436    $n->field_files = $field_files;
 437  }
 438
 439  /**
 440   * Map fields to taxonomy:
 441   * node->field_jah_product_noe_parche ==> product->field_fabric_type (term refernce)
 442   * node->field_jah_product_color ==> product->field_color (term refernce)
 443   * node->field_jah_product_jesnse_rookesh ==> product->field_veneer (term refernce)
 444      */
 445
 446  //field_jah_product_noe_parche ==> product->field_fabric_type (term refernce)
 447  $node['field_jah_product_noe_parche'] = _myimport_trim_field($node['field_jah_product_noe_parche']);
 448  if (!empty($node['field_jah_product_noe_parche'])){
 449    $tag = $node['field_jah_product_noe_parche'][0]['value'];
 450    $term_array = myimport_make_taxonomy_from_term($tag, 'fabric_type');
 451    $p->field_fabric_type = array(
 452      'und' => $term_array,
 453    );
 454  }
 455
 456  //field_jah_product_color ==> product->field_color (term refernce)
 457  $node['field_jah_product_color'] = _myimport_trim_field($node['field_jah_product_color']);
 458  if (!empty($node['field_jah_product_color'])){
 459    $tag = $node['field_jah_product_color'][0]['value'];
 460    $term_array = myimport_make_taxonomy_from_term($tag, 'color');
 461    $p->field_color = array(
 462      'und' => $term_array,
 463    );
 464  }
 465
 466  //field_jah_product_jesnse_rookesh ==> product->field_veneer (term refernce)
 467  $node['field_jah_product_jesnse_rookesh'] = _myimport_trim_field($node['field_jah_product_jesnse_rookesh']);
 468  if (!empty($node['field_jah_product_jesnse_rookesh'])){
 469    $tag = $node['field_jah_product_jesnse_rookesh'][0]['value'];
 470    $term_array = myimport_make_taxonomy_from_term($tag, 'veneer');
 471    $p->field_veneer = array(
 472      'und' => $term_array,
 473    );
 474  }
 475
 476  //Save the product to get product id
 477  commerce_product_save($p);
 478
 479  //node->field_products
 480  $n->field_products = array(
 481    'und' => array(
 482      0 => array(
 483        'product_id' => $p->product_id,
 484      ),
 485    ),
 486  );
 487
 488  /**
 489   * #################################
 490   * Taxonomies: from d6 to d7
 491      *
 492   * Vid 1 => albums (d7_node->field_albums)
 493   * Vid 2 => tags (d7_node->field_tags)
 494   * Vid 3 -> product_type (d7_node->field_product_type)
 495   * Vid 4 => company (d7_node->field_company)
 496   * Vid 5 => //////////////////////Not Implemented (tariqeye sefaresh dadan)
 497   * Vid 6 => price_range (d7_node->field_price_range)
 498   * Vid 7 => lifetime (d7_node->field_lifetime)
 499   * Vid 8 => vogue (d7_node->field_vogue)
 500   * Vid 9 => recommendation (d7_node->field_recommendation)
 501      */
 502
 503  //Import albums
 504  /*
 505  if (isset($node['taxonomy'][1]) && is_array($node['taxonomy'][1])) {
 506    $tid_arr = array_keys($node['taxonomy'][1]);
 507    $tid = $tid_arr[0];
 508    $album_name = _myimport_d6_tid_to_tname($tid);
 509    
 510    $a = array(
 511      'vid' => 1,
 512      'tid' => $tid,
 513      'args' => false,
 514      'tname' => $album_name,
 515    );
 516    
 517    $result = myimport_make_taxonomy($a);
 518    
 519    //$n->field_albums = array(
 520     // 'und' => $result,
 521    //);
 522  }
 523  */
 524  /*
 525  $field_albums = array();
 526  $field_tags = array();
 527  $field_product_type = array();
 528  $field_company = array();
 529  $field_price_range = array();
 530  $field_lifetime = array();
 531  $field_vogue = array();
 532  $field_recommendation = array();
 533  foreach ($node['taxonomy'] as $vid => $tid_arr) {
 534    switch ($vid) {
 535      case 1:
 536        $tid_arr2 = array_keys($tid_arr);
 537        $tname = _myimport_d6_tid_to_tname($tid_arr2[0]);
 538        dpm("myimport_make_taxonomy(array('vid' => $vid, 'tid' => $tid, 'tname' => $tname));");
 539        //$field_albums[] = myimport_make_taxonomy(array('vid' => $vid, 'tid' => $tid, 'tname' => $tname));
 540        //$n->field_albums = array(
 541        //  'und' => $field_albums
 542        //);
 543        break;
 544      
 545      case 'tags':
 546        //foreach ($tid_arr as $tid => $tname) {
 547        //  $field_tags[] = myimport_make_taxonomy(array('vid' => 1, 'tid' => $tid, 'tname' => $tname, 'tags' => TRUE));
 548        //}
 549        
 550        //$n->field_tags = array(
 551        //  'und' => $field_tags
 552        //);
 553        break;
 554      case 3:
 555        foreach ($tid_arr as $tid => $tid2) {
 556          $tname = _myimport_d6_tid_to_tname($tid);
 557          if (!tname) continue;
 558          $field_product_type[] = myimport_make_taxonomy(array('vid' => $vid, 'tid' => $tid, 'tname' => $tname));
 559        }
 560        
 561        $n->field_product_type = array(
 562          'und' => $field_product_type
 563        );
 564        dpm($n->field_product_type);
 565        break;
 566      case 4:
 567        foreach ($tid_arr as $tid => $tid2) {
 568          $tname = _myimport_d6_tid_to_tname($tid);
 569          if (!tname) continue;
 570          $field_company[] = myimport_make_taxonomy(array('vid' => $vid, 'tid' => $tid, 'tname' => $tname));
 571        }
 572        
 573        $n->field_company = array(
 574          'und' => $field_company
 575        );
 576        dpm($n->field_company);
 577        break;
 578      case 6:
 579        foreach ($tid_arr as $tid => $tid2) {
 580          $tname = _myimport_d6_tid_to_tname($tid);
 581          if (!tname) continue;
 582          $field_price_range[] = myimport_make_taxonomy(array('vid' => $vid, 'tid' => $tid, 'tname' => $tname));
 583        }
 584        
 585        $n->field_price_range = array(
 586          'und' => $field_price_range
 587        );
 588        dpm($n->field_price_range);
 589        break;
 590      case 7:
 591        foreach ($tid_arr as $tid => $tid2) {
 592          $tname = _myimport_d6_tid_to_tname($tid);
 593          if (!tname) continue;
 594          $field_lifetime[] = myimport_make_taxonomy(array('vid' => $vid, 'tid' => $tid, 'tname' => $tname));
 595        }
 596        
 597        $n->field_lifetime = array(
 598          'und' => $field_lifetime
 599        );
 600        dpm($n->field_lifetime);
 601        break;
 602      case 8:
 603        foreach ($tid_arr as $tid => $tid2) {
 604          $tname = _myimport_d6_tid_to_tname($tid);
 605          if (!tname) continue;
 606          $field_vogue[] = myimport_make_taxonomy(array('vid' => $vid, 'tid' => $tid, 'tname' => $tname));
 607        }
 608        
 609        $n->field_vogue = array(
 610          'und' => $field_vogue
 611        );
 612        break;
 613        dpm($n->field_lifetime);
 614      case 9:
 615        foreach ($tid_arr as $tid => $tid2) {
 616          $tname = _myimport_d6_tid_to_tname($tid);
 617          if (!tname) continue;
 618          $field_recommendation[] = myimport_make_taxonomy(array('vid' => $vid, 'tid' => $tid, 'tname' => $tname));
 619        }
 620        
 621        $n->field_recommendation = array(
 622          'und' => $field_recommendation
 623        );
 624        dpm($n->field_lifetime);
 625        break;
 626      default:
 627        break;
 628        
 629    }
 630
 631  }
 632  */
 633  node_save($n);
 634  return $n;
 635}
 636
 637function myimport_imort_story($node) {  /////NOT IMPORTING
 638  //There is no content of this type
 639}
 640
 641function myimport_imort_poll($node) {  /////NOT IMPORTING
 642  //Import manually
 643}
 644
 645function myimport_imort_profile($node) {   /////NOT IMPORTING
 646  //Import manually (Postponed because of complexity and needs re-designing
 647}
 648
 649
 650/**
 651 * Create taxonomy_term object from a d6 taxonomy array
 652 */
 653  function myimport_make_taxonomy($taxonomy_array) {
 654    $result = array();
 655
 656  $tid = $taxonomy_array['tid'];
 657  $vid = _myimport_vid6_to_vid7($taxonomy_array['vid']);
 658  $tname = $taxonomy_array['tname'];
 659
 660  if (!$taxonomy_array['tags']) {
 661    $matched_terms = taxonomy_get_term_by_name($tname);
 662    if (empty($matched_terms)) {
 663      //This tag is not created yet. Create it
 664      $term = new stdClass();
 665      $term->vid = $vid;
 666      $term->name = $tname;
 667      taxonomy_term_save($term);
 668      $result[] = array(
 669      'tid' => $term->tid,
 670      'taxonomy_term' => $term,
 671      );
 672    }
 673    else {
 674      foreach ($matched_terms as $j => $term) {
 675        $result = array(
 676        'tid' => $term->tid,
 677        'taxonomy_term' => $term,
 678        );
 679      }
 680    }
 681  }
 682  else {
 683    //general categories
 684    $tags_arr = explode(', ', $tname);
 685    foreach ($tags_arr as $i => $tag) {
 686      $matched_terms = taxonomy_get_term_by_name($tag);
 687      if (empty($matched_terms)) {
 688        //This tag is not created yet. Create it
 689        $term = new stdClass();
 690        $term->vid = $vid; //Save new term in 'Categories' vocabulary
 691        $term->name = $tag;
 692        taxonomy_term_save($term);
 693        $result[] = array(
 694          'tid' => $term->tid,
 695          'taxonomy_term' => $term,
 696        );
 697      }
 698      else {
 699        foreach ($matched_terms as $j => $term) {
 700          $result[] = array(
 701          'tid' => $term->tid,
 702          'taxonomy_term' => $term,
 703          );
 704        }
 705      }
 706    }
 707  }
 708
 709  return $result;
 710}
 711
 712/**
 713 * Make d7 taxonomy array from term name
 714 */
 715  function myimport_make_taxonomy_from_term($tag, $vname) {
 716    $result = array();
 717
 718  switch ($vname) {
 719    case 'fabric_type':
 720      $vid = _myimport_get_vid($vname);
 721
 722    case 'color':
 723      $vid = _myimport_get_vid($vname);
 724    
 725    case 'veneer':
 726      $vid = _myimport_get_vid($vname);
 727
 728  }
 729
 730
 731  $matched_terms = taxonomy_get_term_by_name($tag);
 732  if (empty($matched_terms)) {
 733    //This tag is not created yet. Create it
 734    $term = new stdClass();
 735    $term->vid = $vid;
 736    $term->name = $tag;
 737    taxonomy_term_save($term);
 738    $result[] = array(
 739      'tid' => $term->tid,
 740      'taxonomy_term' => $term,
 741    );
 742  }
 743  else {
 744    foreach ($matched_terms as $j => $term) {
 745      $result[] = array(
 746        'tid' => $term->tid,
 747        'taxonomy_term' => $term,
 748      );
 749    }
 750  }
 751
 752  return $result;
 753}
 754
 755/**
 756 * helper: get d7 vid  from vocabulary machine name
 757 */
 758 function _myimport_get_vid($vname) {
 759    $data = array(
 760    'albums' => 7,
 761    'color' => 3,
 762    'company' => 13,
 763    'fabric_type' => 5,
 764    'lifetime' => 9,
 765    'newsletter' => 2,
 766    'noise_level' => 6,
 767    'price_range' => 8,
 768    'product_type' => 12,
 769    'recommendation' => 11,
 770    'tags' => 1,
 771    'veneer' => 4,
 772    'vogue' => 10,
 773    );
 774    if (isset($data[$vname])) {
 775    return $data[$vname];
 776    }
 777    else {
 778    return NULL;
 779    }
 780 }
 781
 782/**
 783 * helper: get d7 vid  from d6 vid
 784 */
 785 function _myimport_vid6_to_vid7($vid6) {
 786    $data = array(
 787    1 => 7,
 788    //'color' => 3,
 789    4 => 13,
 790    //'fabric_type' => 5,
 791    7 => 9,
 792    //'newsletter' => 2,
 793    //'noise_level' => 6,
 794    6 => 8,
 795    3 => 12,
 796    9 => 11,
 797    2 => 1,
 798    //'veneer' => 4,
 799    8 => 10,
 800    );
 801    if (isset($data[$vid6])) {
 802    return $data[$vid6];
 803    }
 804    else {
 805    return NULL;
 806    }
 807 }
 808
 809/**
 810 * convert d6 format number to d7 format name
 811 */
 812 function myimport_convert_format($format_id) { //////////// CHECKED
 813    $format_id = (int)$format_id;
 814    switch ($format_id) {
 815    case 1:
 816      return 'filtered_html';
 817      break;
 818    case 2:
 819      return 'full_html';
 820      break;
 821    case 5:
 822      return 'administrative';
 823      break;
 824    case 6:
 825      return 'sellers';
 826      break;
 827    default:
 828      return 'filtered_html';
 829      break;
 830    }
 831 }
 832
 833/**
 834 * Convert a d6 file array to d7 stdClass file object and save file
 835 */
 836  function myimport_file($file_array) { /////// CHECKED
 837    static $store = array();
 838
 839  $file_path = $file_array['filepath'];
 840  $file_path = str_replace('sites/www.jahizieh.net/files/', '', $file_path);
 841  $file_path = file_build_uri($file_path);
 842  //dpm($file_array);
 843  if (empty($file_path)) return NULL;
 844
 845  if (isset($store[$file_path])) {
 846    $sql = "SELECT fid FROM {file_managed} WHERE uri = '$file_path'";
 847    $sql_result = db_query($sql);
 848    $row = $sql_result->fetchObject();
 849    if (empty($row)) {
 850      drupal_set_message("File error: " . $file_path);
 851      return NULL;
 852    }
 853    $fid = $row->fid;
 854    return file_load($fid);
 855  }
 856  else {
 857    $store[$file_path] = $file_path;
 858  }
 859
 860  drush_print("Importing file $file_path");
 861  $file = new stdClass();
 862  $file->uid = $file_array['uid'];
 863  $file->filename = $file_array['filename'];
 864  $file->uri = $file_path;
 865  $file->filemime = $file_array['filemime'];
 866  $file->filesize = $file_array['filesize'];
 867  $file->status = $file_array['status'];
 868  $file->timestamp = $file_array['timestamp'];
 869
 870  $new_file = file_save($file);
 871  drush_print("Created file #{$new_file->fid} at {$file_path}");
 872  return $new_file;
 873}
 874
 875/**
 876 * Import multiple files
 877 */
 878 function myimport_file_multiple($files_array1) { /////// CHECKED
 879    $result = array();
 880    foreach ($files_array1 as $c => $file_array2) {
 881    if (is_array($file_array2) && isset($file_array2['filepath'])) {
 882      $result[$c] = (array)myimport_file($file_array2);
 883    }
 884    }
 885    return $result;
 886 }
 887
 888/**
 889 * Import all comments associated with a node in d6 to Drupal7
 890 */
 891  function myimport_import_node_comments($d6_nid, $d7_nid) {  /////// CHECKED
 892    //Connect to d6 database to fetch commetns
 893    $d6_database = array(
 894      'database' => 'mydb',
 895      'username' => 'user',
 896      'password' => 'pass',
 897      'host' => 'localhost',
 898      'driver' => 'mysql',
 899    );
 900    Database::addConnectionInfo('MyImportD6DB', 'default', $d6_database);
 901    db_set_active('MyImportD6DB');
 902
 903  $sql = "SELECT * FROM {comments} WHERE nid=$d6_nid ORDER BY cid ASC";
 904  $sql_result = db_query($sql);
 905  $rows = $sql_result->fetchAll();
 906
 907  //Map d6 cid to d7 cid s
 908  // as [$d6_cid] => [$d7_cid]
 909  static $comment_mapping = array();
 910
 911  foreach ($rows as $c => $comment) {
 912    $cid        = $comment->cid;
 913    $pid        = $comment->pid;
 914    $comment_nid= $comment->nid;
 915    $uid        = $comment->uid;
 916    $subject    = $comment->subject;
 917    $comment_body= $comment->comment;
 918    $hostname   = $comment->hostname;
 919    $timestamp  = $comment->timestamp;
 920    $status     = $comment->status;
 921    $format     = $comment->format;
 922    $thread     = $comment->thread;
 923    $name       = $comment->name;
 924    $mail       = $comment->mail;
 925    $homepage   = $comment->homepage;
 926
 927    $d7comment = new stdClass();
 928    $d7comment->cid = NULL; //to avoid errors caused by drupal 7.14 bugs.
 929    $d7comment->pid = (isset($comment_mapping[$pid]) ? $comment_mapping[$pid] : 0);
 930    $d7comment->nid = $d7_nid;
 931    $d7comment->uid = $uid;
 932    $d7comment->subject = $subject;
 933    $d7comment->comment_body = array(
 934      'und' => array(
 935        0 => array(
 936          'value' => $comment_body,
 937          'format' => myimport_convert_format($format),
 938        ),
 939      ),
 940    );
 941    $d7comment->hostname = $hostname;
 942    $d7comment->created = $timestamp;
 943    $d7comment->status = ($status == 0 ? 1 : 0);
 944    //$d7comment->format
 945    $d7comment->thread = $thread;
 946    $d7comment->name = $name;
 947    $d7comment->mail = $mail;
 948    $d7comment->homepage = $homepage;
 949    $d7comment->notify = 0; //comment_notify module
 950    
 951    //return to default db for comment_save()
 952    db_set_active();
 953    comment_save($d7comment);
 954    db_set_active('MyImportD6DB');
 955    
 956    $comment_mapping[$comment->cid] = $d7comment->cid;
 957  }
 958
 959  //return to default db
 960  db_set_active();
 961}
 962
 963/**
 964 * Helper function: Remove extra empty elements from d6 field
 965 * This is caused by multiple value fields that aren't unlimited
 966 */
 967  function _myimport_trim_field($field) {
 968    $new_field = array();
 969    foreach ($field as $c => $field_item) {
 970    if (is_array($field[$c])) {
 971      $new_field[] = $field[$c];
 972    }
 973    }
 974
 975  return $new_field;
 976}
 977
 978/**
 979 * Helper function: convert d6 term id to term name
 980 */
 981 function _myimport_d6_tid_to_tname($d6tid) {
 982    $data = array (
 983    1 => 'مبلمان',
 984    2 => 'ظرف',
 985    105 => 'SAMSUNG',
 986    106 => 'Omidea',
 987    107 => 'مبل',
 988    108 => 'هنر',
 989    4 => 'جديد',
 990    5 => 'ويژه',
 991    6 => 'تختخواب',
 992    7 => 'سفارش',
 993    8 => 'سفارش',
 994    9 => 'سفارش',
 995    10 => 'فرانسه',
 996    11 => 'فرانسوي',
 997    12 => 'سيب',
 998    13 => 'عالي',
 999    15 => 'قشثل',
1000    17 => 'مبلمان',
1001    18 => 'مبلمان',
1002    19 => 'بوفه',
1003    20 => 'سرويس',
1004    21 => 'تخت',
1005    22 => 'پتو',
1006    33 => 'سرويس',
1007    23 => 'ميز',
1008    24 => 'دکوراسيون',
1009    104 => 'طرح',
1010    102 => 'مبل',
1011    103 => 'شركت',
1012    28 => 'پارچ',
1013    29 => 'بشقاب',
1014    30 => 'ليوان',
1015    31 => 'ديس',
1016    32 => 'ماهيتابه',
1017    34 => 'اجاق',
1018    35 => 'دوام',
1019    36 => 'دوام',
1020    37 => 'دوام',
1021    38 => 'دوام',
1022    39 => 'دوام',
1023    40 => 'يکبار',
1024    41 => 'ارزان',
1025    42 => 'متوسط',
1026    43 => 'نسبتا',
1027    44 => 'گران',
1028    45 => 'بسيار',
1029    46 => 'متداول',
1030    47 => 'کم',
1031    48 => 'نادر',
1032    49 => '100%',
1033    50 => '80%',
1034    51 => '60%',
1035    52 => '50%',
1036    53 => '40%',
1037    54 => '20%',
1038    55 => 'فقغ',
1039    56 => 'تتاا',
1040    57 => 'يف',
1041    58 => 'مبلمان',
1042    59 => 'فرفورژه',
1043    60 => 'مبلمان',
1044    61 => 'ميز',
1045    62 => 'ميز',
1046    63 => 'کابينت',
1047    64 => 'آينه',
1048    65 => 'سيسموني',
1049    66 => 'جالباسي',
1050    67 => 'کتابخانه',
1051    68 => 'دراور',
1052    69 => 'صندلي',
1053    70 => 'ميز',
1054    71 => 'تابلوهاي',
1055    72 => 'مبلمان',
1056    73 => 'مبلمان',
1057    74 => '',
1058    75 => 'ويترين',
1059    76 => 'نماي',
1060    77 => 'مجموعه',
1061    78 => 'ميز',
1062    79 => 'پارچه',
1063    80 => 'زيبا',
1064    81 => 'راحتي',
1065    82 => 'شرکت',
1066    83 => 'مبل',
1067    84 => 'شرکت',
1068    85 => 'مبلمان',
1069    86 => 'مبلمان',
1070    87 => 'شرکت',
1071    88 => 'راحتي',
1072    89 => 'ميزتلفن',
1073    90 => 'صنايع',
1074    91 => 'لوازم',
1075    92 => 'اتو',
1076    93 => 'ماشين',
1077    94 => 'ماشين',
1078    95 => 'ماكروفر',
1079    96 => 'پلوپز',
1080    97 => 'چرخ',
1081    98 => 'چرخ',
1082    99 => 'جارو',
1083    100 => 'يخچال',
1084    101 => 'لوازم',
1085    109 => 'کاناپه',
1086    110 => 'مبل',
1087    111 => 'تختخوابشو',
1088    112 => 'شيشه',
1089    113 => 'توليدي',
1090    114 => 'مبل',
1091    115 => 'مبل',
1092    116 => 'راحتي',
1093    117 => 'مبل',
1094    118 => 'وارداتي',
1095    119 => 'هارداستون',
1096    120 => 'بسيار',
1097    121 => 'يخچال',
1098    122 => 'ايران',
1099    123 => 'ويداس',
1100    124 => 'غذاساز',
1101    125 => 'چرخ',
1102    126 => 'سايد',
1103    127 => 'سامسونگ',
1104    128 => 'جادار',
1105    129 => 'راحتي',
1106    130 => 'خيلي',
1107    131 => 'راحتي',
1108    132 => 'طرح',
1109    133 => 'مبل',
1110    134 => 'خيلي',
1111    135 => 'طراحي',
1112    136 => 'شرکت',
1113    137 => 'خيلي',
1114    138 => 'خيلي',
1115    139 => 'خيلي',
1116    140 => 'ميز',
1117    141 => 'هنر',
1118    142 => 'شيک',
1119    143 => 'اجاق',
1120    144 => 'جبال',
1121    145 => 'طرح',
1122    146 => 'راحتي',
1123    147 => 'ايران',
1124    148 => 'مبل',
1125    149 => 'تختخواب',
1126    150 => 'ايران،',
1127    151 => 'تخت',
1128 );
1129
1130  if (isset($data[$d6tid])) {
1131    return $data[$d6tid];
1132  }
1133  else {
1134    return NULL;
1135  }
1136}

نظرات شما

قسمت نظرات با استفاده از سرویس دیسکاس پیاده سازی شده است. متاسفانه این سرویس از داخل ایران قابل دسترس نیست. لطفا از آی پی خارجی استفاده کنید.