1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2006-11-03 01:26:31 +0000 (Fri, 03 Nov 2006) $ |
---|
10 | // | last modifier : $Author: rvelices $ |
---|
11 | // | revision : $Revision: 1592 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | include(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
---|
29 | |
---|
30 | |
---|
31 | // The function delete_site deletes a site and call the function |
---|
32 | // delete_categories for each primary category of the site |
---|
33 | function delete_site( $id ) |
---|
34 | { |
---|
35 | // destruction of the categories of the site |
---|
36 | $query = ' |
---|
37 | SELECT id |
---|
38 | FROM '.CATEGORIES_TABLE.' |
---|
39 | WHERE site_id = '.$id.' |
---|
40 | ;'; |
---|
41 | $result = pwg_query($query); |
---|
42 | $category_ids = array(); |
---|
43 | while ($row = mysql_fetch_array($result)) |
---|
44 | { |
---|
45 | array_push($category_ids, $row['id']); |
---|
46 | } |
---|
47 | delete_categories($category_ids); |
---|
48 | |
---|
49 | // destruction of the site |
---|
50 | $query = ' |
---|
51 | DELETE FROM '.SITES_TABLE.' |
---|
52 | WHERE id = '.$id.' |
---|
53 | ;'; |
---|
54 | pwg_query($query); |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | // The function delete_categories deletes the categories identified by the |
---|
59 | // (numeric) key of the array $ids. It also deletes (in the database) : |
---|
60 | // - all the elements of the category (delete_elements, see further) |
---|
61 | // - all the links between elements and this category |
---|
62 | // - all the restrictions linked to the category |
---|
63 | // The function works recursively. |
---|
64 | function delete_categories($ids) |
---|
65 | { |
---|
66 | global $counts; |
---|
67 | |
---|
68 | if (count($ids) == 0) |
---|
69 | { |
---|
70 | return; |
---|
71 | } |
---|
72 | |
---|
73 | // add sub-category ids to the given ids : if a category is deleted, all |
---|
74 | // sub-categories must be so |
---|
75 | $ids = get_subcat_ids($ids); |
---|
76 | |
---|
77 | // destruction of all the related elements |
---|
78 | $query = ' |
---|
79 | SELECT id |
---|
80 | FROM '.IMAGES_TABLE.' |
---|
81 | WHERE storage_category_id IN ( |
---|
82 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
83 | ;'; |
---|
84 | $result = pwg_query($query); |
---|
85 | $element_ids = array(); |
---|
86 | while ($row = mysql_fetch_array($result)) |
---|
87 | { |
---|
88 | array_push($element_ids, $row['id']); |
---|
89 | } |
---|
90 | delete_elements($element_ids); |
---|
91 | |
---|
92 | // destruction of the links between images and this category |
---|
93 | $query = ' |
---|
94 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
95 | WHERE category_id IN ( |
---|
96 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
97 | ;'; |
---|
98 | pwg_query($query); |
---|
99 | |
---|
100 | // destruction of the access linked to the category |
---|
101 | $query = ' |
---|
102 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
103 | WHERE cat_id IN ( |
---|
104 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
105 | ;'; |
---|
106 | pwg_query($query); |
---|
107 | |
---|
108 | $query = ' |
---|
109 | DELETE FROM '.GROUP_ACCESS_TABLE.' |
---|
110 | WHERE cat_id IN ( |
---|
111 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
112 | ;'; |
---|
113 | pwg_query($query); |
---|
114 | |
---|
115 | // destruction of the category |
---|
116 | $query = ' |
---|
117 | DELETE FROM '.CATEGORIES_TABLE.' |
---|
118 | WHERE id IN ( |
---|
119 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
120 | ;'; |
---|
121 | pwg_query($query); |
---|
122 | |
---|
123 | if (isset($counts['del_categories'])) |
---|
124 | { |
---|
125 | $counts['del_categories']+= count($ids); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | // The function delete_elements deletes the elements identified by the |
---|
130 | // (numeric) values of the array $ids. It also deletes (in the database) : |
---|
131 | // - all the comments related to elements |
---|
132 | // - all the links between categories and elements |
---|
133 | // - all the favorites associated to elements |
---|
134 | function delete_elements($ids) |
---|
135 | { |
---|
136 | global $counts; |
---|
137 | |
---|
138 | if (count($ids) == 0) |
---|
139 | { |
---|
140 | return; |
---|
141 | } |
---|
142 | |
---|
143 | // destruction of the comments on the image |
---|
144 | $query = ' |
---|
145 | DELETE FROM '.COMMENTS_TABLE.' |
---|
146 | WHERE image_id IN ( |
---|
147 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
148 | ;'; |
---|
149 | pwg_query($query); |
---|
150 | |
---|
151 | // destruction of the links between images and this category |
---|
152 | $query = ' |
---|
153 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
154 | WHERE image_id IN ( |
---|
155 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
156 | ;'; |
---|
157 | pwg_query($query); |
---|
158 | |
---|
159 | // destruction of the links between images and tags |
---|
160 | $query = ' |
---|
161 | DELETE FROM '.IMAGE_TAG_TABLE.' |
---|
162 | WHERE image_id IN ( |
---|
163 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
164 | ;'; |
---|
165 | pwg_query($query); |
---|
166 | |
---|
167 | // destruction of the favorites associated with the picture |
---|
168 | $query = ' |
---|
169 | DELETE FROM '.FAVORITES_TABLE.' |
---|
170 | WHERE image_id IN ( |
---|
171 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
172 | ;'; |
---|
173 | pwg_query($query); |
---|
174 | |
---|
175 | // destruction of the rates associated to this element |
---|
176 | $query = ' |
---|
177 | DELETE FROM '.RATE_TABLE.' |
---|
178 | WHERE element_id IN ( |
---|
179 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
180 | ;'; |
---|
181 | pwg_query($query); |
---|
182 | |
---|
183 | // destruction of the rates associated to this element |
---|
184 | $query = ' |
---|
185 | DELETE FROM '.CADDIE_TABLE.' |
---|
186 | WHERE element_id IN ( |
---|
187 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
188 | ;'; |
---|
189 | pwg_query($query); |
---|
190 | |
---|
191 | // destruction of the image |
---|
192 | $query = ' |
---|
193 | DELETE FROM '.IMAGES_TABLE.' |
---|
194 | WHERE id IN ( |
---|
195 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
196 | ;'; |
---|
197 | pwg_query($query); |
---|
198 | |
---|
199 | if (isset($counts['del_elements'])) |
---|
200 | { |
---|
201 | $counts['del_elements']+= count($ids); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | // The delete_user function delete a user identified by the $user_id |
---|
206 | // It also deletes : |
---|
207 | // - all the access linked to this user |
---|
208 | // - all the links to any group |
---|
209 | // - all the favorites linked to this user |
---|
210 | // - calculated permissions linked to the user |
---|
211 | // - all datas about notifications for the user |
---|
212 | function delete_user($user_id) |
---|
213 | { |
---|
214 | global $conf; |
---|
215 | |
---|
216 | // destruction of the access linked to the user |
---|
217 | $query = ' |
---|
218 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
219 | WHERE user_id = '.$user_id.' |
---|
220 | ;'; |
---|
221 | pwg_query($query); |
---|
222 | |
---|
223 | // destruction of data notification by mail for this user |
---|
224 | $query = ' |
---|
225 | DELETE FROM '.USER_MAIL_NOTIFICATION_TABLE.' |
---|
226 | WHERE user_id = '.$user_id.' |
---|
227 | ;'; |
---|
228 | pwg_query($query); |
---|
229 | |
---|
230 | // destruction of data RSS notification for this user |
---|
231 | $query = ' |
---|
232 | DELETE FROM '.USER_FEED_TABLE.' |
---|
233 | WHERE user_id = '.$user_id.' |
---|
234 | ;'; |
---|
235 | pwg_query($query); |
---|
236 | |
---|
237 | // destruction of the group links for this user |
---|
238 | $query = ' |
---|
239 | DELETE FROM '.USER_GROUP_TABLE.' |
---|
240 | WHERE user_id = '.$user_id.' |
---|
241 | ;'; |
---|
242 | pwg_query($query); |
---|
243 | |
---|
244 | // destruction of the favorites associated with the user |
---|
245 | $query = ' |
---|
246 | DELETE FROM '.FAVORITES_TABLE.' |
---|
247 | WHERE user_id = '.$user_id.' |
---|
248 | ;'; |
---|
249 | pwg_query($query); |
---|
250 | |
---|
251 | // deletion of calculated permissions linked to the user |
---|
252 | $query = ' |
---|
253 | DELETE FROM '.USER_CACHE_TABLE.' |
---|
254 | WHERE user_id = '.$user_id.' |
---|
255 | ;'; |
---|
256 | pwg_query($query); |
---|
257 | |
---|
258 | // deletion of phpwebgallery specific informations |
---|
259 | $query = ' |
---|
260 | DELETE FROM '.USER_INFOS_TABLE.' |
---|
261 | WHERE user_id = '.$user_id.' |
---|
262 | ;'; |
---|
263 | pwg_query($query); |
---|
264 | |
---|
265 | // destruction of the user |
---|
266 | $query = ' |
---|
267 | DELETE FROM '.USERS_TABLE.' |
---|
268 | WHERE '.$conf['user_fields']['id'].' = '.$user_id.' |
---|
269 | ;'; |
---|
270 | pwg_query($query); |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * updates calculated informations about a set of categories : date_last and |
---|
275 | * nb_images. It also verifies that the representative picture is really |
---|
276 | * linked to the category. Optionnaly recursive. |
---|
277 | * |
---|
278 | * @param mixed category id |
---|
279 | * @param boolean recursive |
---|
280 | * @returns void |
---|
281 | */ |
---|
282 | function update_category($ids = 'all', $recursive = false) |
---|
283 | { |
---|
284 | global $conf; |
---|
285 | |
---|
286 | // retrieving all categories to update |
---|
287 | $cat_ids = array(); |
---|
288 | |
---|
289 | $query = ' |
---|
290 | SELECT id |
---|
291 | FROM '.CATEGORIES_TABLE; |
---|
292 | if (is_array($ids)) |
---|
293 | { |
---|
294 | if ($recursive) |
---|
295 | { |
---|
296 | foreach ($ids as $num => $id) |
---|
297 | { |
---|
298 | if ($num == 0) |
---|
299 | { |
---|
300 | $query.= ' |
---|
301 | WHERE '; |
---|
302 | } |
---|
303 | else |
---|
304 | { |
---|
305 | $query.= ' |
---|
306 | OR '; |
---|
307 | } |
---|
308 | $query.= 'uppercats REGEXP \'(^|,)'.$id.'(,|$)\''; |
---|
309 | } |
---|
310 | } |
---|
311 | else |
---|
312 | { |
---|
313 | $query.= ' |
---|
314 | WHERE id IN ('.wordwrap(implode(', ', $ids), 80, "\n").')'; |
---|
315 | } |
---|
316 | } |
---|
317 | $query.= ' |
---|
318 | ;'; |
---|
319 | $cat_ids = array_unique(array_from_query($query, 'id')); |
---|
320 | |
---|
321 | if (count($cat_ids) == 0) |
---|
322 | { |
---|
323 | return false; |
---|
324 | } |
---|
325 | |
---|
326 | // calculate informations about categories retrieved |
---|
327 | $query = ' |
---|
328 | SELECT category_id, |
---|
329 | COUNT(image_id) AS nb_images, |
---|
330 | MAX(date_available) AS date_last |
---|
331 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id |
---|
332 | WHERE category_id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
333 | GROUP BY category_id |
---|
334 | ;'; |
---|
335 | $result = pwg_query($query); |
---|
336 | $datas = array(); |
---|
337 | $query_ids = array(); |
---|
338 | while ( $row = mysql_fetch_array( $result ) ) |
---|
339 | { |
---|
340 | array_push($query_ids, $row['category_id']); |
---|
341 | |
---|
342 | array_push( |
---|
343 | $datas, |
---|
344 | array( |
---|
345 | 'id' => $row['category_id'], |
---|
346 | 'date_last' => $row['date_last'], |
---|
347 | 'nb_images' => $row['nb_images'] |
---|
348 | ) |
---|
349 | ); |
---|
350 | } |
---|
351 | // if all links between a category and elements have disappeared, no line |
---|
352 | // is returned but the update must be done ! |
---|
353 | foreach (array_diff($cat_ids, $query_ids) as $id) |
---|
354 | { |
---|
355 | array_push($datas, array('id' => $id, 'nb_images' => 0)); |
---|
356 | } |
---|
357 | |
---|
358 | $fields = array('primary' => array('id'), |
---|
359 | 'update' => array('date_last', 'nb_images')); |
---|
360 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
361 | |
---|
362 | // representative pictures |
---|
363 | if (count($cat_ids) > 0) |
---|
364 | { |
---|
365 | // find all categories where the setted representative is not possible : |
---|
366 | // the picture does not exist |
---|
367 | $query = ' |
---|
368 | SELECT c.id |
---|
369 | FROM '.CATEGORIES_TABLE.' AS c LEFT JOIN '.IMAGES_TABLE.' AS i |
---|
370 | ON c.representative_picture_id = i.id |
---|
371 | WHERE representative_picture_id IS NOT NULL |
---|
372 | AND c.id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
373 | AND i.id IS NULL |
---|
374 | ;'; |
---|
375 | $wrong_representant = array_from_query($query, 'id'); |
---|
376 | |
---|
377 | if ($conf['allow_random_representative']) |
---|
378 | { |
---|
379 | if (count($wrong_representant) > 0) |
---|
380 | { |
---|
381 | $query = ' |
---|
382 | UPDATE '.CATEGORIES_TABLE.' |
---|
383 | SET representative_picture_id = NULL |
---|
384 | WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 80, "\n").') |
---|
385 | ;'; |
---|
386 | pwg_query($query); |
---|
387 | } |
---|
388 | } |
---|
389 | else |
---|
390 | { |
---|
391 | $to_null = array(); |
---|
392 | $to_rand = array(); |
---|
393 | |
---|
394 | if (count($wrong_representant) > 0) |
---|
395 | { |
---|
396 | // among the categories with an unknown representant, we dissociate |
---|
397 | // categories containing pictures and categories containing no |
---|
398 | // pictures. Indeed, the representant must set to NULL if no picture |
---|
399 | // in the category and set to a random picture otherwise. |
---|
400 | $query = ' |
---|
401 | SELECT id |
---|
402 | FROM '.CATEGORIES_TABLE.' |
---|
403 | WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 80, "\n").') |
---|
404 | AND nb_images = 0 |
---|
405 | ;'; |
---|
406 | $to_null = array_from_query($query, 'id'); |
---|
407 | $to_rand = array_diff($wrong_representant, $to_null); |
---|
408 | } |
---|
409 | |
---|
410 | if (count($to_null) > 0) |
---|
411 | { |
---|
412 | $query = ' |
---|
413 | UPDATE '.CATEGORIES_TABLE.' |
---|
414 | SET representative_picture_id = NULL |
---|
415 | WHERE id IN ('.wordwrap(implode(', ', $to_null), 80, "\n").') |
---|
416 | ;'; |
---|
417 | pwg_query($query); |
---|
418 | } |
---|
419 | |
---|
420 | // If the random representant is not allowed, we need to find |
---|
421 | // categories with elements and with no representant. Those categories |
---|
422 | // must be added to the list of categories to set to a random |
---|
423 | // representant. |
---|
424 | $query = ' |
---|
425 | SELECT id |
---|
426 | FROM '.CATEGORIES_TABLE.' |
---|
427 | WHERE representative_picture_id IS NULL |
---|
428 | AND nb_images != 0 |
---|
429 | AND id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
430 | ;'; |
---|
431 | $to_rand = |
---|
432 | array_unique( |
---|
433 | array_merge( |
---|
434 | $to_rand, |
---|
435 | array_from_query($query, 'id') |
---|
436 | ) |
---|
437 | ); |
---|
438 | |
---|
439 | if (count($to_rand) > 0) |
---|
440 | { |
---|
441 | set_random_representant($to_rand); |
---|
442 | } |
---|
443 | } |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | function date_convert_back( $date ) |
---|
448 | { |
---|
449 | // date arrives at this format : YYYY-MM-DD |
---|
450 | // It must be transformed in DD/MM/YYYY |
---|
451 | if ( $date != '' ) |
---|
452 | { |
---|
453 | list($year,$month,$day) = explode( '-', $date ); |
---|
454 | return $day.'/'.$month.'/'.$year; |
---|
455 | } |
---|
456 | else |
---|
457 | { |
---|
458 | return ''; |
---|
459 | } |
---|
460 | } |
---|
461 | |
---|
462 | /** |
---|
463 | * returns an array containing sub-directories which can be a category, |
---|
464 | * recursive by default |
---|
465 | * |
---|
466 | * directories nammed "thumbnail", "pwg_high" or "pwg_representative" are |
---|
467 | * omitted |
---|
468 | * |
---|
469 | * @param string $basedir |
---|
470 | * @return array |
---|
471 | */ |
---|
472 | function get_fs_directories($path, $recursive = true) |
---|
473 | { |
---|
474 | $dirs = array(); |
---|
475 | |
---|
476 | if (is_dir($path)) |
---|
477 | { |
---|
478 | if ($contents = opendir($path)) |
---|
479 | { |
---|
480 | while (($node = readdir($contents)) !== false) |
---|
481 | { |
---|
482 | if (is_dir($path.'/'.$node) |
---|
483 | and $node != '.' |
---|
484 | and $node != '..' |
---|
485 | and $node != '.svn' |
---|
486 | and $node != 'thumbnail' |
---|
487 | and $node != 'pwg_high' |
---|
488 | and $node != 'pwg_representative') |
---|
489 | { |
---|
490 | array_push($dirs, $path.'/'.$node); |
---|
491 | if ($recursive) |
---|
492 | { |
---|
493 | $dirs = array_merge($dirs, get_fs_directories($path.'/'.$node)); |
---|
494 | } |
---|
495 | } |
---|
496 | } |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | return $dirs; |
---|
501 | } |
---|
502 | |
---|
503 | /** |
---|
504 | * inserts multiple lines in a table |
---|
505 | * |
---|
506 | * @param string table_name |
---|
507 | * @param array dbfields |
---|
508 | * @param array inserts |
---|
509 | * @return void |
---|
510 | */ |
---|
511 | function mass_inserts($table_name, $dbfields, $datas) |
---|
512 | { |
---|
513 | // inserts all found categories |
---|
514 | $query = ' |
---|
515 | INSERT INTO '.$table_name.' |
---|
516 | ('.implode(',', $dbfields).') |
---|
517 | VALUES'; |
---|
518 | foreach ($datas as $insert_id => $insert) |
---|
519 | { |
---|
520 | $query.= ' |
---|
521 | '; |
---|
522 | if ($insert_id > 0) |
---|
523 | { |
---|
524 | $query.= ','; |
---|
525 | } |
---|
526 | $query.= '('; |
---|
527 | foreach ($dbfields as $field_id => $dbfield) |
---|
528 | { |
---|
529 | if ($field_id > 0) |
---|
530 | { |
---|
531 | $query.= ','; |
---|
532 | } |
---|
533 | |
---|
534 | if (!isset($insert[$dbfield]) or $insert[$dbfield] === '') |
---|
535 | { |
---|
536 | $query.= 'NULL'; |
---|
537 | } |
---|
538 | else |
---|
539 | { |
---|
540 | $query.= "'".$insert[$dbfield]."'"; |
---|
541 | } |
---|
542 | } |
---|
543 | $query.=')'; |
---|
544 | } |
---|
545 | $query.= ' |
---|
546 | ;'; |
---|
547 | pwg_query($query); |
---|
548 | } |
---|
549 | |
---|
550 | /** |
---|
551 | * updates multiple lines in a table |
---|
552 | * |
---|
553 | * @param string table_name |
---|
554 | * @param array dbfields |
---|
555 | * @param array datas |
---|
556 | * @return void |
---|
557 | */ |
---|
558 | function mass_updates($tablename, $dbfields, $datas) |
---|
559 | { |
---|
560 | // depending on the MySQL version, we use the multi table update or N |
---|
561 | // update queries |
---|
562 | $query = 'SELECT VERSION() AS version;'; |
---|
563 | list($mysql_version) = mysql_fetch_array(pwg_query($query)); |
---|
564 | if (count($datas) < 10 or version_compare($mysql_version, '4.0.4') < 0) |
---|
565 | { |
---|
566 | // MySQL is prior to version 4.0.4, multi table update feature is not |
---|
567 | // available |
---|
568 | foreach ($datas as $data) |
---|
569 | { |
---|
570 | $query = ' |
---|
571 | UPDATE '.$tablename.' |
---|
572 | SET '; |
---|
573 | $is_first = true; |
---|
574 | foreach ($dbfields['update'] as $num => $key) |
---|
575 | { |
---|
576 | if (!$is_first) |
---|
577 | { |
---|
578 | $query.= ",\n "; |
---|
579 | } |
---|
580 | $query.= $key.' = '; |
---|
581 | if (isset($data[$key]) and $data[$key] != '') |
---|
582 | { |
---|
583 | $query.= '\''.$data[$key].'\''; |
---|
584 | } |
---|
585 | else |
---|
586 | { |
---|
587 | $query.= 'NULL'; |
---|
588 | } |
---|
589 | $is_first = false; |
---|
590 | } |
---|
591 | $query.= ' |
---|
592 | WHERE '; |
---|
593 | foreach ($dbfields['primary'] as $num => $key) |
---|
594 | { |
---|
595 | if ($num > 1) |
---|
596 | { |
---|
597 | $query.= ' AND '; |
---|
598 | } |
---|
599 | $query.= $key.' = \''.$data[$key].'\''; |
---|
600 | } |
---|
601 | $query.= ' |
---|
602 | ;'; |
---|
603 | pwg_query($query); |
---|
604 | } |
---|
605 | } |
---|
606 | else |
---|
607 | { |
---|
608 | // creation of the temporary table |
---|
609 | $query = ' |
---|
610 | SHOW FULL COLUMNS FROM '.$tablename.' |
---|
611 | ;'; |
---|
612 | $result = pwg_query($query); |
---|
613 | $columns = array(); |
---|
614 | $all_fields = array_merge($dbfields['primary'], $dbfields['update']); |
---|
615 | while ($row = mysql_fetch_array($result)) |
---|
616 | { |
---|
617 | if (in_array($row['Field'], $all_fields)) |
---|
618 | { |
---|
619 | $column = $row['Field']; |
---|
620 | $column.= ' '.$row['Type']; |
---|
621 | if (!isset($row['Null']) or $row['Null'] == '') |
---|
622 | { |
---|
623 | $column.= ' NOT NULL'; |
---|
624 | } |
---|
625 | if (isset($row['Default'])) |
---|
626 | { |
---|
627 | $column.= " default '".$row['Default']."'"; |
---|
628 | } |
---|
629 | if (isset($row['Collation']) and $row['Collation'] != 'NULL') |
---|
630 | { |
---|
631 | $column.= " collate '".$row['Collation']."'"; |
---|
632 | } |
---|
633 | array_push($columns, $column); |
---|
634 | } |
---|
635 | } |
---|
636 | |
---|
637 | $temporary_tablename = $tablename.'_'.micro_seconds(); |
---|
638 | |
---|
639 | $query = ' |
---|
640 | CREATE TABLE '.$temporary_tablename.' |
---|
641 | ( |
---|
642 | '.implode(",\n", $columns).', |
---|
643 | PRIMARY KEY ('.implode(',', $dbfields['primary']).') |
---|
644 | ) |
---|
645 | ;'; |
---|
646 | pwg_query($query); |
---|
647 | mass_inserts($temporary_tablename, $all_fields, $datas); |
---|
648 | // update of images table by joining with temporary table |
---|
649 | $query = ' |
---|
650 | UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2 |
---|
651 | SET '. |
---|
652 | implode( |
---|
653 | "\n , ", |
---|
654 | array_map( |
---|
655 | create_function('$s', 'return "t1.$s = t2.$s";'), |
---|
656 | $dbfields['update'] |
---|
657 | ) |
---|
658 | ).' |
---|
659 | WHERE '. |
---|
660 | implode( |
---|
661 | "\n AND ", |
---|
662 | array_map( |
---|
663 | create_function('$s', 'return "t1.$s = t2.$s";'), |
---|
664 | $dbfields['primary'] |
---|
665 | ) |
---|
666 | ).' |
---|
667 | ;'; |
---|
668 | pwg_query($query); |
---|
669 | $query = ' |
---|
670 | DROP TABLE '.$temporary_tablename.' |
---|
671 | ;'; |
---|
672 | pwg_query($query); |
---|
673 | } |
---|
674 | } |
---|
675 | |
---|
676 | /** |
---|
677 | * updates the global_rank of categories under the given id_uppercat |
---|
678 | * |
---|
679 | * @param int id_uppercat |
---|
680 | * @return void |
---|
681 | */ |
---|
682 | function update_global_rank($id_uppercat = 'all') |
---|
683 | { |
---|
684 | $query = ' |
---|
685 | SELECT id,rank |
---|
686 | FROM '.CATEGORIES_TABLE.' |
---|
687 | ;'; |
---|
688 | $result = pwg_query($query); |
---|
689 | $ranks_array = array(); |
---|
690 | while ($row = mysql_fetch_array($result)) |
---|
691 | { |
---|
692 | $ranks_array[$row['id']] = $row['rank']; |
---|
693 | } |
---|
694 | |
---|
695 | // which categories to update ? |
---|
696 | $uppercats_array = array(); |
---|
697 | |
---|
698 | $query = ' |
---|
699 | SELECT id,uppercats |
---|
700 | FROM '.CATEGORIES_TABLE; |
---|
701 | if (is_numeric($id_uppercat)) |
---|
702 | { |
---|
703 | $query.= ' |
---|
704 | WHERE uppercats REGEXP \'(^|,)'.$id_uppercat.'(,|$)\' |
---|
705 | AND id != '.$id_uppercat.' |
---|
706 | '; |
---|
707 | } |
---|
708 | $query.= ' |
---|
709 | ;'; |
---|
710 | $result = pwg_query($query); |
---|
711 | while ($row = mysql_fetch_array($result)) |
---|
712 | { |
---|
713 | $uppercats_array[$row['id']] = $row['uppercats']; |
---|
714 | } |
---|
715 | |
---|
716 | $datas = array(); |
---|
717 | foreach ($uppercats_array as $id => $uppercats) |
---|
718 | { |
---|
719 | array_push( |
---|
720 | $datas, |
---|
721 | array( |
---|
722 | 'id' => $id, |
---|
723 | 'global_rank' => preg_replace( |
---|
724 | '/(\d+)/e', |
---|
725 | "\$ranks_array['$1']", |
---|
726 | str_replace(',', '.', $uppercats) |
---|
727 | ), |
---|
728 | ) |
---|
729 | ); |
---|
730 | } |
---|
731 | |
---|
732 | mass_updates( |
---|
733 | CATEGORIES_TABLE, |
---|
734 | array( |
---|
735 | 'primary' => array('id'), |
---|
736 | 'update' => array('global_rank') |
---|
737 | ), |
---|
738 | $datas |
---|
739 | ); |
---|
740 | } |
---|
741 | |
---|
742 | /** |
---|
743 | * change the visible property on a set of categories |
---|
744 | * |
---|
745 | * @param array categories |
---|
746 | * @param string value |
---|
747 | * @return void |
---|
748 | */ |
---|
749 | function set_cat_visible($categories, $value) |
---|
750 | { |
---|
751 | if (!in_array($value, array('true', 'false'))) |
---|
752 | { |
---|
753 | return false; |
---|
754 | } |
---|
755 | |
---|
756 | // unlocking a category => all its parent categories become unlocked |
---|
757 | if ($value == 'true') |
---|
758 | { |
---|
759 | $uppercats = get_uppercat_ids($categories); |
---|
760 | $query = ' |
---|
761 | UPDATE '.CATEGORIES_TABLE.' |
---|
762 | SET visible = \'true\' |
---|
763 | WHERE id IN ('.implode(',', $uppercats).') |
---|
764 | ;'; |
---|
765 | pwg_query($query); |
---|
766 | } |
---|
767 | // locking a category => all its child categories become locked |
---|
768 | if ($value == 'false') |
---|
769 | { |
---|
770 | $subcats = get_subcat_ids($categories); |
---|
771 | $query = ' |
---|
772 | UPDATE '.CATEGORIES_TABLE.' |
---|
773 | SET visible = \'false\' |
---|
774 | WHERE id IN ('.implode(',', $subcats).') |
---|
775 | ;'; |
---|
776 | pwg_query($query); |
---|
777 | } |
---|
778 | } |
---|
779 | |
---|
780 | /** |
---|
781 | * change the status property on a set of categories : private or public |
---|
782 | * |
---|
783 | * @param array categories |
---|
784 | * @param string value |
---|
785 | * @return void |
---|
786 | */ |
---|
787 | function set_cat_status($categories, $value) |
---|
788 | { |
---|
789 | if (!in_array($value, array('public', 'private'))) |
---|
790 | { |
---|
791 | return false; |
---|
792 | } |
---|
793 | |
---|
794 | // make public a category => all its parent categories become public |
---|
795 | if ($value == 'public') |
---|
796 | { |
---|
797 | $uppercats = get_uppercat_ids($categories); |
---|
798 | $query = ' |
---|
799 | UPDATE '.CATEGORIES_TABLE.' |
---|
800 | SET status = \'public\' |
---|
801 | WHERE id IN ('.implode(',', $uppercats).') |
---|
802 | ;'; |
---|
803 | pwg_query($query); |
---|
804 | } |
---|
805 | // make a category private => all its child categories become private |
---|
806 | if ($value == 'private') |
---|
807 | { |
---|
808 | $subcats = get_subcat_ids($categories); |
---|
809 | $query = ' |
---|
810 | UPDATE '.CATEGORIES_TABLE.' |
---|
811 | SET status = \'private\' |
---|
812 | WHERE id IN ('.implode(',', $subcats).') |
---|
813 | ;'; |
---|
814 | pwg_query($query); |
---|
815 | } |
---|
816 | } |
---|
817 | |
---|
818 | /** |
---|
819 | * returns all uppercats category ids of the given category ids |
---|
820 | * |
---|
821 | * @param array cat_ids |
---|
822 | * @return array |
---|
823 | */ |
---|
824 | function get_uppercat_ids($cat_ids) |
---|
825 | { |
---|
826 | if (!is_array($cat_ids) or count($cat_ids) < 1) |
---|
827 | { |
---|
828 | return array(); |
---|
829 | } |
---|
830 | |
---|
831 | $uppercats = array(); |
---|
832 | |
---|
833 | $query = ' |
---|
834 | SELECT uppercats |
---|
835 | FROM '.CATEGORIES_TABLE.' |
---|
836 | WHERE id IN ('.implode(',', $cat_ids).') |
---|
837 | ;'; |
---|
838 | $result = pwg_query($query); |
---|
839 | while ($row = mysql_fetch_array($result)) |
---|
840 | { |
---|
841 | $uppercats = array_merge($uppercats, |
---|
842 | explode(',', $row['uppercats'])); |
---|
843 | } |
---|
844 | $uppercats = array_unique($uppercats); |
---|
845 | |
---|
846 | return $uppercats; |
---|
847 | } |
---|
848 | |
---|
849 | /** |
---|
850 | * set a new random representant to the categories |
---|
851 | * |
---|
852 | * @param array categories |
---|
853 | */ |
---|
854 | function set_random_representant($categories) |
---|
855 | { |
---|
856 | $datas = array(); |
---|
857 | foreach ($categories as $category_id) |
---|
858 | { |
---|
859 | $query = ' |
---|
860 | SELECT image_id |
---|
861 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
862 | WHERE category_id = '.$category_id.' |
---|
863 | ORDER BY RAND() |
---|
864 | LIMIT 0,1 |
---|
865 | ;'; |
---|
866 | list($representative) = mysql_fetch_array(pwg_query($query)); |
---|
867 | |
---|
868 | array_push( |
---|
869 | $datas, |
---|
870 | array( |
---|
871 | 'id' => $category_id, |
---|
872 | 'representative_picture_id' => $representative, |
---|
873 | ) |
---|
874 | ); |
---|
875 | } |
---|
876 | |
---|
877 | mass_updates( |
---|
878 | CATEGORIES_TABLE, |
---|
879 | array( |
---|
880 | 'primary' => array('id'), |
---|
881 | 'update' => array('representative_picture_id') |
---|
882 | ), |
---|
883 | $datas |
---|
884 | ); |
---|
885 | } |
---|
886 | |
---|
887 | /** |
---|
888 | * order categories (update categories.rank and global_rank database fields) |
---|
889 | * |
---|
890 | * the purpose of this function is to give a rank for all categories |
---|
891 | * (insides its sub-category), even the newer that have none at te |
---|
892 | * beginning. For this, ordering function selects all categories ordered by |
---|
893 | * rank ASC then name ASC for each uppercat. |
---|
894 | * |
---|
895 | * @returns void |
---|
896 | */ |
---|
897 | function ordering() |
---|
898 | { |
---|
899 | $current_rank = 0; |
---|
900 | $current_uppercat = ''; |
---|
901 | |
---|
902 | $query = ' |
---|
903 | SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat |
---|
904 | FROM '.CATEGORIES_TABLE.' |
---|
905 | ORDER BY id_uppercat,rank,name |
---|
906 | ;'; |
---|
907 | $result = pwg_query($query); |
---|
908 | $datas = array(); |
---|
909 | while ($row = mysql_fetch_array($result)) |
---|
910 | { |
---|
911 | if ($row['id_uppercat'] != $current_uppercat) |
---|
912 | { |
---|
913 | $current_rank = 0; |
---|
914 | $current_uppercat = $row['id_uppercat']; |
---|
915 | } |
---|
916 | $data = array('id' => $row['id'], 'rank' => ++$current_rank); |
---|
917 | array_push($datas, $data); |
---|
918 | } |
---|
919 | |
---|
920 | $fields = array('primary' => array('id'), 'update' => array('rank')); |
---|
921 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
922 | } |
---|
923 | |
---|
924 | /** |
---|
925 | * returns the fulldir for each given category id |
---|
926 | * |
---|
927 | * @param array cat_ids |
---|
928 | * @return array |
---|
929 | */ |
---|
930 | function get_fulldirs($cat_ids) |
---|
931 | { |
---|
932 | if (count($cat_ids) == 0) |
---|
933 | { |
---|
934 | return array(); |
---|
935 | } |
---|
936 | |
---|
937 | // caching directories of existing categories |
---|
938 | $query = ' |
---|
939 | SELECT id, dir |
---|
940 | FROM '.CATEGORIES_TABLE.' |
---|
941 | WHERE dir IS NOT NULL |
---|
942 | ;'; |
---|
943 | $result = pwg_query($query); |
---|
944 | $cat_dirs = array(); |
---|
945 | while ($row = mysql_fetch_array($result)) |
---|
946 | { |
---|
947 | $cat_dirs[$row['id']] = $row['dir']; |
---|
948 | } |
---|
949 | |
---|
950 | // caching galleries_url |
---|
951 | $query = ' |
---|
952 | SELECT id, galleries_url |
---|
953 | FROM '.SITES_TABLE.' |
---|
954 | ;'; |
---|
955 | $result = pwg_query($query); |
---|
956 | $galleries_url = array(); |
---|
957 | while ($row = mysql_fetch_array($result)) |
---|
958 | { |
---|
959 | $galleries_url[$row['id']] = $row['galleries_url']; |
---|
960 | } |
---|
961 | |
---|
962 | // categories : id, site_id, uppercats |
---|
963 | $categories = array(); |
---|
964 | |
---|
965 | $query = ' |
---|
966 | SELECT id, uppercats, site_id |
---|
967 | FROM '.CATEGORIES_TABLE.' |
---|
968 | WHERE id IN ( |
---|
969 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
970 | ;'; |
---|
971 | $result = pwg_query($query); |
---|
972 | while ($row = mysql_fetch_array($result)) |
---|
973 | { |
---|
974 | array_push($categories, $row); |
---|
975 | } |
---|
976 | |
---|
977 | // filling $cat_fulldirs |
---|
978 | $cat_fulldirs = array(); |
---|
979 | foreach ($categories as $category) |
---|
980 | { |
---|
981 | $uppercats = str_replace(',', '/', $category['uppercats']); |
---|
982 | $cat_fulldirs[$category['id']] = $galleries_url[$category['site_id']]; |
---|
983 | $cat_fulldirs[$category['id']].= preg_replace('/(\d+)/e', |
---|
984 | "\$cat_dirs['$1']", |
---|
985 | $uppercats); |
---|
986 | } |
---|
987 | |
---|
988 | return $cat_fulldirs; |
---|
989 | } |
---|
990 | |
---|
991 | /** |
---|
992 | * returns an array with all file system files according to |
---|
993 | * $conf['file_ext'] |
---|
994 | * |
---|
995 | * @param string $path |
---|
996 | * @param bool recursive |
---|
997 | * @return array |
---|
998 | */ |
---|
999 | function get_fs($path, $recursive = true) |
---|
1000 | { |
---|
1001 | global $conf; |
---|
1002 | |
---|
1003 | // because isset is faster than in_array... |
---|
1004 | if (!isset($conf['flip_picture_ext'])) |
---|
1005 | { |
---|
1006 | $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); |
---|
1007 | } |
---|
1008 | if (!isset($conf['flip_file_ext'])) |
---|
1009 | { |
---|
1010 | $conf['flip_file_ext'] = array_flip($conf['file_ext']); |
---|
1011 | } |
---|
1012 | |
---|
1013 | $fs['elements'] = array(); |
---|
1014 | $fs['thumbnails'] = array(); |
---|
1015 | $fs['representatives'] = array(); |
---|
1016 | $subdirs = array(); |
---|
1017 | |
---|
1018 | if (is_dir($path)) |
---|
1019 | { |
---|
1020 | if ($contents = opendir($path)) |
---|
1021 | { |
---|
1022 | while (($node = readdir($contents)) !== false) |
---|
1023 | { |
---|
1024 | if (is_file($path.'/'.$node)) |
---|
1025 | { |
---|
1026 | $extension = get_extension($node); |
---|
1027 | |
---|
1028 | // if (in_array($extension, $conf['picture_ext'])) |
---|
1029 | if (isset($conf['flip_picture_ext'][$extension])) |
---|
1030 | { |
---|
1031 | if (basename($path) == 'thumbnail') |
---|
1032 | { |
---|
1033 | array_push($fs['thumbnails'], $path.'/'.$node); |
---|
1034 | } |
---|
1035 | else if (basename($path) == 'pwg_representative') |
---|
1036 | { |
---|
1037 | array_push($fs['representatives'], $path.'/'.$node); |
---|
1038 | } |
---|
1039 | else |
---|
1040 | { |
---|
1041 | array_push($fs['elements'], $path.'/'.$node); |
---|
1042 | } |
---|
1043 | } |
---|
1044 | // else if (in_array($extension, $conf['file_ext'])) |
---|
1045 | else if (isset($conf['flip_file_ext'][$extension])) |
---|
1046 | { |
---|
1047 | array_push($fs['elements'], $path.'/'.$node); |
---|
1048 | } |
---|
1049 | } |
---|
1050 | else if (is_dir($path.'/'.$node) |
---|
1051 | and $node != '.' |
---|
1052 | and $node != '..' |
---|
1053 | and $node != 'pwg_high' |
---|
1054 | and $recursive) |
---|
1055 | { |
---|
1056 | array_push($subdirs, $node); |
---|
1057 | } |
---|
1058 | } |
---|
1059 | } |
---|
1060 | closedir($contents); |
---|
1061 | |
---|
1062 | foreach ($subdirs as $subdir) |
---|
1063 | { |
---|
1064 | $tmp_fs = get_fs($path.'/'.$subdir); |
---|
1065 | |
---|
1066 | $fs['elements'] = array_merge($fs['elements'], |
---|
1067 | $tmp_fs['elements']); |
---|
1068 | |
---|
1069 | $fs['thumbnails'] = array_merge($fs['thumbnails'], |
---|
1070 | $tmp_fs['thumbnails']); |
---|
1071 | |
---|
1072 | $fs['representatives'] = array_merge($fs['representatives'], |
---|
1073 | $tmp_fs['representatives']); |
---|
1074 | } |
---|
1075 | } |
---|
1076 | return $fs; |
---|
1077 | } |
---|
1078 | |
---|
1079 | /** |
---|
1080 | * stupidly returns the current microsecond since Unix epoch |
---|
1081 | */ |
---|
1082 | function micro_seconds() |
---|
1083 | { |
---|
1084 | $t1 = explode(' ', microtime()); |
---|
1085 | $t2 = explode('.', $t1[0]); |
---|
1086 | $t2 = $t1[1].substr($t2[1], 0, 6); |
---|
1087 | return $t2; |
---|
1088 | } |
---|
1089 | |
---|
1090 | /** |
---|
1091 | * synchronize base users list and related users list |
---|
1092 | * |
---|
1093 | * compares and synchronizes base users table (USERS_TABLE) with its child |
---|
1094 | * tables (USER_INFOS_TABLE, USER_ACCESS, USER_CACHE, USER_GROUP) : each |
---|
1095 | * base user must be present in child tables, users in child tables not |
---|
1096 | * present in base table must be deleted. |
---|
1097 | * |
---|
1098 | * @return void |
---|
1099 | */ |
---|
1100 | function sync_users() |
---|
1101 | { |
---|
1102 | global $conf; |
---|
1103 | |
---|
1104 | $query = ' |
---|
1105 | SELECT '.$conf['user_fields']['id'].' AS id |
---|
1106 | FROM '.USERS_TABLE.' |
---|
1107 | ;'; |
---|
1108 | $base_users = array_from_query($query, 'id'); |
---|
1109 | |
---|
1110 | $query = ' |
---|
1111 | SELECT user_id |
---|
1112 | FROM '.USER_INFOS_TABLE.' |
---|
1113 | ;'; |
---|
1114 | $infos_users = array_from_query($query, 'user_id'); |
---|
1115 | |
---|
1116 | // users present in $base_users and not in $infos_users must be added |
---|
1117 | $to_create = array_diff($base_users, $infos_users); |
---|
1118 | |
---|
1119 | if (count($to_create) > 0) |
---|
1120 | { |
---|
1121 | $inserts = array(); |
---|
1122 | |
---|
1123 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
1124 | |
---|
1125 | foreach ($to_create as $user_id) |
---|
1126 | { |
---|
1127 | $insert = array(); |
---|
1128 | $insert['user_id'] = $user_id; |
---|
1129 | $insert['status'] = 'normal'; |
---|
1130 | $insert['template'] = $conf['default_template']; |
---|
1131 | $insert['nb_image_line'] = $conf['nb_image_line']; |
---|
1132 | $insert['nb_line_page'] = $conf['nb_line_page']; |
---|
1133 | $insert['language'] = $conf['default_language']; |
---|
1134 | $insert['recent_period'] = $conf['recent_period']; |
---|
1135 | $insert['expand'] = boolean_to_string($conf['auto_expand']); |
---|
1136 | $insert['show_nb_comments'] = |
---|
1137 | boolean_to_string($conf['show_nb_comments']); |
---|
1138 | $insert['maxwidth'] = $conf['default_maxwidth']; |
---|
1139 | $insert['maxheight'] = $conf['default_maxheight']; |
---|
1140 | $insert['registration_date'] = $dbnow; |
---|
1141 | |
---|
1142 | array_push($inserts, $insert); |
---|
1143 | } |
---|
1144 | |
---|
1145 | mass_inserts(USER_INFOS_TABLE, |
---|
1146 | array_keys($inserts[0]), |
---|
1147 | $inserts); |
---|
1148 | } |
---|
1149 | |
---|
1150 | // users present in user related tables must be present in the base user |
---|
1151 | // table |
---|
1152 | $tables = array( |
---|
1153 | USER_MAIL_NOTIFICATION_TABLE, |
---|
1154 | USER_FEED_TABLE, |
---|
1155 | USER_INFOS_TABLE, |
---|
1156 | USER_ACCESS_TABLE, |
---|
1157 | USER_CACHE_TABLE, |
---|
1158 | USER_GROUP_TABLE |
---|
1159 | ); |
---|
1160 | |
---|
1161 | foreach ($tables as $table) |
---|
1162 | { |
---|
1163 | $query = ' |
---|
1164 | SELECT user_id |
---|
1165 | FROM '.$table.' |
---|
1166 | ;'; |
---|
1167 | $to_delete = array_diff( |
---|
1168 | array_from_query($query, 'user_id'), |
---|
1169 | $base_users |
---|
1170 | ); |
---|
1171 | |
---|
1172 | if (count($to_delete) > 0) |
---|
1173 | { |
---|
1174 | $query = ' |
---|
1175 | DELETE |
---|
1176 | FROM '.$table.' |
---|
1177 | WHERE user_id in ('.implode(',', $to_delete).') |
---|
1178 | ;'; |
---|
1179 | pwg_query($query); |
---|
1180 | } |
---|
1181 | } |
---|
1182 | } |
---|
1183 | |
---|
1184 | /** |
---|
1185 | * updates categories.uppercats field based on categories.id + |
---|
1186 | * categories.id_uppercat |
---|
1187 | * |
---|
1188 | * @return void |
---|
1189 | */ |
---|
1190 | function update_uppercats() |
---|
1191 | { |
---|
1192 | $uppercat_ids = array(); |
---|
1193 | |
---|
1194 | $query = ' |
---|
1195 | SELECT id, id_uppercat |
---|
1196 | FROM '.CATEGORIES_TABLE.' |
---|
1197 | ;'; |
---|
1198 | $result = pwg_query($query); |
---|
1199 | while ($row = mysql_fetch_array($result)) |
---|
1200 | { |
---|
1201 | $uppercat_ids[$row['id']] = |
---|
1202 | !empty($row['id_uppercat']) ? $row['id_uppercat'] : 'NULL'; |
---|
1203 | } |
---|
1204 | |
---|
1205 | // uppercats array associates a category id to the list of uppercats id. |
---|
1206 | $uppercats = array(); |
---|
1207 | |
---|
1208 | foreach (array_keys($uppercat_ids) as $id) |
---|
1209 | { |
---|
1210 | $uppercats[$id] = array(); |
---|
1211 | |
---|
1212 | $uppercat = $id; |
---|
1213 | |
---|
1214 | while ($uppercat != 'NULL') |
---|
1215 | { |
---|
1216 | array_push($uppercats[$id], $uppercat); |
---|
1217 | $uppercat = $uppercat_ids[$uppercat]; |
---|
1218 | } |
---|
1219 | } |
---|
1220 | |
---|
1221 | $datas = array(); |
---|
1222 | |
---|
1223 | foreach ($uppercats as $id => $list) |
---|
1224 | { |
---|
1225 | array_push( |
---|
1226 | $datas, |
---|
1227 | array( |
---|
1228 | 'id' => $id, |
---|
1229 | 'uppercats' => implode(',', array_reverse($list)) |
---|
1230 | ) |
---|
1231 | ); |
---|
1232 | } |
---|
1233 | |
---|
1234 | $fields = array('primary' => array('id'), 'update' => array('uppercats')); |
---|
1235 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
1236 | } |
---|
1237 | |
---|
1238 | /** |
---|
1239 | * update images.path field |
---|
1240 | * |
---|
1241 | * @return void |
---|
1242 | */ |
---|
1243 | function update_path() |
---|
1244 | { |
---|
1245 | $query = ' |
---|
1246 | SELECT DISTINCT(storage_category_id) |
---|
1247 | FROM '.IMAGES_TABLE.' |
---|
1248 | ;'; |
---|
1249 | $cat_ids = array_from_query($query, 'storage_category_id'); |
---|
1250 | $fulldirs = get_fulldirs($cat_ids); |
---|
1251 | |
---|
1252 | foreach ($cat_ids as $cat_id) |
---|
1253 | { |
---|
1254 | $query = ' |
---|
1255 | UPDATE '.IMAGES_TABLE.' |
---|
1256 | SET path = CONCAT(\''.$fulldirs[$cat_id].'\',\'/\',file) |
---|
1257 | WHERE storage_category_id = '.$cat_id.' |
---|
1258 | ;'; |
---|
1259 | pwg_query($query); |
---|
1260 | } |
---|
1261 | } |
---|
1262 | |
---|
1263 | /** |
---|
1264 | * update images.average_rate field |
---|
1265 | * param int $element_id optional, otherwise applies to all |
---|
1266 | * @return void |
---|
1267 | */ |
---|
1268 | function update_average_rate( $element_id=-1 ) |
---|
1269 | { |
---|
1270 | $query = ' |
---|
1271 | SELECT element_id, |
---|
1272 | ROUND(AVG(rate),2) AS average_rate |
---|
1273 | FROM '.RATE_TABLE; |
---|
1274 | if ( $element_id != -1 ) |
---|
1275 | { |
---|
1276 | $query .= ' WHERE element_id=' . $element_id; |
---|
1277 | } |
---|
1278 | $query .= ' GROUP BY element_id;'; |
---|
1279 | |
---|
1280 | $result = pwg_query($query); |
---|
1281 | |
---|
1282 | $datas = array(); |
---|
1283 | |
---|
1284 | while ($row = mysql_fetch_array($result)) |
---|
1285 | { |
---|
1286 | array_push( |
---|
1287 | $datas, |
---|
1288 | array( |
---|
1289 | 'id' => $row['element_id'], |
---|
1290 | 'average_rate' => $row['average_rate'] |
---|
1291 | ) |
---|
1292 | ); |
---|
1293 | } |
---|
1294 | |
---|
1295 | mass_updates( |
---|
1296 | IMAGES_TABLE, |
---|
1297 | array( |
---|
1298 | 'primary' => array('id'), |
---|
1299 | 'update' => array('average_rate') |
---|
1300 | ), |
---|
1301 | $datas |
---|
1302 | ); |
---|
1303 | |
---|
1304 | $query=' |
---|
1305 | SELECT id FROM '.IMAGES_TABLE .' |
---|
1306 | LEFT JOIN '.RATE_TABLE.' ON id=element_id |
---|
1307 | WHERE element_id IS NULL AND average_rate IS NOT NULL'; |
---|
1308 | if ( $element_id != -1 ) |
---|
1309 | { |
---|
1310 | $query .= ' AND id=' . $element_id; |
---|
1311 | } |
---|
1312 | $to_update = array_from_query( $query, 'id'); |
---|
1313 | |
---|
1314 | if ( !empty($to_update) ) |
---|
1315 | { |
---|
1316 | $query=' |
---|
1317 | UPDATE '.IMAGES_TABLE .' |
---|
1318 | SET average_rate=NULL |
---|
1319 | WHERE id IN (' . implode(',',$to_update) . ')'; |
---|
1320 | pwg_query($query); |
---|
1321 | } |
---|
1322 | } |
---|
1323 | |
---|
1324 | /** |
---|
1325 | * change the parent category of the given categories. The categories are |
---|
1326 | * supposed virtual. |
---|
1327 | * |
---|
1328 | * @param array category identifiers |
---|
1329 | * @param int parent category identifier |
---|
1330 | * @return void |
---|
1331 | */ |
---|
1332 | function move_categories($category_ids, $new_parent = -1) |
---|
1333 | { |
---|
1334 | global $page; |
---|
1335 | |
---|
1336 | if (count($category_ids) == 0) |
---|
1337 | { |
---|
1338 | return; |
---|
1339 | } |
---|
1340 | |
---|
1341 | $new_parent = $new_parent < 1 ? 'NULL' : $new_parent; |
---|
1342 | |
---|
1343 | $categories = array(); |
---|
1344 | |
---|
1345 | $query = ' |
---|
1346 | SELECT id, id_uppercat, status, uppercats |
---|
1347 | FROM '.CATEGORIES_TABLE.' |
---|
1348 | WHERE id IN ('.implode(',', $category_ids).') |
---|
1349 | ;'; |
---|
1350 | $result = pwg_query($query); |
---|
1351 | while ($row = mysql_fetch_array($result)) |
---|
1352 | { |
---|
1353 | $categories[$row['id']] = |
---|
1354 | array( |
---|
1355 | 'parent' => empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'], |
---|
1356 | 'status' => $row['status'], |
---|
1357 | 'uppercats' => $row['uppercats'] |
---|
1358 | ); |
---|
1359 | } |
---|
1360 | |
---|
1361 | // is the movement possible? The movement is impossible if you try to move |
---|
1362 | // a category in a sub-category or itself |
---|
1363 | if ('NULL' != $new_parent) |
---|
1364 | { |
---|
1365 | $query = ' |
---|
1366 | SELECT uppercats |
---|
1367 | FROM '.CATEGORIES_TABLE.' |
---|
1368 | WHERE id = '.$new_parent.' |
---|
1369 | ;'; |
---|
1370 | list($new_parent_uppercats) = mysql_fetch_row(pwg_query($query)); |
---|
1371 | |
---|
1372 | foreach ($categories as $category) |
---|
1373 | { |
---|
1374 | // technically, you can't move a category with uppercats 12,125,13,14 |
---|
1375 | // into a new parent category with uppercats 12,125,13,14,24 |
---|
1376 | if (preg_match('/^'.$category['uppercats'].'/', $new_parent_uppercats)) |
---|
1377 | { |
---|
1378 | array_push( |
---|
1379 | $page['errors'], |
---|
1380 | l10n('You cannot move a category in its own sub category') |
---|
1381 | ); |
---|
1382 | return; |
---|
1383 | } |
---|
1384 | } |
---|
1385 | } |
---|
1386 | |
---|
1387 | $tables = |
---|
1388 | array( |
---|
1389 | USER_ACCESS_TABLE => 'user_id', |
---|
1390 | GROUP_ACCESS_TABLE => 'group_id' |
---|
1391 | ); |
---|
1392 | |
---|
1393 | $query = ' |
---|
1394 | UPDATE '.CATEGORIES_TABLE.' |
---|
1395 | SET id_uppercat = '.$new_parent.' |
---|
1396 | WHERE id IN ('.implode(',', $category_ids).') |
---|
1397 | ;'; |
---|
1398 | pwg_query($query); |
---|
1399 | |
---|
1400 | update_uppercats(); |
---|
1401 | ordering(); |
---|
1402 | update_global_rank(); |
---|
1403 | |
---|
1404 | // status and related permissions management |
---|
1405 | if ('NULL' == $new_parent) |
---|
1406 | { |
---|
1407 | $parent_status = 'public'; |
---|
1408 | } |
---|
1409 | else |
---|
1410 | { |
---|
1411 | $query = ' |
---|
1412 | SELECT status |
---|
1413 | FROM '.CATEGORIES_TABLE.' |
---|
1414 | WHERE id = '.$new_parent.' |
---|
1415 | ;'; |
---|
1416 | list($parent_status) = mysql_fetch_row(pwg_query($query)); |
---|
1417 | } |
---|
1418 | |
---|
1419 | if ('private' == $parent_status) |
---|
1420 | { |
---|
1421 | foreach ($categories as $cat_id => $category) |
---|
1422 | { |
---|
1423 | switch ($category['status']) |
---|
1424 | { |
---|
1425 | case 'public' : |
---|
1426 | { |
---|
1427 | set_cat_status(array($cat_id), 'private'); |
---|
1428 | break; |
---|
1429 | } |
---|
1430 | case 'private' : |
---|
1431 | { |
---|
1432 | $subcats = get_subcat_ids(array($cat_id)); |
---|
1433 | |
---|
1434 | foreach ($tables as $table => $field) |
---|
1435 | { |
---|
1436 | $query = ' |
---|
1437 | SELECT '.$field.' |
---|
1438 | FROM '.$table.' |
---|
1439 | WHERE cat_id = '.$cat_id.' |
---|
1440 | ;'; |
---|
1441 | $category_access = array_from_query($query, $field); |
---|
1442 | |
---|
1443 | $query = ' |
---|
1444 | SELECT '.$field.' |
---|
1445 | FROM '.$table.' |
---|
1446 | WHERE cat_id = '.$new_parent.' |
---|
1447 | ;'; |
---|
1448 | $parent_access = array_from_query($query, $field); |
---|
1449 | |
---|
1450 | $to_delete = array_diff($parent_access, $category_access); |
---|
1451 | |
---|
1452 | if (count($to_delete) > 0) |
---|
1453 | { |
---|
1454 | $query = ' |
---|
1455 | DELETE FROM '.$table.' |
---|
1456 | WHERE '.$field.' IN ('.implode(',', $to_delete).') |
---|
1457 | AND cat_id IN ('.implode(',', $subcats).') |
---|
1458 | ;'; |
---|
1459 | pwg_query($query); |
---|
1460 | } |
---|
1461 | } |
---|
1462 | break; |
---|
1463 | } |
---|
1464 | } |
---|
1465 | } |
---|
1466 | } |
---|
1467 | |
---|
1468 | array_push( |
---|
1469 | $page['infos'], |
---|
1470 | sprintf( |
---|
1471 | l10n('%d categories moved'), |
---|
1472 | count($categories) |
---|
1473 | ) |
---|
1474 | ); |
---|
1475 | } |
---|
1476 | |
---|
1477 | /** |
---|
1478 | * create a virtual category |
---|
1479 | * |
---|
1480 | * @param string category name |
---|
1481 | * @param int parent category id |
---|
1482 | * @return array with ('info' and 'id') or ('error') key |
---|
1483 | */ |
---|
1484 | function create_virtual_category($category_name, $parent_id=null) |
---|
1485 | { |
---|
1486 | global $conf; |
---|
1487 | |
---|
1488 | // is the given category name only containing blank spaces ? |
---|
1489 | if (preg_match('/^\s*$/', $category_name)) |
---|
1490 | { |
---|
1491 | return array('error' => l10n('cat_error_name')); |
---|
1492 | } |
---|
1493 | |
---|
1494 | $parent_id = !empty($parent_id) ? $parent_id : 'NULL'; |
---|
1495 | |
---|
1496 | $query = ' |
---|
1497 | SELECT MAX(rank) |
---|
1498 | FROM '.CATEGORIES_TABLE.' |
---|
1499 | WHERE id_uppercat '.(is_numeric($parent_id) ? '= '.$parent_id : 'IS NULL').' |
---|
1500 | ;'; |
---|
1501 | list($current_rank) = mysql_fetch_array(pwg_query($query)); |
---|
1502 | |
---|
1503 | $insert = array( |
---|
1504 | 'name' => $category_name, |
---|
1505 | 'rank' => ++$current_rank, |
---|
1506 | 'commentable' => boolean_to_string($conf['newcat_default_commentable']), |
---|
1507 | 'uploadable' => 'false', |
---|
1508 | ); |
---|
1509 | |
---|
1510 | if ($parent_id != 'NULL') |
---|
1511 | { |
---|
1512 | $query = ' |
---|
1513 | SELECT id, uppercats, global_rank, visible, status |
---|
1514 | FROM '.CATEGORIES_TABLE.' |
---|
1515 | WHERE id = '.$parent_id.' |
---|
1516 | ;'; |
---|
1517 | $parent = mysql_fetch_array(pwg_query($query)); |
---|
1518 | |
---|
1519 | $insert{'id_uppercat'} = $parent{'id'}; |
---|
1520 | $insert{'global_rank'} = $parent{'global_rank'}.'.'.$insert{'rank'}; |
---|
1521 | |
---|
1522 | // at creation, must a category be visible or not ? Warning : if the |
---|
1523 | // parent category is invisible, the category is automatically create |
---|
1524 | // invisible. (invisible = locked) |
---|
1525 | if ('false' == $parent['visible']) |
---|
1526 | { |
---|
1527 | $insert{'visible'} = 'false'; |
---|
1528 | } |
---|
1529 | else |
---|
1530 | { |
---|
1531 | $insert{'visible'} = boolean_to_string($conf['newcat_default_visible']); |
---|
1532 | } |
---|
1533 | |
---|
1534 | // at creation, must a category be public or private ? Warning : if the |
---|
1535 | // parent category is private, the category is automatically create |
---|
1536 | // private. |
---|
1537 | if ('private' == $parent['status']) |
---|
1538 | { |
---|
1539 | $insert{'status'} = 'private'; |
---|
1540 | } |
---|
1541 | else |
---|
1542 | { |
---|
1543 | $insert{'status'} = $conf['newcat_default_status']; |
---|
1544 | } |
---|
1545 | } |
---|
1546 | else |
---|
1547 | { |
---|
1548 | $insert{'visible'} = boolean_to_string($conf['newcat_default_visible']); |
---|
1549 | $insert{'status'} = $conf['newcat_default_status']; |
---|
1550 | $insert{'global_rank'} = $insert{'rank'}; |
---|
1551 | } |
---|
1552 | |
---|
1553 | // we have then to add the virtual category |
---|
1554 | mass_inserts( |
---|
1555 | CATEGORIES_TABLE, |
---|
1556 | array( |
---|
1557 | 'site_id', 'name', 'id_uppercat', 'rank', 'commentable', |
---|
1558 | 'uploadable', 'visible', 'status', 'global_rank', |
---|
1559 | ), |
---|
1560 | array($insert) |
---|
1561 | ); |
---|
1562 | |
---|
1563 | $inserted_id = mysql_insert_id(); |
---|
1564 | |
---|
1565 | $query = ' |
---|
1566 | UPDATE |
---|
1567 | '.CATEGORIES_TABLE.' |
---|
1568 | SET uppercats = \''. |
---|
1569 | (isset($parent) ? $parent{'uppercats'}.',' : ''). |
---|
1570 | $inserted_id. |
---|
1571 | '\' |
---|
1572 | WHERE id = '.$inserted_id.' |
---|
1573 | ;'; |
---|
1574 | pwg_query($query); |
---|
1575 | |
---|
1576 | return array( |
---|
1577 | 'info' => l10n('cat_virtual_added'), |
---|
1578 | 'id' => $inserted_id, |
---|
1579 | ); |
---|
1580 | } |
---|
1581 | |
---|
1582 | /** |
---|
1583 | * Set tags to an image. Warning: given tags are all tags associated to the |
---|
1584 | * image, not additionnal tags. |
---|
1585 | * |
---|
1586 | * @param array tag ids |
---|
1587 | * @param int image id |
---|
1588 | * @return void |
---|
1589 | */ |
---|
1590 | function set_tags($tags, $image_id) |
---|
1591 | { |
---|
1592 | $query = ' |
---|
1593 | DELETE |
---|
1594 | FROM '.IMAGE_TAG_TABLE.' |
---|
1595 | WHERE image_id = '.$image_id.' |
---|
1596 | ;'; |
---|
1597 | pwg_query($query); |
---|
1598 | |
---|
1599 | if (count($tags) > 0) |
---|
1600 | { |
---|
1601 | $inserts = array(); |
---|
1602 | foreach ($tags as $tag_id) |
---|
1603 | { |
---|
1604 | array_push( |
---|
1605 | $inserts, |
---|
1606 | array( |
---|
1607 | 'tag_id' => $tag_id, |
---|
1608 | 'image_id' => $image_id |
---|
1609 | ) |
---|
1610 | ); |
---|
1611 | } |
---|
1612 | mass_inserts( |
---|
1613 | IMAGE_TAG_TABLE, |
---|
1614 | array_keys($inserts[0]), |
---|
1615 | $inserts |
---|
1616 | ); |
---|
1617 | } |
---|
1618 | } |
---|
1619 | |
---|
1620 | /** |
---|
1621 | * Add new tags to a set of images. |
---|
1622 | * |
---|
1623 | * @param array tag ids |
---|
1624 | * @param array image ids |
---|
1625 | * @return void |
---|
1626 | */ |
---|
1627 | function add_tags($tags, $images) |
---|
1628 | { |
---|
1629 | if (count($tags) == 0 or count($tags) == 0) |
---|
1630 | { |
---|
1631 | return; |
---|
1632 | } |
---|
1633 | |
---|
1634 | // we can't insert twice the same {image_id,tag_id} so we must first |
---|
1635 | // delete lines we'll insert later |
---|
1636 | $query = ' |
---|
1637 | DELETE |
---|
1638 | FROM '.IMAGE_TAG_TABLE.' |
---|
1639 | WHERE image_id IN ('.implode(',', $images).') |
---|
1640 | AND tag_id IN ('.implode(',', $tags).') |
---|
1641 | ;'; |
---|
1642 | pwg_query($query); |
---|
1643 | |
---|
1644 | $inserts = array(); |
---|
1645 | foreach ($images as $image_id) |
---|
1646 | { |
---|
1647 | foreach ($tags as $tag_id) |
---|
1648 | { |
---|
1649 | array_push( |
---|
1650 | $inserts, |
---|
1651 | array( |
---|
1652 | 'image_id' => $image_id, |
---|
1653 | 'tag_id' => $tag_id, |
---|
1654 | ) |
---|
1655 | ); |
---|
1656 | } |
---|
1657 | } |
---|
1658 | mass_inserts( |
---|
1659 | IMAGE_TAG_TABLE, |
---|
1660 | array_keys($inserts[0]), |
---|
1661 | $inserts |
---|
1662 | ); |
---|
1663 | } |
---|
1664 | |
---|
1665 | function tag_id_from_tag_name($tag_name) |
---|
1666 | { |
---|
1667 | global $page; |
---|
1668 | |
---|
1669 | if (isset($page['tag_id_from_tag_name_cache'][$tag_name])) |
---|
1670 | { |
---|
1671 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
1672 | } |
---|
1673 | |
---|
1674 | if (function_exists('mysql_real_escape_string')) |
---|
1675 | { |
---|
1676 | $tag_name = mysql_real_escape_string($tag_name); |
---|
1677 | } |
---|
1678 | else |
---|
1679 | { |
---|
1680 | $tag_name = mysql_escape_string($tag_name); |
---|
1681 | } |
---|
1682 | |
---|
1683 | // does the tag already exists? |
---|
1684 | $query = ' |
---|
1685 | SELECT id |
---|
1686 | FROM '.TAGS_TABLE.' |
---|
1687 | WHERE name = \''.$tag_name.'\' |
---|
1688 | ;'; |
---|
1689 | $existing_tags = array_from_query($query, 'id'); |
---|
1690 | |
---|
1691 | if (count($existing_tags) == 0) |
---|
1692 | { |
---|
1693 | mass_inserts( |
---|
1694 | TAGS_TABLE, |
---|
1695 | array('name', 'url_name'), |
---|
1696 | array( |
---|
1697 | array( |
---|
1698 | 'name' => $tag_name, |
---|
1699 | 'url_name' => str2url($tag_name), |
---|
1700 | ) |
---|
1701 | ) |
---|
1702 | ); |
---|
1703 | |
---|
1704 | $page['tag_id_from_tag_name_cache'][$tag_name] = mysql_insert_id(); |
---|
1705 | } |
---|
1706 | else |
---|
1707 | { |
---|
1708 | $page['tag_id_from_tag_name_cache'][$tag_name] = $existing_tags[0]; |
---|
1709 | } |
---|
1710 | |
---|
1711 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
1712 | } |
---|
1713 | |
---|
1714 | function set_tags_of($tags_of) |
---|
1715 | { |
---|
1716 | if (count($tags_of) > 0) |
---|
1717 | { |
---|
1718 | $query = ' |
---|
1719 | DELETE |
---|
1720 | FROM '.IMAGE_TAG_TABLE.' |
---|
1721 | WHERE image_id IN ('.implode(',', array_keys($tags_of)).') |
---|
1722 | ;'; |
---|
1723 | pwg_query($query); |
---|
1724 | |
---|
1725 | $inserts = array(); |
---|
1726 | |
---|
1727 | foreach ($tags_of as $image_id => $tag_ids) |
---|
1728 | { |
---|
1729 | foreach ($tag_ids as $tag_id) |
---|
1730 | { |
---|
1731 | array_push( |
---|
1732 | $inserts, |
---|
1733 | array( |
---|
1734 | 'image_id' => $image_id, |
---|
1735 | 'tag_id' => $tag_id, |
---|
1736 | ) |
---|
1737 | ); |
---|
1738 | } |
---|
1739 | } |
---|
1740 | |
---|
1741 | mass_inserts( |
---|
1742 | IMAGE_TAG_TABLE, |
---|
1743 | array_keys($inserts[0]), |
---|
1744 | $inserts |
---|
1745 | ); |
---|
1746 | } |
---|
1747 | } |
---|
1748 | |
---|
1749 | /** |
---|
1750 | * Do maintenance on all PWG tables |
---|
1751 | * |
---|
1752 | * @return nono |
---|
1753 | */ |
---|
1754 | function do_maintenance_all_tables() |
---|
1755 | { |
---|
1756 | global $prefixeTable; |
---|
1757 | |
---|
1758 | $all_tables = array(); |
---|
1759 | |
---|
1760 | // List all tables |
---|
1761 | $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\';'; |
---|
1762 | $result = pwg_query($query); |
---|
1763 | while ($row = mysql_fetch_array($result)) |
---|
1764 | { |
---|
1765 | array_push($all_tables, $row[0]); |
---|
1766 | } |
---|
1767 | |
---|
1768 | // Repair all tables |
---|
1769 | $query = 'REPAIR TABLE '.implode(', ', $all_tables).';'; |
---|
1770 | pwg_query($query); |
---|
1771 | |
---|
1772 | // Re-Order all tables |
---|
1773 | foreach ($all_tables as $table_name) |
---|
1774 | { |
---|
1775 | $all_primary_key = array(); |
---|
1776 | |
---|
1777 | $query = 'DESC '.$table_name.';'; |
---|
1778 | $result = pwg_query($query); |
---|
1779 | while ($row = mysql_fetch_array($result)) |
---|
1780 | { |
---|
1781 | if ($row['Key'] == 'PRI') |
---|
1782 | { |
---|
1783 | array_push($all_primary_key, $row['Field']); |
---|
1784 | } |
---|
1785 | } |
---|
1786 | |
---|
1787 | if (count($all_primary_key) != 0) |
---|
1788 | { |
---|
1789 | $query = 'ALTER TABLE '.$table_name.' ORDER BY '.implode(', ', $all_primary_key).';'; |
---|
1790 | pwg_query($query); |
---|
1791 | } |
---|
1792 | } |
---|
1793 | |
---|
1794 | // Optimize all tables |
---|
1795 | $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables).';'; |
---|
1796 | pwg_query($query); |
---|
1797 | |
---|
1798 | } |
---|
1799 | |
---|
1800 | /** |
---|
1801 | * Associate a list of images to a list of categories. |
---|
1802 | * |
---|
1803 | * The function will not duplicate links |
---|
1804 | * |
---|
1805 | * @param array images |
---|
1806 | * @param array categories |
---|
1807 | * @return void |
---|
1808 | */ |
---|
1809 | function associate_images_to_categories($images, $categories) |
---|
1810 | { |
---|
1811 | if (count($images) == 0 |
---|
1812 | or count($categories) == 0) |
---|
1813 | { |
---|
1814 | return false; |
---|
1815 | } |
---|
1816 | |
---|
1817 | $query = ' |
---|
1818 | DELETE |
---|
1819 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1820 | WHERE image_id IN ('.implode(',', $images).') |
---|
1821 | AND category_id IN ('.implode(',', $categories).') |
---|
1822 | ;'; |
---|
1823 | pwg_query($query); |
---|
1824 | |
---|
1825 | $inserts = array(); |
---|
1826 | foreach ($categories as $category_id) |
---|
1827 | { |
---|
1828 | foreach ($images as $image_id) |
---|
1829 | { |
---|
1830 | array_push( |
---|
1831 | $inserts, |
---|
1832 | array( |
---|
1833 | 'image_id' => $image_id, |
---|
1834 | 'category_id' => $category_id, |
---|
1835 | ) |
---|
1836 | ); |
---|
1837 | } |
---|
1838 | } |
---|
1839 | |
---|
1840 | mass_inserts( |
---|
1841 | IMAGE_CATEGORY_TABLE, |
---|
1842 | array_keys($inserts[0]), |
---|
1843 | $inserts |
---|
1844 | ); |
---|
1845 | |
---|
1846 | update_category($categories); |
---|
1847 | } |
---|
1848 | |
---|
1849 | /** |
---|
1850 | * Associate images associated to a list of source categories to a list of |
---|
1851 | * destination categories. |
---|
1852 | * |
---|
1853 | * @param array sources |
---|
1854 | * @param array destinations |
---|
1855 | * @return void |
---|
1856 | */ |
---|
1857 | function associate_categories_to_categories($sources, $destinations) |
---|
1858 | { |
---|
1859 | if (count($sources) == 0) |
---|
1860 | { |
---|
1861 | return false; |
---|
1862 | } |
---|
1863 | |
---|
1864 | $query = ' |
---|
1865 | SELECT image_id |
---|
1866 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1867 | WHERE category_id IN ('.implode(',', $sources).') |
---|
1868 | ;'; |
---|
1869 | $images = array_from_query($query, 'image_id'); |
---|
1870 | |
---|
1871 | associate_images_to_categories($images, $destinations); |
---|
1872 | } |
---|
1873 | ?> |
---|