- Timestamp:
- Aug 6, 2012, 11:04:46 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/admin/include/functions.php
r17302 r17424 1486 1486 * Associate a list of images to a list of categories. 1487 1487 * 1488 * The function will not duplicate links 1488 * The function will not duplicate links and will preserve ranks 1489 1489 * 1490 1490 * @param array images … … 1499 1499 return false; 1500 1500 } 1501 1502 $query = ' 1503 DELETE 1501 1502 // get existing associations 1503 $query = ' 1504 SELECT 1505 image_id, 1506 category_id 1504 1507 FROM '.IMAGE_CATEGORY_TABLE.' 1505 1508 WHERE image_id IN ('.implode(',', $images).') 1506 1509 AND category_id IN ('.implode(',', $categories).') 1507 1510 ;'; 1508 pwg_query($query); 1509 1511 $result = pwg_query($query); 1512 1513 $existing = array(); 1514 while ($row = pwg_db_fetch_assoc($result)) 1515 { 1516 $existing[ $row['category_id'] ][] = $row['image_id']; 1517 } 1518 1519 // get max rank of each categories 1510 1520 $query = ' 1511 1521 SELECT … … 1524 1534 ); 1525 1535 1536 // associate only not already associated images 1526 1537 $inserts = array(); 1527 1538 foreach ($categories as $category_id) … … 1531 1542 $current_rank_of[$category_id] = 0; 1532 1543 } 1544 if (!isset($existing[$category_id])) 1545 { 1546 $existing[$category_id] = array(); 1547 } 1533 1548 1534 1549 foreach ($images as $image_id) 1535 1550 { 1536 $rank = ++$current_rank_of[$category_id]; 1537 1538 array_push( 1539 $inserts, 1540 array( 1541 'image_id' => $image_id, 1542 'category_id' => $category_id, 1543 'rank' => $rank, 1544 ) 1545 ); 1546 } 1547 } 1548 1549 mass_inserts( 1550 IMAGE_CATEGORY_TABLE, 1551 array_keys($inserts[0]), 1552 $inserts 1553 ); 1554 1555 update_category($categories); 1556 } 1557 1558 /** 1559 * Disssociate images from all categories except their storage category and 1551 if (!in_array($image_id, $existing[$category_id])) 1552 { 1553 $rank = ++$current_rank_of[$category_id]; 1554 1555 array_push( 1556 $inserts, 1557 array( 1558 'image_id' => $image_id, 1559 'category_id' => $category_id, 1560 'rank' => $rank, 1561 ) 1562 ); 1563 } 1564 } 1565 } 1566 1567 if (count($inserts)) 1568 { 1569 mass_inserts( 1570 IMAGE_CATEGORY_TABLE, 1571 array_keys($inserts[0]), 1572 $inserts 1573 ); 1574 1575 update_category($categories); 1576 } 1577 } 1578 1579 /** 1580 * Dissociate images from all old categories except their storage category and 1560 1581 * associate to new categories. 1582 * 1583 * This function will preserve ranks 1561 1584 * 1562 1585 * @param array images … … 1571 1594 } 1572 1595 1573 // let's first break links with all albums but their "storage album"1596 // let's first break links with all old albums but their "storage album" 1574 1597 $query = ' 1575 1598 DELETE '.IMAGE_CATEGORY_TABLE.'.* … … 1577 1600 JOIN '.IMAGES_TABLE.' ON image_id=id 1578 1601 WHERE id IN ('.implode(',', $images).') 1602 '.((is_array($categories) and count($categories)>0) ? 'AND category_id NOT IN ('.implode(',', $categories).')' : null).' 1579 1603 AND (storage_category_id IS NULL OR storage_category_id != category_id) 1580 1604 ;';
Note: See TracChangeset
for help on using the changeset viewer.