Changeset 1099 for trunk/common


Ignore:
Timestamp:
12/28/09 14:24:37 (2 years ago)
Author:
bogdan2412
Message:

Show tags on filter results page.

Reimplemented tag_build_tree so that it works with more than one level of depth. It now receives all the tags in a single array and returns the tag tree.

Also added some diacritics to some pages.

Location:
trunk/common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/common/db/tags.php

    r1089 r1099  
    6060// Return an array with id's of their parents 
    6161// Each parent id appears only once 
    62 function tag_get_parents($tags) { 
     62function tag_get_parents($tag_ids) { 
    6363    $query = sprintf("SELECT DISTINCT(`parent`) 
    6464            FROM ia_tags 
    65             WHERE `id` IN (%s)", 
    66             implode(",", array_map('db_quote', $tags)) 
     65            WHERE `id` IN (%s) AND `parent` != 0", 
     66            implode(",", array_map('db_quote', $tag_ids)) 
    6767        ); 
    6868    $result = db_fetch_all($query); 
    69     $ret = Array(); 
     69    $ret = array(); 
    7070    foreach ($result as $row) { 
    7171        $ret[] = $row['parent']; 
  • trunk/common/tags.php

    r1089 r1099  
    5252} 
    5353 
    54 // Receives a list of parent tags and children tags 
    55 // For each parent_tag adds an array 'sub_tags' containing 
    56 // an array with all his children tags 
    57 function build_tags_tree($parent_tags, $sub_tags) { 
    58     log_assert(is_array($parent_tags), "Parent tags is not an array"); 
    59     log_assert(is_array($sub_tags), "Children tags is not an array"); 
     54// Receives a list of tags which will be arranged in a tree-like structure 
     55// according to their respective parents. 
     56function tag_build_tree($tags) { 
     57    log_assert(is_array($tags), "tag_build_tree expects an array of tags"); 
     58    $tags_by_id = array(); 
     59    // Group tags by id 
     60    foreach ($tags as &$tag) { 
     61        log_assert(is_tag($tag), "tag_build_tree expects an array of tags"); 
     62        log_assert(isset($tag["id"]), 
     63            "tag_build_tree expects an array of tags with id"); 
    6064 
    61     $parent_tags_key = Array(); 
    62     foreach ($parent_tags as $key => $tag) { 
    63         $parent_tags[$key]['sub_tags'] = Array(); 
    64         $parent_tags_key[$tag['id']] = $key; 
     65        $tags_by_id[$tag["id"]] =& $tag; 
    6566    } 
    6667 
    67     foreach ($sub_tags as $tag) { 
    68         log_assert(isset($parent_tags_key[$tag['parent']]), "Child tag doesn't have a parent"); 
    69         $parent_tag_key = $parent_tags_key[$tag['parent']]; 
    70         $parent_tags[$parent_tag_key]['sub_tags'][] = $tag; 
     68    // Add 'sub_tags' attribute to all tags which have children. 
     69    $has_parent = array(); 
     70    foreach ($tags as &$tag) { 
     71        if (array_key_exists($tag["parent"], $tags_by_id)) { 
     72            $has_parent[$tag["id"]] = true; 
     73 
     74            if (!isset($tags_by_id[$tag["parent"]]["sub_tags"])) { 
     75                $tags_by_id[$tag["parent"]]["sub_tags"] = array(); 
     76            } 
     77            $tags_by_id[$tag["parent"]]["sub_tags"][] =& $tag; 
     78        } 
    7179    } 
    7280 
    73     return $parent_tags; 
     81    // Return only tree roots (tags which have no parents). 
     82    $roots = array(); 
     83    foreach ($tags_by_id as &$tag) { 
     84        if (!isset($has_parent[$tag["id"]])) { 
     85            $roots[] = $tag; 
     86        } 
     87    } 
     88    return $roots; 
    7489} 
    7590 
Note: See TracChangeset for help on using the changeset viewer.