Ignore:
Timestamp:
May 20, 2012, 12:38:07 PM (12 years ago)
Author:
mistic100
Message:
  • display typetags everywhere (not only tags page)
  • little redesign of admin page + code cleaning
File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/typetags/admin.php

    r12483 r15149  
    2626// +-----------------------------------------------------------------------+
    2727
    28 if (isset($_POST['edittypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])))
     28if ( isset($_POST['edittypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])) )
    2929{
    3030  $edited_typetag = array(
     
    3333    'color' => $_POST['typetag_color'],
    3434  );
    35   $page['errors'][] = l10n('typetag_error');
     35 
     36  array_push($page['errors'],  l10n('typetag_error'));
    3637}
    3738else if (isset($_POST['edittypetag']))
    3839{
    39   $typetag = mysql_escape_string($_POST['edited_typetag']);
    40   $typetag_name = mysql_escape_string($_POST['typetag_name']);
    41   $typetag_color = mysql_escape_string($_POST['typetag_color']);
    42 
    43   $all_typetags = pwg_query("
    44     SELECT
    45       id,
    46       name,
    47       color
    48     FROM  `". typetags_TABLE ."`;
    49   ");
    50  
    51   while ($row = mysql_fetch_array($all_typetags))
    52   {
    53     $existing_names[] = $row['name'];
    54     if ($typetag == $row['id'])
    55     {
    56       $current_name = $row['name'];
    57       $current_color = $row['color'];
    58     }
    59   }
    60 
    6140  // we must not rename typetag with an already existing name
    62   if ($typetag_name != $current_name AND in_array($typetag_name, $existing_names))
     41  $query = '
     42SELECT id
     43  FROM '.typetags_TABLE.'
     44  WHERE
     45    name = "'.$_POST['typetag_name'].'"
     46    AND id != '.$_POST['edited_typetag'].'
     47;';
     48
     49  if ( pwg_db_num_rows(pwg_query($query)) )
    6350  {
    6451    $edited_typetag = array(
    65       'id' => $typetag,
    66       'name' => $typetag_name,
    67       'color' => $typetag_color,
     52      'id' => $_POST['edited_typetag'],
     53      'name' => $_POST['typetag_name'],
     54      'color' => $_POST['typetag_color'],
    6855    );
    6956   
    70     $page['errors'][] = l10n('typetag_already_exists');
     57    array_push($page['errors'], l10n('typetag_already_exists'));
    7158  }
    7259  else
    7360  {
    74     pwg_query("
    75       UPDATE `". typetags_TABLE ."` SET
    76         `name` = '". $typetag_name ."',
    77         `color` = '". $typetag_color ."'
    78       WHERE `id` = ". $typetag .";
    79     ");
    80    
    81     $page['infos'][] = l10n('typetag_saved');
     61    $query = '
     62UPDATE '.typetags_TABLE.'
     63  SET
     64    name = "'.$_POST['typetag_name'].'",
     65    color = "'.$_POST['typetag_color'].'"
     66  WHERE id = '.$_POST['edited_typetag'].'
     67;';
     68    pwg_query($query);
     69   
     70    array_push($page['infos'], l10n('typetag_saved'));
    8271  }
    8372}
     
    8978if (isset($_GET['deletetypetag']))
    9079{
    91   $query = "
    92     SELECT name
    93     FROM `". typetags_TABLE ."`
    94     WHERE id = ". $_GET['deletetypetag'] .";
    95   ";
    96   $typetag_name = array_from_query($query, 'name');
    97  
    98   if (count($typetag_name) != 0)
    99   {
    100     pwg_query("
    101       UPDATE `". TAGS_TABLE ."`
    102       SET id_typetags = NULL
    103       WHERE id_typetags = ". $_GET['deletetypetag'] .";
    104     ");
    105 
    106     pwg_query("
    107       DELETE FROM `". typetags_TABLE ."`
    108       WHERE id = ". $_GET['deletetypetag'] .";
    109     ");
    110 
    111     $page['infos'][] = l10n('typetag_suppr').' : '.$typetag_name[0];
     80  $query = '
     81SELECT id
     82  FROM '.typetags_TABLE.'
     83  WHERE id = '.$_GET['deletetypetag'].'
     84;';
     85 
     86  if ( pwg_db_num_rows(pwg_query($query)) )
     87  {
     88    $query = '
     89UPDATE '.TAGS_TABLE.'
     90  SET id_typetags = NULL
     91  WHERE id_typetags = '.$_GET['deletetypetag'].'
     92;';
     93    pwg_query($query);
     94   
     95    $query = '
     96DELETE FROM '.typetags_TABLE.'
     97  WHERE id = '.$_GET['deletetypetag'].'
     98;';
     99    pwg_query($query);
     100   
     101    array_push($page['infos'], l10n('typetag_suppr'));
    112102  }
    113103  else
    114104  {
    115     $page['errors'][] = l10n('typetag_unknown').' : '.$_GET['deletetypetag'];
     105    array_push($page['errors'], l10n('typetag_unknown'));
    116106  }
    117107}
     
    121111// +-----------------------------------------------------------------------+
    122112
    123 if (isset($_POST['addtypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])))
     113if ( isset($_POST['addtypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])) )
    124114{
    125115  $template->assign('typetag', array(
    126     'NAME' => isset($_POST['typetag_name']) ? $_POST['typetag_name'] : '',
    127     'COLOR' => isset($_POST['typetag_color']) ? $_POST['typetag_color'] : '',
     116    'NAME' => $_POST['typetag_name'],
     117    'COLOR' => $_POST['typetag_color'],
    128118  ));
    129119 
    130   $page['errors'][] = l10n('typetag_error');
     120  array_push($page['errors'], l10n('typetag_error'));
    131121}
    132122else if (isset($_POST['addtypetag']))
     
    136126
    137127  // does the tag already exists?
    138   $query = "
    139     SELECT id
    140     FROM `". typetags_TABLE ."`
    141     WHERE name = '". $typetag_name ."';
    142   ";
    143   $existing_tags = array_from_query($query, 'id');
    144 
    145   if (count($existing_tags) == 0)
    146   {
    147     pwg_query("
    148       INSERT INTO `". typetags_TABLE ."`(
    149         `name`,
    150         `color`
    151       )
    152       VALUES(
    153         '". $typetag_name ."',
    154         '". $typetag_color ."'
    155       );
    156     ");
    157 
    158     $page['infos'][] = l10n('typetag_saved');
     128  $query = '
     129SELECT id
     130  FROM '.typetags_TABLE.'
     131  WHERE name = "'.$_POST['typetag_name'].'"
     132';
     133
     134  if ( pwg_db_num_rows(pwg_query($query)) )
     135  {
     136    $template->assign('typetag', array(
     137      'NAME' => $_POST['typetag_name'],
     138      'COLOR' => $_POST['typetag_color'],
     139    ));
     140   
     141    array_push($page['errors'], l10n('typetag_already_exists'));
    159142  }
    160143  else
    161144  {
    162     $template->assign('typetag', array(
    163       'NAME' => $typetag_name,
    164       'COLOR' => $typetag_color,
    165     ));
    166    
    167     $page['errors'][] = l10n('typetag_already_exists');
     145    $query = '
     146INSERT INTO '.typetags_TABLE.'(
     147    name,
     148    color
     149  )
     150  VALUES(
     151    "'.$_POST['typetag_name'].'",
     152    "'.$_POST['typetag_color'].'"
     153  )
     154;';
     155    pwg_query($query);
     156
     157    array_push($page['infos'], l10n('typetag_saved'));
    168158  }
    169159}
     
    175165if (isset($_POST['delete_all_assoc']))
    176166{
    177   pwg_query("UPDATE `". TAGS_TABLE ."` SET id_typetags = NULL;");
    178   $page['infos'][] = l10n('All associations have been removed');
    179 
     167  pwg_query('UPDATE '.TAGS_TABLE.' SET id_typetags = NULL;');
     168  array_push($page['infos'], l10n('All associations have been removed'));
    180169}
    181170else if (isset($_POST['associations']))
    182171{
    183     // beautify the parameters array
    184     $string = preg_replace('#[;]$#', '', $_POST['associations']);
    185     $associations = array();
    186     $a = explode(';', $string);
    187    
    188     foreach ($a as $s)
    189     {
    190        $v = explode(':', $s);
    191        $associations[ltrim($v[0],'t-')] = ltrim($v[1],'tt-');
    192     }
    193 
    194     // save associations
    195     foreach ($associations AS $tag => $typetag)
    196     {
    197       pwg_query("
    198         UPDATE `". TAGS_TABLE ."`
    199         SET id_typetags = ". $typetag ."
    200         WHERE id = ". $tag .";
    201       ");
    202     }
    203    
    204     $page['infos'][] = l10n('typetags_associated');
    205 }
    206 
     172  // beautify the parameters array
     173  $string = preg_replace('#[;]$#', null, $_POST['associations']);
     174  $associations = array();
     175  $a = explode(';', $string);
     176 
     177  foreach ($a as $s)
     178  {
     179    $v = explode(':', $s);
     180    $associations[ltrim($v[0],'t-')] = ltrim($v[1],'tt-');
     181  }
     182
     183  // save associations
     184  $updates = array();
     185  foreach ($associations as $tag => $typetag)
     186  {
     187    array_push($updates, array(
     188      'id' => $tag,
     189      'id_typetags' => $typetag,
     190      ));
     191  }
     192 
     193  mass_updates(
     194    TAGS_TABLE,
     195    array('primary' => array('id'), 'update' => array('id_typetags')),
     196    $updates
     197    );
     198 
     199  array_push($page['infos'], l10n('typetags_associated'));
     200}
     201
     202// +-----------------------------------------------------------------------+
     203// |                          Configuration                                |
     204// +-----------------------------------------------------------------------+
     205if (isset($_POST['save_config']))
     206{
     207  $conf['TypeTags'] = array(
     208    'show_all' => $_POST['show_all'] == 'true',
     209    );
     210   
     211  conf_update_param('TypeTags', serialize($conf['TypeTags']));
     212}
     213
     214$template->assign('SHOW_ALL', $conf['TypeTags']['show_all']);
    207215
    208216// +-----------------------------------------------------------------------+
     
    212220$template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/admin/typetags_admin.tpl'));
    213221
    214 // Récupère tous les tags
    215 $all_tags = pwg_query("
    216   SELECT
    217     t.id as tagid,
    218     t.name as tagname,
    219     tt.id as typetagid,
    220     tt.name as typetagname
    221   FROM `". TAGS_TABLE ."` as t
    222   LEFT JOIN `". typetags_TABLE ."` as tt
    223   ON  t.id_typetags = tt.id
    224   ORDER BY t.name ASC;
    225 ");
    226 
    227 while ($row = pwg_db_fetch_assoc($all_tags)) {
    228   if ($row['typetagname'] == null) $row['typetagid'] = 'NULL';
    229   $row['tagname'] = trigger_event('render_tag_name', $row['tagname']);
     222// get all tags
     223$query = '
     224SELECT
     225    id as tagid,
     226    name as tagname,
     227    id_typetags as typetagid
     228  FROM '.TAGS_TABLE.'
     229  ORDER BY name ASC
     230;';
     231$all_tags = pwg_query($query);
     232
     233while ($row = pwg_db_fetch_assoc($all_tags))
     234{
     235  if (!$row['typetagid']) $row['typetagid'] = 'NULL';
     236  $row['tagname'] = strip_tags(trigger_event('render_tag_name', $row['tagname']));
    230237  $template->append('typetags_association', $row);
    231238}
    232239
    233 // Récupère tous les typetags
    234 $all_typetags = pwg_query("SELECT * FROM `". typetags_TABLE ."` ORDER BY `name`;");
     240// get all typetags
     241$query = 'SELECT * FROM '.typetags_TABLE.' ORDER BY name;';
     242$all_typetags = pwg_query($query);
    235243
    236244while ($row = mysql_fetch_assoc($all_typetags))
     
    243251}
    244252
    245 // formualire d'édition
    246 if (isset($_GET['edittypetag'])) {
     253// edit form
     254if (isset($_GET['edittypetag']))
     255{
    247256  $edited_typetag['id'] = $_GET['edittypetag'];
    248257}
     
    251260{
    252261  $template->assign('edited_typetag', $edited_typetag['id']);
    253 
    254   $tag = pwg_query("
    255     SELECT
    256       id,
    257       name,
    258       color
    259     FROM `". typetags_TABLE ."`
    260     WHERE id = ".$edited_typetag['id'].";
    261   ");
    262   $row = pwg_db_fetch_assoc($tag);
     262 
     263$query = '
     264SELECT
     265    id,
     266    name,
     267    color
     268  FROM '.typetags_TABLE.'
     269  WHERE id = '.$edited_typetag['id'].'
     270;';
     271  $row = pwg_db_fetch_assoc(pwg_query($query));
    263272
    264273  $template->assign('typetag', array(
Note: See TracChangeset for help on using the changeset viewer.