Revision [15737]
This is an old revision of StructDataAction made by DomBonj on 2006-12-02 19:49:45.
StructData Action
NOT Included in any Wikka versionThis is the development page for the structdata action.
Installation
- Save the code below as action/structdata.php
- Give it the same file permissions as the other php files in that directory.
Code
<?php
#
# Defines and optionnally displays a structured data item
#
# @package Actions
# @name structdata
#
# @authors Dominique Bonjour
# @version 0.93
# @input Parameters = (type='mytype' data='datalist' [print=('basic'|'table'|'list'|'false')]
# | req='reqid' [help][p1='val1'][p2='val2"][p3='val3'])
# $datalist: list of 'pname'='pval' parameters, separated by spaces.
# restrictions: pval shall not include a ' followed by a space
# $reqid : the name of a predefined request
# $val1, $val2, $val3 : optional parameters for request $reqid
# $help si présent affiche le mode d'emploi de $reqid
#
# @uses Wakka::ReturnSafeHtml()
# @uses Wakka::LoadAll()
# @uses Wakka::GetPageTag()
#
# @notes Add your code into the flagged areas
#
if (!function_exists('SDerror'))
{
function SDerror($msg) {
return ("<em class='error'>$msg</em>");
}
function SDhandleitem($type, $params, $printmode) {
$table_css = "class='wikka' cellpadding='2' cellspacing='1' border='2'";
$myerr = '';
extract($params);
$mystr="";
switch ($type) {
// custom formatting, data validation and hyperlink addition
case 'ToDo':
if (!$owner) $myerr.= SDerror("The 'owner' parameter is mandatory");
if (!$desc) $myerr.= SDerror("The 'desc' parameter is mandatory");
preg_match('/(\d+)[\-\/](\d+)[\-\/](\d+)/', $date_open, $ymd);
if (!checkdate($ymd[2], $ymd[3], $ymd[1]))
$myerr .= SDerror( "Incorrect format for 'date_open' parameter: use 'yyyy-mm-dd'");
preg_match('/(\d+)[\-\/](\d+)[\-\/](\d+)/', $date_due, $ymd);
if (!checkdate($ymd[2], $ymd[3], $ymd[1]))
$myerr .= SDerror ("Incorrect format for 'date_due' parameter: use 'yyyy-mm-dd'");
break;
case 'Card':
if (!$n) {
$myerr.= SDerror("The 'n' parameter is mandatory");
}
$n_array = split(';', $n);
$name = (isset($n_array[1]) ? (ucwords(strtolower($n_array[1]))." ") : "").ucwords(strtolower($n_array[0]));
$tooltip = isset($tel) ? "phone: $tel" : "";
$tooltip .= (isset($fax) ? ($tooltip ? " / fax: $fax" : "fax: $fax") : "");
$mystr = "<a href='mailto:$email' title='$tooltip'>".$name."</a>". ($org != '' ? " ($org)" : "");
break;
/*------------------------------- begin customization area -----------------------------
case 'mydataitem':
<my validation and hyperlinking code>
$mystr = <my formatting>;
break;
--------------------------------- end customization area -----------------------------*/
}
if (!$mystr) {
// type did not match any custom type : fallback to a standard type
switch ($printmode) {
case 'table':
$mystr = "<table $table_css>"; $r1="<tr>"; $r2="<tr>";
foreach ($params as $name => $value) {
if ($name != 'print') {
$r1 .= "<th class='comment'>$name</th>";
$r2 .= "<td>$value</td>";
}
}
$mystr .= "$r1</tr>$r2</tr></table>";
break;
case 'list':
$mystr = "<table $table_css><tr class='comment'><th>name</th><th>value</th></tr>";
foreach ($params as $name => $value) {
if ($name != 'print') {
$mystr .= "<tr><td class='comment' align='right'>$name</td><td>$value</td></tr>";
}
}
$mystr .= "</table>";
break;
case 'basic':
default:
$mainparam = (isset($value) ? $value : (isset($id) ? $id : (isset($name) ? $name : '')));
$mystr="<span title='structured data item' class='notes'>{$type}".(isset($mainparam)?" [$mainparam]":"")."</span>";
break;
}
}
if ($myerr) {
return ($myerr);
} else if ($printmode != 'false') {
// print (or not) the data item
return ($mystr);
}
}
function SDrunreq ($this, $reqid, $rows, $p1=null, $p2=null, $p3=null) {
$table_css = "class='wikka' cellpadding='2' cellspacing='1' border='2'";
switch($reqid) {
case 'ItemTable': // p1 = type of dataitem
$i = 0;
$keyorder = array();
$mystr0 = "<table $table_css><tr class='comment'>";
foreach ($rows as $row) {
if (($row['_wikkapage']==$this->GetPageTag()) && ($row['type']==$p1)) {
$tmp_tr = array();
$mystr .= "<tr>";
foreach ($row as $key => $val) {
if (($key != '_wikkapage') && ($key != 'type') && ($key != 'print') && !isset($keyorder[$key])) {
$keyorder[$key] = $i++;
}
if (($key != '_wikkapage') && ($key != 'type') && ($key != 'print')) {
$tmp_tr[$keyorder[$key]] = $val;
}
}
for ($j=0; $j < $i; $j++) {
$mystr .= "<td>".$tmp_tr[$j]."</td>";
}
$mystr .= "</tr>";
}
}
// build table header row
$tmp_tr = array();
foreach ($keyorder as $param => $k) {
$tmp_tr[$k] = $param;
}
for ($j=0; $j < $i; $j++) {
$mystr0 .= "<th>".$tmp_tr[$j]."</th>";
}
$mystr = $mystr0. "</tr>". $mystr. "</table>";
if (!$i) $mystr=''; // empty table: display nothing
break;
case 'ToDos': // p1 = this owner only; p2 = this status only
$mydata=array();
$i=0;
foreach ($rows as $row) {
if ($row['type']=='ToDo') {
if ( (!$p1 || ($p1 == $row['owner'])) && (!$p2 || ($p2 == $row['status'])) ) {
// Transform all dates into a common format for sorting purposes
$mydata[date("Y-m-d", strtotime($row['date_open']))."_".$i++] = "<tr><td>{$row['owner']}</td><td>{$row['desc']}</td>".
"<td".((strtotime($row['date_due'])<time())?" style='background-color:red; color:white; '":"").">{$row['date_due']}</td>".
"<td align='center'><a href='{$_SERVER['PHP_SELF']}?wakka={$row['_wikkapage']}'>{$row['_wikkapage']}</a></td></tr>";
}
}
}
krsort($mydata);
$mystr = "<table $table_css><tr class='comment'><th>owner</th><th>description</th><th>due date</th><th>link</th></tr>";
foreach ($mydata as $key => $val) {
$mystr .= "$val";
}
$mystr .= "</table>";
break;
case 'Cards': // p1 = this org only; p2 = sort key
$mydata=array();
$i=0;
foreach ($rows as $row) {
if ($row['type']=='Card') {
if ( (!$p1 || ($p1 == $row['org'])) ) {
$sortkey = strtolower((isset($p2) && $p2) ? $row[$p2] : (isset($row['n']) ? $row['n'] : $i))."__".$i++;
$n_array = split(';', $row['n']);
$mydata[$sortkey] = "<tr><td>{$n_array[0]}</td><td>{$n_array[1]}</td>".
"<td>{$row['email']}</td><td>{$row['tel']}</td><td>{$row['fax']}</td>".(isset($p2) ? "<td>{$row['org']}</td>":"")."</tr>";
}
}
}
ksort($mydata);
$mystr = "<table $table_css><tr class='comment'><th>Last name</th><th>Given name</th><th>Email</th><th>Phone</th><th>Fax</th>".(isset($p2) ? "<th>Organization</th>":"")."</tr>";
foreach ($mydata as $key => $val) {
$mystr .= "$val";
}
$mystr .= "</table>";
break;
/*------------------------------- begin customization area -----------------------------
case 'myreq':
<mycode>
$mystr = <what I want to print>
break;
--------------------------------- end customization area -----------------------------*/
}
return ($mystr);
}
function SDdohelp ($reqid) {
$helpstr = '';
switch($reqid) {
case 'ItemTable':
$helpstr = "$reqid p1=<i>type</i> ";
break;
case 'ToDo':
$helpstr = "$reqid [p1=<i>owner</i>] [p2=<i>status</i>] ";
break;
case 'Cards':
$helpstr = "$reqid [p1=<i>org filter</i>] [p2=<i>sort column name</i>]";
break;
/*------------------------------- begin customization area -----------------------------
case 'myreq':
$helpstr = 'myfoobar';
break;
--------------------------------- end customization area -----------------------------*/
}
return ($helpstr);
}
}
if ((!isset($type) || !$type) && (!isset($req) || !$req) && !isset($help)) {
$output = SDerror("Usage: structdata type='<i>mytype</i>' [print='<i>printmode</i>'] data=\"[more parameters]\"");
}
else if (isset($help))
{
$output = SDdohelp ($req);
}
else if (isset($req))
{ // all relevant data is loaded in an associative array: $myrows
// row format : ('_wikkapage' => <page tag>, 'param1' => <val1>, 'param2' => <val2>, ...)
$query = "SELECT body, tag FROM ".$this->config['table_prefix']."pages WHERE ((latest = 'Y') AND (body LIKE '%{{structdata%'))";
$rows = $this->LoadAll($query);
foreach ($rows as $row) {
if (preg_match_all('/\{\{structdata(.+?)\}\}/', $row['body'], $matches)) {
foreach ($matches[1] as $mym) {
$newrow=array('_wikkapage' => $row['tag']);
if (preg_match('/\s*type\s*=\s*\"(.*?)\"\s*.*?data\s*=\s*\"(.*?)\"/', $mym, $matches2)) {
$newrow['type'] = $matches2[1];
preg_match_all('/\s*?(\w+?)\=\'(.*?)\'\s*?/', $matches2[2], $matches3);
} else if (preg_match('/\s*data\s*=\s*\"(.*?)\"\s*.*?type\s*=\s*\"(.*?)\"/', $mym, $matches2)) {
$newrow['type'] = $matches2[2];
preg_match_all('/\s*?(\w+?)\=\'(.*?)\'\s*?/', $matches2[1], $matches3);
}
for ($i=0; $i < count($matches3[1]); $i++) {
$newrow[strtolower($matches3[1][$i])] = $matches3[2][$i];
}
$myrows[] = $newrow;
}
}
}
// we run the request
$output = SDrunreq ($this, $req, $myrows, (isset($p1)?$p1:''), (isset($p2)?$p2:''), (isset($p3)?$p3:''));
}
else
{ // this is a data item : process it
if ($data) {
preg_match_all('/(\w+?)\=\'(.*?)\'\s*?/', $data, $matches);
for ($i=0; $i<count($matches[1]); $i++) {
// parameter name is case-insensitive
$params[strtolower($matches[1][$i])] = $matches[2][$i];
}
$output = SDhandleitem ($type, $params, isset($print)?$print:'');
} else {
$output = SDerror ("Missing 'data' parameter");
}
}
if ($output) { // print the result
// $output = $this->ReturnSafeHTML($output);
echo $output;
}
?>
#
# Defines and optionnally displays a structured data item
#
# @package Actions
# @name structdata
#
# @authors Dominique Bonjour
# @version 0.93
# @input Parameters = (type='mytype' data='datalist' [print=('basic'|'table'|'list'|'false')]
# | req='reqid' [help][p1='val1'][p2='val2"][p3='val3'])
# $datalist: list of 'pname'='pval' parameters, separated by spaces.
# restrictions: pval shall not include a ' followed by a space
# $reqid : the name of a predefined request
# $val1, $val2, $val3 : optional parameters for request $reqid
# $help si présent affiche le mode d'emploi de $reqid
#
# @uses Wakka::ReturnSafeHtml()
# @uses Wakka::LoadAll()
# @uses Wakka::GetPageTag()
#
# @notes Add your code into the flagged areas
#
if (!function_exists('SDerror'))
{
function SDerror($msg) {
return ("<em class='error'>$msg</em>");
}
function SDhandleitem($type, $params, $printmode) {
$table_css = "class='wikka' cellpadding='2' cellspacing='1' border='2'";
$myerr = '';
extract($params);
$mystr="";
switch ($type) {
// custom formatting, data validation and hyperlink addition
case 'ToDo':
if (!$owner) $myerr.= SDerror("The 'owner' parameter is mandatory");
if (!$desc) $myerr.= SDerror("The 'desc' parameter is mandatory");
preg_match('/(\d+)[\-\/](\d+)[\-\/](\d+)/', $date_open, $ymd);
if (!checkdate($ymd[2], $ymd[3], $ymd[1]))
$myerr .= SDerror( "Incorrect format for 'date_open' parameter: use 'yyyy-mm-dd'");
preg_match('/(\d+)[\-\/](\d+)[\-\/](\d+)/', $date_due, $ymd);
if (!checkdate($ymd[2], $ymd[3], $ymd[1]))
$myerr .= SDerror ("Incorrect format for 'date_due' parameter: use 'yyyy-mm-dd'");
break;
case 'Card':
if (!$n) {
$myerr.= SDerror("The 'n' parameter is mandatory");
}
$n_array = split(';', $n);
$name = (isset($n_array[1]) ? (ucwords(strtolower($n_array[1]))." ") : "").ucwords(strtolower($n_array[0]));
$tooltip = isset($tel) ? "phone: $tel" : "";
$tooltip .= (isset($fax) ? ($tooltip ? " / fax: $fax" : "fax: $fax") : "");
$mystr = "<a href='mailto:$email' title='$tooltip'>".$name."</a>". ($org != '' ? " ($org)" : "");
break;
/*------------------------------- begin customization area -----------------------------
case 'mydataitem':
<my validation and hyperlinking code>
$mystr = <my formatting>;
break;
--------------------------------- end customization area -----------------------------*/
}
if (!$mystr) {
// type did not match any custom type : fallback to a standard type
switch ($printmode) {
case 'table':
$mystr = "<table $table_css>"; $r1="<tr>"; $r2="<tr>";
foreach ($params as $name => $value) {
if ($name != 'print') {
$r1 .= "<th class='comment'>$name</th>";
$r2 .= "<td>$value</td>";
}
}
$mystr .= "$r1</tr>$r2</tr></table>";
break;
case 'list':
$mystr = "<table $table_css><tr class='comment'><th>name</th><th>value</th></tr>";
foreach ($params as $name => $value) {
if ($name != 'print') {
$mystr .= "<tr><td class='comment' align='right'>$name</td><td>$value</td></tr>";
}
}
$mystr .= "</table>";
break;
case 'basic':
default:
$mainparam = (isset($value) ? $value : (isset($id) ? $id : (isset($name) ? $name : '')));
$mystr="<span title='structured data item' class='notes'>{$type}".(isset($mainparam)?" [$mainparam]":"")."</span>";
break;
}
}
if ($myerr) {
return ($myerr);
} else if ($printmode != 'false') {
// print (or not) the data item
return ($mystr);
}
}
function SDrunreq ($this, $reqid, $rows, $p1=null, $p2=null, $p3=null) {
$table_css = "class='wikka' cellpadding='2' cellspacing='1' border='2'";
switch($reqid) {
case 'ItemTable': // p1 = type of dataitem
$i = 0;
$keyorder = array();
$mystr0 = "<table $table_css><tr class='comment'>";
foreach ($rows as $row) {
if (($row['_wikkapage']==$this->GetPageTag()) && ($row['type']==$p1)) {
$tmp_tr = array();
$mystr .= "<tr>";
foreach ($row as $key => $val) {
if (($key != '_wikkapage') && ($key != 'type') && ($key != 'print') && !isset($keyorder[$key])) {
$keyorder[$key] = $i++;
}
if (($key != '_wikkapage') && ($key != 'type') && ($key != 'print')) {
$tmp_tr[$keyorder[$key]] = $val;
}
}
for ($j=0; $j < $i; $j++) {
$mystr .= "<td>".$tmp_tr[$j]."</td>";
}
$mystr .= "</tr>";
}
}
// build table header row
$tmp_tr = array();
foreach ($keyorder as $param => $k) {
$tmp_tr[$k] = $param;
}
for ($j=0; $j < $i; $j++) {
$mystr0 .= "<th>".$tmp_tr[$j]."</th>";
}
$mystr = $mystr0. "</tr>". $mystr. "</table>";
if (!$i) $mystr=''; // empty table: display nothing
break;
case 'ToDos': // p1 = this owner only; p2 = this status only
$mydata=array();
$i=0;
foreach ($rows as $row) {
if ($row['type']=='ToDo') {
if ( (!$p1 || ($p1 == $row['owner'])) && (!$p2 || ($p2 == $row['status'])) ) {
// Transform all dates into a common format for sorting purposes
$mydata[date("Y-m-d", strtotime($row['date_open']))."_".$i++] = "<tr><td>{$row['owner']}</td><td>{$row['desc']}</td>".
"<td".((strtotime($row['date_due'])<time())?" style='background-color:red; color:white; '":"").">{$row['date_due']}</td>".
"<td align='center'><a href='{$_SERVER['PHP_SELF']}?wakka={$row['_wikkapage']}'>{$row['_wikkapage']}</a></td></tr>";
}
}
}
krsort($mydata);
$mystr = "<table $table_css><tr class='comment'><th>owner</th><th>description</th><th>due date</th><th>link</th></tr>";
foreach ($mydata as $key => $val) {
$mystr .= "$val";
}
$mystr .= "</table>";
break;
case 'Cards': // p1 = this org only; p2 = sort key
$mydata=array();
$i=0;
foreach ($rows as $row) {
if ($row['type']=='Card') {
if ( (!$p1 || ($p1 == $row['org'])) ) {
$sortkey = strtolower((isset($p2) && $p2) ? $row[$p2] : (isset($row['n']) ? $row['n'] : $i))."__".$i++;
$n_array = split(';', $row['n']);
$mydata[$sortkey] = "<tr><td>{$n_array[0]}</td><td>{$n_array[1]}</td>".
"<td>{$row['email']}</td><td>{$row['tel']}</td><td>{$row['fax']}</td>".(isset($p2) ? "<td>{$row['org']}</td>":"")."</tr>";
}
}
}
ksort($mydata);
$mystr = "<table $table_css><tr class='comment'><th>Last name</th><th>Given name</th><th>Email</th><th>Phone</th><th>Fax</th>".(isset($p2) ? "<th>Organization</th>":"")."</tr>";
foreach ($mydata as $key => $val) {
$mystr .= "$val";
}
$mystr .= "</table>";
break;
/*------------------------------- begin customization area -----------------------------
case 'myreq':
<mycode>
$mystr = <what I want to print>
break;
--------------------------------- end customization area -----------------------------*/
}
return ($mystr);
}
function SDdohelp ($reqid) {
$helpstr = '';
switch($reqid) {
case 'ItemTable':
$helpstr = "$reqid p1=<i>type</i> ";
break;
case 'ToDo':
$helpstr = "$reqid [p1=<i>owner</i>] [p2=<i>status</i>] ";
break;
case 'Cards':
$helpstr = "$reqid [p1=<i>org filter</i>] [p2=<i>sort column name</i>]";
break;
/*------------------------------- begin customization area -----------------------------
case 'myreq':
$helpstr = 'myfoobar';
break;
--------------------------------- end customization area -----------------------------*/
}
return ($helpstr);
}
}
if ((!isset($type) || !$type) && (!isset($req) || !$req) && !isset($help)) {
$output = SDerror("Usage: structdata type='<i>mytype</i>' [print='<i>printmode</i>'] data=\"[more parameters]\"");
}
else if (isset($help))
{
$output = SDdohelp ($req);
}
else if (isset($req))
{ // all relevant data is loaded in an associative array: $myrows
// row format : ('_wikkapage' => <page tag>, 'param1' => <val1>, 'param2' => <val2>, ...)
$query = "SELECT body, tag FROM ".$this->config['table_prefix']."pages WHERE ((latest = 'Y') AND (body LIKE '%{{structdata%'))";
$rows = $this->LoadAll($query);
foreach ($rows as $row) {
if (preg_match_all('/\{\{structdata(.+?)\}\}/', $row['body'], $matches)) {
foreach ($matches[1] as $mym) {
$newrow=array('_wikkapage' => $row['tag']);
if (preg_match('/\s*type\s*=\s*\"(.*?)\"\s*.*?data\s*=\s*\"(.*?)\"/', $mym, $matches2)) {
$newrow['type'] = $matches2[1];
preg_match_all('/\s*?(\w+?)\=\'(.*?)\'\s*?/', $matches2[2], $matches3);
} else if (preg_match('/\s*data\s*=\s*\"(.*?)\"\s*.*?type\s*=\s*\"(.*?)\"/', $mym, $matches2)) {
$newrow['type'] = $matches2[2];
preg_match_all('/\s*?(\w+?)\=\'(.*?)\'\s*?/', $matches2[1], $matches3);
}
for ($i=0; $i < count($matches3[1]); $i++) {
$newrow[strtolower($matches3[1][$i])] = $matches3[2][$i];
}
$myrows[] = $newrow;
}
}
}
// we run the request
$output = SDrunreq ($this, $req, $myrows, (isset($p1)?$p1:''), (isset($p2)?$p2:''), (isset($p3)?$p3:''));
}
else
{ // this is a data item : process it
if ($data) {
preg_match_all('/(\w+?)\=\'(.*?)\'\s*?/', $data, $matches);
for ($i=0; $i<count($matches[1]); $i++) {
// parameter name is case-insensitive
$params[strtolower($matches[1][$i])] = $matches[2][$i];
}
$output = SDhandleitem ($type, $params, isset($print)?$print:'');
} else {
$output = SDerror ("Missing 'data' parameter");
}
}
if ($output) { // print the result
// $output = $this->ReturnSafeHTML($output);
echo $output;
}
?>
CategoryUserContributions