<?php
/**
 * template functions for dokubook template
 * 
 * @license:    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author:     Michael Klier <chi@chimeric.de>
 */

if(!defined('DW_LF')) define('DW_LF',"\n");

// load language files
require_once(DOKU_TPLINC.'lang/en/lang.php');
if(@file_exists(DOKU_TPLINC.'lang/'.$conf['lang'].'/lang.php')) {
    require_once(DOKU_TPLINC.'lang/'.$conf['lang'].'/lang.php');
}

/**
 * checks if a file called logo.png or logo.jpg exists
 * and uses it as logo, uses the dokuwiki logo by default
 *
 * @author Michael Klier <chi@chimeric.de>
 */
function tpl_logo() {
    global $conf;
    
    $out = '';

    switch(true) {
        case(@file_exists(DOKU_TPLINC.'images/logo.jpg')):
            $logo = DOKU_TPL.'images/logo.jpg';
            break;
        case(@file_exists(DOKU_TPLINC.'images/logo.jpeg')):
            $logo = DOKU_TPL.'images/logo.jpeg';
            break;
        case(@file_exists(DOKU_TPLINC.'images/logo.png')):
            $logo = DOKU_TPL.'images/logo.png';
            break;
        default:
            $logo = DOKU_TPL.'images/dokuwiki-128.png';
            break;
    }

    $out .= '<a href="' . DOKU_BASE . '" name="dokuwiki__top" id="dokuwiki__top" accesskey="h" title="[ALT+H]">';
    $out .= '  <img class="logo" src="' . $logo . '" alt="' . $conf['title'] . '" /></a>' . DW_LF;

    print ($out);
}

/**
 * generates the sidebar contents
 *
 * @author Michael Klier <chi@chimeric.de>
 */
function tpl_sidebar() {
    global $lang;
    global $ID;
    global $INFO;

    $svID  = cleanID($ID);
    $navpn = tpl_getConf('pagename');
    $path  = explode(':',$svID);
    $found = false;
    $sb    = '';

    if(tpl_getConf('closedwiki') && empty($INFO['userinfo'])) {
        print '<span class="sb_label">' . $lang['toolbox'] . '</span>' . DW_LF;
        print '<div id="toolbox" class="sidebar_box">' . DW_LF;
        tpl_actionlink('login');
        print '</div>' . DW_LF;
        return;
    }

    // main navigation
    print '<span class="sb_label">' . $lang['navigation'] . '</span>' . DW_LF;
    print '<div id="navigation" class="sidebar_box">' . DW_LF;

    print ts_sidebar('navigation');

    print '</div>' . DW_LF;
    print '<span class="sb_label">' . $lang['index'] . '</span>' . DW_LF;
    print '<div id="index" class="sidebar_box">' . DW_LF;

    print ts_sidebar('index');

    print '</div>' . DW_LF;

    // generate the searchbox
    print '<span class="sb_label">' . strtolower($lang['btn_search']) . '</span>' . DW_LF;
    print '<div id="search">' . DW_LF;
    tpl_searchform();
    print '</div>' . DW_LF;

    // generate the toolbox
    print '<span class="sb_label">' . $lang['toolbox'] . '</span>' . DW_LF;
    print '<div id="toolbox" class="sidebar_box">' . DW_LF;
    tpl_actionlink('admin');
    tpl_actionlink('index');
    tpl_actionlink('recent');
    tpl_actionlink('backlink');
    tpl_actionlink('profile');
    tpl_actionlink('login');
    print '</div>' . DW_LF;
}

/**
 * removes the TOC of the sidebar-pages and shows a edit-button if user has enough rights
 * 
 * @author Michael Klier <chi@chimeric.de>
 */
function p_sidebar_xhtml($sb) {
    $data = p_wiki_xhtml($sb,'',false);
    if(auth_quickaclcheck($sb) >= AUTH_EDIT) {
        $data .= '<div class="secedit">' . html_btn('secedit',$sb,'',array('do'=>'edit','rev'=>'','post')) . '</div>';
    }
    return preg_replace('/<div class="toc">.*?(<\/div>\n<\/div>)/s', '', $data);
}

/**
 * puts text into our sidebar using lang/en/$ns.txt
 */
function ts_sidebar($ns){
  global $conf;

    $sb_path = 'lang/en';
  if (@file_exists(DOKU_TPLINC.'lang/'.$conf['lang'].'/'.$ns.'.txt'))
    $sb_path = '../../../lib/tpl/tribalshout/lang/'.$conf['lang'];
  elseif (@file_exists(DOKU_TPLINC.'lang/en/'.$ns.'.txt'))
    $sb_path = '../../../lib/tpl/tribalshout/lang/en';

  print p_locale_xhtml($sb_path.'/'.$ns);
}

/**
 * Like the action buttons but links
 *
 * Available links are
 *
 *  article - links to the main page itself
 *  discussion - links to Talk:<namespace>
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
 * @see    tpl_button
 * adapted by davek
 */
function ts_tpl_actionlink($type,$pre='',$suf=''){
  global $ID;
  global $INFO;
  global $REV;
  global $ACT;
  global $conf;
  global $lang;
  global $auth;

  // check disabled actions and fix the badly named ones
  $ctype = $type;
  if($type == 'history') $ctype='revisions';
  if(!actionOK($ctype)) return false;

  switch($type){
    case 'article':
	  //$ID = ereg_replace('^talk:','Talk:',$ID);
      if (ereg('^talk:', $ID))
      	tpl_link(wl(ereg_replace('^talk:','',$ID)),$pre.$lang['btn_article'].$suf,'class="action article" accesskey="v" rel="nofollow"');
      else
      	tpl_link(wl($ID),$pre.$lang['btn_article'].$suf,'class="action article" accesskey="v" rel="nofollow"');
      return true;
    case 'discussion':
      if (ereg('^talk:', $ID)) {
        tpl_link(wl($ID),$pre.$lang['btn_discussion'].$suf,'class="action discussion" style="margin-right:40px;" accesskey="d" rel="nofollow"');
      } else {
        tpl_link(wl('talk:'.$ID),$pre.$lang['btn_discussion'].$suf,'class="action" style="margin-right:40px;" accesskey="d" rel="nofollow"');
      }
      return true;
    default:
      print '[unknown link type]';
      return true;
  }
}