helper_plugin_data::_formatData PHP Method

_formatData() public method

Return XHTML formated data, depending on column type
public _formatData ( array $column, string $value, Doku_Renderer_xhtml $R ) : string
$column array
$value string
$R Doku_Renderer_xhtml
return string
    function _formatData($column, $value, Doku_Renderer_xhtml $R)
    {
        global $conf;
        $vals = explode("\n", $value);
        $outs = array();
        //multivalued line from db result for pageid and wiki has only in first value the ID
        $storedID = '';
        foreach ($vals as $val) {
            $val = trim($val);
            if ($val == '') {
                continue;
            }
            $type = $column['type'];
            if (is_array($type)) {
                $type = $type['type'];
            }
            switch ($type) {
                case 'page':
                    $val = $this->_addPrePostFixes($column['type'], $val);
                    $val = $this->ensureAbsoluteId($val);
                    $outs[] = $R->internallink($val, null, null, true);
                    break;
                case 'title':
                    list($id, $title) = explode('|', $val, 2);
                    $id = $this->_addPrePostFixes($column['type'], $id);
                    $id = $this->ensureAbsoluteId($id);
                    $outs[] = $R->internallink($id, $title, null, true);
                    break;
                case 'pageid':
                    list($id, $title) = explode('|', $val, 2);
                    //use ID from first value of the multivalued line
                    if ($title == null) {
                        $title = $id;
                        if (!empty($storedID)) {
                            $id = $storedID;
                        }
                    } else {
                        $storedID = $id;
                    }
                    $id = $this->_addPrePostFixes($column['type'], $id);
                    $outs[] = $R->internallink($id, $title, null, true);
                    break;
                case 'nspage':
                    // no prefix/postfix here
                    $val = ':' . $column['key'] . ":{$val}";
                    $outs[] = $R->internallink($val, null, null, true);
                    break;
                case 'mail':
                    list($id, $title) = explode(' ', $val, 2);
                    $id = $this->_addPrePostFixes($column['type'], $id);
                    $id = obfuscate(hsc($id));
                    if (!$title) {
                        $title = $id;
                    } else {
                        $title = hsc($title);
                    }
                    if ($conf['mailguard'] == 'visible') {
                        $id = rawurlencode($id);
                    }
                    $outs[] = '<a href="mailto:' . $id . '" class="mail" title="' . $id . '">' . $title . '</a>';
                    break;
                case 'url':
                    $val = $this->_addPrePostFixes($column['type'], $val);
                    $outs[] = $this->external_link($val, false, 'urlextern');
                    break;
                case 'tag':
                    // per default use keyname as target page, but prefix on aliases
                    if (!is_array($column['type'])) {
                        $target = $column['key'] . ':';
                    } else {
                        $target = $this->_addPrePostFixes($column['type'], '');
                    }
                    $outs[] = '<a href="' . wl(str_replace('/', ':', cleanID($target)), $this->_getTagUrlparam($column, $val)) . '" title="' . sprintf($this->getLang('tagfilter'), hsc($val)) . '" class="wikilink1">' . hsc($val) . '</a>';
                    break;
                case 'timestamp':
                    $outs[] = dformat($val);
                    break;
                case 'wiki':
                    global $ID;
                    $oldid = $ID;
                    list($ID, $data) = explode('|', $val, 2);
                    //use ID from first value of the multivalued line
                    if ($data == null) {
                        $data = $ID;
                        $ID = $storedID;
                    } else {
                        $storedID = $ID;
                    }
                    $data = $this->_addPrePostFixes($column['type'], $data);
                    // Trim document_{start,end}, p_{open,close} from instructions
                    $allinstructions = p_get_instructions($data);
                    $wraps = 1;
                    if (isset($allinstructions[1]) && $allinstructions[1][0] == 'p_open') {
                        $wraps++;
                    }
                    $instructions = array_slice($allinstructions, $wraps, -$wraps);
                    $outs[] = p_render('xhtml', $instructions, $byref_ignore);
                    $ID = $oldid;
                    break;
                default:
                    $val = $this->_addPrePostFixes($column['type'], $val);
                    //type '_img' or '_img<width>'
                    if (substr($type, 0, 3) == 'img') {
                        $width = (int) substr($type, 3);
                        if (!$width) {
                            $width = $this->getConf('image_width');
                        }
                        list($mediaid, $title) = explode('|', $val, 2);
                        if ($title === null) {
                            $title = $column['key'] . ': ' . basename(str_replace(':', '/', $mediaid));
                        } else {
                            $title = trim($title);
                        }
                        if (media_isexternal($val)) {
                            $html = $R->externalmedia($mediaid, $title, $align = null, $width, $height = null, $cache = null, $linking = 'direct', true);
                        } else {
                            $html = $R->internalmedia($mediaid, $title, $align = null, $width, $height = null, $cache = null, $linking = 'direct', true);
                        }
                        if (strpos($html, 'mediafile') === false) {
                            $html = str_replace('href', 'rel="lightbox" href', $html);
                        }
                        $outs[] = $html;
                    } else {
                        $outs[] = hsc($val);
                    }
            }
        }
        return join(', ', $outs);
    }

Usage Example

 function testFormatData()
 {
     global $conf;
     $helper = new helper_plugin_data();
     $renderer = new data_dummy_renderer();
     $this->assertEquals('value1, value2, val', $helper->_formatData(array('type' => ''), "value1\n value2\n val", $renderer));
     $this->assertEquals('link: page ', $helper->_formatData(array('type' => 'page'), "page", $renderer));
     $this->assertEquals('link: page title', $helper->_formatData(array('type' => 'title'), "page|title", $renderer));
     $this->assertEquals('link: page title', $helper->_formatData(array('type' => 'pageid'), "page|title", $renderer));
     $this->assertEquals('link: :key:page ', $helper->_formatData(array('type' => 'nspage', 'key' => 'key'), "page", $renderer));
     $conf['mailguard'] = '';
     $this->assertEquals('<a href="mailto:pa:ge" class="mail" title="pa:ge">pa:ge</a>', $helper->_formatData(array('type' => 'mail'), "pa:ge", $renderer));
     $this->assertEquals('<a href="mailto:pa:ge" class="mail" title="pa:ge">some user</a>', $helper->_formatData(array('type' => 'mail'), "pa:ge some user", $renderer));
     $conf['mailguard'] = 'visible';
     $this->assertEquals('<a href="mailto:pa%3Age" class="mail" title="pa%3Age">pa:ge</a>', $helper->_formatData(array('type' => 'mail'), "pa:ge", $renderer));
     $this->assertEquals('<a href="mailto:pa%3Age" class="mail" title="pa%3Age">some user</a>', $helper->_formatData(array('type' => 'mail'), "pa:ge some user", $renderer));
     $this->assertEquals('<a href=\'url\' class=\'urlextern\' rel="nofollow">url</a>', $helper->_formatData(array('type' => 'url'), "url", $renderer));
     $this->assertEquals('<a href="' . wl('start', array('dataflt[0]' => '_=value')) . '" title="Show pages matching \'value\'" class="wikilink1">value</a>', $helper->_formatData(array('type' => 'tag'), "value", $renderer));
     $this->assertEquals(strftime('%Y/%m/%d %H:%M', 1234567), $helper->_formatData(array('type' => 'timestamp'), "1234567", $renderer));
     $this->assertEquals('<strong>bla</strong>', $helper->_formatData(array('type' => 'wiki'), '|**bla**', $renderer));
     $this->assertEquals('<a href="' . ml('wiki:dokuwiki-128.png') . '" class="media" rel="lightbox"><img src="' . ml('wiki:dokuwiki-128.png', array('w' => 300)) . '" alt=": dokuwiki-128.png" title=": dokuwiki-128.png" width="300" /></a>', $helper->_formatData(array('type' => 'img300'), 'wiki:dokuwiki-128.png', $renderer));
 }
All Usage Examples Of helper_plugin_data::_formatData