[9829] | 1 | <?php |
---|
[9863] | 2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
[9829] | 3 | |
---|
| 4 | load_language('plugin.lang', typetags_PATH); |
---|
| 5 | |
---|
| 6 | function get_color_text($color) |
---|
| 7 | { |
---|
[10987] | 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'; |
---|
[9829] | 22 | } |
---|
| 23 | |
---|
| 24 | // +-----------------------------------------------------------------------+ |
---|
| 25 | // | edit typetags | |
---|
| 26 | // +-----------------------------------------------------------------------+ |
---|
| 27 | |
---|
[9863] | 28 | if (isset($_POST['edittypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color']))) |
---|
[9829] | 29 | { |
---|
[10987] | 30 | $edited_typetag = array( |
---|
| 31 | 'id' => $_POST['edited_typetag'], |
---|
| 32 | 'name' => $_POST['typetag_name'], |
---|
| 33 | 'color' => $_POST['typetag_color'], |
---|
| 34 | ); |
---|
| 35 | $page['errors'][] = l10n('typetag_error'); |
---|
[9863] | 36 | } |
---|
| 37 | else if (isset($_POST['edittypetag'])) |
---|
| 38 | { |
---|
[10987] | 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']); |
---|
[9829] | 42 | |
---|
[10987] | 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 | } |
---|
[9829] | 60 | |
---|
[10987] | 61 | // we must not rename typetag with an already existing name |
---|
| 62 | if ($typetag_name != $current_name AND in_array($typetag_name, $existing_names)) |
---|
| 63 | { |
---|
| 64 | $edited_typetag = array( |
---|
| 65 | 'id' => $typetag, |
---|
| 66 | 'name' => $typetag_name, |
---|
| 67 | 'color' => $typetag_color, |
---|
| 68 | ); |
---|
| 69 | |
---|
| 70 | $page['errors'][] = l10n('typetag_already_exists'); |
---|
| 71 | } |
---|
| 72 | else |
---|
| 73 | { |
---|
| 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'); |
---|
| 82 | } |
---|
[9829] | 83 | } |
---|
| 84 | |
---|
| 85 | // +-----------------------------------------------------------------------+ |
---|
[9863] | 86 | // | delete typetags | |
---|
[9829] | 87 | // +-----------------------------------------------------------------------+ |
---|
| 88 | |
---|
[9863] | 89 | if (isset($_GET['deletetypetag'])) |
---|
[9829] | 90 | { |
---|
[10987] | 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 | "); |
---|
[9829] | 105 | |
---|
[10987] | 106 | pwg_query(" |
---|
| 107 | DELETE FROM `". typetags_TABLE ."` |
---|
| 108 | WHERE id = ". $_GET['deletetypetag'] ."; |
---|
| 109 | "); |
---|
[9863] | 110 | |
---|
[10987] | 111 | $page['infos'][] = l10n('typetag_suppr').' : '.$typetag_name[0]; |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | { |
---|
| 115 | $page['errors'][] = l10n('typetag_unknown').' : '.$_GET['deletetypetag']; |
---|
| 116 | } |
---|
[9829] | 117 | } |
---|
| 118 | |
---|
| 119 | // +-----------------------------------------------------------------------+ |
---|
| 120 | // | add a typetag | |
---|
| 121 | // +-----------------------------------------------------------------------+ |
---|
| 122 | |
---|
[9863] | 123 | if (isset($_POST['addtypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color']))) |
---|
[9829] | 124 | { |
---|
[10987] | 125 | $template->assign('typetag', array( |
---|
| 126 | 'NAME' => isset($_POST['typetag_name']) ? $_POST['typetag_name'] : '', |
---|
| 127 | 'COLOR' => isset($_POST['typetag_color']) ? $_POST['typetag_color'] : '', |
---|
| 128 | )); |
---|
| 129 | |
---|
| 130 | $page['errors'][] = l10n('typetag_error'); |
---|
[9829] | 131 | } |
---|
[9863] | 132 | else if (isset($_POST['addtypetag'])) |
---|
[9829] | 133 | { |
---|
[10987] | 134 | $typetag_name = $_POST['typetag_name']; |
---|
| 135 | $typetag_color = $_POST['typetag_color']; |
---|
[9829] | 136 | |
---|
[10987] | 137 | // 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'); |
---|
[9829] | 144 | |
---|
[10987] | 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 | "); |
---|
[9829] | 157 | |
---|
[10987] | 158 | $page['infos'][] = l10n('typetag_saved'); |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | { |
---|
| 162 | $template->assign('typetag', array( |
---|
| 163 | 'NAME' => $typetag_name, |
---|
| 164 | 'COLOR' => $typetag_color, |
---|
| 165 | )); |
---|
| 166 | |
---|
| 167 | $page['errors'][] = l10n('typetag_already_exists'); |
---|
| 168 | } |
---|
[9829] | 169 | } |
---|
| 170 | |
---|
| 171 | // +-----------------------------------------------------------------------+ |
---|
| 172 | // | Associate Tag to Typetage | |
---|
| 173 | // +-----------------------------------------------------------------------+ |
---|
| 174 | |
---|
[9863] | 175 | if (isset($_POST['delete_all_assoc'])) |
---|
[9829] | 176 | { |
---|
[10987] | 177 | pwg_query("UPDATE `". TAGS_TABLE ."` SET id_typetags = NULL;"); |
---|
| 178 | $page['infos'][] = l10n('All associations have been removed'); |
---|
[9829] | 179 | |
---|
[9863] | 180 | } |
---|
| 181 | else if (isset($_POST['associations'])) |
---|
[9829] | 182 | { |
---|
[10987] | 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 | } |
---|
[9829] | 193 | |
---|
[10987] | 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'); |
---|
[9829] | 205 | } |
---|
| 206 | |
---|
[9863] | 207 | |
---|
[9829] | 208 | // +-----------------------------------------------------------------------+ |
---|
| 209 | // | template init | |
---|
| 210 | // +-----------------------------------------------------------------------+ |
---|
| 211 | |
---|
| 212 | $template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/admin/typetags_admin.tpl')); |
---|
| 213 | |
---|
[9863] | 214 | // Récupère tous les tags |
---|
| 215 | $all_tags = pwg_query(" |
---|
[10987] | 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; |
---|
[9863] | 225 | "); |
---|
[9829] | 226 | |
---|
[9863] | 227 | while ($row = pwg_db_fetch_assoc($all_tags)) { |
---|
[10987] | 228 | if ($row['typetagname'] == null) $row['typetagid'] = 'NULL'; |
---|
[12483] | 229 | $row['tagname'] = trigger_event('render_tag_name', $row['tagname']); |
---|
[10987] | 230 | $template->append('typetags_association', $row); |
---|
[9829] | 231 | } |
---|
| 232 | |
---|
[9863] | 233 | // Récupère tous les typetags |
---|
| 234 | $all_typetags = pwg_query("SELECT * FROM `". typetags_TABLE ."` ORDER BY `name`;"); |
---|
[9829] | 235 | |
---|
[9863] | 236 | while ($row = mysql_fetch_assoc($all_typetags)) |
---|
[9829] | 237 | { |
---|
[10987] | 238 | $row['color_text'] = get_color_text($row['color']); |
---|
| 239 | $row['u_edit'] = typetags_ADMIN . '&edittypetag=' . $row['id']; |
---|
| 240 | $row['u_delete'] = typetags_ADMIN . '&deletetypetag=' . $row['id']; |
---|
| 241 | |
---|
| 242 | $template->append('typetags_selection', $row); |
---|
[9829] | 243 | } |
---|
| 244 | |
---|
[9863] | 245 | // formualire d'édition |
---|
| 246 | if (isset($_GET['edittypetag'])) { |
---|
[10987] | 247 | $edited_typetag['id'] = $_GET['edittypetag']; |
---|
[9863] | 248 | } |
---|
[9829] | 249 | |
---|
[9863] | 250 | if (isset($edited_typetag['id'])) |
---|
[9829] | 251 | { |
---|
[10987] | 252 | $template->assign('edited_typetag', $edited_typetag['id']); |
---|
[9829] | 253 | |
---|
[10987] | 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); |
---|
[9829] | 263 | |
---|
[10987] | 264 | $template->assign('typetag', array( |
---|
| 265 | 'ID' => $row['id'], |
---|
| 266 | 'OLD_NAME' => $row['name'], |
---|
| 267 | 'OLD_COLOR' => $row['color'], |
---|
| 268 | 'COLOR_TEXT' => get_color_text($row['color']), |
---|
| 269 | 'NAME' => isset($edited_typetag['name']) ? $edited_typetag['name'] : $row['name'], |
---|
| 270 | 'COLOR'=> isset($edited_typetag['color']) ? $edited_typetag['color'] : $row['color'], |
---|
| 271 | )); |
---|
[9829] | 272 | } |
---|
| 273 | |
---|
| 274 | // +-----------------------------------------------------------------------+ |
---|
| 275 | // | sending html code | |
---|
| 276 | // +-----------------------------------------------------------------------+ |
---|
| 277 | |
---|
[9863] | 278 | $template->assign('typetags_ADMIN', typetags_ADMIN); |
---|
[9829] | 279 | $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content'); |
---|
| 280 | |
---|
| 281 | ?> |
---|