<?php
/**
 * Function for DokuWiki template Typo
 *
 * @author Michael Klier <chi@chimeric.de>
 */

/**
 * Prints the navigation
 *
 * @author Michael Klier <chi@chimeric.de>
 */
function tpl_navigation() {
    global $ID;
    global $conf;
    $navpage = tpl_getConf('navigation_page');
    list($ns, $chunk) = explode(':', $ID, 2);
    $navpage = (page_exists($ns.':'.$navpage)) ? $ns.':'.$navpage : $navpage;

    print '<div class="navigation">' . DOKU_LF;
    if(!page_exists($navpage)) {
        if(@file_exists(DOKU_TPLINC.'lang/'. $conf['lang'].'/nonavigation.txt')) {
            $out = p_render('xhtml', p_get_instructions(io_readFile(DOKU_TPLINC.'lang/'.$conf['lang'].'/nonavigation.txt')), $info);
        } else {
            $out = p_render('xhtml', p_get_instructions(io_readFile(DOKU_TPLINC.'lang/en/nonavigation.txt')), $info);
        }
        $link = '<a href="' . wl($navpage) . '" class="wikilink2">' . $navpage . '</a>' . DOKU_LF;
        print str_replace('LINK', $link, $out);
    } else {
        print p_wiki_xhtml($navpage);
    }
    print '</div>';
}

/**
 * Prints the actions links
 *
 * @author Michael Klier <chi@chimeric.de>
 */
function tpl_actions() {
    $actions = array('admin', 'revert', 'edit', 'history', 'recent', 'backlink', 'subscription', 'index', 'login', 'profile');

    print '<div class="sidebar_box">' . DOKU_LF;
    print '  <ul>' . DOKU_LF;

    foreach($actions as $action) {
        if(!actionOK($action)) continue;
        // start output buffering
        if($action == 'edit') {
            // check if new page button plugin is available
            if(!plugin_isdisabled('npd') && ($npd =& plugin_load('helper', 'npd'))) {
                $npb = $npd->html_new_page_button(true);
                if($npb) {
                    print '    <li><div class="li">';
                    print $npb;
                    print '</div></li>' . DOKU_LF;
                }
            }
        }
        ob_start();
        print '     <li><div class="li">';
        if(tpl_actionlink($action)) {
            print '</div></li>' . DOKU_LF;
            ob_end_flush();
        } else {
            ob_end_clean();
        }
    }
    print '  </ul>' . DOKU_LF;
    print '</div>' . DOKU_LF;
}

/**
 * Print the search form
 *
 * If the first parameter is given a div with the ID 'qsearch_out' will
 * be added which instructs the ajax pagequicksearch to kick in and place
 * its output into this div. The second parameter controls the propritary
 * attribute autocomplete. If set to false this attribute will be set with an
 * value of "off" to instruct the browser to disable it's own built in
 * autocompletion feature (MSIE and Firefox)
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 *
 * @param bool $ajax
 * @param bool $autocomplete
 * @return bool
 */

function tpl_searchform2($ajax = true, $autocomplete = true) {
    global $lang;
    global $ACT;
    global $QUERY;
    global $ID;

    // don't print the search form if search action has been disabled
    if(!actionOK('search')) return false;

    $searchForm = new dokuwiki\Form\Form([
        'action' => wl(),
        'method' => 'get',
        'role' => 'search',
        'class' => 'search',
        'id' => 'dw__search',
    ], true);
    $searchForm->addTagOpen('div')->addClass('no');
    $searchForm->setHiddenField('do', 'search');
    $searchForm->setHiddenField('id', $ID);
    $searchForm->addTextInput('q')
        ->addClass('edit')
        ->attrs([
            'title' => '[F]',
            'accesskey' => 'f',
            'placeholder' => $lang['btn_search'],
            'autocomplete' => $autocomplete ? 'on' : 'off',
        ])
        ->id('qsearch__in')
        ->val($ACT === 'search' ? $QUERY : '')
        ->useInput(false)
    ;
    $searchForm->addButton('', $lang['btn_search'])->attrs([
        'type' => 'submit',
        'class' => 'button',
        'title' => $lang['btn_search'],
    ]);
    if ($ajax) {
        $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup');
        $searchForm->addTagClose('div');
    }
    $searchForm->addTagClose('div');
    trigger_event('FORM_QUICKSEARCH_OUTPUT', $searchForm);

    echo $searchForm->toHTML();

    return true;
}

// vim:ts=4:sw=4:et:enc=utf-8:
