Changeset 17981


Ignore:
Timestamp:
09/18/12 13:45:51 (7 years ago)
Author:
plg
Message:

merge r17424 from trunk to branch 2.4

bug 2716: modifying a photo reset it's rank
modify associate_images_to_categories() and move_images_to_categories() to preserve ranks
please test before 2.4.4 ! :-D

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.4/admin/include/functions.php

    r17766 r17981  
    14891489 * Associate a list of images to a list of categories. 
    14901490 * 
    1491  * The function will not duplicate links 
     1491 * The function will not duplicate links and will preserve ranks 
    14921492 * 
    14931493 * @param array images 
     
    15021502    return false; 
    15031503  } 
    1504  
    1505   $query = ' 
    1506 DELETE 
     1504   
     1505  // get existing associations 
     1506  $query = ' 
     1507SELECT 
     1508    image_id, 
     1509    category_id 
    15071510  FROM '.IMAGE_CATEGORY_TABLE.' 
    15081511  WHERE image_id IN ('.implode(',', $images).') 
    15091512    AND category_id IN ('.implode(',', $categories).') 
    15101513;'; 
    1511   pwg_query($query); 
    1512  
     1514  $result = pwg_query($query); 
     1515   
     1516  $existing = array(); 
     1517  while ($row = pwg_db_fetch_assoc($result)) 
     1518  { 
     1519    $existing[ $row['category_id'] ][] = $row['image_id']; 
     1520  } 
     1521 
     1522  // get max rank of each categories 
    15131523  $query = ' 
    15141524SELECT 
     
    15271537    ); 
    15281538 
     1539  // associate only not already associated images 
    15291540  $inserts = array(); 
    15301541  foreach ($categories as $category_id) 
     
    15341545      $current_rank_of[$category_id] = 0; 
    15351546    } 
     1547    if (!isset($existing[$category_id])) 
     1548    { 
     1549      $existing[$category_id] = array(); 
     1550    } 
    15361551 
    15371552    foreach ($images as $image_id) 
    15381553    { 
    1539       $rank = ++$current_rank_of[$category_id]; 
    1540  
    1541       array_push( 
    1542         $inserts, 
    1543         array( 
    1544           'image_id' => $image_id, 
    1545           'category_id' => $category_id, 
    1546           'rank' => $rank, 
    1547           ) 
    1548         ); 
    1549     } 
    1550   } 
    1551  
    1552   mass_inserts( 
    1553     IMAGE_CATEGORY_TABLE, 
    1554     array_keys($inserts[0]), 
    1555     $inserts 
    1556     ); 
    1557  
    1558   update_category($categories); 
    1559 } 
    1560  
    1561 /** 
    1562  * Disssociate images from all categories except their storage category and 
     1554      if (!in_array($image_id, $existing[$category_id])) 
     1555      { 
     1556        $rank = ++$current_rank_of[$category_id]; 
     1557 
     1558        array_push( 
     1559          $inserts, 
     1560          array( 
     1561            'image_id' => $image_id, 
     1562            'category_id' => $category_id, 
     1563            'rank' => $rank, 
     1564            ) 
     1565          ); 
     1566      } 
     1567    } 
     1568  } 
     1569 
     1570  if (count($inserts)) 
     1571  { 
     1572    mass_inserts( 
     1573      IMAGE_CATEGORY_TABLE, 
     1574      array_keys($inserts[0]), 
     1575      $inserts 
     1576      ); 
     1577 
     1578    update_category($categories); 
     1579  } 
     1580} 
     1581 
     1582/** 
     1583 * Dissociate images from all old categories except their storage category and 
    15631584 * associate to new categories. 
     1585 * 
     1586 * This function will preserve ranks 
    15641587 * 
    15651588 * @param array images 
     
    15741597  } 
    15751598 
    1576   // let's first break links with all albums but their "storage album" 
     1599  // let's first break links with all old albums but their "storage album" 
    15771600  $query = ' 
    15781601DELETE '.IMAGE_CATEGORY_TABLE.'.* 
     
    15801603    JOIN '.IMAGES_TABLE.' ON image_id=id 
    15811604  WHERE id IN ('.implode(',', $images).') 
     1605    '.((is_array($categories) and count($categories)>0) ? 'AND category_id NOT IN ('.implode(',', $categories).')' : null).' 
    15821606    AND (storage_category_id IS NULL OR storage_category_id != category_id) 
    15831607;'; 
Note: See TracChangeset for help on using the changeset viewer.