/**
 * Creates and returns an array of tags for the jsTree ajax loader.
 * If the tag is empty, the configured menu2 (admin) main tags are used.
 *
 * @param string                          $tag Tag name to fetch subtags for
 * @param SemanticScuttle_Service_Tag2Tag $t2t Tag relation service
 *
 * @return array Array of tag data suitable for the jsTree ajax loader
 */
function assembleAdminTagData($tag, SemanticScuttle_Service_Tag2Tag $t2t)
{
    if ($tag == '') {
        $linkedTags = $GLOBALS['menu2Tags'];
    } else {
        $linkedTags = $t2t->getAdminLinkedTags($tag, '>');
    }
    $tagData = array();
    foreach ($linkedTags as $tag) {
        //FIXME: the hasChildren code is nasty, because it causes too many
        // queries onto the database
        $hasChildren = 0 < count($t2t->getAdminLinkedTags($tag, '>'));
        $tagData[] = createTagArray($tag, $hasChildren);
    }
    return $tagData;
}
function assembleLinkedTagData($tags, $uId, $loadParentTags, SemanticScuttle_Service_Tag2Tag $t2t)
{
    $tagData = array();
    if (count($tags) == 0) {
        //no tags given -> show the 4 most used top-level tags
        $orphewTags = $t2t->getOrphewTags('>', $uId, 4, 'nb');
        #$orphewTags = $t2t->getOrphewTags('>', $uId);
        foreach ($orphewTags as $orphewTag) {
            $tags[] = $orphewTag['tag'];
        }
        $loadParentTags = true;
    }
    if ($loadParentTags) {
        //find parent tags + append the selected tags as children afterwards
        foreach ($tags as $tag) {
            $parentTags = $t2t->getLinkedTags($tag, '>', $uId, true);
            if (count($parentTags) > 0) {
                foreach ($parentTags as $parentTag) {
                    $ta = createTagArray($parentTag, true, true, true);
                    //FIXME: find out if there are subtags
                    $tac = createTagArray($tag, true);
                    $ta['children'][] = $tac;
                    $tagData[] = $ta;
                }
            } else {
                //no parent tags -> display it normally
                //FIXME: find out if there are subtags
                $tagData[] = createTagArray($tag, true);
            }
        }
    } else {
        //just find the linked tags
        foreach ($tags as $tag) {
            $linkedTags = $t2t->getLinkedTags($tag, '>', $uId);
            foreach ($linkedTags as $linkedTag) {
                //FIXME: find out if there are subtags
                $tagData[] = createTagArray($linkedTag, true);
            }
        }
    }
    return $tagData;
}