<?php/**
 * Yana Plugin "[%$ID%]"
 *
 * [%$BESCHREIBUNG%]
 *
 * @author      [%$AUTOR%]
[%if $VERSION%] * @version     [%$VERSION%]
[%/if%] *
 * @package     plugin
[%if $PACKAGE%] * @subpackage  [%$PACKAGE%]
[%/if%] */

/**
 * «plugin» class "plugin_[%$ID%]"
 *
 * @access      public
 * @package     plugin
[%if $PACKAGE%] * @subpackage  [%$PACKAGE%]
[%/if%] */
class plugin_[%$ID%] extends plugin
{

[%if $DATABASE.TABLES%]
    /**
     * Connection to data source (API)
     *
     * Is created by a factory function and can be either a connection to "some" database or a text-file,
     * depending on user settings. The correct type will be determined automatically. Take a look at the
     * documentation of class 'bufferedDbStream' for details on this API. (all alternative classes implement
     * the same interface, regardless whether the source is a file or a databse)
     *
     * {@internal
     *
     * NOTE:
     * As of PHP 5 the notation "var $database" became deprecated, resulting in an E_STRICT_NOTICE.
     * So you may want to change this to "private $database" for PHP 5. Just be aware that doing so will
     * break backwards compatibility with PHP 4.
     *
     * }
     *
     * @access  private
     * @var     object serializeableObject  Database-API with Query-Builder (also works with text-files)
     *
     * @ignore
     */
    var $database;
[%/if%]

    /**
     * Constructor
     *
     * @access  public
     * @name    plugin_[%$ID%]::__construct()
     * @param   string  $name  a name for this plugin
     */
    function plugin_[%$ID%]($name)
    {
        assert('is_string($name);');

        $this->name = $name;
[%if $DATABASE.TABLES%]
        global $YANA;
        $this->database = $YANA->connect("[%$ID%]");
[%/if%]
    }

    /**
     * Default event handler
     *
     * returns bool(true) on success and bool(false) on error
     *
     * @access  public
     * @return  bool
     * @name    plugin_[%$ID%]::_default()
     * @param   string  $event  name of the called event in lower-case
     * @param   array   $ARGS   array of arguments passed to the function
     */
    function _default($event, $ARGS)
    {
        /* NOTE:
         * Fnction "assert()" is a helper for debugging your plugins.
         * It evaluates the given string as an PHP-expression and issues
         * an E_NOTICE to screen / log-file if it evaluates to bool(false).
         *
         * "Assertions" can be turned off if your application has reached
         * production stage by setting the appropriate debug-constant in
         * index.php
         */

        /* check input data */
        assert('is_string($event);');
        assert('is_array($ARGS);');
        settype($event, "string");
        settype($ARGS, "array");
        $event = strtolower($event);
        
        /* global variables */
        global $YANA;

[%if $DATABASE.TABLES%]
        $YANA->setVar('DATABASE', $this->database->structure->getStructure());
[%/if%]
        /* do something */

        /* NOTE: event handlers should always return a boolean value.
         * Return "true" on success or "false" on error.
         */
        return true;
    }

/* BEGIN custom Event-handlers */

    /* NOTE:
     * All member-functions stated here act as action handlers
     * (event handlers) and may be called directly in a browser
     * by typing index.php?action=function_name
     *
     * (you may exclude a single function from this behaviour by
     * starting it's name with an underscore '_')
     *
     * While it is not required by default, it is thus highly
     * recommended that you specify all functions within
     * the "INTERFACE"-section of your plugin.config.
     * Refer to the manual for details.
     *
     * For security reasons you may want to override this behaviour
     * to deny access by default if no "INTERFACE"-section AND
     * "INTERFACE.function_name.PERMISSION" entry is provided
     * by setting "DEFAULT.EVENT.PERMISSION" to "-1" in the
     * config/system.config file.
     * Note that this might break up some plugins!
     * As an alternative: setting the value to "1" will require
     * users to be at least registered and logged in to be able
     * to call "unprotected" functions.
     */

[%foreach key="NAME" item="CURRENT" from=$INTERFACE%]

    /**
     * [%$NAME%]
     *
     * returns bool(true) on success and bool(false) on error
     *
     * Type:        [%$CURRENT.TYPE%]
     * Permission:  [%$CURRENT.PERMISSION%]
     * Templates:   [%if $CURRENT.TEMPLATE%][%$CURRENT.TEMPLATE%] [%/if%][%$CURRENT.INSERT%]
     *
     * @access  public
     * @return  bool
     * @name    plugin_[%$ID%]::[%$NAME%]()
     * @param   array  $ARGS  array of params passed to the function
     */
    function [%$NAME%] ($ARGS)
    {
        /* check input data */
        assert('is_array($ARGS);');
        settype($ARGS, "array");

        /* global variables */
        global $YANA;

        /* do something */
        return true;
    }

[%/foreach%]

/* END custom Event-handlers */

/* BEGIN auto-generated Event-handlers */

[%foreach key="NAME" item="CURRENT" from=$AUTO_INTERFACE%]
[%assign var="NAME" value=$NAME|lower%]

    /**
     * [%$NAME%]
     *
     * returns bool(true) on success and bool(false) on error
     *
     * Type:        [%$CURRENT.TYPE%]
     * Permission:  [%$CURRENT.PERMISSION%]
     * Templates:   [%if $CURRENT.TEMPLATE%][%$CURRENT.TEMPLATE%] [%/if%][%$CURRENT.INSERT%]
     *
     * @access  public
     * @return  bool
     * @name    plugin_[%$ID%]::[%$NAME%]()
     * @param   array  $ARGS  array of params passed to the function
     */
    function [%$NAME%] ($ARGS)
    {
        /* check input data */
        assert('is_array($ARGS);');
        settype($ARGS, "array");

        /* global variables */
        global $YANA;

[%if $CURRENT.PRESET == "new" && $CURRENT.TYPE == "write"%][%assign var="TABLE" value=$CURRENT.TABLE|upper%]
       /* build new entry */
       $new_entry = array();
[%foreach key="CURRENT_NAME" item="CURRENT_FIELD" from=$DATABASE.TABLES.$TABLE.CONTENT%][%if (!$CURRENT_FIELD.HIDDEN) && (!$CURRENT_FIELD.PROTECTED) && ($CURRENT_FIELD.DISPLAY.HIDDEN !== true ) && (!$CURRENT_FIELD.DISPLAY.HIDDEN.NEW) && (!$CURRENT_FIELD.DISPLAY.READONLY.NEW) && $CURRENT_NAME|upper != $DATABASE.TABLES.$TABLE.PRIMARY_KEY|upper%][%assign var="CURRENT_NAME" value=$CURRENT_NAME|lower%]
[%if $CURRENT_FIELD.TYPE == "time" %]
        $new_entry['[%$CURRENT_NAME%]'] = DisplayUtility::toTimestamp('[%$CURRENT_NAME%]_', $ARGS);
[%else%]
        if (!empty($ARGS['[%$CURRENT_NAME%]'])) {
            $new_entry['[%$CURRENT_NAME%]'] = $ARGS['[%$CURRENT_NAME%]'];
        }
[%/if%]
[%/if%][%/foreach%]
         plugin_[%$ID%]::_untaintInput($new_entry);
        /* insert new entry into table */
        if (!$this->database->insert("[%$CURRENT.TABLE|lower%].*",$new_entry)) {
            $YANA->report(new warning('ungueltige_eingabe'));
            return false;
        }
        return $this->database->write();
[%elseif $CURRENT.PRESET == "delete"%]
        /* check if user forgot to mark at least 1 row */
        if (empty($ARGS['selected_entries']) || !is_array($ARGS['selected_entries'])) {
            $YANA->report(new warning('ungueltige_eingabe'));
            return false;
        }
        /* remove entry from database */
        foreach ($ARGS['selected_entries'] as $id)
        {
            if (!$this->database->remove("[%$CURRENT.TABLE|lower%].${id}")) {
                /* entry does not exist */
                $YANA->report(new warning('ungueltige_eingabe'));
                return false;
            }
        } /* end for */
        /* commit changes */
        return $this->database->write();
[%elseif $CURRENT.PRESET == "edit" && $CURRENT.TYPE == "write"%][%assign var="TABLE" value=$CURRENT.TABLE|upper%]
        /* check if user forgot to mark at least 1 row */
        if (empty($ARGS['selected_entries']) || !is_array($ARGS['selected_entries'])) {
            $YANA->report(new warning('ungueltige_eingabe'));
            return false;
        }
        foreach ($ARGS['selected_entries'] as $id)
        {
            /* build new entry */
            $new_entry = array();
[%foreach key="CURRENT_NAME" item="CURRENT_FIELD" from=$DATABASE.TABLES.$TABLE.CONTENT%][%if (!$CURRENT_FIELD.HIDDEN) && (!$CURRENT_FIELD.PROTECTED) && ($CURRENT_FIELD.DISPLAY.HIDDEN !== true ) && (!$CURRENT_FIELD.DISPLAY.HIDDEN.EDIT) && (!$CURRENT_FIELD.DISPLAY.READONLY.EDIT)%][%assign var="CURRENT_NAME" value=$CURRENT_NAME|lower%]
[%if $CURRENT_FIELD.TYPE == "time" %]
            $new_entry['[%$CURRENT_NAME%]'] = DisplayUtility::toTimestamp("id_${id}_[%$CURRENT_NAME%]_", $ARGS);
[%else%]
            if (!empty($ARGS["id_${id}_[%$CURRENT_NAME|lower%]"])) {
                $new_entry['[%$CURRENT_NAME%]'] = $ARGS["id_${id}_[%$CURRENT_NAME|lower%]"];
            }
[%/if%]
[%/if%][%/foreach%]
            /* before doing anything, check if entry exists */
            if (!$this->database->exists("[%$CURRENT.TABLE|lower%].${id}")) {
                /* no such entry */
                $YANA->report(new warning('ungueltige_eingabe'));
                return false;
            /* update the row */
            } else if (!$this->database->insert("[%$CURRENT.TABLE|lower%].${id}", $new_entry)) {
                /* unable to perform update - possibly readonly */
                return false;
            }
        } /* end for */
        /* commit changes */
        return $this->database->write();
[%elseif $CURRENT.PRESET == "search"%]
        return true;
[%elseif $CURRENT.TEMPLATE === "MESSAGE" %]
        $YANA->message("OK", "200"[%if $CURRENT.TYPE == "config" %], "index"[%/if%]);
        return true;
[%else%]
        /* do something */
        return true;
[%/if%]
    }

[%/foreach%]

/* END auto-generated Event-handlers */

/* BEGIN auto-generated functions */
[%if $DATABASE.TABLES%]

    /* NOTE:
     * By convention you can hide any member function from the plugin manager
     * by using an underscore '_' as the first character of the function's name.
     * This results in: any such function (event) can not be triggered directly
     * by the user through pointing the "action" parameter in the url to that
     * function name, like: index.php?action=function_name
     *
     * (Using underscores is a common PHP4 notation for "private")
     *
     * This also means, if for some reason you need to create a public event handler
     * whose name somehow has to begin with an underscore, you need to declare this
     * within the class' default event handler.
     */

    /**
     * remove tags from user form data
     *
     * @static
     * @access  private
     * @name    plugin_[%$ID%]::_untaintInput()
     * @param   array   $input  associative array containing user form data
     *
     * @ignore
     */
    function _untaintInput(&$input)
    {
        if (is_array($input)) {
            foreach ($input as $a => $b)
            {
                $input[$a] = untaintInput($b, "string", 0, YANA_ESCAPE_USERTEXT);
            }
        }
    }

[%/if%]
/* END auto-generated functions */

}
?>