﻿<?php
/**
 * Database Management functions 
 * There is a dokuwiki folder called db and selecting this namespace triggers the DB plugin
 *
 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author   Langham Associates <enquiries@langhamAssociates.com>
 *
 * v1.0.4
 */
 // Folder called database required and selecting this namespace triggers DB plugin

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
// The code secific to the  db plugin
require_once('database.php');

/**
 * All DokuWiki plugins to extend the action function
 * need to inherit from this class
 */
class action_plugin_database extends DokuWiki_Action_Plugin {

        function admin_plugin_acl(){
            $this->setupLocale();
        }
/**
 * return some info
 * 
 * this function is required by dokuwiki
 *
 */
    function getInfo(){
      return array(
        'author' => 'Langham Associates',
        'email'  => 'enquiries@langhamassociates.com',
        'date'   => '2008-04-06',
        'name'   => 'Database Manager',
        'desc'   => 'Manage your own Database within the wiki',
        'url'    => 'http://www.langhamassociates.com/wiki/doku.php?id=start',
      );
    }
/**
 * Register the functionality with dokuwiki
 * 
 * this function is required by dokuwiki
 *
 */
    function register(&$controller) {

        global $dbName;
        $dbName = 'database';
        $controller->register_hook('TPL_ACT_RENDER',
                                   'BEFORE',
                                    $this,
                                   'handle_render',
                                    array());

    }
/**
 *
 * Handle the hook declared before dokuwiki renders the body 
 *
 *
 */
    function handle_render(&$event, $param) {
        global $IDX;
        global $ACT;
        $ID=getID();
        global $dbName;
        global $environment;
        global $myLang;
        global $action;
        global $tableId;
        global $recordId;
        global $conf;
        global $lookupPath;
        global $dictionaryPath;
        $this->setupLocale();
        $myLang=$this->lang;
        $environment= new Environment();
        $command = substr($IDX,strlen($dbName)+1);
        $tableId = substr($command,0,strpos($command,'-')+1); // bypass the default if the following apply


        if (isset($_REQUEST['action']) || isset($_REQUEST['fn'])  // already in db code
           ||                                         // or
             (
             substr($IDX,0,strlen($dbName))==$dbName  // in db nameSpace
             &&                                       // and
             strlen($IDX)>strlen($dbName)             // selected a folder 
             )
              ) {
// There are two methods of determing the action
// 1. Using an icon with name='fn[action][param(s)]' and params separated by |
// 2. Using a form with hidden fields of action, table and record

    // Handle buttons and convert into actions
            if (isset($_REQUEST['fn'])) {
        // extract the command and any specific parameters
        // submit button name is of the form - fn[cmd][param(s)]
                $fn   = $_REQUEST['fn'];

                if (is_array($fn)) {
                    $action = key($fn);
                    $param = is_array($fn[$action]) ? key($fn[$action]) : null;
                    $elements = explode($environment->COLUMN_SEPARATOR, $param);
                    $tableId  = $elements[0];
                    $recordId = $elements[1];
                }
            } else {
// Pick up the action
                if (isset($_REQUEST["action"])) {
                    $action=$_REQUEST["action"];
// Pick up the table and record if they have been set
                    if (isset($_REQUEST["table"]))
                        $tableId  = $_REQUEST["table"];
                    if (isset($_REQUEST["record"]))
                        $recordId = $_REQUEST["record"];
                }
            }
// prevent the default dokuwiki output
            if ($action != 'home' ) {
// ************ Main call to database code ****************************
                $event->preventDefault();    
               
// call the db functions which will handle the processing from now on 
                action_db();
// *********************************************************************
            } else {
// home - so run the main wiki page                
                $_REQUEST['id']='start';
                $_REQUEST['idx']=$dbName;
            }
        }
    }
}
?>