add 'property' cck node using drupal_execute (d6)

  1. function mymodule_import() {
  2.     // get the old data
  3.     $old_db = mysql_connect("host", "user", "pass") or die('Could not connect to mysql server.' );
  4.     mysql_select_db('old_db', $old_db) or die('Could not select database.');
  5.     $sql = "select * from old_table";
  6.     $result = mysql_query($sql, $old_db);
  7.     while ($row = mysql_fetch_object($result)) {
  8.         $data[] = $row;
  9.     }
  10.    
  11.     // till this is working, only do it with one row
  12.     $row = $data[0];
  13.  
  14.     $form_state = array();
  15.     module_load_include('inc', 'node', 'node.pages');
  16.     $node = array('type' => 'property');
  17.  
  18.     // array of values based on mapping between old & new dbs
  19.     $form_state['values']['field_id'][0]["value"] = $row->UniqueID;
  20.     $form_state['values']['field_block'][0]["value"] = $row->Block;
  21.     $form_state['values']['field_lot'][0]["value"] = $row->Lot;
  22.     $form_state['values']['field_zoning'][0]["value"] = $row->Zoning;
  23.     $form_state['values']['field_description'][0]["value"] = $row->Comment;
  24.     $form_state['values']['field_annual_tax'][0]["value"] = nums_only($row->Taxes);
  25.     $form_state['values']['field_tax_land'][0]["value"] = nums_only($row->Land_Assessment);
  26.     $form_state['values']['field_tax_improvements'][0]["value"] = nums_only($row->Improvement_Assesment);
  27.     $form_state['values']['field_address1'][0]["value"] = $row->Addr1;
  28.     $form_state['values']['field_address2'][0]["value"] = $row->Addr2;
  29.     $form_state['values']['field_city'][0]["value"] = $row->City;
  30.     $form_state['values']['field_state'][0]["value"] = $row->State;
  31.     $form_state['values']['field_zip'][0]["value"] = $row->Zip;
  32.    
  33.     // node meta data
  34.     $form_state['values']['status'] = 1;
  35.     $form_state['values']['name'] = 'Joe Manager';
  36.  
  37.     /*
  38.     these fields may be buggy because they're tricky or not in old db
  39.     # Region
  40.     # Type
  41.     # Use
  42.     # Building Size
  43.     # Land Area
  44.     # Land Unit
  45.     # Price
  46.     # Price Type
  47.     # Commission
  48.     # Commission Type
  49.     # From date
  50.     # Owner
  51.     */
  52.  
  53.     $form_state['values']['field_region'][0]["value"] = "Eastern Region";
  54.     $form_state['values']['field_type'][0]["value"] = $row->Proptype;
  55.     $form_state['values']['field_use'][0]["value"] = $row->Proptype;
  56.     $form_state['values']['field_building_size'][0]["value"] = nums_only($row->Building_Size);
  57.     $form_state['values']['field_land_area'][0]["value"] = nums_only($row->Land_Size);
  58.     $form_state['values']['field_land_unit'][0]["value"] = "acres";
  59.     $form_state['values']['field_land_area'][0]["value"] = nums_only($row->Land_Size);
  60.     $form_state['values']['field_price'][0]["value"] = nums_only($row->Price);
  61.     $form_state['values']['field_price_type'][0]["value"] = "gross";
  62.     $form_state['values']['field_commission'][0]["value"] = 1;
  63.     $form_state['values']['field_commission_type'][0]["value"] = "%";
  64.     $form_state['values']['field_owner'][0]["uid"] = "11";
  65.  
  66.     // this one should mostly work, but there may be some problems
  67.     $form_state['values']['field_contract_type'][0]["value"] = "For " . $row->PriceType;
  68.    
  69.     drupal_execute('property_node_form', $form_state, (object)$node);
  70.     $msg = "wrote property " . $row->UniqueID . " (or tried to, anyway)";
  71.     drupal_set_message($msg);
  72.     drupal_goto('properties');    
  73. }
  74.  
  75. function nums_only($str) {
  76.     return preg_replace("/[^0-9]/", "", $str);
  77. }