Ignore:
Timestamp:
Aug 6, 2012, 11:04:46 AM (12 years ago)
Author:
mistic100
Message:

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
  • trunk/admin/include/functions.php

    r17302 r17424  
    14861486 * Associate a list of images to a list of categories.
    14871487 *
    1488  * The function will not duplicate links
     1488 * The function will not duplicate links and will preserve ranks
    14891489 *
    14901490 * @param array images
     
    14991499    return false;
    15001500  }
    1501 
    1502   $query = '
    1503 DELETE
     1501 
     1502  // get existing associations
     1503  $query = '
     1504SELECT
     1505    image_id,
     1506    category_id
    15041507  FROM '.IMAGE_CATEGORY_TABLE.'
    15051508  WHERE image_id IN ('.implode(',', $images).')
    15061509    AND category_id IN ('.implode(',', $categories).')
    15071510;';
    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
    15101520  $query = '
    15111521SELECT
     
    15241534    );
    15251535
     1536  // associate only not already associated images
    15261537  $inserts = array();
    15271538  foreach ($categories as $category_id)
     
    15311542      $current_rank_of[$category_id] = 0;
    15321543    }
     1544    if (!isset($existing[$category_id]))
     1545    {
     1546      $existing[$category_id] = array();
     1547    }
    15331548
    15341549    foreach ($images as $image_id)
    15351550    {
    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
    15601581 * associate to new categories.
     1582 *
     1583 * This function will preserve ranks
    15611584 *
    15621585 * @param array images
     
    15711594  }
    15721595
    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"
    15741597  $query = '
    15751598DELETE '.IMAGE_CATEGORY_TABLE.'.*
     
    15771600    JOIN '.IMAGES_TABLE.' ON image_id=id
    15781601  WHERE id IN ('.implode(',', $images).')
     1602    '.((is_array($categories) and count($categories)>0) ? 'AND category_id NOT IN ('.implode(',', $categories).')' : null).'
    15791603    AND (storage_category_id IS NULL OR storage_category_id != category_id)
    15801604;';
Note: See TracChangeset for help on using the changeset viewer.