Ejemplo n.º 1
0
function keywords_widget(&$ret, &$relays, &$roots)
{
    global $content, $keywords;
    $keywords = new KeyWords();
    $tree = $keywords->getTree();
    if (empty($tree)) {
        return null;
    }
    $keys = $content->getKeywords();
    $ret = array();
    $relays = array();
    $all = $keywords->get();
    foreach ($all as $key) {
        if (!empty($key['relay'])) {
            $root = $key;
            while ($root['parent']) {
                $root = $all[$root['parent']];
            }
            $relays[$key['keyid']] = array('keyid' => $key['relay'], 'root' => $root['keyid'], 'values' => array(), 'labels' => array());
        }
    }
    $roots = array();
    foreach ($tree as $key) {
        // makes toplevel widget
        $keyid = $key['keyid'];
        $roots[] = $keyid;
        $name = "keywords[{$keyid}]";
        $words = new KeyFormSelect('', $name);
        $words->addOption('', _MD_KEY_NONE);
        $opts = key_expand($key['child']);
        $words->addOptions($opts);
        $find = null;
        foreach ($opts as $opt) {
            $oid = $opt['keyid'];
            $check = true;
            if (empty($oid)) {
                continue;
            }
            foreach (explode(',', $oid) as $id) {
                if (!in_array($id, $keys)) {
                    $check = false;
                }
                $pid = $all[$id]['parent'];
                if (isset($relays[$pid])) {
                    $relays[$pid]['values'][] = $opt['keyid'];
                    $relays[$pid]['labels'][] = $opt['name'];
                }
            }
            if ($check) {
                $find = $oid;
            }
        }
        if ($find) {
            $words->setValue($find);
        }
        $words->setExtra('onchange="changeKeyword(this);"');
        $ret[$key['keyid']] = $words->render();
    }
    return $ret;
}