1<?php
2/**
3*    @name action plugin toolbuttondel
4*    @description deletes toolbar buttons selected from configuration manager
5*    @author Myron Turner <turnermm02@shaw.ca>
6*/
7if (!defined('DOKU_INC')) die();
8class action_plugin_toolbuttondel extends DokuWiki_Action_Plugin {
9    private $select_head = array();
10    private $key_type_chars  = array();  // will be merged with autoheads
11    private $misc_icons = array();
12
13    function register(Doku_Event_Handler $controller) {
14        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'delete_buttons', array ());
15    }
16
17
18    function delete_buttons(& $event, $param) {
19       $this->parse_options($select_picker);
20       $this->formats($event);
21       $this->select_headers($event,$select_picker);
22       $this->pickers($event);
23       $this->links($event);
24    }
25
26 function links(& $event) {
27     $which= $this->getConf('links');
28     $excludes = explode(',',$which);
29     for($i=0; $i<count($event->data); $i++) {
30           switch ($event->data[$i]['type'])
31           {
32                 case 'format':
33                     if(!in_array ('external' , $excludes)) break;
34                     if(isset($event->data[$i]['open']) && $event->data[$i]['open']=='[[')
35                         unset($event->data[$i]);
36                         break;
37               case 'linkwiz':
38                     if(!in_array ('internal' , $excludes)) break;
39                         unset($event->data[$i]);
40                         break;
41               case 'mediapopup':
42                     if(!in_array ('media' , $excludes)) break;
43                     if(strpos($event->data[$i]['url'],'mediamanager') !== false) {
44                         unset($event->data[$i]);
45                   }
46                   break;
47           }
48     }
49    }
50
51function parse_options(&$select_picker = false) {
52    $auto_headers = array('same'=>'8','higher'=>'0','lower'=>'9');
53    $key_codes = array(
54       'bold' =>'b',
55       'italic' =>'i',
56       'underline' =>'u',
57       'code' =>'c',
58       'delete' =>'d',
59       'ordered list' =>'-',
60       'unordered list' =>'.',
61       'signature' =>'y'
62   );
63    $misc_names = array('smileys'=>'smiley', 'special'=>'chars','horizontal' =>'hr');
64
65    $this->key_type_chars=$this->get_array('key_types',$key_codes);
66
67    $misc = $this->getConf('misc');
68    $misc = explode(',',$misc);
69    $_misc = array();
70    for($i = 0; $i <count($misc); $i++) {
71       $a = explode(' ', $misc[$i]);
72       $_misc[] = $a[0];
73    }
74
75    $this->misc_icons=$this->get_array($_misc,$misc_names);
76
77    $headers=array();
78    $autohead=$this->get_array('headers',$auto_headers,$headers);
79    $this->key_type_chars = array_merge($this->key_type_chars,$autohead);
80
81    if(!$headers) return;
82    if(in_array('select',$headers)) {
83       $select_picker = true;
84    }
85    for($i=0; $i<=5; $i++) {
86       if(in_array($i,$headers)) {
87           $this->select_head[] = $i;
88       }
89    }
90
91}
92
93function get_array($conf,$cmp, &$tmp=false) {
94   $ar = array();
95    if(!is_array($conf)) {
96        $str = $this->getConf($conf);
97        $conf = explode(',',$str);
98    }
99
100   foreach($cmp  as $name=>$val) {
101       if(in_array($name, $conf)){
102          $ar[] = $val;
103       }
104    }
105
106   if(is_array($tmp)) $tmp = $conf;
107   return $ar;
108}
109
110function pickers(& $event) {
111       if(!$this->misc_icons) return;
112       $ckeys =  preg_quote(implode(';',$this->misc_icons));
113       $ckeys = str_replace(';','|',$ckeys);
114
115
116        for($i=0; $i<count($event->data); $i++) {
117        if(preg_match('/'. $ckeys . '/',$event->data[$i]['icon'])) {
118           unset($event->data[$i]);
119         }
120     }
121}
122
123 /*
124   checks all formats with keys including autoheads, except for the
125   selected headlines, each of which is a separate array under a sub array to the Select Headling picker,
126   which is named 'list'
127 */
128function formats(& $event) {
129       if(!$this->key_type_chars) return;
130       $ckeys =  preg_quote(implode(';',$this->key_type_chars));
131       $ckeys = str_replace(';','|',$ckeys);
132       $tmp = array();
133        for($i=0; $i<count($event->data); $i++) {
134            if($ckeys && !preg_match('/'. $ckeys .'/' ,$event->data[$i]['key'])) {
135           $tmp[] = $event->data[$i];
136          }
137
138       }
139
140       $event->data = $tmp;
141
142}
143
144 function select_headers(& $event,$select=false) {
145    for($i=0; $i<count($event->data); $i++) {
146        if(array_key_exists('class', $event->data[$i]) && $event->data[$i]['class'] == 'pk_hl') {
147              if($select) {
148                   unset($event->data[$i]);
149                 }
150              else $this->check_selheader_keys($event->data[$i]['list']);
151            break;
152        }
153   }
154 }
155
156/*
157Assigns to new array because original keeps moving when unset is used on original
158*/
159function check_selheader_keys(& $list) {
160   if(!$this->select_head) return;
161   $tmp = array();
162   for($i=0; $i<count($list); $i++) {
163       if(!in_array($list[$i]['key'],$this->select_head)) {
164          $tmp[] =   $list[$i];
165       }
166   }
167
168   $list = $tmp;
169
170}
171
172function write_debug($data) {
173  return;
174  if (!$handle = fopen(DOKU_INC .'toolbar.txt', 'a')) {
175    return;
176    }
177  if(is_array($data)) {
178     $data = print_r($data,true);
179  }
180    // Write $somecontent to our opened file.
181    fwrite($handle, "$data\n");
182    fclose($handle);
183
184}
185
186}