Changeset 6698


Ignore:
Timestamp:
07/23/10 23:33:56 (10 years ago)
Author:
plg
Message:

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)

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/cat_list.php

    r6363 r6698  
    5656function save_categories_order($categories) 
    5757{ 
     58  $current_rank_for_id_uppercat = array(); 
    5859  $current_rank = 0; 
     60   
    5961  $datas = array(); 
    60   foreach ($categories as $id) 
    61   { 
    62     array_push($datas, array('id' => $id, 'rank' => ++$current_rank)); 
     62  foreach ($categories as $category) 
     63  { 
     64    if (is_array($category)) 
     65    { 
     66      $id = $category['id']; 
     67      $id_uppercat = $category['id_uppercat']; 
     68 
     69      if (!isset($current_rank_for_id_uppercat[$id_uppercat])) 
     70      { 
     71        $current_rank_for_id_uppercat[$id_uppercat] = 0; 
     72      } 
     73      $current_rank = ++$current_rank_for_id_uppercat[$id_uppercat]; 
     74    } 
     75    else 
     76    { 
     77      $id = $category; 
     78      $current_rank++; 
     79    } 
     80     
     81    array_push($datas, array('id' => $id, 'rank' => $current_rank)); 
    6382  } 
    6483  $fields = array('primary' => array('id'), 'update' => array('rank')); 
     
    112131else if (isset($_POST['submitOrder'])) 
    113132{ 
    114   asort($_POST['catOrd'], SORT_NUMERIC); 
    115   save_categories_order(array_keys($_POST['catOrd'])); 
    116  
    117   array_push( 
    118     $page['infos'], 
    119     l10n('Categories manual order was saved') 
    120     ); 
    121 } 
    122 // sort categories alpha-numerically 
    123 else if (isset($_POST['submitOrderAlphaNum'])) 
    124 { 
    125   $query = ' 
    126 SELECT id, name 
     133  if ('manual' == $_POST['order_type']) 
     134  { 
     135    asort($_POST['catOrd'], SORT_NUMERIC); 
     136    save_categories_order(array_keys($_POST['catOrd'])); 
     137 
     138    array_push( 
     139      $page['infos'], 
     140      l10n('Categories manual order was saved') 
     141      ); 
     142  } 
     143  else 
     144  { 
     145    $query = ' 
     146SELECT id 
    127147  FROM '.CATEGORIES_TABLE.' 
    128148  WHERE id_uppercat '. 
    129149    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).' 
    130150;'; 
    131   $result = pwg_query($query); 
    132   while ($row = pwg_db_fetch_assoc($result)) 
    133   { 
    134     $categories[ $row['id'] ] = strtolower($row['name']); 
    135   } 
    136  
    137   asort($categories, SORT_REGULAR); 
    138   save_categories_order(array_keys($categories)); 
    139  
    140   array_push( 
    141     $page['infos'], 
    142     l10n('Categories ordered alphanumerically') 
    143     ); 
    144 } 
    145 // sort categories alpha-numerically reverse 
    146 else if (isset($_POST['submitOrderAlphaNumReverse'])) 
    147 { 
    148   $query = ' 
    149 SELECT id, name 
     151    $category_ids = array_from_query($query, 'id'); 
     152 
     153    if (isset($_POST['recursive'])) 
     154    { 
     155      $category_ids = get_subcat_ids($category_ids); 
     156    } 
     157 
     158    $categories = array(); 
     159    $names = array(); 
     160    $id_uppercats = array(); 
     161   
     162    $query = ' 
     163SELECT id, name, id_uppercat 
    150164  FROM '.CATEGORIES_TABLE.' 
    151   WHERE id_uppercat '. 
    152     (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).' 
     165  WHERE id IN ('.implode(',', $category_ids).') 
    153166;'; 
    154   $result = pwg_query($query); 
    155   while ($row = pwg_db_fetch_assoc($result)) 
    156   { 
    157     $categories[ $row['id'] ] = strtolower($row['name']); 
    158   } 
    159  
    160   arsort($categories, SORT_REGULAR); 
    161   save_categories_order(array_keys($categories)); 
    162  
    163   array_push( 
    164     $page['infos'], 
    165     l10n('Categories ordered alphanumerically reverse') 
    166     ); 
     167    $result = pwg_query($query); 
     168    while ($row = pwg_db_fetch_assoc($result)) 
     169    { 
     170      array_push( 
     171        $categories, 
     172        array( 
     173          'id' => $row['id'], 
     174          'id_uppercat' => $row['id_uppercat'], 
     175          ) 
     176        ); 
     177      array_push( 
     178        $names, 
     179        $row['name'] 
     180        ); 
     181    } 
     182 
     183    array_multisort( 
     184      $names, 
     185      SORT_REGULAR, 
     186      'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC, 
     187      $categories 
     188      ); 
     189    save_categories_order($categories); 
     190 
     191    array_push( 
     192      $page['infos'], 
     193      l10n('Categories automatically sorted') 
     194      ); 
     195  } 
    167196} 
    168197 
  • trunk/admin/themes/default/default-layout.css

    r6622 r6698  
    7070 
    7171/* categoryOrdering */ 
     72FORM#categoryOrdering .orderParams { 
     73  line-height:30px; 
     74  margin-bottom:10px; 
     75} 
     76 
    7277SELECT.categoryList { 
    7378        width: 100%; 
  • trunk/admin/themes/default/template/cat_list.tpl

    r6363 r6698  
    1717        cat = ar[i].split('cat_'); 
    1818        document.getElementsByName('catOrd[' + cat[1] + ']')[0].value = i; 
     19      } 
     20    }); 
     21 
     22    jQuery("input[name=order_type]").click(function () {ldelim} 
     23      jQuery("#automatic_order_params").hide(); 
     24      if (jQuery("input[name=order_type]:checked").val() == "automatic") {ldelim} 
     25        jQuery("#automatic_order_params").show(); 
    1926      } 
    2027    }); 
     
    4047{if count($categories) } 
    4148<form id="categoryOrdering" action="{$F_ACTION}" method="post"> 
    42   <p> 
    43     <input type="hidden" name="pwg_token" value="{$PWG_TOKEN}"> 
     49  <input type="hidden" name="pwg_token" value="{$PWG_TOKEN}"> 
     50 
     51  <div class="orderParams"> 
    4452    <input class="submit" name="submitOrder" type="submit" value="{'Save order'|@translate}" {$TAG_INPUT_ENABLED}> 
    45     <input class="submit" name="submitOrderAlphaNum" type="submit" value="{'Order alphanumerically'|@translate}" {$TAG_INPUT_ENABLED}> 
    46         <input class="submit" name="submitOrderAlphaNumReverse" type="submit" value="{'Order alphanumerically reverse'|@translate}" {$TAG_INPUT_ENABLED}> 
    47   </p> 
     53    <label><input type="radio" name="order_type" value="manual" checked="checked"> {'manual order'|@translate}</label> 
     54    <label><input type="radio" name="order_type" value="automatic"> {'automatic order'|@translate}</label> 
     55    <span id="automatic_order_params" style="display:none"> 
     56      <select name="ascdesc"> 
     57        <option value="asc">{'ascending'|@translate}</option> 
     58        <option value="desc">{'descending'|@translate}</option> 
     59      </select> 
     60      <label><input type="checkbox" name="recursive"> {'apply to sub-categories'|@translate}</label> 
     61    </span> 
     62  </div> 
     63 
    4864  <ul class="categoryUl"> 
    4965 
     
    86102    {/foreach} 
    87103  </ul> 
    88   <p> 
    89     <input class="submit" name="submitOrder" type="submit" value="{'Save order'|@translate}" {$TAG_INPUT_ENABLED}> 
    90     <input class="submit" name="submitOrderAlphaNum" type="submit" value="{'Order alphanumerically'|@translate}" {$TAG_INPUT_ENABLED}> 
    91         <input class="submit" name="submitOrderAlphaNumReverse" type="submit" value="{'Order alphanumerically reverse'|@translate}" {$TAG_INPUT_ENABLED}> 
    92   </p> 
    93  
    94104</form> 
    95105{/if} 
  • trunk/language/en_UK/admin.lang.php

    r6676 r6698  
    634634$lang['Themes'] = "Themes"; 
    635635$lang['Instructions to use Piwigo'] = "Instructions to use Piwigo"; 
    636 $lang['Order alphanumerically'] = "Alphanumerical order"; 
    637 $lang['Order alphanumerically reverse'] = "Reverse alphanumerical order"; 
    638636$lang['Installed Themes'] = "Installed Themes"; 
    639637$lang['Add New Theme'] = "Add a new theme"; 
     
    684682$lang['Add write access to the "%s" directory'] = 'Add write access to the "%s" directory'; 
    685683$lang['Administration Home'] = 'Administration Home'; 
    686 $lang['Categories ordered alphanumerically'] = 'Categories sorted by alphanumerical order'; 
    687 $lang['Categories ordered alphanumerically reverse'] = 'Categories sorted by reverse alphanumeric order'; 
    688684$lang['Change Admin Colors'] = 'Change administration colors'; 
    689685$lang['Delete this theme'] = 'Delete this theme'; 
     
    771767$lang['Error on file "%s" : %s'] = 'Error on file "%s" : %s'; 
    772768$lang['Menu Management'] = 'Menus'; 
     769$lang['apply to sub-categories'] = 'apply to sub-categories'; 
     770$lang['automatic order'] = 'automatic order'; 
     771$lang['manual order'] = 'manual order'; 
     772$lang['Categories automatically sorted'] = 'Categories automatically sorted'; 
    773773?> 
  • trunk/language/fr_FR/admin.lang.php

    r6678 r6698  
    635635$lang['Themes'] = "Thèmes"; 
    636636$lang['Instructions to use Piwigo'] = "Instructions pour utiliser Piwigo"; 
    637 $lang['Order alphanumerically'] = "Ordre alphanumérique"; 
    638 $lang['Order alphanumerically reverse'] = "Ordre alphanumérique inverse"; 
    639637$lang['Installed Themes'] = "Thèmes installés"; 
    640638$lang['Add New Theme'] = "Ajouter un thème"; 
     
    686684$lang['Add write access to the "%s" directory'] = 'Ajoutez l\'accès en écriture pour le répertoire "%s"'; 
    687685$lang['Administration Home'] = 'Accueil administration'; 
    688 $lang['Categories ordered alphanumerically'] = 'Catégories triés par ordre alphanumérique'; 
    689 $lang['Categories ordered alphanumerically reverse'] = 'Catégories triés par ordre alphanumérique inverse'; 
    690686$lang['Change Admin Colors'] = 'Modifier les couleurs de l\'administration'; 
    691687$lang['Delete this theme'] = 'Supprimer ce thème'; 
     
    776772$lang['Error on file "%s" : %s'] = 'Erreur sur le fichier "%s" : %s'; 
    777773$lang['Menu Management'] = 'Menus'; 
     774$lang['apply to sub-categories'] = 'appliquer aux sous-catégories'; 
     775$lang['automatic order'] = 'ordre automatique'; 
     776$lang['manual order'] = 'ordre manuel'; 
     777$lang['Categories automatically sorted'] = 'Les catégories ont été triées automatiquement'; 
    778778?> 
Note: See TracChangeset for help on using the changeset viewer.