Formate has been transformed in a plugin that extends MODx placeholders and document variables.
You can find [b]PHx it in the MODx Repository at http://www.modxcms.com/PHx-1006.html[/b]
Hi this is a BETA release of a plugin I’m working on. It also supports nested Formate tags. Sorry for the short doc on this one but just play with it and you’ll find out. I created this plugin so I won’t have to use snippets for some standard formatting.
Description
Formats / translates values for presentation (ea. date formatting from unix timestamps and userid to username for example).
The tags can be used in chunks/templates
Plugin Code
/*
* Name: Formate
* Version: 0.2
* Type: Plugin
* Author: Armand "bS" Pondman ([email protected])
* Updated: July 27, 2006 21:00 CET
*
* Description:
*
* Formats / translates values for presentation (ea. date formatting from unix timestamps and userid to username for example)
*
*/
## DO NOT EDIT BELOW THIS LINE ##
class dpParser {
var $_output;
var $_namespace;
var $_delimiter;
function dpParser ( $template = "", $namespace = "", $delimiter = "|" ) {
$this->_output = $template;
$this->_namespace = $namespace;
$this->_delimiter = $delimiter;
}
function Render() {
return $this->Parse($this->_output);
}
function Parse($template) {
#echo '~(?=\[%'.$this->_namespace.'((.|\r|\n)*?)%\])~';
if (preg_match_all('~(?=\[%'.$this->_namespace.'((.|\r|\n)*?)%\])~',$template, $matches)) {
$count = count($matches[1]);
$var_search = array();
$var_replace = array();
for($i=0; $i<$count; $i++) {
$match = $matches[1][$i];
if(!strpos($match, "[%".$this->_namespace)) {
$var_search[] = "[%".$this->_namespace.$match."%]";
$var_replace[] = $this->Filter($match);
}
}
$template = $this->Parse(str_replace($var_search, $var_replace, $template));
}
return $template;
}
function Filter($action) {
global $modx;
$arg = explode($this->_delimiter,$action);
switch(strtoupper(trim($arg[0]))) {
// TEST
case "ASIS":
$dpreplace = $arg[1];
break;
// POST objects
case "POST":
$dpreplace = $_POST[trim($arg[1])];
break;
// GET objects
case "GET":
$dpreplace = $_GET[trim($arg[1])];
break;
// Calculations
case "CALC":
$filter = preg_replace("~([a-zA-Z\n\r\t\s])~","", trim($arg[1]));
$dpreplace = eval("return ".$filter.";");
break;
// Date Formatting
case "DATE":
$rawdate = 0;
$rawdates = explode(",",$arg[1]);
foreach ( $rawdates as $rdate ) {
$rawdate = trim($rdate);
if($rawdate > 0) {break;}
}
$dpreplace = strftime($arg[2],$rawdate);
break;
// Web User Lookup
case "WUSR":
$getfield = trim($arg[1]);
$getuserid = trim($arg[2]);
$tbluser = $modx->getFullTableName("web_users");
$tbluatt = $modx->getFullTableName("web_user_attributes");
$sqlu = "SELECT a.username as username, b.* FROM $tbluser a INNER JOIN $tbluatt b ON a.id = b.internalKey WHERE a.id = '".$modx->db->escape($getuserid)."'";
$rs = $modx->db->query($sqlu);
$user = $modx->db->getRow($rs);
$dpreplace = $user[$getfield];
break;
// Manager User Lookup
case "MUSR":
$getfield = trim($arg[1]);
$getuserid = trim($arg[2]);
$tbluser = $modx->getFullTableName("manager_users");
$tbluatt = $modx->getFullTableName("user_attributes");
$sqlu = "SELECT a.username as username, b.* FROM $tbluser a INNER JOIN $tbluatt b ON a.id = b.internalKey WHERE a.id = '".$modx->db->escape($getuserid)."'";
$rs = $modx->db->query($sqlu);
$user = $modx->db->getRow($rs);
$dpreplace = $user[$getfield];
break;
default:
break;
}
return $dpreplace;
}
}
$e = &$modx->Event;
switch($e->name) {
case 'OnWebPagePrerender':
$newOutput = new dpParser($modx->documentOutput);
$modx->documentOutput = $newOutput->Render();
break;
default:
return;
break;
}
Setup
- create a new plugin
- copy/paste code
- hook it up with the "OnWebPagePrerender" event
Usage
date:
[% date | value | string %]
value: unix timestamp (seperate multiple timestamps with a comma and it will translate the first value that’s not 0)
string: like
strftime (
http://nl3.php.net/manual/nl/function.strftime.php)
example:
[% DATE | [*publishedon*],[*createdon*] | %a %B %d, %Y %R %]
calc:
[% calc| calculation %]
calculation: just the basic math functionality
examples:
[% CALC| 1+2+3 %] (outputs: 6)
[% CALC| 5+[*somevalue*] %] (outputs: 11 if ’somevalue’ is 6)
[% CALC| [% CALC| 5+[*somevalue*] %]-1+2+3 %] (outputs: 15, if taken the above example)
wusr:
[% WUSR | dbfield | userid %]
dbfield: name of the db field you wanna show
userid: the id of the web user
example:
[% WUSR | username | [*createdby*] %]
musr:
[% MUSR | dbfield | userid %]
dbfield: name of the db field you wanna show
userid: the id of the manageruser
example:
[% MUSR | username | [*createdby*] %]