dw-whoisinyourhackspace/syntax.php

102 lines
2.8 KiB
PHP
Raw Normal View History

2021-02-16 17:05:26 +01:00
<?php
/**
* DokuWiki Plugin whoisinyourhackspace (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Tim Schumacher <tim.daniel.schumacher@gmail.com>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'syntax.php';
class syntax_plugin_whoisinyourhackspace extends DokuWiki_Syntax_Plugin {
public function getType() {
return 'substition';
}
public function getPType() {
return 'block';
}
public function getSort() {
return 0;
}
public function connectTo($mode) {
$this->Lexer->addSpecialPattern("(?:\[wiyh '.*?'\]|\[wiyh\])", $mode,'plugin_whoisinyourhackspace');
2021-02-16 17:05:26 +01:00
}
public function handle($match, $state, $pos, Doku_Handler $handler)
{
if (DOKU_LEXER_SPECIAL !== $state) {
return [];
}
2021-02-16 17:05:26 +01:00
$matches = [];
2021-02-16 17:05:26 +01:00
if (false !== preg_match("/wiyh '(.*?)'/", $match, $matches)) {
return [$state, $match, $matches[1] ?? null];
}
2021-02-16 17:05:26 +01:00
return [$state, $match];
}
2021-02-16 17:05:26 +01:00
public function render($format, Doku_Renderer $renderer, $data) {
global $conf;
2021-02-16 17:05:26 +01:00
[$state, $match, $url] = $data;
2021-02-16 17:05:26 +01:00
if($format != 'xhtml') return false;
2021-02-16 17:05:26 +01:00
$api_path = $url ?: $this->getConf('api_path');
2021-02-16 17:05:26 +01:00
if (empty ($api_path)) {
$renderer->doc .= "<div>{$this->getLang('wiyh_no_api_path')}</div>";
2021-02-16 17:05:26 +01:00
return true;
}
if (false === $file = file_get_contents($api_path)) {
$renderer->doc .= "<div>{$this->getLang('wiyh_fetch_error')}</div>";
2021-02-16 17:05:26 +01:00
return true;
}
$api = json_decode($file);
$content = '';
$content .= '<div class="hackerspace-room-state">';
$content .= "<h3>" . $this->getLang('wiyh_heading') . "</h3>";
if ($api->state->open) {
2021-02-18 06:41:03 +01:00
$message = "{$api->space} {$this->getLang('wiyh_open')}";
$content .= $api->state->icon->open ? "<img class=\"icon\" src=\"{$api->state->icon->open}\" alt=\"{$message}\" title=\"{$message}\" />" : '';
$content .= "<p class=\"text\">{$message}</p>";
} else {
2021-02-18 06:41:03 +01:00
$message = "{$api->space} {$this->getLang('wiyh_closed')}";
2021-02-17 22:21:25 +01:00
$content .= $api->state->icon->closed ? "<img class=\"icon\" src=\"{$api->state->icon->closed}\" alt=\"{$message}\" title=\"{$message}\" />" : '';
$content .= "<p class=\"text\">{$message}</p>";
}
$date = new DateTime();
$date->setTimeStamp($api->state->lastchange);
2021-02-18 06:44:26 +01:00
$content .= "<p class=\"text\">{$this->getLang('wiyh_since')} " . $date->format('d.m.Y H:i') . " Uhr</p>";
$content .= '<hr />';
$content .= '</div>';
$renderer->doc .= $content;
return true;
}
2021-02-16 17:05:26 +01:00
}
// vim:ts=4:sw=4:et: