1 | <?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 | } |
---|
23 | |
---|
24 | // +-----------------------------------------------------------------------+ |
---|
25 | // | edit typetags | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
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 = ' |
---|
42 | SELECT 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)) ) |
---|
50 | { |
---|
51 | $edited_typetag = array( |
---|
52 | 'id' => $_POST['edited_typetag'], |
---|
53 | 'name' => $_POST['typetag_name'], |
---|
54 | 'color' => $_POST['typetag_color'], |
---|
55 | ); |
---|
56 | |
---|
57 | array_push($page['errors'], l10n('typetag_already_exists')); |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
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); |
---|
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 = ' |
---|
81 | SELECT 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'].'" |
---|
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')); |
---|
142 | } |
---|
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 | } |
---|
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, |
---|
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 | ); |
---|
210 | |
---|
211 | conf_update_param('TypeTags', serialize($conf['TypeTags'])); |
---|
212 | } |
---|
213 | |
---|
214 | $template->assign('SHOW_ALL', $conf['TypeTags']['show_all']); |
---|
215 | |
---|
216 | // +-----------------------------------------------------------------------+ |
---|
217 | // | template init | |
---|
218 | // +-----------------------------------------------------------------------+ |
---|
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 | } |
---|
239 | |
---|
240 | // get all typetags |
---|
241 | $query = 'SELECT * FROM '.typetags_TABLE.' ORDER BY name;'; |
---|
242 | $all_typetags = pwg_query($query); |
---|
243 | |
---|
244 | while ($row = mysql_fetch_assoc($all_typetags)) |
---|
245 | { |
---|
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); |
---|
251 | } |
---|
252 | |
---|
253 | // edit form |
---|
254 | if (isset($_GET['edittypetag'])) |
---|
255 | { |
---|
256 | $edited_typetag['id'] = $_GET['edittypetag']; |
---|
257 | } |
---|
258 | |
---|
259 | if (isset($edited_typetag['id'])) |
---|
260 | { |
---|
261 | $template->assign('edited_typetag', $edited_typetag['id']); |
---|
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)); |
---|
272 | |
---|
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 | )); |
---|
281 | |
---|
282 | $template->assign('IN_EDIT', true); |
---|
283 | } |
---|
284 | |
---|
285 | // +-----------------------------------------------------------------------+ |
---|
286 | // | sending html code | |
---|
287 | // +-----------------------------------------------------------------------+ |
---|
288 | |
---|
289 | $template->assign('typetags_ADMIN', typetags_ADMIN); |
---|
290 | $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content'); |
---|
291 | |
---|
292 | ?> |
---|