1<?php
2
3class admin_plugin_move_tree extends DokuWiki_Admin_Plugin {
4
5    const TYPE_PAGES = 1;
6    const TYPE_MEDIA = 2;
7
8    /**
9     * @param $language
10     * @return bool
11     */
12    public function getMenuText($language) {
13        return false; // do not show in Admin menu
14    }
15
16
17    /**
18     * If this admin plugin is for admins only
19     *
20     * @return bool false
21     */
22    function forAdminOnly() {
23        return false;
24    }
25
26    /**
27     * no-op
28     */
29    public function handle() {
30    }
31
32    public function html() {
33        global $ID;
34
35        echo $this->locale_xhtml('tree');
36
37        echo '<noscript><div class="error">' . $this->getLang('noscript') . '</div></noscript>';
38
39        echo '<div id="plugin_move__tree">';
40
41        echo '<div class="tree_root tree_pages">';
42        echo '<h3>' . $this->getLang('move_pages') . '</h3>';
43        $this->htmlTree(self::TYPE_PAGES);
44        echo '</div>';
45
46        echo '<div class="tree_root tree_media">';
47        echo '<h3>' . $this->getLang('move_media') . '</h3>';
48        $this->htmlTree(self::TYPE_MEDIA);
49        echo '</div>';
50
51        /** @var helper_plugin_move_plan $plan */
52        $plan = plugin_load('helper', 'move_plan');
53        echo '<div class="controls">';
54        if($plan->isCommited()) {
55            echo '<div class="error">' . $this->getLang('moveinprogress') . '</div>';
56        } else {
57            $form = new Doku_Form(array('action' => wl($ID), 'id' => 'plugin_move__tree_execute'));
58            $form->addHidden('id', $ID);
59            $form->addHidden('page', 'move_main');
60            $form->addHidden('json', '');
61            $form->addElement(form_makeCheckboxField('autoskip', '1', $this->getLang('autoskip'), '', '', ($this->getConf('autoskip') ? array('checked' => 'checked') : array())));
62            $form->addElement('<br />');
63            $form->addElement(form_makeCheckboxField('autorewrite', '1', $this->getLang('autorewrite'), '', '', ($this->getConf('autorewrite') ? array('checked' => 'checked') : array())));
64            $form->addElement('<br />');
65            $form->addElement('<br />');
66            $form->addElement(form_makeButton('submit', 'admin', $this->getLang('btn_start')));
67            $form->printForm();
68        }
69        echo '</div>';
70
71        echo '</div>';
72    }
73
74    /**
75     * print the HTML tree structure
76     *
77     * @param int $type
78     */
79    protected function htmlTree($type = self::TYPE_PAGES) {
80        $data = $this->tree($type);
81
82        // wrap a list with the root level around the other namespaces
83        array_unshift(
84            $data, array(
85                        'level' => 0, 'id' => '*', 'type' => 'd',
86                        'open'  => 'true', 'label' => $this->getLang('root')
87                   )
88        );
89        echo html_buildlist(
90            $data, 'tree_list idx',
91            array($this, 'html_list'),
92            array($this, 'html_li')
93        );
94    }
95
96    /**
97     * Build a tree info structure from media or page directories
98     *
99     * @param int    $type
100     * @param string $open The hierarchy to open FIXME not supported yet
101     * @param string $base The namespace to start from
102     * @return array
103     */
104    public function tree($type = self::TYPE_PAGES, $open = '', $base = '') {
105        global $conf;
106
107        $opendir = utf8_encodeFN(str_replace(':', '/', $open));
108        $basedir = utf8_encodeFN(str_replace(':', '/', $base));
109
110        $opts = array(
111            'pagesonly'  => ($type == self::TYPE_PAGES),
112            'listdirs'   => true,
113            'listfiles'  => true,
114            'sneakyacl'  => $conf['sneaky_index'],
115            'showmsg'    => false,
116            'depth'      => 1,
117            'showhidden' => true
118        );
119
120        $data = array();
121        if($type == self::TYPE_PAGES) {
122            search($data, $conf['datadir'], 'search_universal', $opts, $basedir);
123        } elseif($type == self::TYPE_MEDIA) {
124            search($data, $conf['mediadir'], 'search_universal', $opts, $basedir);
125        }
126
127        return $data;
128    }
129
130    /**
131     * Item formatter for the tree view
132     *
133     * User function for html_buildlist()
134     *
135     * @author Andreas Gohr <andi@splitbrain.org>
136     */
137    function html_list($item) {
138        $ret = '';
139        // what to display
140        if(!empty($item['label'])) {
141            $base = $item['label'];
142        } else {
143            $base = ':' . $item['id'];
144            $base = substr($base, strrpos($base, ':') + 1);
145        }
146
147        if($item['id'] == '*') $item['id'] = '';
148
149        if ($item['id']) {
150            $ret .= '<input type="checkbox" /> ';
151        }
152
153        // namespace or page?
154        if($item['type'] == 'd') {
155            $ret .= '<a href="' . $item['id'] . '" class="idx_dir">';
156            $ret .= $base;
157            $ret .= '</a>';
158        } else {
159            $ret .= '<a class="wikilink1">';
160            $ret .= noNS($item['id']);
161            $ret .= '</a>';
162        }
163
164        if($item['id']) $ret .= '<img class="rename" src="'. DOKU_BASE .'lib/plugins/move/images/rename.png" />';
165        else $ret .= '<img class="add" src="' . DOKU_BASE . 'lib/plugins/move/images/folder_add.png" />';
166
167        return $ret;
168    }
169
170    /**
171     * print the opening LI for a list item
172     *
173     * @param array $item
174     * @return string
175     */
176    function html_li($item) {
177        if($item['id'] == '*') $item['id'] = '';
178
179        $params          = array();
180        $params['class'] = ' type-' . $item['type'];
181        if($item['type'] == 'd') $params['class'] .= ' ' . ($item['open'] ? 'open' : 'closed');
182        $params['data-name']   = noNS($item['id']);
183        $params['data-id']     = $item['id'];
184        $attr                  = buildAttributes($params);
185
186        return  "<li $attr>";
187    }
188
189}
190