Index: extensions/Back2Front/Back2Front.php
===================================================================
--- extensions/Back2Front/Back2Front.php	(revision 10821)
+++ extensions/Back2Front/Back2Front.php	(revision 10823)
@@ -65,157 +65,177 @@
   $conf['back2front'] = explode(',',$conf['back2front']);
   
-  if ($page['page'] == 'picture_modify')
-  {
-    /* change values */
-    if (isset($_POST['b2f_submit']))
-    {
-      /* picture is verso */
-      if (isset($_POST['b2f_is_verso']))
-      {
-        /* frontside exists */
-        if (picture_exists($_POST['b2f_front_id']))
+  if ($page['page'] != 'picture_modify')
+  {
+    return;
+  }
+  
+/* SAVE VALUES */
+  if (isset($_POST['b2f_submit']))
+  {
+    /* picture is verso */
+    if (isset($_POST['b2f_is_verso']))
+    {
+      /* catch all verso and recto ids */
+      $query = "SELECT image_id, verso_id
+        FROM ".B2F_TABLE.";";
+      $all_recto_verso = array_combine(array_from_query($query, 'image_id'), array_from_query($query, 'verso_id'));
+      
+      /* verso don't exists */
+      if (!picture_exists($_POST['b2f_front_id']))
+      {
+        $template->append('errors', l10n('Unknown id for frontside picture : ').$_POST['b2f_front_id']);
+      }
+      /* verso same as recto  */
+      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
+      {
+        $template->append('errors', l10n('Backside and frontside can\'t be the same picture'));
+      }
+      /* recto has already a verso */
+      else if (in_array($_POST['b2f_front_id'], array_keys($all_recto_verso)))
+      {
+          $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
+          $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$recto_current_verso['id'];
+          $template->append('errors', l10n('This picture has already a backside : ').'<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>');
+      }
+      /* recto is already a verso */
+      else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso)))
+      {
+          $recto_is_verso['id'] = $_POST['b2f_front_id'];
+          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$recto_is_verso['id'];
+          $template->append('errors', l10n('This picture is already a backside : ').'<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>');
+      }
+      /* everything is fine */
+      else
+      {
+        // get current categories
+        $query = "SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";";
+        $verso_categories = array_from_query($query, 'category_id');
+        
+        // insert or update verso associations
+        pwg_query("INSERT INTO ".B2F_TABLE."
+          VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".implode(',',$verso_categories)."')
+          ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].";");
+        
+        // move the verso ?
+        if (isset($_POST['b2f_move_verso']))
         {
-          /* search if recto has already a verso */
-          $query = "SELECT verso_id
-            FROM ".B2F_TABLE."
-            WHERE image_id = ".$_POST['b2f_front_id'].";";
-          $result = pwg_query($query);
-          list($recto_current_verso['id']) = pwg_db_fetch_row($result);
-          
-          if (pwg_db_num_rows($result) AND $recto_current_verso['id'] != $_GET['image_id'])
+          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
+            WHERE image_id = ".$_GET['image_id'].";");
+            
+          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."
+            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].", NULL);");
+            
+          // random representant for each categories
+          set_random_representant($verso_categories);
+        }
+      
+        $template->assign(array(
+          'B2F_IS_VERSO' => 'checked="checked"',
+          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
+        ));
+        
+        $template->append('infos', l10n('This picture is now the backside of the picture n° ').$_POST['b2f_front_id']);
+      }
+    }
+    /* picture isn't verso */
+    else
+    {
+      /* search if it was a verso */
+      $query = "SELECT categories
+        FROM ".B2F_TABLE."
+        WHERE verso_id = ".$_GET['image_id'].";";
+      $result = pwg_query($query);
+      
+      /* it must be restored to its original categories (see criteria on maintain.inc) */
+      if (pwg_db_num_rows($result))
+      {
+        // original categories
+        list($item['categories']) = pwg_db_fetch_row($result);
+        // catch current categories
+        $versos_infos = pwg_query("SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";");
+        while (list($verso_cat) = pwg_db_fetch_row($versos_infos))
+        {
+          $current_verso_cats[] = $verso_cat;
+        }
+        
+        /* if verso € 'versos' cat only */
+        if (count($current_verso_cats) == 1 AND $current_verso_cats[0] == $conf['back2front'][0])
+        {
+          foreach (explode(',',$item['categories']) as $cat)
           {
-            $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$recto_current_verso['id'];
-            $template->append('errors', l10n('This picture has already a backside : ').'<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>');
+            $datas[] = array(
+              'image_id' => $_GET['image_id'],
+              'category_id' => $cat,
+              );
           }
-          /* recto is clean */
-          else
+          if (isset($datas))
           {
-            $verso_categories = implode(',',array_keys($template->get_template_vars('associated_options')));
-            pwg_query("INSERT INTO ".B2F_TABLE."
-              VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".$verso_categories."')
-              ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].";");
-            
-            /* move the verso ? */
-            if (isset($_POST['b2f_move_verso']))
-            {
-              pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
-                WHERE image_id = ".$_GET['image_id'].";");
-                
-              pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."
-                VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].", NULL);");
-            }
-          
-            $template->assign(array(
-              'B2F_IS_VERSO' => 'checked="checked"',
-              'B2F_FRONT_ID' => $_POST['b2f_front_id'],
-            ));
-            
-            $template->append('infos', l10n('This picture is now the backside of the picture n° ').$_POST['b2f_front_id']);
+            mass_inserts(
+              IMAGE_CATEGORY_TABLE,
+              array('image_id', 'category_id'),
+              $datas
+              );
           }
         }
-        else
-        {
-          $template->append('errors', l10n('Unknown id for frontside picture : ').$_POST['b2f_front_id']);
-        }
-      }
-      /* picture isn't verso */
-      else
-      {
-        /* search if it was a verso */
-        $query = "SELECT categories
-          FROM ".B2F_TABLE."
-          WHERE verso_id = ".$_GET['image_id'].";";
-        $result = pwg_query($query);
-        
-        /* it must be restored to its original categories (see criteria on maintain.inc) */
-        if (pwg_db_num_rows($result))
-        {
-          /* original categories */
-          list($item['categories']) = pwg_db_fetch_row($result);
-          /* catch current categories */
-          $versos_infos = pwg_query("SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";");
-          while (list($verso_cat) = pwg_db_fetch_row($versos_infos))
-          {
-            $current_verso_cats[] = $verso_cat;
-          }
-          /* if verso € 'versos' cat only */
-          if (count($current_verso_cats) == 1 AND $current_verso_cats[0] == $conf['back2front'][0])
-          {
-            foreach (explode(',',$item['categories']) as $cat)
-            {
-              $datas[] = array(
-                'image_id' => $_GET['image_id'],
-                'category_id' => $cat,
-                );
-            }
-            if (isset($datas))
-            {
-              mass_inserts(
-                IMAGE_CATEGORY_TABLE,
-                array('image_id', 'category_id'),
-                $datas
-                );
-            }
-          }
+        
+        pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
+          WHERE image_id = ".$_GET['image_id']." AND category_id = ".$conf['back2front'][0].";");
+        
+        pwg_query("DELETE FROM ".B2F_TABLE." 
+          WHERE verso_id = ".$_GET['image_id'].";");
           
-          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
-            WHERE image_id = ".$_GET['image_id']." AND category_id = ".$conf['back2front'][0].";");
-          
-          pwg_query("DELETE FROM ".B2F_TABLE." 
-            WHERE verso_id = ".$_GET['image_id'].";");
-            
-          $template->append('infos', l10n('This picture is no longer a backside'));
-        }
-      }
-    }
-    /* get saved values */
+        $template->append('infos', l10n('This picture is no longer a backside'));
+      }
+    }
+  }
+  
+/* GET SAVED VALUES */
+  if ($template->get_template_vars('B2F_IS_VERSO') == null)
+  {
+    /* is the picture a verso ? */
+    $query = "
+      SELECT image_id
+      FROM ".B2F_TABLE."
+      WHERE verso_id = ".$_GET['image_id']."
+    ;";
+    $result = pwg_query($query);
+    
+    if (pwg_db_num_rows($result))
+    {
+      list($recto_id) = pwg_db_fetch_row($result);
+      $template->assign(array(
+        'B2F_IS_VERSO' => 'checked="checked"',
+        'B2F_FRONT_ID' => $recto_id,
+      ));
+    }
+    /* is the picture a front ? */
     else
     {
-      /* is the pisture a verso ? */
-      $query = "
-        SELECT image_id
+      $query = "SELECT verso_id
         FROM ".B2F_TABLE."
-        WHERE verso_id = ".$_GET['image_id']."
-      ;";
+        WHERE image_id = ".$_GET['image_id'].";";
       $result = pwg_query($query);
       
       if (pwg_db_num_rows($result))
       {
-        list($recto_id) = pwg_db_fetch_row($result);
+        include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
+        
+        $item = pwg_db_fetch_assoc($result);
+        $query = "SELECT id, name, file
+          FROM ".IMAGES_TABLE."
+          WHERE id = ".$item['verso_id'].";";
+        $item = pwg_db_fetch_assoc(pwg_query($query));
+        
         $template->assign(array(
-          'B2F_IS_VERSO' => 'checked="checked"',
-          'B2F_FRONT_ID' => $recto_id,
+          'B2F_VERSO_ID' => $item['id'],
+          'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$item['id'],
+          'B2F_VERSO_NAME' => get_image_name($item['name'], $item['file']),
         ));
       }
-      /* is the picture a front ? */
-      else
-      {
-        $query = "SELECT verso_id
-          FROM ".B2F_TABLE."
-          WHERE image_id = ".$_GET['image_id'].";";
-        $result = pwg_query($query);
-        
-        if (pwg_db_num_rows($result))
-        {
-          include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
-          
-          $item = pwg_db_fetch_assoc($result);
-          $query = "SELECT id, name, file
-            FROM ".IMAGES_TABLE."
-            WHERE id = ".$item['verso_id'].";";
-          $item = pwg_db_fetch_assoc(pwg_query($query));
-          
-          $template->assign(array(
-            'B2F_VERSO_ID' => $item['id'],
-            'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$item['id'],
-            'B2F_VERSO_NAME' => get_image_name($item['name'], $item['file']),
-          ));
-        }
-      }
-    }
-    
-    $template->set_filename('B2F_picture_modify', dirname(__FILE__).'/template/picture_modify.tpl');
-    $template->concat('ADMIN_CONTENT', $template->parse('B2F_picture_modify', true));
-  }
+    }
+  }
+  
+  $template->set_filename('B2F_picture_modify', dirname(__FILE__).'/template/picture_modify.tpl');
+  $template->concat('ADMIN_CONTENT', $template->parse('B2F_picture_modify', true));
 }
 
