<?php
/**
 * DokuWiki Plugin jstweak (Admin Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author Volker Bürckel <volker@vrbdev.eu>
 */

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

class admin_plugin_jstweak extends DokuWiki_Admin_Plugin {

    /**
     * Should carry out any processing required by the plugin.
     * Not needed here. Admin Component is for information output only.
    public function handle() {
    }
    */

    /** 
     * Render HTML output: Show list of replacement scripts or display an error.
     */
    public function html() {
        global $ID;
        $tweak_dir = $this->getConf('local_script_dir');

        // check if dir is set in config
        if (empty($tweak_dir)) {
            echo '<h1>'.$this->getLang('error').'</h1>';
            echo '<p>'.$this->getLang('please_set_local_script_dir').'</p>';
        }
        // check if tweak_dir is a directory in the filesystem
        elseif (!is_dir(DOKU_CONF . $tweak_dir)) {
            echo '<h1>'.$this->getLang('error').'</h1>';
            echo '<p>'.$this->getLang('please_create_local_script_dir').'<br/>';
            echo '<i>'.DOKU_CONF.$tweak_dir.'</i></p>';
        }
        // list all plugin names with tweak scripts available
        else {
            echo '<h1>'.$this->getLang('menu').'</h1>';
            if (!str_ends_with($tweak_dir, '/')) {
                $tweak_dir .= '/';
            }
            $count = 0;
            echo '<ul>';
            $plugins = array_diff(scandir(DOKU_CONF . $tweak_dir), array('.', '..'));
            foreach ($plugins as $plugin) {
                if ($count == 0) {
                    echo '<ul>';
                }
                // check if there's a script.js file in the plugin's tweak directory
                if (file_exists(DOKU_CONF . $tweak_dir . $plugin . '/script.js')) {
                    echo '<li>'.$plugin.'</li>';
                    $count ++;
                }

            }
            if ($count == 0) {
                echo '<p><i>./.</i></p>';
            }
            else {
                echo '</ul>';
            }
        }
        // show status
        if ($this->getConf('active')) {
            echo '<p><b>'.$this->getLang('is_active').'</b></p>';
        }
        else {
            echo '<p><b>'.$this->getLang('is_inactive').'</b></p>';
        }
        // provide link to plugin's own config area
        $config_url = wl('', ['do'=>'admin', 'page'=>'config']);
        echo '<a href="'.$config_url.'#plugin____jstweak____plugin_settings_name">[&neArr;CONFIG]</a>';
    }
}