Changeset 11155


Ignore:
Timestamp:
05/31/11 22:25:40 (8 years ago)
Author:
plg
Message:

merge r11152 from branch 2.2 to trunk

feature 1622 added: pwg.categories.getList is now able to return a tree with
the new "tree_output" option. Only compatible with json/php output formats.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_category.inc.php

    r8802 r11155  
    534534  return $image_id; 
    535535} 
     536 
     537/** 
     538 * create a tree from a flat list of categories, no recursivity for high speed 
     539 */ 
     540function categories_flatlist_to_tree($categories) 
     541{ 
     542  $tree = array(); 
     543  $key_of_cat = array(); 
     544   
     545  foreach ($categories as $key => &$node) 
     546  { 
     547    $key_of_cat[$node['id']] = $key; 
     548     
     549    if (!isset($node['id_uppercat'])) 
     550    { 
     551      $tree[$key] = &$node; 
     552    } 
     553    else 
     554    { 
     555      if (!isset($categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'])) 
     556      { 
     557        $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'] = array(); 
     558      } 
     559       
     560      $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'][$key] = &$node; 
     561    } 
     562  } 
     563   
     564  return $tree; 
     565} 
    536566?> 
  • trunk/include/ws_functions.inc.php

    r11118 r11155  
    433433  global $user,$conf; 
    434434 
     435  if ($params['tree_output']) 
     436  { 
     437    if (!isset($_GET['format']) or !in_array($_GET['format'], array('php', 'json'))) 
     438    { 
     439      // the algorithm used to build a tree from a flat list of categories 
     440      // keeps original array keys, which is not compatible with 
     441      // PwgNamedArray. 
     442      // 
     443      // PwgNamedArray is useful to define which data is an attribute and 
     444      // which is an element in the XML output. The "hierarchy" output is 
     445      // only compatible with json/php output. 
     446       
     447      return new PwgError(405, "The tree_output option is only compatible with json/php output formats"); 
     448    } 
     449  } 
     450 
    435451  $where = array('1=1'); 
    436452  $join_type = 'INNER'; 
     
    472488 
    473489  $query = ' 
    474 SELECT id, name, permalink, uppercats, global_rank, 
     490SELECT id, name, permalink, uppercats, global_rank, id_uppercat, 
    475491    comment, 
    476492    nb_images, count_images AS total_nb_images, 
     
    515531  } 
    516532  usort($cats, 'global_rank_compare'); 
    517   return array( 
    518     'categories' => new PwgNamedArray( 
    519       $cats, 
    520       'category', 
    521       array( 
    522         'id', 
    523         'url', 
    524         'nb_images', 
    525         'total_nb_images', 
    526         'nb_categories', 
    527         'date_last', 
    528         'max_date_last', 
     533 
     534  if ($params['tree_output']) 
     535  {  
     536    return categories_flatlist_to_tree($cats); 
     537  } 
     538  else 
     539  { 
     540    return array( 
     541      'categories' => new PwgNamedArray( 
     542        $cats, 
     543        'category', 
     544        array( 
     545          'id', 
     546          'url', 
     547          'nb_images', 
     548          'total_nb_images', 
     549          'nb_categories', 
     550          'date_last', 
     551          'max_date_last', 
     552          ) 
    529553        ) 
    530       ) 
    531     ); 
     554      ); 
     555  } 
    532556} 
    533557 
  • trunk/ws.php

    r10686 r11155  
    8282        'recursive' => array('default'=>false), 
    8383        'public' => array('default'=>false), 
    84       ), 
    85       'retrieves a list of categories' ); 
     84        'tree_output' => array('default'=>false), 
     85      ), 
     86      'retrieves a list of categories (tree_output option only compatible with json/php output format' ); 
    8687 
    8788  $service->addMethod('pwg.images.addComment', 'ws_images_addComment', 
Note: See TracChangeset for help on using the changeset viewer.