<?php
/*
 * Script for create multiple categories for Magento v1.0
 *
 * @author Peter Kadlec
 * @version 0.1
 */

/* 
 * This script create multiple categories from given CSV or TXT file.
 * It creates ONE category with subcategories from each file.
 * Main category identification MUST be on the FIRST line of file.  
 * For proper functionality, follow instructions:
 *  1) create CSV or TXT file with following structure:
 *     "category_name";"page_title";"url_key";"description";"meta_keywords";"meta_description"
 *  2) on line 43 change path to your own file
 *  3) change connecting information to your database
 *  4) run the script  
 *  If you want to create more than one category with subcategories, 
 *  repeat this for each category (of course with correct file given)    
 */  																																																																																																																																																																																																																																																										
   
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PWD', '');
DEFINE ('DB_SERVER', 'localhost');
DEFINE ('DB_NAME', 'db_testing');
  
if ($dbc = @mysql_connect (DB_SERVER, DB_USER, DB_PWD)) {
  if (!mysql_select_db (DB_NAME)) {
    echo "Can't open database: ". mysql_error();
    exit();
  }
} else {
    echo "Can't connect to server: ". mysql_error();
	  exit();
}

mysql_query('SET character_set_results=cp1250');
mysql_query('SET character_set_connection=cp1250');
mysql_query('SET character_set_client=cp1250');

// PATH to your file with categories to be created
$fileContent = file ('categories.csv');

$mainCategoryCount = 0;
$positionSubCat = 0;
while (list ($line_num, $line) = each ($fileContent)) {
  $vars = str_replace('"','', explode(";", $line));
  $mainCategoryCount += 1;
  mysql_query('INSERT INTO catalog_category_entity VALUES ("","3","3","0",NOW(),NOW(),"1","","")');
  $entityID = mysql_insert_id();
    if($mainCategoryCount == 1) {
      $mainCategoryID = $entityID;
      $path = "1/2/".$mainCategoryID;
      $position = 1;
      $rewriteCategoryPath = $vars[2];
      $rewritePath = $rewriteCategoryPath;
    } else {
      $path = "1/2/".$mainCategoryID."/".$entityID;
      $positionSubCat += 1; 
      $position = $positionSubCat;
      $rewritePath = $rewriteCategoryPath."/".$vars[2];
    }      
  mysql_query('UPDATE catalog_category_entity SET path = "'.$path.'", position = '.$position.' WHERE entity_id = '.$entityID);    
  mysql_query('INSERT INTO catalog_category_entity_int VALUES ("","3","32","0","'.$entityID.'","1")');
  mysql_query('INSERT INTO catalog_category_entity_int VALUES ("","3","40","0","'.$entityID.'","1")');
  mysql_query('INSERT INTO catalog_category_entity_text VALUES ("","3","25","0","'.$entityID.'","'.$vars[3].'")');
  mysql_query('INSERT INTO catalog_category_entity_text VALUES ("","3","28","0","'.$entityID.'","'.$vars[4].'")');
  mysql_query('INSERT INTO catalog_category_entity_text VALUES ("","3","29","0","'.$entityID.'","'.$vars[5].'")');
  mysql_query('INSERT INTO catalog_category_entity_text VALUES ("","3","44","0","'.$entityID.'","")');
  mysql_query('INSERT INTO catalog_category_entity_varchar VALUES ("","3","22","0","'.$entityID.'","'.$vars[0].'")');
  mysql_query('INSERT INTO catalog_category_entity_varchar VALUES ("","3","24","0","'.$entityID.'","'.$vars[2].'")');
  mysql_query('INSERT INTO catalog_category_entity_varchar VALUES ("","3","27","0","'.$entityID.'","'.$vars[1].'")');
  mysql_query('INSERT INTO catalog_category_entity_varchar VALUES ("","3","30","0","'.$entityID.'","PRODUCTS")');
  mysql_query('INSERT INTO catalog_category_entity_varchar VALUES ("","3","39","0","'.$entityID.'","")');
  mysql_query('INSERT INTO catalog_category_entity_varchar VALUES ("","3","43","0","'.$entityID.'","")');
  mysql_query('INSERT INTO catalog_category_entity_varchar VALUES ("","3","38","1","'.$entityID.'","'.$rewritePath.'")');
  mysql_query('INSERT INTO catalog_category_entity_varchar VALUES ("","3","38","0","'.$entityID.'","'.$rewritePath.'")');
  mysql_query('INSERT INTO core_url_rewrite VALUES ("","1","'.$entityID.'",NULL,"category/'.$entityID.'","'.$rewritePath.'","catalog/category/view/id/'.$entityID.'","1","", NULL)');
}
?> 