Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from fykosak/dev-remove-newsfeed
Browse files Browse the repository at this point in the history
remove submodules
  • Loading branch information
cevro authored Jul 22, 2021
2 parents 4e15f85 + 7daadb7 commit 58ccd9e
Show file tree
Hide file tree
Showing 11 changed files with 469 additions and 13 deletions.
6 changes: 0 additions & 6 deletions .gitmodules

This file was deleted.

2 changes: 1 addition & 1 deletion FYKOSTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static function getFullNav(): BootstrapNavBar
$fullMenu->addLangSelect('justify-content-end');
return $fullMenu;
}

private static function getPrimaryNav(): BootstrapNavBar
{
$primaryMenu = new BootstrapNavBar('primary', 'navbar bg-light');
Expand Down
257 changes: 257 additions & 0 deletions inc/BootstrapNavBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
<?php

namespace FYKOS\dokuwiki\template\NavBar;

use dokuwiki\Menu\AbstractMenu;
use dokuwiki\Menu\PageMenu;
use dokuwiki\Menu\SiteMenu;
use dokuwiki\Menu\UserMenu;

class BootstrapNavBar
{

private array $data = [];

private ?string $brand = null;

private string $className;

private string $id;

public function __construct(string $id, string $className)
{
$this->id = $id;
$this->className = $className;
}

public function addTools(?string $class = '', bool $allowLoggedInOnly = false): void
{
global $INFO;
global $lang;
if ($allowLoggedInOnly && !$INFO['userinfo']['name']) {
return;
}
$userName = (($INFO['userinfo']['name'] != null) ? ($lang['loggedinas'] .
$INFO['userinfo']['name']) : tpl_getLang('nologin'));

$this->data[] = [
'class' => 'nav ' . $class,
'data' => [
new NavBarItem(null, '<span class="nav-item fa fa-cogs"></span>', 1, null),
new NavBarItem(null, '<div class="dropdown-item"><span class="fa fa-user"></span>' . $userName . '</div>', 2, null),
... $this->addUserMenu(),
... $this->addPageMenu(),
... $this->addSiteMenu(),
],
];
}

public function addBrand(string $href = '', ?string $text = null, ?string $imageSrc = null): void
{
$html = '<a class="navbar-brand" href="' . wl(cleanID($href)) . '">';
if ($imageSrc) {
$html .= '<img src="' . tpl_basedir() . $imageSrc .
'" width="30" height="30" class="d-inline-block align-top" alt="">';
}
if ($text) {
$html .= $text;
}
$html .= '</a>';
$this->brand = $html;
}


public function render(): string
{
$html = '<nav class="navbar navbar-toggleable-md ' . $this->className . '">';
if ($this->brand) {
$html .= $this->brand;
}
$html .= '
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="' . '#mainNavbar' . $this->id . '"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation">';

$html .= '<span class="navbar-toggler-icon"></span>';
$html .= '</button>
<div class="collapse navbar-collapse" id="mainNavbar' . $this->id . '">';
foreach ($this->data as $item) {
$html .= $this->renderItem($item['data'], $item['class']);
}
$html .= '</div>';
$html .= '</nav>';
return $html;
}

/**
* @param $filename string
* @return NavBarItem[]
*/
private function parseMenuFile(string $filename): array
{
$filePath = wikiFN($filename);
$data = [];
if (file_exists($filePath)) {
$lines = array_filter(file($filePath),
function ($line) {
return preg_match('/^\s+\*/', $line);
});

$numLines = count($lines);
for ($i = 0; $i < $numLines; $i++) {
if (!$lines[$i]) {
continue;
}
[$prefix, $content] = explode('*', $lines[$i]);
$level = (int)strlen($prefix) / 2;
$level = ($level > 2) ? 2 : $level;

if (!preg_match('/\s*\[\[[^\]]+\]\]/', $content)) {
continue;
}
$content = str_replace([']', '['], '', trim($content));
[$id, $content, $icon] = explode('|', $content);
$data[] = new NavBarItem($id, $content, $level, $icon);
}
}
return $data;
}

public function addMenuText(string $file, ?string $class = null): void
{
global $conf;
$pageLang = $conf['lang'];
$menuFileName = 'system/' . $file . '_' . $pageLang;

$this->data[] = [
'class' => 'nav ' . $class ?? '',
'data' => $this->parseMenuFile($menuFileName),
];
}

public function addLangSelect(?string $class = null): void
{
global $conf;
$data = [];
if (!isset($conf['available_lang']) || !is_countable($conf['available_lang']) || !count($conf['available_lang'])) {
return;
}
$data[] = new NavBarItem(null, '<span class="fa fa-language"></span>', 1, null);

foreach ($conf['available_lang'] as $currentLang) {
$data[] = new NavBarItem(null, '<a
href="' . $currentLang['content']['url'] . '"
class="dropdown-item ' . $currentLang['content']['class'] . ' ' .
($currentLang['code'] == $conf['lang'] ? 'active' : '') . '"
' . $currentLang['content']['more'] . '
>' . $currentLang['content']['text'] . ' </a> ', 2, null);
}
$this->data[] = [
'class' => 'nav ' . $class ?? '',
'data' => $data,
];
}

/**
* @return NavBarItem[]
*/
private function addUserMenu(): array
{
global $lang;
return [
new NavBarItem(null, '<div class="dropdown-header"><span class="glyphicon glyphicon-user"></span> ' .
$lang['user_tools'] . ' .</div>', 2, null),
...$this->prepareMenuItems(new UserMenu()),
];
}

/**
* @return NavBarItem[]
*/
private function addSiteMenu(): array
{
global $lang;
return [new NavBarItem(null, '<div class="dropdown-header"><span class="glyphicon glyphicon-user"></span> ' .
$lang['site_tools'] . ' .</div>', 2, null),
...$this->prepareMenuItems(new SiteMenu()),
];
}

/**
* @return NavBarItem[]
*/
private function addPageMenu(): array
{
global $lang;
return [
new NavBarItem(null, '<div class="dropdown-header"><span class="fa fa-user-o"></span>' . $lang['page_tools'] . '</div>', 2, null),
...$this->prepareMenuItems(new PageMenu()),
];
}

private function prepareMenuItems(AbstractMenu $menu): array
{
$data = [];
foreach ($menu->getItems() as $item) {
$data[] = new NavBarItem(null, $item->asHtmlLink('dropdown-item '), 2, null);
}
return $data;
}

/**
* @param NavBarItem[] $data
* @param string $class
* @return string
*/
private function renderItem(array $data, string $class): string
{
$inLI = false;
$inUL = false;

$html = ' <div class="nav navbar-nav ' . $class . '" > ';

foreach ($data as $k => $item) {
$link = $item->getLink();
$title = (isset($item->icon) ? ' <span class="' . $item->icon . '" ></span > ' : '') . $item->content;
if ($item->level == 1) {
if ($inUL) {
$inUL = false;
$html .= '</div>';
}
if ($inLI) {
$inLI = false;
$html .= '</div>';
}
/* is next level 2? */
if (isset($data[$k + 1]) && $data[$k + 1]->level == 2) {
$inLI = true;
$html .= '<div class="dropdown nav-item"><a href="' . $link .
'" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >' .
$title . '<span class="caret"></span></a>';
} else {
$html .= '<a class="nav-item nav-link" href="' . $link . '">' . $title . '</a>';
}
} elseif ($item->level == 2) {
if (!$inUL) {
$inUL = true;
$html .= '<div class="dropdown-menu" role="menu">' . "\n";
}

if (isset($item->pageId)) {
$html .= '<a class="dropdown-item" href="' . $link . '">' . $title . '</a>';
} else {
$html .= $title;
}
}
}
if ($inUL) {
$html .= '</div>';
}
if ($inLI) {
$html .= '</div>';
}
$html .= '</div>';
return $html;
}
}
116 changes: 116 additions & 0 deletions inc/Jumbotron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace fksTemplate\Jumbotron;

use FYKOS\dokuwiki\template\NavBar\BootstrapNavBar;

require_once(dirname(__FILE__) . '/JumbotronItem.php');

final class Jumbotron
{
public static function render(BootstrapNavBar $secondMenu, string $pageId): string
{
$items = self::getItemsByPage($pageId);
$html = '<div class="container-fluid header-image jumbotron">';
$html .= '<div class="carousel-container">';
$html .= self::innerRender($items);
$html .= '</div>';
$html .= '<div class="row nav-container hidden-md-down second-nav">';
$html .= $secondMenu->render();
$html .= '</div>';
$html .= '</div>';
return $html;
}

public static function innerRender(array $items): string
{
$id = uniqid();
$indicators = [];
$parsedItems = [];
foreach ($items as $key => $item) {
$indicators[] = '<li data-target="#' . $id . '" data-slide-to="' . $key . '"></li>';
$parsedItems[] = self::getCarouselItem($item, !$key);
}
$html = '<div id="' . $id . '" class="feed-carousel carousel slide mb-3" data-ride="carousel">';

$html .= '<ol class="carousel-indicators">';
$html .= join('', $indicators);
$html .= '</ol>';


$html .= '<div class="carousel-inner" role="listbox">';
$html .= join('', $parsedItems);
$html .= '</div>';

$html .= '</div>';
return $html;
}

private static function getCarouselItem(JumbotronItem $item, bool $active): string
{
$style = null;
if (is_string($item->backgrounds['outer'])) {
$style = 'background-image: url(' . ml($item->backgrounds['outer'], ['w' => 1200]) . ')';
}

$html = '<div class="carousel-item ' .
($style ? '' : 'bg-' . $item->backgrounds['inner'] . '-fade ') .
($active ? ' active' : '') .
'" style="' . ($style ?? '') . '">
<div class="mx-auto col-lg-8 col-xl-5">
<div class=" jumbotron-inner-container d-block ' . ($style ? 'bg-' . $item->backgrounds['inner'] . '-fade ' : '') . '">';

$html .= '<h1>' . hsc($item->headline) . '</h1>';
$html .= '<p>' . p_render('xhtml', p_get_instructions($item->text), $info) . '</p>';
$html .= self::getButtons($item);

$html .= '</div></div></div>';
return $html;
}

private static function getButtons(JumbotronItem $item): string
{
$html = '';
foreach ($item->buttons as $button) {
$id = $button['page'];

if (preg_match('|^https?://|', $id)) {
$href = hsc($id);
} else {
$href = wl($id, null, true);
}
$html .= '<p><a class="btn btn-outline-secondary" href="' . $href . '">' . $button['title'] . '</a></p>';

}
return $html;
}

private static function getDataFromJSON(string $dataPage): array
{
$content = io_readFile(wikiFN($dataPage));
$data = json_decode($content, true);
$items = [];
if ($data) {
foreach ($data as $datum) {
$items[] = new JumbotronItem($datum);
}
}
return $items;
}

/**
* @param string $page
* @return JumbotronItem[]
*/
public static function getItemsByPage(string $page): array
{
switch ($page) {
case 'start':
return self::getDataFromJSON('jumbotron-data-cs');
case 'en':
return self::getDataFromJSON('jumbotron-data-en');
default:
return [];
}
}
}
Loading

0 comments on commit 58ccd9e

Please sign in to comment.