mirror of
https://github.com/DanielnetoDotCom/YouPHPTube
synced 2025-10-03 01:39:24 +02:00
Enhance order assignment logic in sortChannelsBySubscribers.php to ensure unique order values and handle duplicates effectively
This commit is contained in:
parent
50354a0cfd
commit
fd7b1fc415
1 changed files with 42 additions and 6 deletions
|
@ -33,21 +33,57 @@ uasort($sections, function ($a, $b) {
|
|||
return $b['total_subscribers'] <=> $a['total_subscribers'];
|
||||
});
|
||||
|
||||
// Get all existing order values and sort them
|
||||
// Get all available current_order values (sorted ascending)
|
||||
$availableOrders = array_column($sections, 'current_order');
|
||||
sort($availableOrders);
|
||||
|
||||
// Assign the sorted order values to the sorted channels
|
||||
$orderIndex = 0;
|
||||
// Remove duplicates to ensure unique order values
|
||||
$availableOrders = array_unique($availableOrders);
|
||||
$availableOrders = array_values($availableOrders); // Re-index array
|
||||
|
||||
// If we don't have enough unique orders, generate additional ones
|
||||
$totalChannels = count($sections);
|
||||
$uniqueOrdersCount = count($availableOrders);
|
||||
|
||||
if ($uniqueOrdersCount < $totalChannels) {
|
||||
$maxOrder = max($availableOrders);
|
||||
for ($i = $uniqueOrdersCount; $i < $totalChannels; $i++) {
|
||||
$availableOrders[] = ++$maxOrder;
|
||||
}
|
||||
}
|
||||
|
||||
// Reassign the lowest available order to the user with most subscribers
|
||||
$index = 0;
|
||||
foreach ($sections as $key => &$section) {
|
||||
$obj->$key = $availableOrders[$orderIndex];
|
||||
$section['new_order'] = $availableOrders[$orderIndex];
|
||||
$orderIndex++;
|
||||
$newOrder = $availableOrders[$index];
|
||||
$obj->$key = $newOrder;
|
||||
$section['new_order'] = $newOrder;
|
||||
$index++;
|
||||
}
|
||||
|
||||
// Save the new object data
|
||||
AVideoPlugin::setObjectData('Gallery', $obj);
|
||||
|
||||
// Final check to ensure no duplicates
|
||||
$finalOrders = array_column($sections, 'new_order');
|
||||
$duplicates = array_diff_assoc($finalOrders, array_unique($finalOrders));
|
||||
|
||||
if (!empty($duplicates)) {
|
||||
echo "WARNING: Duplicate orders detected! Fixing...\n";
|
||||
|
||||
// Fix duplicates by reassigning sequential orders
|
||||
$orderCounter = 1;
|
||||
foreach ($sections as $key => &$section) {
|
||||
$obj->$key = $orderCounter;
|
||||
$section['new_order'] = $orderCounter;
|
||||
$orderCounter++;
|
||||
}
|
||||
|
||||
// Save again with fixed orders
|
||||
AVideoPlugin::setObjectData('Gallery', $obj);
|
||||
echo "Fixed: Assigned sequential orders starting from 1.\n";
|
||||
}
|
||||
|
||||
// Optional: output results
|
||||
echo "Reordered Channels:\n";
|
||||
foreach ($sections as $data) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue