Skip to content

Commit

Permalink
Refactor Header class to use templates
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
  • Loading branch information
MauricioFauth committed Sep 23, 2018
1 parent 05b064c commit a686ff8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 110 deletions.
142 changes: 32 additions & 110 deletions libraries/classes/Header.php
Expand Up @@ -120,11 +120,18 @@ class Header
*/
private $userPreferences;

/**
* @var Template $template
*/
private $template;

/**
* Creates a new class instance
*/
public function __construct()
{
$this->template = new Template();

$this->_isEnabled = true;
$this->_isAjax = false;
$this->_bodyId = '';
Expand Down Expand Up @@ -408,10 +415,11 @@ public function getDisplay(): string
if (! $this->_headerIsSent) {
if (! $this->_isAjax && $this->_isEnabled) {
$this->sendHttpHeaders();
$retval .= $this->_getHtmlStart();
$retval .= $this->_getMetaTags();
$retval .= $this->_getLinkTags();
$retval .= $this->getTitleTag();

$baseDir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR : '';
$uniqueValue = $GLOBALS['PMA_Config']->getThemeUniqueValue();
$themePath = $GLOBALS['pmaThemePath'];
$version = self::getVersionParameter();

// The user preferences have been merged at this point
// so we can conditionally add CodeMirror
Expand All @@ -436,11 +444,22 @@ public function getDisplay(): string
if ($this->_userprefsOfferImport) {
$this->_scripts->addFile('config.js');
}
$retval .= $this->_scripts->getDisplay();
$retval .= '<noscript>';
$retval .= '<style>html{display:block}</style>';
$retval .= '</noscript>';
$retval .= $this->_getBodyStart();

$retval .= $this->template->render('header/header', [
'lang' => $GLOBALS['lang'],
'allow_third_party_framing' => $GLOBALS['cfg']['AllowThirdPartyFraming'],
'is_print_view' => $this->_isPrintView,
'base_dir' => $baseDir,
'unique_value' => $uniqueValue,
'theme_path' => $themePath,
'version' => $version,
'text_dir' => $GLOBALS['text_dir'],
'server' => $GLOBALS['server'] ?? null,
'title' => $this->getTitleTag(),
'scripts' => $this->_scripts->getDisplay(),
'body_id' => $this->_bodyId,
]);

if ($this->_menuEnabled && $GLOBALS['server'] > 0) {
$nav = new Navigation();
$retval .= $nav->getDisplay();
Expand Down Expand Up @@ -633,97 +652,16 @@ public function sendHttpHeaders(): void
$this->_headerIsSent = true;
}

/**
* Returns the DOCTYPE and the start HTML tag
*
* @return string DOCTYPE and HTML tags
*/
private function _getHtmlStart(): string
{
$lang = $GLOBALS['lang'];
$dir = $GLOBALS['text_dir'];

$retval = "<!DOCTYPE HTML>";
$retval .= "<html lang='$lang' dir='$dir'>";
$retval .= '<head>';

return $retval;
}

/**
* Returns the META tags
*
* @return string the META tags
*/
private function _getMetaTags(): string
{
$retval = '<meta charset="utf-8" />';
$retval .= '<meta name="referrer" content="no-referrer" />';
$retval .= '<meta name="robots" content="noindex,nofollow" />';
$retval .= '<meta http-equiv="X-UA-Compatible" content="IE=Edge" />';
$retval .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) {
$retval .= '<style id="cfs-style">html{display: none;}</style>';
}
return $retval;
}

/**
* Returns the LINK tags for the favicon and the stylesheets
*
* @return string the LINK tags
*/
private function _getLinkTags(): string
{
$retval = '<link rel="icon" href="favicon.ico" '
. 'type="image/x-icon" />'
. '<link rel="shortcut icon" href="favicon.ico" '
. 'type="image/x-icon" />';
// stylesheets
$basedir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR : '';
$theme_id = $GLOBALS['PMA_Config']->getThemeUniqueValue();
$theme_path = $GLOBALS['pmaThemePath'];
$v = self::getVersionParameter();

if ($this->_isPrintView) {
$retval .= '<link rel="stylesheet" type="text/css" href="'
. $basedir . 'print.css?' . $v . '" />';
} else {
// load jQuery's CSS prior to our theme's CSS, to let the theme
// override jQuery's CSS
$retval .= '<link rel="stylesheet" type="text/css" href="'
. $theme_path . '/jquery/jquery-ui.css" />';
$retval .= '<link rel="stylesheet" type="text/css" href="'
. $basedir . 'js/vendor/codemirror/lib/codemirror.css?' . $v . '" />';
$retval .= '<link rel="stylesheet" type="text/css" href="'
. $basedir . 'js/vendor/codemirror/addon/hint/show-hint.css?' . $v . '" />';
$retval .= '<link rel="stylesheet" type="text/css" href="'
. $basedir . 'js/vendor/codemirror/addon/lint/lint.css?' . $v . '" />';
$retval .= '<link rel="stylesheet" type="text/css" href="'
. $basedir . 'phpmyadmin.css.php?'
. 'nocache=' . $theme_id . $GLOBALS['text_dir']
. (isset($GLOBALS['server']) ? '&amp;server=' . $GLOBALS['server'] : '')
. '" />';
// load Print view's CSS last, so that it overrides all other CSS while
// 'printing'
$retval .= '<link rel="stylesheet" type="text/css" href="'
. $theme_path . '/css/printview.css?' . $v . '" media="print" id="printcss"/>';
}

return $retval;
}

/**
* Returns the TITLE tag
*
* @return string the TITLE tag
*/
public function getTitleTag(): string
{
$retval = "<title>";
$retval .= $this->_getPageTitle();
$retval .= "</title>";
return $retval;
return $this->template->render('header/title_tag', [
'title' => $this->getPageTitle(),
]);
}

/**
Expand All @@ -732,7 +670,7 @@ public function getTitleTag(): string
*
* @return string
*/
private function _getPageTitle(): string
private function getPageTitle(): string
{
if (strlen($this->_title) == 0) {
if ($GLOBALS['server'] > 0) {
Expand All @@ -755,22 +693,6 @@ private function _getPageTitle(): string
return $this->_title;
}

/**
* Returns the close tag to the HEAD
* and the start tag for the BODY
*
* @return string HEAD and BODY tags
*/
private function _getBodyStart(): string
{
$retval = "</head><body";
if (strlen($this->_bodyId)) {
$retval .= " id='" . $this->_bodyId . "'";
}
$retval .= ">";
return $retval;
}

/**
* Returns some warnings to be displayed at the top of the page
*
Expand Down
30 changes: 30 additions & 0 deletions templates/header/header.twig
@@ -0,0 +1,30 @@
<!doctype html>
<html lang="{{ lang }}" dir="{{ text_dir }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="referrer" content="no-referrer">
<meta name="robots" content="noindex,nofollow">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
{% if not allow_third_party_framing -%}
<style id="cfs-style">html{display: none;}</style>
{%- endif %}

<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
{% if is_print_view %}
<link rel="stylesheet" type="text/css" href="{{ base_dir }}print.css?{{ version }}">
{% else %}
<link rel="stylesheet" type="text/css" href="{{ theme_path }}/jquery/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="{{ base_dir }}js/vendor/codemirror/lib/codemirror.css?{{ version }}">
<link rel="stylesheet" type="text/css" href="{{ base_dir }}js/vendor/codemirror/addon/hint/show-hint.css?{{ version }}">
<link rel="stylesheet" type="text/css" href="{{ base_dir }}js/vendor/codemirror/addon/lint/lint.css?{{ version }}">
<link rel="stylesheet" type="text/css" href="{{ base_dir }}phpmyadmin.css.php?nocache={{ unique_value }}{{ text_dir }}
{{- server is not empty ? '&server=' ~ server }}">
<link rel="stylesheet" type="text/css" href="{{ theme_path }}/css/printview.css?{{ version }}" media="print" id="printcss">
{% endif %}
{{ title|raw }}
{{ scripts|raw }}
<noscript><style>html{display:block}</style></noscript>
</head>
<body{{ body_id is not empty ? ' id=' ~ body_id }}>
1 change: 1 addition & 0 deletions templates/header/title_tag.twig
@@ -0,0 +1 @@
<title>{{ title }}</title>

1 comment on commit a686ff8

@lem9
Copy link
Contributor

@lem9 lem9 commented on a686ff8 Sep 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #14626

Please sign in to comment.