Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.90 KB | None | 0 0
  1. <?php
  2.  
  3. function dealer_migrate_menu() {
  4.   $items = array();
  5.   $items['admin/settings/dealer-migrate'] = array(
  6.     'title' => 'Dealer Migrate',
  7.     'description' => 'Simple page to migrate old dealer info to the new site.',
  8.     'page callback' => 'drupal_get_form',
  9.     'page arguments' => array('_dealer_migrate_action_form'),
  10.     'type' => MENU_NORMAL_ITEM,
  11.     'access arguments' => array('access content'),
  12.   );
  13.   return $items;
  14. }
  15.  
  16. function _dealer_migrate_action_form(&$form_state) {
  17.  
  18.   $form = array();
  19.  
  20.   $form['migrate'] = array(
  21.     '#type' => 'submit',
  22.     '#value' => t('Migrate Data'),
  23.     '#submit' => array('_dealer_migrate_submit'),
  24.   );
  25.   $form['clear'] = array(
  26.     '#type' => 'submit',
  27.     '#value' => t('Clear transferred data'),
  28.     '#submit' => array('_dealer_migrate_clear'),
  29.   );
  30.  
  31.   return $form;
  32. }
  33.  
  34. function _dealer_migrate_submit($form_id, &$form_state) {
  35.     $conn = mysql_connect('localhost','root','root');
  36.     mysql_select_db('oldcradlepoint');
  37.        
  38.     $locationlid = array();
  39.    
  40.     $locationdata = mysql_query("SELECT * FROM location", $conn);
  41.    
  42.     while($row = mysql_fetch_array($locationdata)) {
  43.         $locationinsert = array(
  44.             'lid' => $row['lid'],
  45.             'street' => $row['street'],
  46.             'additional' => $row['additional'],
  47.             'city' => $row['city'],
  48.             'province' => $row['province'],
  49.             'postal_code' => $row['postal_code'],
  50.             'country' => $row['country'],
  51.             'latitude' => $row['latitude'],
  52.             'longitude' => $row['longitude'],
  53.             'source' => $row['source'],
  54.             'is_primary' => $row['is_primary'],
  55.         );
  56.        
  57.         drupal_write_record('location', $locationinsert);
  58.         $last = db_last_insert_id('location', 'lid');
  59.        
  60.         $locationlid[$row['lid']] = $last;
  61.     }
  62.    
  63.     $newnodenids = array();
  64.    
  65.   $data = mysql_query("SELECT * FROM node WHERE type = 'dealer'", $conn);
  66.   while($row = mysql_fetch_array($data)) {
  67.     $vid = $row['vid'];
  68.     $nid = $row['nid'];
  69.     $title = $row['title'];
  70.  
  71.     $data2 = mysql_query("SELECT * FROM  content_type_dealer WHERE nid = $nid LIMIT 1", $conn);
  72.    
  73.     while($row2 = mysql_fetch_array($data2)) {
  74.         $vid = $row2['vid'];
  75.         $nid = $row2['nid'];
  76.         $website = $row2['field_dealer_website_url'];
  77.         $website_title = $row2['field_dealer_website_title'];
  78.         $phone_country_code = $row2['field_dealer_phone_country_code'];
  79.         $phone_area_code = $row2['field_dealer_phone_area_code'];
  80.         $phone_local_number = $row2['field_dealer_phone_local_number'];
  81.         $phone_number = $phone_area_code.$phone_local_number;
  82.        
  83.         $newnode = new stdClass();
  84.         $newnode->title = $title;
  85.         $newnode->body = "";
  86.        
  87.         global $user;
  88.                
  89.         $newnode->uid = $user->uid;
  90.         $newnode->type = 'partner_location';
  91.         $newnode->status = 1;
  92.         $newnode->promote = 0;
  93.         node_save($newnode);
  94.        
  95.         // because node_save passes in $newnode by REFERENCE, it fills in additional information to the node object once it's saved into the database. Like the new NID it will get
  96.        
  97.         $new_node_nid = $newnode->nid;
  98.        
  99.         $newnodenids[$row2['nid']] = $new_node_nid;
  100.        
  101.         $insert = array(
  102.           'vid' => $new_node_nid,
  103.           'nid' => $new_node_nid,
  104.           'field_dealer_phone_number_number' => $phone_number,
  105.           'field_dealer_phone_number_country_codes' => 'us',
  106.           'field_dealer_website_url' => $website,
  107.           'field_dealer_website_title' => $website_title,
  108.         );
  109.         drupal_write_record('content_type_partner_location', $insert, array("vid"));
  110.     }
  111.   }
  112.  
  113.   $locationinstancedata = mysql_query("SELECT * FROM location_instance", $conn);
  114.  
  115.   while($row = mysql_fetch_array($locationinstancedata)) {
  116.     $locationinstance = array(
  117.         'nid' => $newnodenids[$row['nid']],
  118.         'vid' => $newnodenids[$row['nid']],
  119.         'lid' => $locationlid[$row['lid']],
  120.     );
  121.     drupal_write_record('location_instance', $locationinstance);
  122.   }
  123. }
  124.  
  125. function _dealer_migrate_clear($form_id, &$form_state) {
  126.   // code to clear out what you did above
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement