Advertisement
Guest User

Untitled

a guest
May 30th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. <?php
  2.  
  3. if(!is_numeric($_GET['id']))die('Please specify a category ID');
  4.  
  5. $catId = $_GET['id'];
  6.  
  7. $xml = simplexml_load_file('app/etc/local.xml');
  8. $host = $xml->global->resources->default_setup->connection->host;
  9. $username = $xml->global->resources->default_setup->connection->username;
  10. $password = $xml->global->resources->default_setup->connection->password;
  11. $dbname = $xml->global->resources->default_setup->connection->dbname;
  12. $res = mysql_pconnect($host, $username, $password);
  13. mysql_select_db($dbname);
  14.  
  15. $catsDone = 0;
  16. duplicate_entity($catId);
  17.  
  18. // Fix the children count for all (sub)categories
  19. $sql = "CREATE TABLE catalog_category_entity_tmp LIKE catalog_category_entity";
  20. mysql_query($sql);
  21.  
  22. $sql = "INSERT INTO catalog_category_entity_tmp SELECT * FROM catalog_category_entity";
  23. mysql_query($sql);
  24.  
  25. $sql = "UPDATE catalog_category_entity cce SET children_count = ( SELECT count(cce2.entity_id) – 1 as children_county FROM catalog_category_entity_tmp cce2 WHERE PATH LIKE CONCAT(cce.path,'%') )";
  26. mysql_query($sql);
  27.  
  28. $sql = "DROP TABLE catalog_category_entity_tmp";
  29. mysql_query($sql);
  30.  
  31. echo $catsDone . '<p>Categories duplicated.</p>';
  32.  
  33. function duplicate_entity($id, $parent_id = null) {
  34.  
  35. global $catsDone;
  36.  
  37. mysql_query("SET NAMES 'utf8'");
  38. // Grab category to copy
  39. $sql = "SELECT * FROM catalog_category_entity WHERE entity_id = " . $id;
  40. $query_entity = mysql_query($sql);
  41.  
  42. $entity = mysql_fetch_object($query_entity);
  43.  
  44. if(!$parent_id) $parent_id = $entity->parent_id;
  45.  
  46. mysql_query("INSERT INTO catalog_category_entity (entity_type_id, attribute_set_id, parent_id, created_at, updated_at, path, position, level, children_count) VALUES ({$entity->entity_type_id}, {$entity->attribute_set_id}, {$parent_id}, NOW(), NOW(), '', {$entity->position}, {$entity->level}, {$entity->children_count})");
  47. $newEntityId = mysql_insert_id();
  48.  
  49. $query = mysql_query("SELECT path FROM catalog_category_entity WHERE entity_id = " . $parent_id);
  50. $parent = mysql_fetch_object($query);
  51. $path = $parent->path . '/' . $newEntityId;
  52.  
  53. mysql_query("UPDATE catalog_category_entity SET path='". $path."' WHERE entity_id=". $newEntityId);
  54.  
  55. foreach(array('datetime', 'decimal', 'int', 'text', 'varchar') as $dataType) {
  56. $sql = "SELECT * FROM catalog_category_entity_" . $dataType . " WHERE entity_id=" . $entity->entity_id;
  57. $query = mysql_query($sql);
  58.  
  59. while ($value = mysql_fetch_object($query)) if (is_null($value->value)) {
  60. mysql_query("INSERT INTO catalog_category_entity_".$dataType." (entity_type_id, attribute_id, store_id, entity_id, value) VALUES ({$value->entity_type_id}, {$value->attribute_id}, {$value->store_id}, {$newEntityId}, NULL)");
  61. } else {
  62. mysql_query("INSERT INTO catalog_category_entity_".$dataType." (entity_type_id, attribute_id, store_id, entity_id, value) VALUES ({$value->entity_type_id}, {$value->attribute_id}, {$value->store_id}, {$newEntityId}, '{$value->value}')");
  63. }
  64. }
  65.  
  66. //for Products
  67. $sql = "SELECT * FROM catalog_category_product WHERE category_id = " . $id;
  68. $query = mysql_query($sql);
  69.  
  70. while ($value = mysql_fetch_object($query)) {
  71. $sql = "INSERT INTO catalog_category_product (category_id, product_id, position) VALUES ({$newEntityId},{$value->product_id},{$value->position})";
  72. echo $sql;
  73.  
  74. if(!mysql_query($sql))
  75. echo("Error");
  76. }
  77.  
  78. $sql = "SELECT entity_id FROM catalog_category_entity WHERE parent_id = " . $id;
  79. $query = mysql_query($sql);
  80.  
  81. while ($entity = mysql_fetch_object($query)) {
  82. duplicate_entity($entity->entity_id, $newEntityId);
  83. }
  84. $catsDone++;
  85.  
  86. }
  87.  
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement