<?php
/**
 * @author     Thomas Meyer
 * @licence    http://creativecommons.org/licenses/by-nc-sa/2.5/
 * @package    yana
 * @subpackage plugins
 */

/**
 * @access     public
 * @package    yana
 * @subpackage plugins
 */
class plugin_config extends plugin
{

    /**
     * Default event handler
     *
     * intentionally left blank
     *
     * @access  public
     * @name    plugin_config::_default()
     * @param   string  $event  name of the called event in lower-case
     * @param   array   $ARGS   array of params passed to the function
     * @return  bool
     *
     * @ignore
     */
    function _default($event, $ARGS)
    {
        /* check input data */
        assert('is_string($event);');
        assert('is_array($ARGS);');
        settype($event, "string");
        settype($ARGS, "array");

        /* global variables */
        global $YANA;

        /* do something */
        return true;
    }

    /**
     * create administration panel
     *
     * @access  public
     * @param   array  $ARGS  ignored
     * @return  bool
     */
    function index ($ARGS)
    {
        /* this function expects no arguments */

        global $YANA;

        /* require language file 'admin.config' */
        $YANA->language->read("admin");

        /* get list of all installed plugins */
        $PLUGINFILES = $YANA->plugins->getPluginList();

        /* create options for select-boxes */
        $YANA->setVar('LANGUAGEFILES',$YANA->language->getContent());
        $YANA->setVar('SKINFILES',    $YANA->skin->getContent());
        $YANA->setVar('PLUGINFILES', $PLUGINFILES);
        $YANA->setVar('PROFILES',    $YANA->registry->files['PROFILEDIR']->getContent());

        /* The "options" menu imports a html-file for each plugin.
         * To do so, this section provides the menu with the full path names.
         */
        $OPTIONFILES = array();
        $dir = $YANA->plugins->getPluginDir();
        foreach ($PLUGINFILES as $a => $b)
        {
            $b = $b['PLUGIN'];
            if (!empty($b['CONFIG_TEMPLATE']) && isset($b['ACTIVE']) && $b['ACTIVE'] > 0) {
                $OPTIONFILES[count($OPTIONFILES)] = $dir.$b['CONFIG_TEMPLATE'];
            }
        }
        $YANA->setVar("OPTIONFILES",$OPTIONFILES);
        
        return true;
    }

    /**
     * create a menu to edit default settings of the framework
     *
     * @access  public
     * @param   array  $ARGS  ignored
     * @return  bool
     */
    function get_config_default ($ARGS)
    {
        /* this function expects no arguments */

        global $YANA;

        /* require language file 'admin.config' */
        $YANA->language->read("admin");

        /* set a description text */
        $YANA->setVar("BESCHREIBUNG", $YANA->getVar("LANGUAGE.DESCR_ADMIN"));

        /* provide data for select-boxes */
        $YANA->setVar("LANGFILE",     $YANA->language->getContent());
        $YANA->setVar("SKINFILE",     $YANA->skin->getContent());

        /* check, if the output file is writeable */
        $YANA->setVar("WRITEABLE",    $YANA->registry->files['config']->is_writeable());

        return true;
    }

    /**
     * create a menu to edit profile settings
     *
     * Note: if no profile specific settings are available,
     * the framework automatically falls back to its default settings.
     *
     * @access  public
     * @param   array  $ARGS  ignored
     * @return  bool
     */
    function get_config_profile ($ARGS)
    {
        /* this function expects no arguments */

        global $YANA;
        $YANA->language->read("admin");
        $YANA->setVar("BESCHREIBUNG", $YANA->getVar("LANGUAGE.DESCR_MOD"));
        $YANA->setVar("LANGFILE",     $YANA->language->getContent());
        $YANA->setVar("SKINFILE",     $YANA->skin->getContent());
        $YANA->setVar("WRITEABLE",    $YANA->registry->files['config']->is_writeable());
        return true;
    }

    /**
     * save form data to the frameworks default configuration
     *
     * This function may be used by other plugins to provide configuration
     * menus with practically no coding. See manual cookbook for an example.
     *
     * @access  public
     * @param   array  $ARGS  user form data
     * @return  bool
     */
    function set_config_default ($ARGS)
    {
        assert('is_array($ARGS);');
        settype($ARGS, "array");
        global $YANA;

        /* first reinitialize the config file, so we can be sure it contains the most recent data */
        $YANA->registry->files['config']->init();
        /* then overwrite previous settings with new settings provided by the user form */
        $YANA->registry->files['config']->insert($this->_genAdminFile($ARGS));
        /* flush the template cache, so changes will become visible immediately */
        smartTemplate::clearCache();

        /* write changes to disk, if it fails, issue an error and provide a log entry. */
        if (!$YANA->registry->files['config']->failSafeWrite()) {
            $YANA->report( new log("Unable to write file", $YANA->registry->files['config']->getFilename()));
            return false;
        } else {
            return true;
        }
    }

    /**
     * save form data to the current profile's configuration
     *
     * This function may be used by other plugins to provide configuration
     * menus with practically no coding. See manual cookbook for an example.
     *
     * @access  public
     * @param   array  $ARGS  user form data
     * @return  bool
     */
    function set_config_profile ($ARGS)
    {
        assert('is_array($ARGS);');
        settype($ARGS, "array");
        global $YANA;
        $YANA->registry->files['config']->init();
        $YANA->registry->files['config']->insert($this->_genAdminFile($ARGS));
        smartTemplate::clearCache();

        if (!$YANA->registry->files['config']->failSafeWrite()) {
            $YANA->report( new log("Unable to write file", $YANA->registry->files['config']->getFilename()));
            return false;
        } else {
            return true;
        }
    }

    /**
     * Activate / deactivate plugins
     *
     * This computes the user form data provided by $_POST.
     * Where the provided key is the id of a plugin and the value
     * is wether bool(true) to activate or bool(false) to deactive
     * the plugin.
     *
     * Note: it does'nt matter here, if a plugin named $key really
     * exists, since the PluginManager-Class does this checking for us.
     *
     * @access  public
     * @param   array  $ARGS  ignored
     * @return  bool
     */
    function save_pluginlist ($ARGS)
    {
        global $YANA;
        if (empty($ARGS['pluginlist']) || !is_array($ARGS['pluginlist']) || (isset($ARGS['plugins']) && !is_array($ARGS['plugins']))) {
            $YANA->message('ALERT', 'UNGUELTIGE_EINGABE', 'index');
            return false;
        } else {
            foreach($ARGS['pluginlist'] as $plugin)
            {
                /* We don't mind, wether $plugin is plugin or not, since
                 * the PluginManager does this checking for us.
                 */
                if (in_array($plugin,$ARGS['plugins'])) {
                    $test = $YANA->plugins->setActive($plugin,1);
                } else {
                    $test = $YANA->plugins->setActive($plugin,0);
                }
                if ($test === false) {
                    $YANA->message('ALERT', 'UNGUELTIGE_EINGABE', 'index');
                    return false;
                }
            }
            /* save changes and refresh the plugin cache */
            return ($YANA->plugins->refreshPluginFile());
        }
    }

    /**
     * refresh the plugin cache
     *
     * Use this after you installed / uninstalled a plugin
     * to refresh the cache.
     *
     * @access  public
     * @param   array  $ARGS  ignored
     * @return  bool
     */
    function refresh_pluginlist ($ARGS)
    {
        /* this function expects no arguments */

        global $YANA;
        return $YANA->plugins->refreshPluginFile();
    }

    /**
     * event handler
     *
     * @access public
     * @param array $ARGS array of params passed to the function
     * @return boolean
     */
    function config_create_profile ($ARGS)
    {
        assert('is_array($ARGS);');
        settype($ARGS, "array");
        global $YANA;
        $configFile = $YANA->registry->get("config");
        $REF        = $configFile->get("*");
        $profileDir = $YANA->registry->get("PROFILEDIR");
        $profileDir = $profileDir->getFilename();
        $newProfile = new configfile($profileDir.$ARGS['id'].'.cfg');
        if ($newProfile->exists()) {
            $YANA->report(new error("allready_exists"));
            return false;
        } else if (!$newProfile->create()) {
            $YANA->setVar("FILE", $newProfile->getFilename());
            $YANA->report(new error("file_not_created"));
            return false;
        } else if (!$newProfile->set($REF)) {
            $YANA->report(new error("500"));
            return false;
        } else if (!$newProfile->write()) {
            $YANA->setVar("FILE", $newProfile->getFilename());
            $YANA->report(new error("not_writeable"));
            return false;
        } else {
            return true;
        }
    }

    /**
     * event handler
     *
     * @access public
     * @param array $ARGS array of params passed to the function
     * @return boolean
     */
    function config_usermode ($ARGS)
    {
        assert('is_array($ARGS);');
        settype($ARGS, "array");
        global $YANA;
        $configFile = $YANA->registry->get("config");
        $REF        = $configFile->get("*");
        $REF["PROFILE"]["USERMODE"] = !$REF["PROFILE"]["USERMODE"];
        if (!$configFile->insert($REF) || !$configFile->write()) {
            $YANA->report( new log("Unable to write file", $configFile->getFilename()));
            return false;
        } else {
            return true;
        }
        return false;
    }

    /**
     * event handler
     *
     * @access public
     * @param array $ARGS array of params passed to the function
     * @return boolean
     */
    function about ($ARGS)
    {
        assert('is_array($ARGS);');
        settype($ARGS, "array");
        global $YANA;
        $YANA->language->read("admin");
        if (empty($ARGS['type']) || empty($ARGS['target'])) {
            return false;
        }
        $info = array();
        switch ($ARGS['type']) 
        {
            case "plugin":
                $info = $YANA->plugins->getInfo($ARGS['target']);
            break;
            case "skin":
                $info = $YANA->skin->getInfo($ARGS['target']);
            break;
            case "language":
                $info = $YANA->language->getInfo($ARGS['target']);
            break;
            default:
                return false;
            break;
        }
        if (!empty($info)) {
            $YANA->setVar("INFO",$info);
            return true;
        } else {
            return false;
        }
    }

    /**
     * _genAdminFile
     *
     * array _genAdminFile(array $input)
     *
     * @access private
     * @param array $input
     * @return array
     * @ignore
     */
    function _genAdminFile($input)
    {
        settype($input, "array");
        $input = changeCaseArray($input,CASE_UPPER);

        unset($input['ID']);
        unset($input['ACTION']);
        unset($input['TARGET']);
        unset($input[strtoupper(session_name())]);

        $output = array();
        foreach ($input as $key=>$element)
        {
            $element = strip_tags( (string) $element);
            if (preg_match("/\//", $key)) {
                $key = preg_replace("/\//", ".", $key);
                Hashtable::set($output, $key, $element);
            } else {
                $output[$key] = $element;
            }

        }

        return array('PROFILE' => $output);
    }

}

// © 2006, Thomas Meyer www.All-Community.de
// Copyright Hinweis nicht entfernen!
?>