1<?php
2/**
3 * Flowchartjs Admin Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Hua Gao <ghbore@gmail.com>
7 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
11
12require_once(DOKU_PLUGIN . 'admin.php');
13
14/**
15 * All DokuWiki plugins to extend the admin function
16 * need to inherit from this class
17 */
18class admin_plugin_flowchartjs extends DokuWiki_Admin_Plugin {
19
20    /**
21     * Carry out required processing
22     */
23    public function handle() {
24    	if (!isset($_FILES['_new']) && !isset($_POST['_del'])) return;
25        if (! checkSecurityToken()) return;
26	if (isset($_FILES['_new']) && $_FILES['_new']['error'] == 0){
27		if ('json' != pathinfo($_FILES['_new']['name'], PATHINFO_EXTENSION)){
28			msg($_FILES['_new']['name'].' is not a json file', 2);
29		}else {
30			move_uploaded_file($_FILES['_new']['tmp_name'],
31				DOKU_PLUGIN.'flowchartjs/styles/'.$_FILES['_new']['name']);
32			msg($_FILES['_new']['name'].' has been successfully uploaded', 1);
33		}
34	}
35	if (isset($_POST['_del'])){
36		foreach ($_POST['_del'] as $s){
37			if (unlink(DOKU_PLUGIN.'flowchartjs/styles/'.$s.'.json')){
38				msg($s." has been successfully deleted", 1);
39			}else {
40				msg('It\'s failed to delete '.$s, 2);
41			}
42		}
43	}
44    }
45
46    /**
47     * Output html of the admin page
48     */
49    public function html() {
50    	echo $this->locale_xhtml('intro');
51        $form = new Doku_Form(array('method'=>'post', 'enctype'=>'multipart/form-data'));
52	$form->addElement( form_makeFileField(
53		'_new', 'Add new or update flowchart style (in JSON format)'
54	));
55	$form->addElement('<div>Or/and delete styles:</div>');
56	$styles = array_map(function ($fn){return pathinfo($fn, PATHINFO_FILENAME);},
57			glob(DOKU_PLUGIN.'flowchartjs/styles/*.json')
58		);
59	foreach ($styles as $s){
60		$form->addElement( form_makeCheckboxField(
61			'_del[]', $s, $s
62		));
63		$form->addElement('<br />');
64	}
65	$form->addElement( form_makeButton('submit', 'admin', 'Update') );
66	$form->printForm();
67    }
68
69    /**
70     * Return true for access only by admins (config:superuser) or false if managers are allowed as well
71     *
72     * @return bool
73     */
74    public function forAdminOnly() {
75        return false;
76    }
77}
78
79//Setup VIM: ex: et ts=4 :
80