Index: /extensions/physical2virtual/admin.php
===================================================================
--- /extensions/physical2virtual/admin.php	(revision 27163)
+++ /extensions/physical2virtual/admin.php	(revision 27164)
@@ -5,25 +5,24 @@
 global $conf, $template, $page;
 
-if (isset($_POST['submit']))
+if (isset($_POST['submit']) or isset($_POST['convert']))
 {
   $conf['physical2virtual'] = array(
     'lock_physical' => isset($_POST['lock_physical']), 
     'private_physical' => isset($_POST['private_physical']), 
+    'store_structure' => isset($_POST['store_structure']), 
     );
   if (isset($_POST['parent_cat']) and $_POST['parent_cat'] > 0)
     $conf['physical2virtual']['parent_cat'] = $_POST['parent_cat'];
 
-  conf_update_param('physical2virtual', serialize($conf['physical2virtual']));
-  $page['infos'][] = l10n('Information data registered in database');
-}
-if (isset($_POST['convert']))
-{
-  $conf['physical2virtual']['lock_physical'] = isset($_POST['lock_physical']);
-  $conf['physical2virtual']['private_physical'] = isset($_POST['private_physical']);
-  if (isset($_POST['parent_cat']) and $_POST['parent_cat'] > 0)
-    $conf['physical2virtual']['parent_cat'] = $_POST['parent_cat'];
-
-  physical2virtual_convert();
-  $page['infos'][] = l10n('Convertion done');
+  if (isset($_POST['submit']))
+  {
+    conf_update_param('physical2virtual', serialize($conf['physical2virtual']));
+    $page['infos'][] = l10n('Information data registered in database');
+  }
+  if (isset($_POST['convert']))
+  {
+    physical2virtual_convert();
+    $page['infos'][] = l10n('Convertion done');
+  }
 }
 
Index: /extensions/physical2virtual/language/en_UK/plugin.lang.php
===================================================================
--- /extensions/physical2virtual/language/en_UK/plugin.lang.php	(revision 27163)
+++ /extensions/physical2virtual/language/en_UK/plugin.lang.php	(revision 27164)
@@ -6,4 +6,5 @@
 $lang['Convert now'] = 'Convert now';
 $lang['Convertion done'] = 'Convertion done';
+$lang['Store folder structure'] = 'Store folder structure';
 
 ?>
Index: /extensions/physical2virtual/language/ru_RU/plugin.lang.php
===================================================================
--- /extensions/physical2virtual/language/ru_RU/plugin.lang.php	(revision 27163)
+++ /extensions/physical2virtual/language/ru_RU/plugin.lang.php	(revision 27164)
@@ -5,5 +5,5 @@
 $lang['Make physical albums private after converting'] = 'Делать физические альбомы приватными после конвертирования';
 $lang['Convert now'] = 'Конвертировать сейчас';
-$lang['Convertion done'] = 'Конвертирование выполнено';
+$lang['Store folder structure'] = 'Сохранять структуру папок';
 
 ?>
Index: /extensions/physical2virtual/main.inc.php
===================================================================
--- /extensions/physical2virtual/main.inc.php	(revision 27163)
+++ /extensions/physical2virtual/main.inc.php	(revision 27164)
@@ -30,4 +30,20 @@
 }
 
+function find_or_create_virtual_category($category_name, $parent_id=null, $options=array())
+{
+  if (!is_null($parent_id))
+  {
+    $sub_ids = get_subcat_ids(array($parent_id));
+    foreach ($sub_ids as $id)
+    {
+      $cat_info = get_cat_info($id);
+      if ($cat_info['name'] == $category_name and !$cat_info['dir'])
+        return $cat_info;
+    }
+  }
+
+  return create_virtual_category($category_name, $parent_id, $options);
+}
+
 function physical2virtual_convert()
 {
@@ -35,12 +51,5 @@
 
   $query = '
-    SELECT name
-    FROM '.CATEGORIES_TABLE.'
-    WHERE dir IS NULL
-  ;';  // get virtual
-  $virt_cats = array_from_query($query, 'name'); 
-  
-  $query = '
-    SELECT id, name
+    SELECT id, name, id_uppercat
     FROM '.CATEGORIES_TABLE.'
     WHERE dir IS NOT NULL
@@ -50,42 +59,45 @@
   {
     $virt_name = $row['name'];
-    $created = false;
-    if (!in_array($virt_name, $virt_cats))
+
+    $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
+        
+    if ($conf['physical2virtual']['store_structure'])
     {
-      $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
-      $virt_id = create_virtual_category($virt_name, $parent)['id'];
-      $created = true;
-
-      if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical'])
+      // get array of physical parents
+      $cat_info = get_cat_info($row['id']);
+      $uppers = $cat_info['upper_names'];
+      array_pop($uppers); // remove themself
+      
+      foreach ($uppers as $upper)
       {
-        $cat_info = get_cat_info($row['id']);
-        if ($cat_info['visible'] == true)
-        {
-          set_cat_visible(array($row['id']), false);
-        }
-      }
-      if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
-      {
-        set_cat_status(array($row['id']), 'private'); 
+        $parent = find_or_create_virtual_category($upper['name'], $parent)['id'];
       }
     }
-    else
+
+    $virt_cat = find_or_create_virtual_category($virt_name, $parent);
+//    $virt_cat = create_virtual_category($virt_name, $parent)['id']; // use this for duplicate folders name
+
+    if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical'])
     {
-      // Get existen virt category ID
-      $query = '
-        SELECT id FROM '.CATEGORIES_TABLE.' WHERE dir IS NULL AND name = \''.pwg_db_real_escape_string($virt_name).'\'
-      ;';
-      list($virt_id) = pwg_db_fetch_row(pwg_query($query));
+      $cat_info = get_cat_info($row['id']);
+      if ($cat_info['visible'] == true)
+      {
+        set_cat_visible(array($row['id']), false);
+      }
+    }
+    if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
+    {
+      set_cat_status(array($row['id']), 'private'); 
     }
 
     $query = '
       INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
-        SELECT '.$virt_id.', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
+        SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
     ;';
     pwg_query($query);
 
-    if ($created)
+    if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id']))
     {
-      set_random_representant(array($virt_id));
+      set_random_representant(array($virt_cat['id']));
     }
   }
Index: /extensions/physical2virtual/maintain.inc.php
===================================================================
--- /extensions/physical2virtual/maintain.inc.php	(revision 27163)
+++ /extensions/physical2virtual/maintain.inc.php	(revision 27164)
@@ -20,4 +20,5 @@
     'update_timeout' => 3,
     'last_update' => 0,
+    'store_structure' => true,
   );
 
Index: /extensions/physical2virtual/template/admin.tpl
===================================================================
--- /extensions/physical2virtual/template/admin.tpl	(revision 27163)
+++ /extensions/physical2virtual/template/admin.tpl	(revision 27164)
@@ -29,4 +29,10 @@
     </label>
   </li>
+  <li>
+    <input type="checkbox" id="store_structure" name="store_structure"{if $physical2virtual.store_structure} checked="checked"{/if}>
+    <label for="store_structure">
+      <b>{'Store folder structure'|@translate}</b>
+    </label>
+  </li>
 </ul>
 </fieldset>
