<?php
/**
 *
 * @license   GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author    Tom N Harris <tnharris@whoopdedo.org>
 */

// must be run within DokuWiki
if(!defined('DOKU_INC')) die();

class action_plugin_formatplus extends DokuWiki_Action_Plugin {

  /**
   * register the eventhandlers
   */
  function register(&$contr){
    $contr->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'toolbar_event', array());
  }

  /**
   *
   * @author  Tom N Harris    <tnharris@whoopdedo.org>
   */
  function toolbar_event(&$event, $param){
    $disabled = explode(',',$this->getConf('disable_syntax'));
    $disabled = array_map('trim',$disabled);
    $buttons = array();
    if (!in_array('smallcaps', $disabled))
      $buttons[] = array(
          'type'  => 'format',
          'title' => $this->getLang('smallcaps'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/caps.png',
          'open'  => '!!',
          'close' => '!!',
          'block' => false
          );
    if (!in_array('sample', $disabled))
      $buttons[] = array(
          'type'  => 'format',
          'title' => $this->getLang('sample'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/samp.png',
          'open'  => '$$',
          'close' => '$$',
          'block' => false
          );
    if (!in_array('variable', $disabled))
      $buttons[] = array(
          'type'  => 'format',
          'title' => $this->getLang('variable'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/var.png',
          'open'  => '??',
          'close' => '??',
          'block' => false
          );
    if (!in_array('keyboard', $disabled))
      $buttons[] = array(
          'type'  => 'format',
          'title' => $this->getLang('keyboard'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/kbd.png',
          'open'  => '``',
          'close' => '``',
          'block' => false
          );
    if (!in_array('definition', $disabled))
      $buttons[] = array(
          'type'  => 'format',
          'title' => $this->getLang('definition'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/dfn.png',
          'open'  => '@@',
          'close' => '@@',
          'block' => false
          );
    if (!in_array('citation', $disabled))
      $buttons[] = array(
          'type'  => 'format',
          'title' => $this->getLang('citation'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/cite.png',
          'open'  => '&&',
          'close' => '&&',
          'block' => false
          );
    if (!in_array('inverse', $disabled))
      $buttons[] = array(
          'type'  => 'format',
          'title' => $this->getLang('inverse'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/inv.png',
          'open'  => '/!',
          'close' => '!/',
          'block' => false
          );
    if (!in_array('quote', $disabled)) {
      $buttons[] = array(
          'type'  => 'format',
          'title' => $this->getLang('quote'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/q.png',
          'open'  => '""',
          'close' => '""',
          'block' => false
          );
    }
    $buttons2 = array();
    if (!in_array('blockquote', $disabled))
      $buttons2[] = array(
          'type'  => 'format',
          'title' => $this->getLang('blockquote'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/quote.png',
          'open'  => '<quote >',
          'close' => '</quote>',
          'block' => true
          );
    if (!in_array('ins_del', $disabled)) {
      $buttons2[] = array(
          'type'  => 'format',
          'title' => $this->getLang('insert'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/ins.png',
          'open'  => '/+',
          'close' => '+/',
          'block' => false
          );
      $buttons2[] = array(
          'type'  => 'format',
          'title' => $this->getLang('blockinsert'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/insert.png',
          'open'  => '<ins >',
          'close' => '</ins>',
          'block' => true
          );
      $buttons2[] = array(
          'type'  => 'format',
          'title' => $this->getLang('delete'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/del.png',
          'open'  => '/-',
          'close' => '-/',
          'block' => false
          );
      $buttons2[] = array(
          'type'  => 'format',
          'title' => $this->getLang('blockdelete'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/delete.png',
          'open'  => '<del >',
          'close' => '</del>',
          'block' => true
          );
    }
    if (!in_array('super_sub', $disabled)) {
      $buttons2[] = array(
          'type'  => 'format',
          'title' => $this->getLang('sub'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/sub.png',
          'open'  => '/,',
          'close' => ',/',
          'block' => false
          );
      $buttons2[] = array(
          'type'  => 'format',
          'title' => $this->getLang('super'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/super.png',
          'open'  => '/^',
          'close' => '^/',
          'block' => false
          );
    }
    $menu =& $event->data;
    if ($this->getConf('toplevel')) {
      $menu = array_merge($menu, $buttons);
      $menu[] = array(
          'type'  => 'picker',
          'title' => $this->getLang('title'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/formatplus.png',
          'list'  => $buttons2,
          'block' => true
          );
    } else {
      $menu[] = array(
          'type'  => 'picker',
          'title' => $this->getLang('title'),
          'icon'  => DOKU_BASE.'lib/plugins/formatplus/images/formatplus.png',
          'list'  => array_merge($buttons, $buttons2),
          'block' => true
          );
    }
  }

}
