Ignore:
Timestamp:
Jan 12, 2014, 7:13:40 PM (10 years ago)
Author:
mistic100
Message:

integrate in new tags manager + rename in Coloured Tags
(my apologies to translators :-) )

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/typetags/admin.php

    r21361 r26665  
    11<?php
    2 if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
    3 
    4 load_language('plugin.lang', typetags_PATH);
    5 
    6 function get_color_text($color)
    7 {
    8   if (strlen($color) == 7)
    9   {
    10     $rgb[] = hexdec(substr($color, 1, 2))/255;
    11     $rgb[] = hexdec(substr($color, 3, 2))/255;
    12     $rgb[] = hexdec(substr($color, 5, 2))/255;
    13   }
    14   else if (strlen($color) == 4)
    15   {
    16     $rgb[] = hexdec(substr($color, 1, 1))/15;
    17     $rgb[] = hexdec(substr($color, 2, 1))/15;
    18     $rgb[] = hexdec(substr($color, 3, 1))/15;
    19   }
    20   $l = (min($rgb) + max($rgb)) / 2;
    21   return $l > 0.45 ? '#000' : '#fff';
    22 }
     2defined('TYPETAGS_PATH') or die('Hacking attempt!');
     3
     4load_language('plugin.lang', TYPETAGS_PATH);
     5
     6include_once(TYPETAGS_PATH . 'include/functions.inc.php');
     7
    238
    249// +-----------------------------------------------------------------------+
    2510// |                                edit typetags                          |
    2611// +-----------------------------------------------------------------------+
    27 
    28 if ( isset($_POST['edittypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])) )
    29 {
    30   $edited_typetag = array(
    31     'id' => $_POST['edited_typetag'],
    32     'name' => $_POST['typetag_name'],
    33     'color' => $_POST['typetag_color'],
    34   );
    35  
    36   array_push($page['errors'],  l10n('typetag_error'));
    37 }
    38 else if (isset($_POST['edittypetag']))
    39 {
    40   // we must not rename typetag with an already existing name
    41   $query = '
     12if (isset($_POST['edittypetag']))
     13{
     14  if (empty($_POST['typetag_name']) or empty($_POST['typetag_color']))
     15  {
     16    $page['errors'][] = l10n('You must fill all fields (name and color)');
     17  }
     18  else
     19  {
     20    // we must not rename typetag with an already existing name
     21    $query = '
    4222SELECT id
    43   FROM '.typetags_TABLE.'
     23  FROM ' . TYPETAGS_TABLE . '
    4424  WHERE
    45     name = "'.$_POST['typetag_name'].'"
    46     AND id != '.$_POST['edited_typetag'].'
    47 ;';
    48 
    49   if ( pwg_db_num_rows(pwg_query($query)) )
     25    name = "' . pwg_db_real_escape_string($_POST['typetag_name']) . '"
     26    AND id != ' . $_POST['edited_typetag'] . '
     27;';
     28
     29    if (pwg_db_num_rows(pwg_query($query)))
     30    {
     31      $page['errors'][] = l10n('This name is already used');
     32    }
     33    else if ( ($color = check_color($_POST['typetag_color'])) === false )
     34    {
     35      $page['errors'][] = l10n('Invalid color');
     36    }
     37    else
     38    {
     39      $query = '
     40UPDATE '.TYPETAGS_TABLE.'
     41  SET
     42    name = "' . pwg_db_real_escape_string($_POST['typetag_name']) . '",
     43    color = "' . $color . '"
     44  WHERE id = ' . $_POST['edited_typetag'] . '
     45;';
     46      pwg_query($query);
     47
     48      $page['infos'][] = l10n('Color saved');
     49    }
     50  }
     51
     52  if (count($page['errors']))
    5053  {
    5154    $edited_typetag = array(
     
    5356      'name' => $_POST['typetag_name'],
    5457      'color' => $_POST['typetag_color'],
    55     );
    56    
    57     array_push($page['errors'], l10n('typetag_already_exists'));
     58      );
     59  }
     60}
     61
     62// +-----------------------------------------------------------------------+
     63// |                           delete typetags                             |
     64// +-----------------------------------------------------------------------+
     65if (isset($_GET['deletetypetag']))
     66{
     67  $query = '
     68UPDATE ' . TAGS_TABLE . '
     69  SET id_typetags = NULL
     70  WHERE id_typetags = ' . intval($_GET['deletetypetag']) . '
     71;';
     72  pwg_query($query);
     73
     74  $query = '
     75DELETE FROM ' . TYPETAGS_TABLE . '
     76  WHERE id = ' . intval($_GET['deletetypetag']) . '
     77;';
     78  pwg_query($query);
     79
     80  if (pwg_db_changes())
     81  {
     82    $page['infos'][] = l10n('Color deleted');
     83  }
     84}
     85
     86// +-----------------------------------------------------------------------+
     87// |                               add a typetag                           |
     88// +-----------------------------------------------------------------------+
     89if (isset($_POST['addtypetag']))
     90{
     91  if (empty($_POST['typetag_name']) or empty($_POST['typetag_color']))
     92  {
     93    $page['errors'][] = l10n('You must fill all fields (name and color)');
    5894  }
    5995  else
    6096  {
     97    $typetag_name = $_POST['typetag_name'];
     98    $typetag_color = $_POST['typetag_color'];
     99
     100    // does the tag already exists?
    61101    $query = '
    62 UPDATE '.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'));
    71   }
    72 }
    73 
    74 // +-----------------------------------------------------------------------+
    75 // |                           delete typetags                             |
    76 // +-----------------------------------------------------------------------+
    77 
    78 if (isset($_GET['deletetypetag']))
    79 {
    80   $query = '
    81102SELECT id
    82   FROM '.typetags_TABLE.'
    83   WHERE id = '.$_GET['deletetypetag'].'
    84 ;';
    85  
    86   if ( pwg_db_num_rows(pwg_query($query)) )
    87   {
    88     $query = '
    89 UPDATE '.TAGS_TABLE.'
    90   SET id_typetags = NULL
    91   WHERE id_typetags = '.$_GET['deletetypetag'].'
    92 ;';
    93     pwg_query($query);
    94    
    95     $query = '
    96 DELETE FROM '.typetags_TABLE.'
    97   WHERE id = '.$_GET['deletetypetag'].'
    98 ;';
    99     pwg_query($query);
    100    
    101     array_push($page['infos'], l10n('typetag_suppr'));
    102   }
    103   else
    104   {
    105     array_push($page['errors'], l10n('typetag_unknown'));
    106   }
    107 }
    108 
    109 // +-----------------------------------------------------------------------+
    110 // |                               add a typetag                           |
    111 // +-----------------------------------------------------------------------+
    112 
    113 if ( isset($_POST['addtypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])) )
    114 {
    115   $template->assign('typetag', array(
    116     'NAME' => $_POST['typetag_name'],
    117     'COLOR' => $_POST['typetag_color'],
    118   ));
    119  
    120   array_push($page['errors'], l10n('typetag_error'));
    121 }
    122 else if (isset($_POST['addtypetag']))
    123 {
    124   $typetag_name = $_POST['typetag_name'];
    125   $typetag_color = $_POST['typetag_color'];
    126 
    127   // does the tag already exists?
    128   $query = '
    129 SELECT id
    130   FROM '.typetags_TABLE.'
    131   WHERE name = "'.$_POST['typetag_name'].'"
     103  FROM ' . TYPETAGS_TABLE . '
     104  WHERE name = "' . pwg_db_real_escape_string($_POST['typetag_name']) . '"
    132105';
    133106
    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'));
    142   }
    143   else
    144   {
    145     $query = '
    146 INSERT INTO '.typetags_TABLE.'(
     107    if (pwg_db_num_rows(pwg_query($query)))
     108    {
     109      $page['errors'][] = l10n('This name is already used');
     110    }
     111    else if ( ($color = check_color($_POST['typetag_color'])) === false )
     112    {
     113      $page['errors'][] = l10n('Invalid color');
     114    }
     115    else
     116    {
     117      $query = '
     118INSERT INTO ' . TYPETAGS_TABLE . '(
    147119    name,
    148120    color
    149121  )
    150122  VALUES(
    151     "'.$_POST['typetag_name'].'",
    152     "'.$_POST['typetag_color'].'"
     123    "' . pwg_db_real_escape_string($_POST['typetag_name']) . '",
     124    "' . $color . '"
    153125  )
    154126;';
    155     pwg_query($query);
    156 
    157     array_push($page['infos'], l10n('typetag_saved'));
    158   }
    159 }
    160 
    161 // +-----------------------------------------------------------------------+
    162 // |                           Associate Tag to Typetage                   |
    163 // +-----------------------------------------------------------------------+
    164 
    165 if (isset($_POST['delete_all_assoc']))
    166 {
    167   pwg_query('UPDATE '.TAGS_TABLE.' SET id_typetags = NULL;');
    168   array_push($page['infos'], l10n('All associations have been removed'));
    169 }
    170 else if (isset($_POST['associations']))
    171 {
    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,
     127      pwg_query($query);
     128
     129      $page['infos'][] = l10n('Color added');
     130    }
     131  }
     132
     133  if (count($page['errors']))
     134  {
     135    $template->assign('typetag', array(
     136      'NAME' => $_POST['typetag_name'],
     137      'COLOR' => $_POST['typetag_color'],
    190138      ));
    191139  }
    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'));
    200140}
    201141
     
    208148    'show_all' => $_POST['show_all'] == 'true',
    209149    );
    210    
     150
    211151  conf_update_param('TypeTags', serialize($conf['TypeTags']));
     152  $page['infos'][] = l10n('Information data registered in database');
    212153}
    213154
     
    217158// |                             template init                             |
    218159// +-----------------------------------------------------------------------+
    219 
    220 $template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/admin/typetags_admin.tpl'));
    221 
    222 // get all tags
    223 $query = '
    224 SELECT
    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 
    233 while ($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']));
    237   $template->append('typetags_association', $row);
    238 }
     160$template->assign('TYPETAGS_PATH', TYPETAGS_PATH);
     161$template->assign('F_ACTION', TYPETAGS_ADMIN);
    239162
    240163// get all typetags
    241 $query = 'SELECT * FROM '.typetags_TABLE.' ORDER BY name;';
    242 $all_typetags = pwg_query($query);
    243 
    244 while ($row = pwg_db_fetch_assoc($all_typetags))
     164$query = 'SELECT * FROM ' . TYPETAGS_TABLE . ' ORDER BY name;';
     165$result = pwg_query($query);
     166
     167while ($row = pwg_db_fetch_assoc($result))
    245168{
    246169  $row['color_text'] = get_color_text($row['color']);
    247   $row['u_edit'] = typetags_ADMIN . '&edittypetag=' . $row['id'];
    248   $row['u_delete'] = typetags_ADMIN . '&deletetypetag=' . $row['id'];
    249  
    250   $template->append('typetags_selection', $row);
     170  $row['u_edit'] = TYPETAGS_ADMIN . '&amp;edittypetag=' . $row['id'];
     171  $row['u_delete'] = TYPETAGS_ADMIN . '&amp;deletetypetag=' . $row['id'];
     172
     173  $template->append('typetags', $row);
    251174}
    252175
     
    254177if (isset($_GET['edittypetag']))
    255178{
    256   $edited_typetag['id'] = $_GET['edittypetag'];
    257 }
    258 
    259 if (isset($edited_typetag['id']))
     179  $edited_typetag['id'] = intval($_GET['edittypetag']);
     180}
     181
     182if (isset($edited_typetag['id']) and $edited_typetag['id']>0)
    260183{
    261184  $template->assign('edited_typetag', $edited_typetag['id']);
    262  
     185
    263186$query = '
    264 SELECT
    265     id,
    266     name,
    267     color
    268   FROM '.typetags_TABLE.'
    269   WHERE id = '.$edited_typetag['id'].'
     187SELECT *
     188  FROM ' . TYPETAGS_TABLE . '
     189  WHERE id = ' . $edited_typetag['id'] . '
    270190;';
    271191  $row = pwg_db_fetch_assoc(pwg_query($query));
     
    278198    'NAME' => isset($edited_typetag['name']) ? $edited_typetag['name'] : $row['name'],
    279199    'COLOR'=> isset($edited_typetag['color']) ? $edited_typetag['color'] : $row['color'],
    280   ));
    281  
     200    ));
     201
    282202  $template->assign('IN_EDIT', true);
    283203}
     
    286206// |                           sending html code                           |
    287207// +-----------------------------------------------------------------------+
    288 
    289 $template->assign('typetags_ADMIN', typetags_ADMIN);
    290 $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
    291 
    292 ?>
     208$template->set_filename('typetags', realpath(TYPETAGS_PATH . 'template/admin.tpl'));
     209$template->assign_var_from_handle('ADMIN_CONTENT', 'typetags');
Note: See TracChangeset for help on using the changeset viewer.