1 | <?php |
---|
2 | |
---|
3 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
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 | elseif (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['submit']) and !is_adviser()) |
---|
29 | { |
---|
30 | $typetag = mysql_escape_string($_POST['edited_typetag']); |
---|
31 | $typetag_name = mysql_escape_string($_POST['typetag_name']); |
---|
32 | $typetag_color = mysql_escape_string($_POST['typetag_color']); |
---|
33 | |
---|
34 | $existing_names = array(); |
---|
35 | |
---|
36 | $query = ' |
---|
37 | SELECT id, name, color |
---|
38 | FROM '.typetags_TABLE.' |
---|
39 | ;'; |
---|
40 | $result = pwg_query($query); |
---|
41 | while ($row = mysql_fetch_array($result)) |
---|
42 | { |
---|
43 | array_push($existing_names, $row['name']); |
---|
44 | if ($typetag == $row['id']) |
---|
45 | { |
---|
46 | $current_name = $row['name']; |
---|
47 | $current_color = $row['color']; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | // we must not rename typetag with an already existing name |
---|
52 | if ($typetag_name != $current_name and in_array($typetag_name, $existing_names)) |
---|
53 | { |
---|
54 | array_push($page['errors'], l10n('typetags_already exists')); |
---|
55 | |
---|
56 | $edited_typetag = array( |
---|
57 | 'id' => $typetag, |
---|
58 | 'name' => $typetag_name, |
---|
59 | 'color' => $typetag_color); |
---|
60 | } |
---|
61 | else |
---|
62 | { |
---|
63 | array_push($page['infos'], l10n('typetags_saved')); |
---|
64 | |
---|
65 | mass_updates(typetags_TABLE, |
---|
66 | array( |
---|
67 | 'primary' => array('id'), |
---|
68 | 'update' => array('name', 'color')), |
---|
69 | array( |
---|
70 | array( |
---|
71 | 'id' => $typetag, |
---|
72 | 'name' => $typetag_name, |
---|
73 | 'color' => $typetag_color))); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | // +-----------------------------------------------------------------------+ |
---|
78 | // | delete typetags | |
---|
79 | // +-----------------------------------------------------------------------+ |
---|
80 | |
---|
81 | if (isset($_POST['deletetypetag']) and isset($_POST['typetags']) and !is_adviser()) |
---|
82 | { |
---|
83 | $query = ' |
---|
84 | SELECT name |
---|
85 | FROM '.typetags_TABLE.' |
---|
86 | WHERE id IN ('.implode(',', $_POST['typetags']).') |
---|
87 | ;'; |
---|
88 | $typetag_names = array_from_query($query, 'name'); |
---|
89 | |
---|
90 | $query = ' UPDATE '. TAGS_TABLE .' |
---|
91 | SET id_typetags = NULL |
---|
92 | WHERE id_typetags IN ('.implode(',', $_POST['typetags']).') |
---|
93 | ;'; |
---|
94 | pwg_query($query); |
---|
95 | |
---|
96 | $query = ' |
---|
97 | DELETE |
---|
98 | FROM '.typetags_TABLE.' |
---|
99 | WHERE id IN ('.implode(',', $_POST['typetags']).') |
---|
100 | ;'; |
---|
101 | pwg_query($query); |
---|
102 | |
---|
103 | array_push($page['infos'], |
---|
104 | l10n_dec('typetag_suppr', 'typetags_suppr', count($typetag_names)) |
---|
105 | .' : '.implode(', ', $typetag_names) |
---|
106 | ); |
---|
107 | } |
---|
108 | |
---|
109 | // +-----------------------------------------------------------------------+ |
---|
110 | // | add a typetag | |
---|
111 | // +-----------------------------------------------------------------------+ |
---|
112 | |
---|
113 | if (isset($_POST['addtypetag']) and (empty($_POST['add_typetag']) or empty ($_POST['hexval']))) |
---|
114 | { |
---|
115 | array_push($page['errors'], l10n('typetags_error')); |
---|
116 | |
---|
117 | $template->assign('typetag', array( |
---|
118 | 'NAME' => isset($_POST['add_typetag']) ? $_POST['add_typetag'] : '', |
---|
119 | 'COLOR' => isset($_POST['hexval']) ? $_POST['hexval'] : '')); |
---|
120 | } |
---|
121 | elseif (isset($_POST['addtypetag']) and !empty($_POST['add_typetag']) and !empty ($_POST['hexval']) and !is_adviser()) |
---|
122 | { |
---|
123 | $typetag_name = $_POST['add_typetag']; |
---|
124 | $typetag_color = $_POST['hexval']; |
---|
125 | |
---|
126 | // does the tag already exists? |
---|
127 | $query = ' |
---|
128 | SELECT id |
---|
129 | FROM '.typetags_TABLE.' |
---|
130 | WHERE name = \''.$typetag_name.'\' |
---|
131 | ;'; |
---|
132 | $existing_tags = array_from_query($query, 'id'); |
---|
133 | |
---|
134 | if (count($existing_tags) == 0) |
---|
135 | { |
---|
136 | mass_inserts( |
---|
137 | typetags_TABLE, |
---|
138 | array('name', 'color'), |
---|
139 | array( |
---|
140 | array( |
---|
141 | 'name' => $typetag_name, |
---|
142 | 'color' => $typetag_color, |
---|
143 | ) |
---|
144 | ) |
---|
145 | ); |
---|
146 | |
---|
147 | array_push($page['infos'], l10n('typetags_saved')); |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | array_push($page['errors'],l10n('typetags_already exists')); |
---|
152 | |
---|
153 | $template->assign('typetag', array( |
---|
154 | 'NAME' => $typetag_name, |
---|
155 | 'COLOR' => $typetag_color)); |
---|
156 | } |
---|
157 | } |
---|
158 | elseif (isset($_POST['add']) and !is_adviser()) |
---|
159 | { |
---|
160 | array_push($page['errors'], l10n('typetags_error')); |
---|
161 | } |
---|
162 | |
---|
163 | // +-----------------------------------------------------------------------+ |
---|
164 | // | Associate Tag to Typetage | |
---|
165 | // +-----------------------------------------------------------------------+ |
---|
166 | |
---|
167 | |
---|
168 | if (isset($_POST['associate']) AND !empty($_POST['assoc_tags']) AND ($_POST['typetaglist'] != 0)) |
---|
169 | { |
---|
170 | $typetag_id = $_POST['typetaglist']; |
---|
171 | |
---|
172 | $query = ' UPDATE '. TAGS_TABLE .' |
---|
173 | SET id_typetags = '. $typetag_id .' |
---|
174 | WHERE id IN ('.implode(',', $_POST['assoc_tags']).') |
---|
175 | ;'; |
---|
176 | pwg_query($query); |
---|
177 | |
---|
178 | array_push( |
---|
179 | $page['infos'], |
---|
180 | sprintf( |
---|
181 | l10n('typetags_associated'), |
---|
182 | stripslashes($typetag_id) |
---|
183 | ) |
---|
184 | ); |
---|
185 | } |
---|
186 | elseif (isset($_POST['associate']) and !is_adviser()) |
---|
187 | { |
---|
188 | array_push($page['errors'], l10n('error_associate')); |
---|
189 | } |
---|
190 | |
---|
191 | // +-----------------------------------------------------------------------+ |
---|
192 | // | Dissociate Tag to Typetage | |
---|
193 | // +-----------------------------------------------------------------------+ |
---|
194 | |
---|
195 | if (isset($_POST['dissociate']) AND !empty($_POST['dissoc_tags']) and !is_adviser()) |
---|
196 | { |
---|
197 | $associated_tag_id = implode(',', $_POST['dissoc_tags']); |
---|
198 | |
---|
199 | $query = ' UPDATE '. TAGS_TABLE .' |
---|
200 | SET id_typetags = NULL |
---|
201 | WHERE id IN (' . $associated_tag_id . ') |
---|
202 | ;'; |
---|
203 | pwg_query($query); |
---|
204 | |
---|
205 | |
---|
206 | array_push($page['infos'], |
---|
207 | sprintf(l10n('typetags_dissociated'), |
---|
208 | stripslashes($associated_tag_id ) |
---|
209 | ) |
---|
210 | ); |
---|
211 | } |
---|
212 | elseif (isset($_POST['dissociate']) and !is_adviser()) |
---|
213 | { |
---|
214 | array_push($page['errors'], l10n('error_dissociate')); |
---|
215 | } |
---|
216 | |
---|
217 | // +-----------------------------------------------------------------------+ |
---|
218 | // | template init | |
---|
219 | // +-----------------------------------------------------------------------+ |
---|
220 | |
---|
221 | $template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/typetags_admin.tpl')); |
---|
222 | |
---|
223 | //Recupere les tags associés |
---|
224 | $query = ' |
---|
225 | SELECT t.id , t.name, tt.color |
---|
226 | FROM ' . TAGS_TABLE . ' as t |
---|
227 | INNER JOIN ' .typetags_TABLE. ' as tt |
---|
228 | ON t.id_typetags = tt.id |
---|
229 | WHERE t.id_typetags is NOT NULL |
---|
230 | ORDER by t.id'; |
---|
231 | $result = pwg_query($query); |
---|
232 | |
---|
233 | $tags = array(); |
---|
234 | |
---|
235 | while ($row = mysql_fetch_assoc($result)) |
---|
236 | { |
---|
237 | array_push($tags, $row); |
---|
238 | } |
---|
239 | |
---|
240 | usort($tags, 'name_compare'); |
---|
241 | $template->assign('typetags_dissociation', $tags); |
---|
242 | |
---|
243 | // Récupère tous les tags |
---|
244 | $query = ' |
---|
245 | SELECT t.id , t.name, tt.color |
---|
246 | FROM ' . TAGS_TABLE . ' as t |
---|
247 | LEFT JOIN ' .typetags_TABLE.' as tt |
---|
248 | ON t.id_typetags = tt.id'; |
---|
249 | $result = pwg_query($query); |
---|
250 | |
---|
251 | $tags = array(); |
---|
252 | |
---|
253 | while ($row = mysql_fetch_assoc($result)) |
---|
254 | { |
---|
255 | array_push($tags, $row); |
---|
256 | } |
---|
257 | |
---|
258 | usort($tags, 'name_compare'); |
---|
259 | $template->assign('typetags_association', $tags); |
---|
260 | |
---|
261 | // +-----------------------------------------------------------------------+ |
---|
262 | // | form creation | |
---|
263 | // +-----------------------------------------------------------------------+ |
---|
264 | |
---|
265 | $query = 'SELECT * FROM '.typetags_TABLE.';'; |
---|
266 | $result = pwg_query($query); |
---|
267 | $typetags = array(); |
---|
268 | while ($row = mysql_fetch_assoc($result)) |
---|
269 | { |
---|
270 | $row['color_text'] = get_color_text($row['color']); |
---|
271 | array_push($typetags, $row); |
---|
272 | } |
---|
273 | |
---|
274 | usort($typetags, 'name_compare'); |
---|
275 | |
---|
276 | $template->assign('typetags_selection', $typetags); |
---|
277 | |
---|
278 | if (isset($_POST['edittypetag']) and isset($_POST['typetags'])) |
---|
279 | { |
---|
280 | if (count($_POST['typetags']) > 1) |
---|
281 | { |
---|
282 | array_push($page['errors'], l10n('You should select only one Typetag to edit.')); |
---|
283 | } |
---|
284 | else |
---|
285 | { |
---|
286 | $edited_typetag['id'] = $_POST['typetags'][0]; |
---|
287 | } |
---|
288 | } |
---|
289 | if (isset($edited_typetag)) |
---|
290 | { |
---|
291 | $template->assign('edited_typetag', $edited_typetag['id']); |
---|
292 | |
---|
293 | $query = ' |
---|
294 | SELECT id, name, color |
---|
295 | FROM '.typetags_TABLE.' |
---|
296 | WHERE id = '.$edited_typetag['id'].' |
---|
297 | ;'; |
---|
298 | $result = pwg_query($query); |
---|
299 | while ($row = mysql_fetch_array($result)) |
---|
300 | { |
---|
301 | $template->assign('typetag', |
---|
302 | array( |
---|
303 | 'ID' => $row['id'], |
---|
304 | 'OLD_NAME' => $row['name'], |
---|
305 | 'OLD_COLOR' => $row['color'], |
---|
306 | 'COLOR_TEXT' => get_color_text($row['color']), |
---|
307 | 'NAME' => isset($edited_typetag['name']) ? $edited_typetag['name'] : $row['name'], |
---|
308 | 'COLOR'=> isset($edited_typetag['color']) ? $edited_typetag['color'] : $row['color'])); |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | // +-----------------------------------------------------------------------+ |
---|
313 | // | sending html code | |
---|
314 | // +-----------------------------------------------------------------------+ |
---|
315 | |
---|
316 | $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content'); |
---|
317 | |
---|
318 | ?> |
---|