=====StructData Action=====
>>==See also:==
- Documentation: StructDataActionInfo.
==works with:==
- Wikka 1.1.6.2 to 1.1.6.5
>>//NOT Included in any Wikka version//{{lastedit show="3"}}
This is the development page for the Structdata action.
===Installation===
- Save the code below as ##actions/structdata.php##
- Give it the same file permissions as the other php files in that directory.
- To achieve the best visual rendering of tables in versions 1.1.6.2 and 1.1.6.3, add the second code block below to the ##css/wikka.css## file
=== Code ===
1. New ##actions/structdata.php## file:
%%(php)
mytype\' [print=\'printmode\'] data="[more parameters]"');
if (!defined('SD_HELP_TODOS')) define ('SD_HELP_TODOS', ' [p1=owner] [p2=status] ');
if (!defined('SD_HELP_ITEMTABLE')) define ('SD_HELP_ITEMTABLE', ' p1=type [p2=column order list] [p3=scope]');
if (!defined('SD_HELP_CARDS')) define ('SD_HELP_CARDS', ' [p1=org filter] [p2=sort column name] [p3=format (vCard|table)] ');
if (!function_exists('SDerror'))
{
function SDerror($msg)
{
return (''. $msg. '
');
}
function SDformat_hcard_element($eltype, $elvalue)
{
switch ($eltype)
{
case 'family-name':
$ret = ''. ucwords(strtolower($elvalue)) .'';
break;
case 'given-name':
$ret = ''. ucwords(strtolower($elvalue)) .'';
break;
case 'additional-name':
$ret = ''. ucwords(strtolower($elvalue)) .'';
break;
case 'url':
$ret = '%s';
break;
case 'org':
$ret = '
'. $elvalue .'
';
break;
case 'title':
$ret = ''. $elvalue .'
';
break;
case 'note':
$ret = ''. $elvalue .'
';
break;
case 'email':
$ret = '';
break;
case 'adr':
$adr_array = split(';', $elvalue);
$ret = ''. $adr_array[0] .''.
((isset($adr_array[1]) && ($adr_array[1]!='')) ? ''. $adr_array[1] .'': '') .
((isset($adr_array[2]) && ($adr_array[2]!='')) ? ''. $adr_array[2] .'': '') .
((isset($adr_array[3]) && ($adr_array[3]!='')) ? ''. $adr_array[3] .'': '') .
((isset($adr_array[4]) && ($adr_array[4]!='')) ? ''. $adr_array[4] .'': '') .
'
';
break;
default: // ignore silently
$ret = '';
}
return $ret;
}
function SDhandle_item($type, $params, $printmode)
{
$table_css = 'class="data" 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(sprintf(SD_ERROR_PARAMETER_MANDATORY, 'owner'));
}
if (!$desc)
{
$myerr .= SDerror(sprintf(SD_ERROR_PARAMETER_MANDATORY, 'desc'));
}
preg_match('/(\d+)[\-\/](\d+)[\-\/](\d+)/', $date_open, $ymd);
if (!checkdate($ymd[2], $ymd[3], $ymd[1]))
{
$myerr .= SDerror(sprintf(SD_ERROR_INCORRECT_DATE_FORMAT, 'date_open'));
}
preg_match('/(\d+)[\-\/](\d+)[\-\/](\d+)/', $date_due, $ymd2);
if (!checkdate($ymd2[2], $ymd2[3], $ymd2[1]))
{
$myerr .= SDerror(sprintf(SD_ERROR_INCORRECT_DATE_FORMAT, 'date_due'));
}
break;
case 'Card':
if (!$n)
{
$myerr .= SDerror(sprintf(SD_ERROR_PARAMETER_MANDATORY, 'n'));
}
$n_array = split(';', $n);
if ('hCard' == $printmode)
{ // hCard microformat
$lastname = SDformat_hcard_element('family-name', $n_array[0]);
$firstname = isset($n_array[1]) ? SDformat_hcard_element('given-name', $n_array[1]).' ' : '';
$middlename = isset($n_array[2]) ? SDformat_hcard_element('additional-name', $n_array[2]) : '';
$fullname = ''. $firstname . $lastname .''. $middlename .'';
$hyperlink = isset($url) ? SDformat_hcard_element('url', $url) : '%s';
$orgname = '';
if (isset($org))
{
if ($org == $n)
{ // this is an organization's card: merge name and organization
$fullname = str_replace('class="fn"', 'class="fn org"', $fullname);
}
else
{
$orgname = SDformat_hcard_element('org', $org);
}
}
$phone = isset($tel) ? ''. $tel .'
' : '';
$fax = isset($fax) ? 'fax '. $fax .'
' : '';
$address = isset($adr) ? SDformat_hcard_element('adr', $adr) : '';
$mystr = ''. sprintf($hyperlink, $fullname). (isset($title) ? SDformat_hcard_element('title', $title) : '').
$orgname . (isset($email) ? SDformat_hcard_element('email', $email) : ''). $phone . $fax.
$address .(isset($note) ? SDformat_hcard_element('note', $note) : '').'
';
}
else
{ // default print mode
$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 = "". $name .''. ($org != '' ? ' ('.$org.')' : '');
}
break;
/*------------------------------- begin customization area -----------------------------
case 'mydataitem':
$mystr = ;
break;
--------------------------------- end customization area -----------------------------*/
}
if (!$mystr)
{ // type did not match any custom type : fallback to a standard type
echo ($printmode);
switch ($printmode)
{
case 'table':
$mystr = '';
$r1 = '';
$r2 = '
';
foreach ($params as $name => $value)
{
if ($name != 'print')
{
$r1 .= ''. $name .' | ';
$r2 .= ''. $value .' | ';
}
}
$mystr .= $r1 .'
'. $r2 .'
';
break;
case 'list':
$mystr = 'name | value |
';
foreach ($params as $name => $value)
{
if ($name != 'print')
{
$mystr .= ''. $name .' | '. $value .' |
';
}
}
$mystr .= '
';
break;
case 'basic':
default:
$mainparam = (isset($value) ? $value : (isset($id) ? $id : (isset($name) ? $name : '')));
$mystr = ''. $type . (isset($mainparam) ? ' ['. $mainparam .']' : '') .'';
break;
}
}
if ($myerr)
{
return ($myerr);
}
else if ($printmode != 'false')
{ // display the data item
return ($mystr);
}
}
function SDbuild_vcard($row)
{
$n_array = split(';', $row['n']);
$name = (isset($n_array[3])? $n_array[3].' ' : '').(isset($n_array[1]) ? (ucwords(strtolower($n_array[1])).' '):'').(isset($n_array[2])? $n_array[2].' ': '').ucwords(strtolower($n_array[0]));
$str = "BEGIN:VCARD\r\nVERSION:2.1\r\nN:". $row['n'] ."\r\nFN:". $name ."\r\n";
$str .= $row['org'] ? 'ORG:'. $row['org'] ."\r\n" : '';
$str .= $row['title'] ? 'TITLE:'. $row['title'] ."\r\n" : '';
$str .= $row['url'] ? 'URL:'. $row['url'] ."\r\n" : '';
$str .= $row['tel'] ? 'TEL:'. $row['tel'] ."\r\n" : '';
$str .= $row['fax'] ? 'TEL;FAX:'. $row['fax'] ."\r\n" : '';
$str .= $row['email'] ? 'EMAIL;INTERNET:'. $row['email'] ."\r\n" : '';
if (isset($row['adr']))
{
$adr_array = split(';', $row['adr']);
$str .= 'ADR:;;'. $adr_array[0] .';'. (isset($adr_array[1]) ? $adr_array[1] : '') .
';'. (isset($adr_array[3]) ? $adr_array[3] : '') .
';'. (isset($adr_array[2]) ? $adr_array[2] : '') .
';'. (isset($adr_array[4]) ? $adr_array[4] : '') . "\r\n";
}
$str .= $row['note'] ? 'NOTE:'. $row['note'] ."\r\n" : '';
$str .= "END:VCARD\r\n";
return ($str);
}
function SDis_in_scope($thisone, $row, $type, $scope=null)
{
if ('all' == $scope)
{
$ret = ($thisone->HasAccess('read', $row['_wikkapage'])) && ($row['type'] == $type);
}
else if ('user' == $scope)
{
$ret = ($thisone->UserIsOwner($row['_wikkapage'])) && ($row['type'] == $type);
}
else
{ // default scope = page
$ret = ($row['_wikkapage'] == $thisone->GetPageTag()) && ($row['type'] == $type);
}
return $ret;
}
function SDrun_req($thisone, $reqid, $rows, $p1=null, $p2=null, $p3=null)
{ // $rows array row format: ('_wikkapage' => , 'type' => , 'field1' => , 'field2' => , ...)
$table_css = 'class="data" cellpadding="2" cellspacing="1" border="2"';
switch($reqid)
{
case 'ItemTable': // p1 = type of dataitem, p2 = column order list, p3 = scope
$i = 0;
$keyorder = array();
if ($p2)
{
$mycols = split(',', $p2);
foreach ($mycols as $lacol)
{
$keyorder[$lacol] = $i++;
}
}
$mystr0 = '';
foreach ($rows as $row)
{
if (SDis_in_scope($thisone, $row, $p1, $p3))
{
$tmp_tr = array();
$mystr .= '
';
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 .= ''. $tmp_tr[$j] .' | ';
}
$mystr .= '
';
}
}
// build table header row
$tmp_tr = array();
foreach ($keyorder as $param => $k)
{
$tmp_tr[$k] = $param;
}
for ($j = 0; $j < $i; $j++)
{
$mystr0 .= ''. $tmp_tr[$j] .' | ';
}
$mystr = $mystr0 .''. $mystr. '
';
if (!$i)
{ // empty table: display nothing
$mystr = '';
}
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++] = "{$row['owner']} | {$row['desc']} | ".
"{$row['date_due']} | ".
"{$row['_wikkapage']} |
";
}
}
}
krsort($mydata);
$mystr = 'owner | description | due date | page link |
';
foreach ($mydata as $key => $val)
{
$mystr .= $val;
}
$mystr .= '
';
break;
case 'Cards': // p1 = this org only; p2 = sort key ; p3 = format (vCard or table)
$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] = "{$n_array[0]} | {$n_array[1]} | ".
"{$row['email']} | {$row['tel']} | {$row['fax']} | ".(isset($p2) ? "{$row['org']} | ":'') .'
';
$myvcards[$sortkey] = SDbuild_vcard($row);
}
}
}
if ($p3 == 'vCard')
{
$mystr = '';
$cnt = 0;
ksort ($myvcards);
foreach ($myvcards as $key => $card)
{
$mystr .= "$card\r\n";
$cnt++;
}
$mystr ='';
}
else
{ // default (table) format
ksort($mydata);
$mystr = 'Last name | Given name | Email | Phone | Fax | '. (isset($p2) ? 'Organization | ' : '') .'
';
foreach ($mydata as $key => $val)
{
$mystr .= $val;
}
$mystr .= '
';
}
break;
/*------------------------------- begin customization area -----------------------------
case 'myreq':
$mystr =
break;
--------------------------------- end customization area -----------------------------*/
}
return ($mystr);
}
function SDdo_help($reqid)
{
$helpstr = '';
switch($reqid)
{
case 'ItemTable':
$helpstr = $reqid .SD_HELP_ITEMTABLE;
break;
case 'ToDos':
$helpstr = $reqid .SD_HELP_TODOs;
break;
case 'Cards':
$helpstr = $reqid .SD_HELP_CARDS;
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(SD_ERROR_USAGE);
}
else if (isset($help))
{ // display one-line help for $req
$output = SDdo_help($req);
}
else if (isset($req))
{ // this is a request: load all relevant data in an associative array: $myrows
$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('/type=\"(.*?)\"[ \t]*.*?data=\"(.*?)\"/', $mym, $matches2))
{ // order: type, then data
$newrow['type'] = $matches2[1];
preg_match_all('/(\w+?)\=\'(.*?)\'/', $matches2[2], $matches3);
}
else if (preg_match('/data=\"(.*?)\"[ \t]*.*?type=\"(.*?)\"/', $mym, $matches2))
{ // order: data, then type
$newrow['type'] = $matches2[2];
preg_match_all('/(\w+?)\=\'(.*?)\'/', $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 = SDrun_req($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 = SDhandle_item($type, $params, isset($print) ? $print : '');
}
else
{
$output = SDerror(sprintf(SD_ERROR_PARAMETER_MANDATORY, 'data'));
}
}
if ($output)
{ // print the result
$output = $this->ReturnSafeHTML($output);
echo $output;
}
?>
%%
2. [versions 1.1.6.2 & 1.1.6.3 only] Add the following lines to ##css/wikka.css##:
%%(php)
table.data {
border: 2px solid #CCC;
border-collapse: collapse;
border-spacing: 0;
}
table.data caption {
border: 1px solid #CCC;
font-size: 95%;
color: #666;
margin:5px 0;
padding:2px;
}
table.data thead {
background-color: #DDD;
}
table.data tfoot {
background-color: #DDD;
}
table.data th {
border: 1px solid #CCC;
padding: .1em .25em;
}
table.data thead th {
background-color: #DDD;
}
table.data tfoot th {
background-color: #DDD;
}
table.data tbody th {
background-color: #EEE;
}
table.data tbody tr:hover {
background-color: #E9E9F9;
}
table.data tbody tr.alt:hover {
background-color: #E9E9F9;
}
table.data td {
border: 1px solid #CCC;
padding: .1em .25em;
}
table.data td.number {
text-align: right;
}
table.data td.datetime {
text-align: right;
white-space: nowrap;
}
/* --- alternate row & column color --- */
table.data tr.alt {
background-color: #EEE;
}
table.data th.c1 {
background-color: #CDD;
}
table.data th.c2 {
background-color: #DCC;
}
table.data th.c3 {
background-color: #DDC;
}
table.data th.c4 {
background-color: #CDC;
}
table.data th.c5 {
background-color: #CCD;
}
table.data th.c6 {
background-color: #DCD;
}
table.data td.c1 {
background-color: #EFF;
}
table.data td.c2 {
background-color: #FEE;
}
table.data td.c3 {
background-color: #FFE;
}
table.data td.c4 {
background-color: #EFE;
}
table.data td.c5 {
background-color: #EEF;
}
table.data td.c6 {
background-color: #FEF;
}
table.data tr.alt td.c1 {
background-color: #DEE;
}
table.data tr.alt td.c2 {
background-color: #EDD;
}
table.data tr.alt td.c3 {
background-color: #EED;
}
table.data tr.alt td.c4 {
background-color: #DED;
}
table.data tr.alt td.c5 {
background-color: #DDE;
}
table.data tr.alt td.c6 {
background-color: #EDE;
}
%%
----
CategoryUserContributions