[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 | |
---|
[15149] | 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 | ); |
---|
[15149] | 35 | |
---|
| 36 | array_push($page['errors'], l10n('typetag_error')); |
---|
[9863] | 37 | } |
---|
| 38 | else if (isset($_POST['edittypetag'])) |
---|
| 39 | { |
---|
[15149] | 40 | // we must not rename typetag with an already existing name |
---|
| 41 | $query = ' |
---|
| 42 | SELECT id |
---|
| 43 | FROM '.typetags_TABLE.' |
---|
| 44 | WHERE |
---|
| 45 | name = "'.$_POST['typetag_name'].'" |
---|
| 46 | AND id != '.$_POST['edited_typetag'].' |
---|
| 47 | ;'; |
---|
[9829] | 48 | |
---|
[15149] | 49 | if ( pwg_db_num_rows(pwg_query($query)) ) |
---|
[10987] | 50 | { |
---|
| 51 | $edited_typetag = array( |
---|
[15149] | 52 | 'id' => $_POST['edited_typetag'], |
---|
| 53 | 'name' => $_POST['typetag_name'], |
---|
| 54 | 'color' => $_POST['typetag_color'], |
---|
[10987] | 55 | ); |
---|
| 56 | |
---|
[15149] | 57 | array_push($page['errors'], l10n('typetag_already_exists')); |
---|
[10987] | 58 | } |
---|
| 59 | else |
---|
| 60 | { |
---|
[15149] | 61 | $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); |
---|
[10987] | 69 | |
---|
[15149] | 70 | array_push($page['infos'], l10n('typetag_saved')); |
---|
[10987] | 71 | } |
---|
[9829] | 72 | } |
---|
| 73 | |
---|
| 74 | // +-----------------------------------------------------------------------+ |
---|
[9863] | 75 | // | delete typetags | |
---|
[9829] | 76 | // +-----------------------------------------------------------------------+ |
---|
| 77 | |
---|
[9863] | 78 | if (isset($_GET['deletetypetag'])) |
---|
[9829] | 79 | { |
---|
[15149] | 80 | $query = ' |
---|
| 81 | SELECT id |
---|
| 82 | FROM '.typetags_TABLE.' |
---|
| 83 | WHERE id = '.$_GET['deletetypetag'].' |
---|
| 84 | ;'; |
---|
[10987] | 85 | |
---|
[15149] | 86 | if ( pwg_db_num_rows(pwg_query($query)) ) |
---|
[10987] | 87 | { |
---|
[15149] | 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')); |
---|
[10987] | 102 | } |
---|
| 103 | else |
---|
| 104 | { |
---|
[15149] | 105 | array_push($page['errors'], l10n('typetag_unknown')); |
---|
[10987] | 106 | } |
---|
[9829] | 107 | } |
---|
| 108 | |
---|
| 109 | // +-----------------------------------------------------------------------+ |
---|
| 110 | // | add a typetag | |
---|
| 111 | // +-----------------------------------------------------------------------+ |
---|
| 112 | |
---|
[15149] | 113 | if ( isset($_POST['addtypetag']) and (empty($_POST['typetag_name']) or empty($_POST['typetag_color'])) ) |
---|
[9829] | 114 | { |
---|
[10987] | 115 | $template->assign('typetag', array( |
---|
[15149] | 116 | 'NAME' => $_POST['typetag_name'], |
---|
| 117 | 'COLOR' => $_POST['typetag_color'], |
---|
[10987] | 118 | )); |
---|
| 119 | |
---|
[15149] | 120 | array_push($page['errors'], l10n('typetag_error')); |
---|
[9829] | 121 | } |
---|
[9863] | 122 | else if (isset($_POST['addtypetag'])) |
---|
[9829] | 123 | { |
---|
[10987] | 124 | $typetag_name = $_POST['typetag_name']; |
---|
| 125 | $typetag_color = $_POST['typetag_color']; |
---|
[9829] | 126 | |
---|
[10987] | 127 | // does the tag already exists? |
---|
[15149] | 128 | $query = ' |
---|
| 129 | SELECT id |
---|
| 130 | FROM '.typetags_TABLE.' |
---|
| 131 | WHERE name = "'.$_POST['typetag_name'].'" |
---|
| 132 | '; |
---|
[9829] | 133 | |
---|
[15149] | 134 | if ( pwg_db_num_rows(pwg_query($query)) ) |
---|
[10987] | 135 | { |
---|
| 136 | $template->assign('typetag', array( |
---|
[15149] | 137 | 'NAME' => $_POST['typetag_name'], |
---|
| 138 | 'COLOR' => $_POST['typetag_color'], |
---|
[10987] | 139 | )); |
---|
| 140 | |
---|
[15149] | 141 | array_push($page['errors'], l10n('typetag_already_exists')); |
---|
[10987] | 142 | } |
---|
[15149] | 143 | else |
---|
| 144 | { |
---|
| 145 | $query = ' |
---|
| 146 | INSERT 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')); |
---|
| 158 | } |
---|
[9829] | 159 | } |
---|
| 160 | |
---|
| 161 | // +-----------------------------------------------------------------------+ |
---|
| 162 | // | Associate Tag to Typetage | |
---|
| 163 | // +-----------------------------------------------------------------------+ |
---|
| 164 | |
---|
[9863] | 165 | if (isset($_POST['delete_all_assoc'])) |
---|
[9829] | 166 | { |
---|
[15149] | 167 | pwg_query('UPDATE '.TAGS_TABLE.' SET id_typetags = NULL;'); |
---|
| 168 | array_push($page['infos'], l10n('All associations have been removed')); |
---|
[9863] | 169 | } |
---|
| 170 | else if (isset($_POST['associations'])) |
---|
[9829] | 171 | { |
---|
[15149] | 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 | } |
---|
[9829] | 182 | |
---|
[15149] | 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 | // +-----------------------------------------------------------------------+ |
---|
| 205 | if (isset($_POST['save_config'])) |
---|
| 206 | { |
---|
| 207 | $conf['TypeTags'] = array( |
---|
| 208 | 'show_all' => $_POST['show_all'] == 'true', |
---|
| 209 | ); |
---|
[10987] | 210 | |
---|
[15149] | 211 | conf_update_param('TypeTags', serialize($conf['TypeTags'])); |
---|
[9829] | 212 | } |
---|
| 213 | |
---|
[15149] | 214 | $template->assign('SHOW_ALL', $conf['TypeTags']['show_all']); |
---|
[9863] | 215 | |
---|
[9829] | 216 | // +-----------------------------------------------------------------------+ |
---|
| 217 | // | template init | |
---|
| 218 | // +-----------------------------------------------------------------------+ |
---|
| 219 | |
---|
| 220 | $template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/admin/typetags_admin.tpl')); |
---|
| 221 | |
---|
[15149] | 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); |
---|
[9829] | 232 | |
---|
[15149] | 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'])); |
---|
[10987] | 237 | $template->append('typetags_association', $row); |
---|
[9829] | 238 | } |
---|
| 239 | |
---|
[15149] | 240 | // get all typetags |
---|
| 241 | $query = 'SELECT * FROM '.typetags_TABLE.' ORDER BY name;'; |
---|
| 242 | $all_typetags = pwg_query($query); |
---|
[9829] | 243 | |
---|
[9863] | 244 | while ($row = mysql_fetch_assoc($all_typetags)) |
---|
[9829] | 245 | { |
---|
[10987] | 246 | $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); |
---|
[9829] | 251 | } |
---|
| 252 | |
---|
[15149] | 253 | // edit form |
---|
| 254 | if (isset($_GET['edittypetag'])) |
---|
| 255 | { |
---|
[10987] | 256 | $edited_typetag['id'] = $_GET['edittypetag']; |
---|
[9863] | 257 | } |
---|
[9829] | 258 | |
---|
[9863] | 259 | if (isset($edited_typetag['id'])) |
---|
[9829] | 260 | { |
---|
[10987] | 261 | $template->assign('edited_typetag', $edited_typetag['id']); |
---|
[15149] | 262 | |
---|
| 263 | $query = ' |
---|
| 264 | SELECT |
---|
| 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)); |
---|
[9829] | 272 | |
---|
[10987] | 273 | $template->assign('typetag', array( |
---|
| 274 | 'ID' => $row['id'], |
---|
| 275 | 'OLD_NAME' => $row['name'], |
---|
| 276 | 'OLD_COLOR' => $row['color'], |
---|
| 277 | 'COLOR_TEXT' => get_color_text($row['color']), |
---|
| 278 | 'NAME' => isset($edited_typetag['name']) ? $edited_typetag['name'] : $row['name'], |
---|
| 279 | 'COLOR'=> isset($edited_typetag['color']) ? $edited_typetag['color'] : $row['color'], |
---|
| 280 | )); |
---|
[15477] | 281 | |
---|
| 282 | $template->assign('IN_EDIT', true); |
---|
[9829] | 283 | } |
---|
| 284 | |
---|
| 285 | // +-----------------------------------------------------------------------+ |
---|
| 286 | // | sending html code | |
---|
| 287 | // +-----------------------------------------------------------------------+ |
---|
| 288 | |
---|
[9863] | 289 | $template->assign('typetags_ADMIN', typetags_ADMIN); |
---|
[9829] | 290 | $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content'); |
---|
| 291 | |
---|
| 292 | ?> |
---|