Difference between revisions of "Extension:SimpleForms.php"

From Organic Design wiki
m
m
Line 19: Line 19:
 
var $id = 0;
 
var $id = 0;
 
var $tagname;
 
var $tagname;
 +
var $opentag;
 +
var $closetag;
  
 
# Constructor
 
# Constructor
Line 26: Line 28:
 
$wgParser->setFunctionHook($wgSimpleFormsInputMagic,array($this,'input'));
 
$wgParser->setFunctionHook($wgSimpleFormsInputMagic,array($this,'input'));
 
$wgParser->setFunctionHook($wgSimpleFormsQsMagic,  array($this,'qs'));
 
$wgParser->setFunctionHook($wgSimpleFormsQsMagic,  array($this,'qs'));
$this->tagname = 'this_should_be_random';
+
$this->tagname = 'this_should_be_random';
 +
$this->opentag  = 'this_should_be_random2';
 +
$this->closetag = 'this_should_be_random3';
 
$wgParser->setHook($this->tagname,'tagHook');
 
$wgParser->setHook($this->tagname,'tagHook');
 
}
 
}
Line 46: Line 50:
  
 
function input(&$parser) {
 
function input(&$parser) {
$input = "(((input";
+
$ot = $this->opentag;
 +
$ct = $this->closetag;
 +
$content = '';
 +
 
 +
# Process args
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
if (preg_match('/^(.+?)=(.+)$/',$arg,$match)) $input .= " $match[1]=\"$match[2]\"";
+
if (preg_match('/^([a-z0-9_]+?)=(.+)$/',$arg,$match)) $argv[strtolower(trim($match[1]))] = trim($match[2]);
 +
else $content = trim($arg);
 +
}
 +
$type = $argv['type'];
 +
unset($argv['type']);
 +
 +
# Textarea
 +
if ($argv['type'] == 'textarea') $input = $ot."textarea$args$ct$content$ot/textarea$ct";
 +
 
 +
# Select list
 +
elseif ($argv['type'] == 'select' && preg_match_all('/^[\\[\\*]*(.*?)[\\]]*$/m',$content,$options)) {
 +
$options = "{$ot}option$ct".join("$ot/option$ct\n{$ot}option$ct",$options[1])."{$ot}/option$ct";
 +
$input = $ot."select$args$ct$options$ot/select$ct";
 
}
 
}
return "$input/)))";
+
 
 +
# Submit button (changes to onClick)
 +
elseif ($argv['type'] == 'submit' && $method == 'live') {
 +
}
 +
 
 +
# Default: render as normal input element
 +
else $input = $ot."input$args/$ct";
 +
 
 +
return $input;
 
}
 
}
  
Line 58: Line 86:
  
 
function tagHook(&$text) {
 
function tagHook(&$text) {
$text = str_replace("(((","<",str_replace(")))",">",$text));
+
$text = str_replace($this->opentag,"<",str_replace($this->closetag,">",$text));
 
return "<form>$text</form>";
 
return "<form>$text</form>";
 
}
 
}

Revision as of 00:09, 28 April 2007

<?

  1. MediaWiki SimpleForms Extension
  2. - See http://www.mediawiki.org/wiki/Extension:Simple_Forms for installation and usage details
  3. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  4. - Author: http://www.organicdesign.co.nz/nad
  5. {{#Security:*|dev}}{{#Security:view|*}}Template:Php

$wgSimpleFormsVersion = 'Version 0.01 (2007-04-25)';

$wgSimpleFormsFormMagic = "form"; # the parser-function name for form containers $wgSimpleFormsInputMagic = "input"; # the parser-function name for form inputs $wgSimpleFormsQsMagic = "qs"; # the parser-function name for query-string access

$wgExtensionFunctions[] = 'wfSetupSimpleForms'; $wgHooks['LanguageGetMagic'][] = 'wfSimpleFormsLanguageGetMagic';

class SimpleForms {

var $id = 0; var $tagname; var $opentag; var $closetag;

# Constructor function SimpleForms() { global $wgParser,$wgHooks,$wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsQsMagic; $wgParser->setFunctionHook($wgSimpleFormsFormMagic, array($this,'form')); $wgParser->setFunctionHook($wgSimpleFormsInputMagic,array($this,'input')); $wgParser->setFunctionHook($wgSimpleFormsQsMagic, array($this,'qs')); $this->tagname = 'this_should_be_random'; $this->opentag = 'this_should_be_random2'; $this->closetag = 'this_should_be_random3'; $wgParser->setHook($this->tagname,'tagHook'); }

function form(&$parser) { global $wgJavaScriptFunctions; $id = $this->id++; $tag = $this->tagname; $args = ; foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)=(.+)$/',$arg,$match)) $args .= " $match[1]=\"$match[2]\""; else $form = $arg; } $wgJavaScriptFunctions['SimpleFormsFunctions'] = ' function live() { } '; return "<$tag $args>$form</$tag>"; }

function input(&$parser) { $ot = $this->opentag; $ct = $this->closetag; $content = ;

# Process args foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^([a-z0-9_]+?)=(.+)$/',$arg,$match)) $argv[strtolower(trim($match[1]))] = trim($match[2]); else $content = trim($arg); } $type = $argv['type']; unset($argv['type']);

# Textarea if ($argv['type'] == 'textarea') $input = $ot."textarea$args$ct$content$ot/textarea$ct";

# Select list elseif ($argv['type'] == 'select' && preg_match_all('/^[\\[\\*]*(.*?)[\\]]*$/m',$content,$options)) { $options = "{$ot}option$ct".join("$ot/option$ct\n{$ot}option$ct",$options[1])."{$ot}/option$ct"; $input = $ot."select$args$ct$options$ot/select$ct"; }

# Submit button (changes to onClick) elseif ($argv['type'] == 'submit' && $method == 'live') { }

# Default: render as normal input element else $input = $ot."input$args/$ct";

return $input; }

function qs(&$parser,$key) { return $_REQUEST[$key]; }

function tagHook(&$text) { $text = str_replace($this->opentag,"<",str_replace($this->closetag,">",$text)); return "<form>$text</form>"; }

# Add the javascript to the output object if not added yet and there is at least one tree # - should add all items in $wgJavaScriptFunctions function onOutputPageBeforeHTML(&$out) { global $wgJavaScriptFunctions; foreach ($wgJavaScriptFunctions as $js) $out->addScript("\n<script type=\"text/javascript\">$js</script>\n"); return true; }

}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupSimpleForms() { global $wgHooks,$wgSimpleForms,$wgJavaScriptFunctions; $wgSimpleForms = new TreeView($wgTreeViewMagic); if (!is_array($wgJavaScriptFunctions)) { $wgJavaScriptFunctions = array(); $wgHooks['OutputPageBeforeHTML'][] = $this; } }

  1. Needed in MediaWiki >1.8.0 for magic word hooks to work properly

function wfSimpleFormsLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsQsMagic; $magicWords[$wgSimpleFormsFormMagic] = array(0,$wgSimpleFormsFormMagic); $magicWords[$wgSimpleFormsInputMagic] = array(0,$wgSimpleFormsInputMagic); $magicWords[$wgSimpleFormsQsMagic] = array(0,$wgSimpleFormsQsMagic); return true; } ?>