mirror of
https://github.com/Piwigo/Piwigo.git
synced 2026-03-28 17:42:57 +01:00
feature 408: ability to automatically sort sub-categories. The number of SQL
queries is constant, no matter the deepth of your tree. + refactor to avoid duplicate code (ascending/desceding should be only a single parameter in a function, not 20 lines of duplicated code) git-svn-id: http://piwigo.org/svn/trunk@6698 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
@@ -55,11 +55,30 @@ if (!empty($_POST) or isset($_GET['delete']))
|
||||
*/
|
||||
function save_categories_order($categories)
|
||||
{
|
||||
$current_rank_for_id_uppercat = array();
|
||||
$current_rank = 0;
|
||||
|
||||
$datas = array();
|
||||
foreach ($categories as $id)
|
||||
foreach ($categories as $category)
|
||||
{
|
||||
array_push($datas, array('id' => $id, 'rank' => ++$current_rank));
|
||||
if (is_array($category))
|
||||
{
|
||||
$id = $category['id'];
|
||||
$id_uppercat = $category['id_uppercat'];
|
||||
|
||||
if (!isset($current_rank_for_id_uppercat[$id_uppercat]))
|
||||
{
|
||||
$current_rank_for_id_uppercat[$id_uppercat] = 0;
|
||||
}
|
||||
$current_rank = ++$current_rank_for_id_uppercat[$id_uppercat];
|
||||
}
|
||||
else
|
||||
{
|
||||
$id = $category;
|
||||
$current_rank++;
|
||||
}
|
||||
|
||||
array_push($datas, array('id' => $id, 'rank' => $current_rank));
|
||||
}
|
||||
$fields = array('primary' => array('id'), 'update' => array('rank'));
|
||||
mass_updates(CATEGORIES_TABLE, $fields, $datas);
|
||||
@@ -111,59 +130,69 @@ else if (isset($_POST['submitAdd']))
|
||||
// save manual category ordering
|
||||
else if (isset($_POST['submitOrder']))
|
||||
{
|
||||
asort($_POST['catOrd'], SORT_NUMERIC);
|
||||
save_categories_order(array_keys($_POST['catOrd']));
|
||||
if ('manual' == $_POST['order_type'])
|
||||
{
|
||||
asort($_POST['catOrd'], SORT_NUMERIC);
|
||||
save_categories_order(array_keys($_POST['catOrd']));
|
||||
|
||||
array_push(
|
||||
$page['infos'],
|
||||
l10n('Categories manual order was saved')
|
||||
);
|
||||
}
|
||||
// sort categories alpha-numerically
|
||||
else if (isset($_POST['submitOrderAlphaNum']))
|
||||
{
|
||||
$query = '
|
||||
SELECT id, name
|
||||
array_push(
|
||||
$page['infos'],
|
||||
l10n('Categories manual order was saved')
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = '
|
||||
SELECT id
|
||||
FROM '.CATEGORIES_TABLE.'
|
||||
WHERE id_uppercat '.
|
||||
(!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
|
||||
;';
|
||||
$result = pwg_query($query);
|
||||
while ($row = pwg_db_fetch_assoc($result))
|
||||
{
|
||||
$categories[ $row['id'] ] = strtolower($row['name']);
|
||||
}
|
||||
$category_ids = array_from_query($query, 'id');
|
||||
|
||||
asort($categories, SORT_REGULAR);
|
||||
save_categories_order(array_keys($categories));
|
||||
if (isset($_POST['recursive']))
|
||||
{
|
||||
$category_ids = get_subcat_ids($category_ids);
|
||||
}
|
||||
|
||||
array_push(
|
||||
$page['infos'],
|
||||
l10n('Categories ordered alphanumerically')
|
||||
);
|
||||
}
|
||||
// sort categories alpha-numerically reverse
|
||||
else if (isset($_POST['submitOrderAlphaNumReverse']))
|
||||
{
|
||||
$query = '
|
||||
SELECT id, name
|
||||
$categories = array();
|
||||
$names = array();
|
||||
$id_uppercats = array();
|
||||
|
||||
$query = '
|
||||
SELECT id, name, id_uppercat
|
||||
FROM '.CATEGORIES_TABLE.'
|
||||
WHERE id_uppercat '.
|
||||
(!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
|
||||
WHERE id IN ('.implode(',', $category_ids).')
|
||||
;';
|
||||
$result = pwg_query($query);
|
||||
while ($row = pwg_db_fetch_assoc($result))
|
||||
{
|
||||
$categories[ $row['id'] ] = strtolower($row['name']);
|
||||
$result = pwg_query($query);
|
||||
while ($row = pwg_db_fetch_assoc($result))
|
||||
{
|
||||
array_push(
|
||||
$categories,
|
||||
array(
|
||||
'id' => $row['id'],
|
||||
'id_uppercat' => $row['id_uppercat'],
|
||||
)
|
||||
);
|
||||
array_push(
|
||||
$names,
|
||||
$row['name']
|
||||
);
|
||||
}
|
||||
|
||||
array_multisort(
|
||||
$names,
|
||||
SORT_REGULAR,
|
||||
'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC,
|
||||
$categories
|
||||
);
|
||||
save_categories_order($categories);
|
||||
|
||||
array_push(
|
||||
$page['infos'],
|
||||
l10n('Categories automatically sorted')
|
||||
);
|
||||
}
|
||||
|
||||
arsort($categories, SORT_REGULAR);
|
||||
save_categories_order(array_keys($categories));
|
||||
|
||||
array_push(
|
||||
$page['infos'],
|
||||
l10n('Categories ordered alphanumerically reverse')
|
||||
);
|
||||
}
|
||||
|
||||
// +-----------------------------------------------------------------------+
|
||||
|
||||
@@ -69,6 +69,11 @@ TABLE.plugins ul.pluginsActions {
|
||||
TABLE.plugins ul.pluginsActions li { display: inline; }
|
||||
|
||||
/* categoryOrdering */
|
||||
FORM#categoryOrdering .orderParams {
|
||||
line-height:30px;
|
||||
margin-bottom:10px;
|
||||
}
|
||||
|
||||
SELECT.categoryList {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,13 @@
|
||||
document.getElementsByName('catOrd[' + cat[1] + ']')[0].value = i;
|
||||
}
|
||||
});
|
||||
|
||||
jQuery("input[name=order_type]").click(function () {ldelim}
|
||||
jQuery("#automatic_order_params").hide();
|
||||
if (jQuery("input[name=order_type]:checked").val() == "automatic") {ldelim}
|
||||
jQuery("#automatic_order_params").show();
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -39,12 +46,21 @@
|
||||
|
||||
{if count($categories) }
|
||||
<form id="categoryOrdering" action="{$F_ACTION}" method="post">
|
||||
<p>
|
||||
<input type="hidden" name="pwg_token" value="{$PWG_TOKEN}">
|
||||
<input type="hidden" name="pwg_token" value="{$PWG_TOKEN}">
|
||||
|
||||
<div class="orderParams">
|
||||
<input class="submit" name="submitOrder" type="submit" value="{'Save order'|@translate}" {$TAG_INPUT_ENABLED}>
|
||||
<input class="submit" name="submitOrderAlphaNum" type="submit" value="{'Order alphanumerically'|@translate}" {$TAG_INPUT_ENABLED}>
|
||||
<input class="submit" name="submitOrderAlphaNumReverse" type="submit" value="{'Order alphanumerically reverse'|@translate}" {$TAG_INPUT_ENABLED}>
|
||||
</p>
|
||||
<label><input type="radio" name="order_type" value="manual" checked="checked"> {'manual order'|@translate}</label>
|
||||
<label><input type="radio" name="order_type" value="automatic"> {'automatic order'|@translate}</label>
|
||||
<span id="automatic_order_params" style="display:none">
|
||||
<select name="ascdesc">
|
||||
<option value="asc">{'ascending'|@translate}</option>
|
||||
<option value="desc">{'descending'|@translate}</option>
|
||||
</select>
|
||||
<label><input type="checkbox" name="recursive"> {'apply to sub-categories'|@translate}</label>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<ul class="categoryUl">
|
||||
|
||||
{foreach from=$categories item=category}
|
||||
@@ -85,11 +101,5 @@
|
||||
</li>
|
||||
{/foreach}
|
||||
</ul>
|
||||
<p>
|
||||
<input class="submit" name="submitOrder" type="submit" value="{'Save order'|@translate}" {$TAG_INPUT_ENABLED}>
|
||||
<input class="submit" name="submitOrderAlphaNum" type="submit" value="{'Order alphanumerically'|@translate}" {$TAG_INPUT_ENABLED}>
|
||||
<input class="submit" name="submitOrderAlphaNumReverse" type="submit" value="{'Order alphanumerically reverse'|@translate}" {$TAG_INPUT_ENABLED}>
|
||||
</p>
|
||||
|
||||
</form>
|
||||
{/if}
|
||||
|
||||
@@ -633,8 +633,6 @@ $lang['Tools'] = "Tools";
|
||||
$lang['Photos'] = "Photos";
|
||||
$lang['Themes'] = "Themes";
|
||||
$lang['Instructions to use Piwigo'] = "Instructions to use Piwigo";
|
||||
$lang['Order alphanumerically'] = "Alphanumerical order";
|
||||
$lang['Order alphanumerically reverse'] = "Reverse alphanumerical order";
|
||||
$lang['Installed Themes'] = "Installed Themes";
|
||||
$lang['Add New Theme'] = "Add a new theme";
|
||||
$lang['Forbid this theme to users'] = "Forbid this theme to users";
|
||||
@@ -683,8 +681,6 @@ $lang['Your configuration settings are saved'] = 'Your configuration settings ar
|
||||
$lang['Active Themes'] = 'Active Themes';
|
||||
$lang['Add write access to the "%s" directory'] = 'Add write access to the "%s" directory';
|
||||
$lang['Administration Home'] = 'Administration Home';
|
||||
$lang['Categories ordered alphanumerically'] = 'Categories sorted by alphanumerical order';
|
||||
$lang['Categories ordered alphanumerically reverse'] = 'Categories sorted by reverse alphanumeric order';
|
||||
$lang['Change Admin Colors'] = 'Change administration colors';
|
||||
$lang['Delete this theme'] = 'Delete this theme';
|
||||
$lang['Directory does not exist'] = 'Directory does not exist';
|
||||
@@ -770,4 +766,8 @@ $lang['File upload stopped by extension'] = 'File upload stopped by extension';
|
||||
$lang['Unknown upload error'] = 'Unknown upload error';
|
||||
$lang['Error on file "%s" : %s'] = 'Error on file "%s" : %s';
|
||||
$lang['Menu Management'] = 'Menus';
|
||||
$lang['apply to sub-categories'] = 'apply to sub-categories';
|
||||
$lang['automatic order'] = 'automatic order';
|
||||
$lang['manual order'] = 'manual order';
|
||||
$lang['Categories automatically sorted'] = 'Categories automatically sorted';
|
||||
?>
|
||||
|
||||
@@ -634,8 +634,6 @@ $lang['Tools'] = "Outils";
|
||||
$lang['Photos'] = "Photos";
|
||||
$lang['Themes'] = "Thèmes";
|
||||
$lang['Instructions to use Piwigo'] = "Instructions pour utiliser Piwigo";
|
||||
$lang['Order alphanumerically'] = "Ordre alphanumérique";
|
||||
$lang['Order alphanumerically reverse'] = "Ordre alphanumérique inverse";
|
||||
$lang['Installed Themes'] = "Thèmes installés";
|
||||
$lang['Add New Theme'] = "Ajouter un thème";
|
||||
$lang['Forbid this theme to users'] = "Interdire ce thème aux utilisateurs";
|
||||
@@ -685,8 +683,6 @@ $lang['Your configuration settings are saved'] = 'Vos paramètres de configurati
|
||||
$lang['Active Themes'] = 'Thèmes activés';
|
||||
$lang['Add write access to the "%s" directory'] = 'Ajoutez l\'accès en écriture pour le répertoire "%s"';
|
||||
$lang['Administration Home'] = 'Accueil administration';
|
||||
$lang['Categories ordered alphanumerically'] = 'Catégories triés par ordre alphanumérique';
|
||||
$lang['Categories ordered alphanumerically reverse'] = 'Catégories triés par ordre alphanumérique inverse';
|
||||
$lang['Change Admin Colors'] = 'Modifier les couleurs de l\'administration';
|
||||
$lang['Delete this theme'] = 'Supprimer ce thème';
|
||||
$lang['Directory does not exist'] = 'Le répertoire n\'existe pas';
|
||||
@@ -775,4 +771,8 @@ $lang['File upload stopped by extension'] = 'Le transfert du fichier a été arr
|
||||
$lang['Unknown upload error'] = 'Erreur inconnue survenue lors du transfert';
|
||||
$lang['Error on file "%s" : %s'] = 'Erreur sur le fichier "%s" : %s';
|
||||
$lang['Menu Management'] = 'Menus';
|
||||
$lang['apply to sub-categories'] = 'appliquer aux sous-catégories';
|
||||
$lang['automatic order'] = 'ordre automatique';
|
||||
$lang['manual order'] = 'ordre manuel';
|
||||
$lang['Categories automatically sorted'] = 'Les catégories ont été triées automatiquement';
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user