Index: extensions/typetags/admin.php
===================================================================
--- extensions/typetags/admin.php	(revision 12483)
+++ extensions/typetags/admin.php	(revision 15149)
@@ -26,5 +26,5 @@
 // +-----------------------------------------------------------------------+
 
-if (isset($_POST['edittypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])))
+if ( isset($_POST['edittypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])) )
 {
   $edited_typetag = array(
@@ -33,51 +33,40 @@
     'color' => $_POST['typetag_color'],
   );
-  $page['errors'][] = l10n('typetag_error');
+  
+  array_push($page['errors'],  l10n('typetag_error'));
 }
 else if (isset($_POST['edittypetag']))
 {
-  $typetag = mysql_escape_string($_POST['edited_typetag']);
-  $typetag_name = mysql_escape_string($_POST['typetag_name']);
-  $typetag_color = mysql_escape_string($_POST['typetag_color']);
-
-  $all_typetags = pwg_query("
-    SELECT 
-      id, 
-      name, 
-      color
-    FROM  `". typetags_TABLE ."`;
-  ");
-  
-  while ($row = mysql_fetch_array($all_typetags))
-  {
-    $existing_names[] = $row['name'];
-    if ($typetag == $row['id'])
-    {
-      $current_name = $row['name'];
-      $current_color = $row['color'];
-    }
-  }
-
   // we must not rename typetag with an already existing name
-  if ($typetag_name != $current_name AND in_array($typetag_name, $existing_names))
+  $query = '
+SELECT id
+  FROM '.typetags_TABLE.'
+  WHERE
+    name = "'.$_POST['typetag_name'].'"
+    AND id != '.$_POST['edited_typetag'].'
+;';
+
+  if ( pwg_db_num_rows(pwg_query($query)) )
   {
     $edited_typetag = array(
-      'id' => $typetag,
-      'name' => $typetag_name,
-      'color' => $typetag_color,
+      'id' => $_POST['edited_typetag'],
+      'name' => $_POST['typetag_name'],
+      'color' => $_POST['typetag_color'],
     );
     
-    $page['errors'][] = l10n('typetag_already_exists');
+    array_push($page['errors'], l10n('typetag_already_exists'));
   }
   else
   {
-    pwg_query("
-      UPDATE `". typetags_TABLE ."` SET 
-        `name` = '". $typetag_name ."',
-        `color` = '". $typetag_color ."'
-      WHERE `id` = ". $typetag .";
-    ");
-    
-    $page['infos'][] = l10n('typetag_saved');
+    $query = '
+UPDATE '.typetags_TABLE.'
+  SET 
+    name = "'.$_POST['typetag_name'].'",
+    color = "'.$_POST['typetag_color'].'"
+  WHERE id = '.$_POST['edited_typetag'].'
+;';
+    pwg_query($query);
+    
+    array_push($page['infos'], l10n('typetag_saved'));
   }
 }
@@ -89,29 +78,30 @@
 if (isset($_GET['deletetypetag']))
 {
-  $query = "
-    SELECT name
-    FROM `". typetags_TABLE ."`
-    WHERE id = ". $_GET['deletetypetag'] .";
-  ";
-  $typetag_name = array_from_query($query, 'name');
-  
-  if (count($typetag_name) != 0)
-  {
-    pwg_query("
-      UPDATE `". TAGS_TABLE ."`
-      SET id_typetags = NULL
-      WHERE id_typetags = ". $_GET['deletetypetag'] .";
-    ");
-
-    pwg_query("
-      DELETE FROM `". typetags_TABLE ."`
-      WHERE id = ". $_GET['deletetypetag'] .";
-    ");
-
-    $page['infos'][] = l10n('typetag_suppr').' : '.$typetag_name[0];
+  $query = '
+SELECT id
+  FROM '.typetags_TABLE.'
+  WHERE id = '.$_GET['deletetypetag'].'
+;';
+  
+  if ( pwg_db_num_rows(pwg_query($query)) )
+  {
+    $query = '
+UPDATE '.TAGS_TABLE.'
+  SET id_typetags = NULL
+  WHERE id_typetags = '.$_GET['deletetypetag'].'
+;';
+    pwg_query($query);
+    
+    $query = '
+DELETE FROM '.typetags_TABLE.'
+  WHERE id = '.$_GET['deletetypetag'].'
+;';
+    pwg_query($query);
+    
+    array_push($page['infos'], l10n('typetag_suppr'));
   }
   else
   {
-    $page['errors'][] = l10n('typetag_unknown').' : '.$_GET['deletetypetag'];
+    array_push($page['errors'], l10n('typetag_unknown'));
   }
 }
@@ -121,12 +111,12 @@
 // +-----------------------------------------------------------------------+
 
-if (isset($_POST['addtypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])))
+if ( isset($_POST['addtypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])) )
 {
   $template->assign('typetag', array(
-    'NAME' => isset($_POST['typetag_name']) ? $_POST['typetag_name'] : '',
-    'COLOR' => isset($_POST['typetag_color']) ? $_POST['typetag_color'] : '',
+    'NAME' => $_POST['typetag_name'],
+    'COLOR' => $_POST['typetag_color'],
   ));
   
-  $page['errors'][] = l10n('typetag_error');
+  array_push($page['errors'], l10n('typetag_error'));
 }
 else if (isset($_POST['addtypetag']))
@@ -136,34 +126,34 @@
 
   // does the tag already exists?
-  $query = "
-    SELECT id
-    FROM `". typetags_TABLE ."`
-    WHERE name = '". $typetag_name ."';
-  ";
-  $existing_tags = array_from_query($query, 'id');
-
-  if (count($existing_tags) == 0)
-  {
-    pwg_query("
-      INSERT INTO `". typetags_TABLE ."`(
-        `name`,
-        `color`
-      )
-      VALUES(
-        '". $typetag_name ."',
-        '". $typetag_color ."'
-      );
-    ");
-
-    $page['infos'][] = l10n('typetag_saved');
+  $query = '
+SELECT id
+  FROM '.typetags_TABLE.'
+  WHERE name = "'.$_POST['typetag_name'].'"
+';
+
+  if ( pwg_db_num_rows(pwg_query($query)) )
+  {
+    $template->assign('typetag', array(
+      'NAME' => $_POST['typetag_name'],
+      'COLOR' => $_POST['typetag_color'],
+    ));
+    
+    array_push($page['errors'], l10n('typetag_already_exists'));
   }
   else
   {
-    $template->assign('typetag', array(
-      'NAME' => $typetag_name,
-      'COLOR' => $typetag_color,
-    ));
-    
-    $page['errors'][] = l10n('typetag_already_exists');
+    $query = '
+INSERT INTO '.typetags_TABLE.'(
+    name,
+    color
+  )
+  VALUES(
+    "'.$_POST['typetag_name'].'",
+    "'.$_POST['typetag_color'].'"
+  )
+;';
+    pwg_query($query);
+
+    array_push($page['infos'], l10n('typetag_saved'));
   }
 }
@@ -175,34 +165,52 @@
 if (isset($_POST['delete_all_assoc'])) 
 {
-  pwg_query("UPDATE `". TAGS_TABLE ."` SET id_typetags = NULL;");
-  $page['infos'][] = l10n('All associations have been removed');
-
+  pwg_query('UPDATE '.TAGS_TABLE.' SET id_typetags = NULL;');
+  array_push($page['infos'], l10n('All associations have been removed'));
 } 
 else if (isset($_POST['associations'])) 
 {
-    // beautify the parameters array
-    $string = preg_replace('#[;]$#', '', $_POST['associations']);
-    $associations = array();
-    $a = explode(';', $string);
-    
-    foreach ($a as $s) 
-    {
-       $v = explode(':', $s);
-       $associations[ltrim($v[0],'t-')] = ltrim($v[1],'tt-');
-    }
-
-    // save associations
-    foreach ($associations AS $tag => $typetag) 
-    {
-      pwg_query("
-        UPDATE `". TAGS_TABLE ."`
-        SET id_typetags = ". $typetag ."
-        WHERE id = ". $tag .";
-      ");
-    }
-    
-    $page['infos'][] = l10n('typetags_associated');
-}
-
+  // beautify the parameters array
+  $string = preg_replace('#[;]$#', null, $_POST['associations']);
+  $associations = array();
+  $a = explode(';', $string);
+  
+  foreach ($a as $s) 
+  {
+    $v = explode(':', $s);
+    $associations[ltrim($v[0],'t-')] = ltrim($v[1],'tt-');
+  }
+
+  // save associations
+  $updates = array();
+  foreach ($associations as $tag => $typetag) 
+  {
+    array_push($updates, array(
+      'id' => $tag,
+      'id_typetags' => $typetag,
+      ));
+  }
+  
+  mass_updates(
+    TAGS_TABLE, 
+    array('primary' => array('id'), 'update' => array('id_typetags')),
+    $updates
+    );
+  
+  array_push($page['infos'], l10n('typetags_associated'));
+}
+
+// +-----------------------------------------------------------------------+
+// |                          Configuration                                |
+// +-----------------------------------------------------------------------+
+if (isset($_POST['save_config']))
+{
+  $conf['TypeTags'] = array(
+    'show_all' => $_POST['show_all'] == 'true',
+    );
+    
+  conf_update_param('TypeTags', serialize($conf['TypeTags']));
+}
+
+$template->assign('SHOW_ALL', $conf['TypeTags']['show_all']);
 
 // +-----------------------------------------------------------------------+
@@ -212,25 +220,25 @@
 $template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/admin/typetags_admin.tpl'));
 
-// Récupère tous les tags
-$all_tags = pwg_query("
-  SELECT 
-    t.id as tagid,
-    t.name as tagname, 
-    tt.id as typetagid,
-    tt.name as typetagname
-  FROM `". TAGS_TABLE ."` as t 
-  LEFT JOIN `". typetags_TABLE ."` as tt
-  ON  t.id_typetags = tt.id
-  ORDER BY t.name ASC;
-");
-
-while ($row = pwg_db_fetch_assoc($all_tags)) {
-  if ($row['typetagname'] == null) $row['typetagid'] = 'NULL';
-  $row['tagname'] = trigger_event('render_tag_name', $row['tagname']);
+// get all tags
+$query = '
+SELECT 
+    id as tagid,
+    name as tagname, 
+    id_typetags as typetagid
+  FROM '.TAGS_TABLE.'
+  ORDER BY name ASC
+;';
+$all_tags = pwg_query($query);
+
+while ($row = pwg_db_fetch_assoc($all_tags))
+{
+  if (!$row['typetagid']) $row['typetagid'] = 'NULL';
+  $row['tagname'] = strip_tags(trigger_event('render_tag_name', $row['tagname']));
   $template->append('typetags_association', $row);
 }
 
-// Récupère tous les typetags
-$all_typetags = pwg_query("SELECT * FROM `". typetags_TABLE ."` ORDER BY `name`;");
+// get all typetags
+$query = 'SELECT * FROM '.typetags_TABLE.' ORDER BY name;';
+$all_typetags = pwg_query($query);
 
 while ($row = mysql_fetch_assoc($all_typetags))
@@ -243,6 +251,7 @@
 }
 
-// formualire d'édition
-if (isset($_GET['edittypetag'])) {
+// edit form
+if (isset($_GET['edittypetag']))
+{
   $edited_typetag['id'] = $_GET['edittypetag'];
 }
@@ -251,14 +260,14 @@
 {
   $template->assign('edited_typetag', $edited_typetag['id']);
-
-  $tag = pwg_query("
-    SELECT 
-      id,
-      name,
-      color
-    FROM `". typetags_TABLE ."`
-    WHERE id = ".$edited_typetag['id'].";
-  ");
-  $row = pwg_db_fetch_assoc($tag);
+  
+$query = '
+SELECT 
+    id,
+    name,
+    color
+  FROM '.typetags_TABLE.'
+  WHERE id = '.$edited_typetag['id'].'
+;';
+  $row = pwg_db_fetch_assoc(pwg_query($query));
 
   $template->assign('typetag', array(
