Vorlage:CalcForm/code
Zur Navigation springen
Zur Suche springen
<?php
/**
* MediaWiki extension: CalcForm
* =============================
*
* To activate, edit your LocalSettings.php,
* save this code as 'CalcForm.php' and copy the file into the subdirectory CalcForm of your extensions-directory and add
* require_once("$IP/extensions/CalcForm/CalcForm.php");
*/
# User Settings
# require_once("$IP/extensions/CalcForm/CalcForm.php");
# Parameter
# inlabels ... Beschriftung der Eingabefelder
# inids ... eindeutige IDs für die Eingabefelder (muss je Seite eindeutig sein)
# ivalues ... Vorbelegung der Eingabefelder
# outlabels ... Beschriftung der Ausgabefelder
# outids ... eindeutige IDs für die Ausgabefelder (muss je Seite eindeutig sein)
# onclick ... Berechnungsvorschrift, was bei Mausklick passieren soll
# oninput ... Berechnungsvorschrift, was bei Dateneingabe passieren soll
# zumindest eine der beiden Eingaben muss vorhanden sein
# die fehlende wird automatisch durch die andere ergänzt
# *********************************************************************************************
# Formatierung
# marginright ... Abstand zwischen Beschriftung und Ein/Ausgabefeld
# marginleft ... Abstand links
# fontsize ... Schriftgröße
# lwidth ... Breite der Beschriftungsfelder
# nwidth ... Breite der numerischen Felder
# End User Settings
# Diese Erweiterung erzeugt ein Formular zur Durchführung einfacher Berechnungen.
# Damit sollen gewisse Überprüfungen einfacher gemacht werden.
# Durch eine Extension wird die Aktivierung von RawHTML im Wiki überflüssig,
# somit werden viele Bearbeitungsschritte wieder einfacher
# es wird ein Formular erzeugt, dieses besitzt jedoch keinen Submit-Button
# jedes Formular erhält eine zufällige ID, dadurch können mehrere Formulare auf einer Seite
# verwendet werden
# JavaScript muss aktiviert sein, sonst funktioniert die Berechnung nicht
# im IE funktioniert die Berechnung ebenfalls nicht
define("X_CALC_TAG", "calc");
define("X_CALC_HOOK", "hookCalc");
define("X_CALC_NAME", "CalcForm");
if ( !defined( 'MEDIAWIKI' ) ) {
die( "This is not a valid entry point.\n" );
}
$wgExtensionCredits['parserhooks'][] = array(
'path' => __FILE__,
'name' => X_CALC_NAME,
'version' => '0.9',
'author' =>$extension_AUTH,
'url' => "$wgServer$wgScriptPath/index.php/Extension:" . X_CALC_NAME,
'description' => 'erzeugt Eingabeformulare zum Rechnen'
);
$wgExtensionFunctions[] = 'wfCalcFormExtension';
/*
* Setup OraForm extension.
* Sets a parser hook for <X_CALC_HOOK></X_CALC_HOOK>.
*/
function wfCalcFormExtension()
{
$extension_name = X_CALC_NAME;
new $extension_name();
}
class CalcForm
{
public function __construct()
{
global $wgParser;
$wgParser->setHook(X_CALC_TAG, array(&$this, X_CALC_HOOK));
}
public function hookCalc($input, $argv, $parser)
{
global $IP, $wgServer, $wgScriptPath, $wgUploadPath, $wgHooks, $wgParser, $wgHtml5, $wgScript, $wgTitle, $wgOut;
#return debug_zval_dump($_POST);
$input = trim($input);
$br = '<br>';
$onclick = $oninput = $inlabels = $outlabels = $inids = $outids = $ivalues = '' ;
$lwidth = '10em'; $nwidth = '5em';
$owidth = $iwidth = $lwidth;
$marginright = '0.5em'; $marginleft = '0'; $marginbottom = '0.5em'; $oright='0';
$fontsize = '1em'; $addstyle= '';
foreach ( $argv as $arg => $val )
{
$$arg = $val; # die Variablen erhalten den Namen der Parameter
}
if (empty($onclick) and empty($oninput)) return "irgendeine Berechnungsvorschrift sollte schon vorhanden sein";
else {
if (empty($onclick)) $onclick=$oninput;
if (empty($oninput)) $oninput=$onclick;
}
# Ein- und Ausgabebeschriftungen werden als Text übergeben, der durch ? getrennt ist
$ilabels = preg_split("/[\?\t\r\n\f\x0B]/", $inlabels);
$olabels = preg_split("/[\?\t\r\n\f\x0B]/", $outlabels);
# Ids für In- und Out-Felder
# deswegen erforderlich, damit mehrere Formulare auf einer Seite verwendet werden können
# automatische Generierung ist mir derzeit zu kompliziert
# da ich die Berechnungsergebnisse dann irgendwie auf diese Id's hintrimmen müsste
$iids = preg_split("/[\?\t\r\n\f\x0B]/", $inids);
$oids = preg_split("/[\?\t\r\n\f\x0B]/", $outids);
$istyle = "width: $iwidth; display: inline-block; font-size: $fontsize; font-weight: bold; padding: 0 0.5em;" .
"background-image: linear-gradient(#eaeaea, #f9f9f9, #eaeaea); margin-right: $marginright; margin-bottom: $marginbottom;";
$ostyle = "width: $owidth; display: inline-block; font-size: $fontsize; font-weight: bold; padding: 0 0.5em;" .
"background-image: linear-gradient(#eaeaea, #f9f9f9, #eaeaea); margin-right: $marginright; margin-bottom: $marginbottom;";
$nstyle = "width: $nwidth; font-size: $fontsize; display: inline-block; background-image: linear-gradient(#f9f9f9, #eaeaea); " .
"padding: 0 0.2em 0 0; margin-bottom: $marginbottom; margin-right: $oright;";
#return $style . $lstyle;
if (empty($class)) $class = "mono";
$izahl = count($iids);
$ozahl = count($oids);
$ivals = array_fill(0, $izahl, '');
$ivals = !empty($ivalues) ? preg_split("/[\?\t\r\n\f\x0B]/", $ivalues) : array_fill(0, $izahl, '');
$iput = "<div style='margin-left: $marginleft;$addstyle'>";
for ($x = 0; $x < $izahl; $x++)
{
$iput .= "<div style='$addstyle'><div style='$istyle' class='$class'>$ilabels[$x]</div><input " .
"style='$nstyle' id='$iids[$x]' type='text' value='$ivals[$x]' min='0'></input></div>
";
}
$iput .= '</div>';
$oput = '';
for ($x = 0; $x < $ozahl; $x++)
{
$oput .= "<div style='$ostyle' class='$class'>$olabels[$x]</div><output " .
"style='$nstyle' class='$class' id='$oids[$x]' type='text'></output>";
}
$formid = str_shuffle('abcdefghijklm');
$myform = "<form id='$formid' oninput='$oninput' onclick='$onclick'>" . $iput . $oput . "</form>";
return $myform;
}
}