Skip to content

Commit

Permalink
Removed usage of Framework::$charset and simplified Page (now always …
Browse files Browse the repository at this point in the history
…HTML5 doctype)
  • Loading branch information
bfanger committed Jul 27, 2018
1 parent d122e21 commit 82c31a2
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 98 deletions.
68 changes: 63 additions & 5 deletions src/CrudFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Exception;
use Sledgehammer\Mvc\Document\Xml;
use Sledgehammer\Core\Framework;
use Sledgehammer\Core\Json;

/**
* Folder for basic CRUD operations on a Repository model.
Expand Down Expand Up @@ -41,7 +43,7 @@ public function generateContent()
try {
return parent::generateContent();
} catch (Exception $e) {
return Json::error($e);
return static::error($e);
}
}

Expand Down Expand Up @@ -93,7 +95,7 @@ protected function read()
$instance = $this->load($_REQUEST[$this->primaryKey]);
$data = $this->extract($instance, $this->maxRecursion);

return Json::succes($data);
return static::success($data);
}

private function load($id)
Expand All @@ -115,7 +117,7 @@ private function update()
$repo = getRepository($this->repository);
$repo->save($this->model, $instance);

return Json::success();
return static::success();
}

/**
Expand All @@ -129,7 +131,7 @@ private function create()
$instance = $repo->create($this->model, $this->getNewValues());
$repo->save($model, $instance);
// redirect();
// return Json::success($this->primaryKey => $instance->{$this->primaryKey});
// return static::success($this->primaryKey => $instance->{$this->primaryKey});
}

/**
Expand Down Expand Up @@ -164,7 +166,7 @@ private function delete()
$repo = getRepository($this->repository);
$repo->delete($this->model, $_POST[$this->primaryKey]);
//throw new Exception('Verwijderen van '.$this->subject.' #'.$_POST[$this->primarykey].' is mislukt');
return Json::success();
return static::success();
}

/**
Expand All @@ -191,4 +193,60 @@ protected function format($data, $format)
}
return new Json($data);
}


/**
* Reports the error/exception to the ErrorHandler and returns the error as Json object.
* The javascript client should detect and report the error to the user:
* if (result.success !== true) { alert(result.error); }.
*
* @param string|Exception $error The error message or Exception
* @param int $http The HTTP status code (defaults to 400 Bad Request)
*
* @return Json
*/
protected static function error($error, $http = 400)
{
// if (headers_sent() === false && DebugR::isEnabled()) {
// ErrorHandler::instance()->html = false;
// }
if ($error instanceof Exception) {
\Sledgehammer\report_exception($error);
$error = $error->getMessage();
} else {
\Sledgehammer\warning($error);
}

return new Json([
'success' => false,
'error' => $error,
], [
'http' => [
'Status' => $http.' '.Framework::$statusCodes[$http],
'Content-Type' => 'application/json'
// 'Content-Type' => (ErrorHandler::instance()->html ? 'text/html; charset=utf-8' : 'application/json'),
],
]);
}

/**
* Short for "new Json(array('success' => true))".
*
* @param mixed $data [optional] The data payload
*
* @return Json
*/
protected static function success($data = null)
{
if ($data === null) {
return new Json(array(
'success' => true,
));
}

return new Json(array(
'success' => true,
'data' => $data,
));
}
}
37 changes: 8 additions & 29 deletions src/Document/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,17 @@
*/
class Page extends Base implements Document
{
/**
* Bepaald de template die door wordt gebruikt. xhtml, html of ajax.
*
* @var string
*/
public $doctype;

/**
* The value of the "ContentType: " header.
* Set to "application/xhtml+xml" for XHTML.
*/
public $contentType = 'text/html';

/**
* @var Component
*/
public $content;

/**
* Bepaald of de statusbalk getoond word. (Wordt automatisch bepaald door de ErrorHandler->html waarde).
*
* @var bool
*/
public $showStatusbar;
public $statusbar = false;

/**
* Tags die in de <head> vallen.
Expand All @@ -54,13 +42,6 @@ class Page extends Base implements Document
*/
private $headers;

public function __construct($doctype = 'html')
{
$this->doctype = $doctype;
$this->contentType = 'text/html; charset='.strtolower(Framework::$charset);
$this->showStatusbar = ErrorHandler::instance()->html; // Als er html error getoond mogen worden, toon dan ook de statusbalk.
}

/**
* Vraag de headers op en werk de interne headers array bij.
*
Expand All @@ -70,17 +51,16 @@ public function getHeaders()
{
$headers = [
'http' => [
'Content-Type' => $this->contentType,
'Content-Type' => 'text/html; charset=utf-8',
],
'charset' => Framework::$charset,
'charset' => 'UTF-8',
'htmlParameters' => [],
'bodyParameters' => [],
];
if (defined('Sledgehammer\WEBPATH') && \Sledgehammer\WEBPATH != '/' && file_exists(\Sledgehammer\PATH.'application/public/favicon.ico')) {
$headers['link']['favicon'] = ['rel' => 'shortcut icon', 'href' => \Sledgehammer\WEBROOT.'favicon.ico', 'type' => 'image/x-icon'];
}
// $headers['http']['Content-Type'] = 'application/xhtml+xml';
if (ErrorHandler::instance()->html) {
if ($this->statusbar) {
$headers['css']['debug'] = \Sledgehammer\WEBROOT.'core/css/debug.css';
}
$this->headers = \Sledgehammer\merge_headers($headers, $this->content);
Expand All @@ -106,7 +86,7 @@ public function render()
'htmlParameters' => \Sledgehammer\implode_xml_parameters($this->headers['htmlParameters']),
'bodyParameters' => \Sledgehammer\implode_xml_parameters($this->headers['bodyParameters']),
'body' => $this->content,
'showStatusbar' => $this->showStatusbar,
'statusbar' => $this->statusbar,
];

$validHeaders = ['http', 'title', 'charset', 'css', 'meta', 'link', 'javascript', 'htmlParameters', 'bodyParameters'];
Expand All @@ -132,10 +112,9 @@ public function render()
$head['link'][] = ['href' => $url, 'type' => 'text/css', 'rel' => 'stylesheet'];
}
}
$eot = ($this->doctype === 'xhtml') ? ' />' : '>'; // End of Tag instellen
foreach ($head as $tag => $tags) {
foreach ($tags as $parameters) {
$variables['head'][] = '<'.$tag.\Sledgehammer\implode_xml_parameters($parameters).$eot;
$variables['head'][] = '<'.$tag.\Sledgehammer\implode_xml_parameters($parameters).'>';
}
}
if (isset($this->headers['javascript'])) {
Expand All @@ -148,7 +127,7 @@ public function render()
$variables['head'][] = ob_get_clean();
}
}
$template = new Template('sledgehammer/mvc/templates/doctype/'.$this->doctype.'.php', $variables);
$template = new Template('sledgehammer/mvc/templates/page.php', $variables);
$template->render();
}

Expand Down
5 changes: 1 addition & 4 deletions src/Document/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getHeaders()
*
* @return SimpleXMLElement
*/
public static function build($data, $charset = null)
public static function build($data, $charset = 'UTF-8')
{
if (is_object($data)) {
$root = get_class($data);
Expand All @@ -81,9 +81,6 @@ public static function build($data, $charset = null)
$root = key($data);
$elements = current($data);
}
if ($charset === null) {
$charset = Framework::$charset;
}
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="'.$charset.'"?><'.$root.' />');
self::addNodes($xml, $elements, $root);

Expand Down
5 changes: 1 addition & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@ function is_valid_component(&$component = '__UNDEFINED__')
*
* @return string
*/
function implode_xml_parameters($parameterArray, $charset = null)
function implode_xml_parameters($parameterArray, $charset = 'UTF-8')
{
$xml = '';
if ($charset === null) {
$charset = Framework::$charset;
}
foreach ($parameterArray as $key => $value) {
$xml .= ' '.$key.'="'.htmlentities($value, ENT_COMPAT, $charset).'"';
}
Expand Down
16 changes: 9 additions & 7 deletions templates/dialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Sledgehammer\Core\Url;

echo "<div class=\"modal-dialog\">\n";
echo "\t<div class=\"modal-content\">\n";
echo "\t\t<div class=\"modal-header\">";
echo " <div class=\"modal-content\">\n";
echo " <div class=\"modal-header\">";
echo '<h4 class="modal-title">';
echo Html::escape($title), "</h4></div>\n";
echo "\t\t<div class=\"modal-body\">\n\t\t\t", $body, "\n\t\t</div>\n";
echo " <div class=\"modal-body\">\n";
echo " ", $body, "\n";
echo " </div>\n";
if (count($choices) !== 0) {
echo "\t\t<form class=\"modal-footer\" action=\"".Url::getCurrentURL().'" method="'.$method."\">\n";
echo " <form class=\"modal-footer\" action=\"".Url::getCurrentURL().'" method="'.$method."\">\n";
$indexed = \Sledgehammer\is_indexed($choices);
foreach (array_reverse($choices) as $answer => $choice) {
if (is_array($choice) === false) {
Expand All @@ -25,9 +27,9 @@
$choice['value'] = $answer;
}
$button = new Button($choice);
echo "\t\t\t", $button, "\n";
echo " ", $button, "\n";
}
echo "\t\t</form>\n";
echo " </form>\n";
}
echo "\t</div>\n";
echo " </div>\n";
echo "</div>\n";
6 changes: 0 additions & 6 deletions templates/doctype/ajax.php

This file was deleted.

28 changes: 0 additions & 28 deletions templates/doctype/xhtml.php

This file was deleted.

8 changes: 4 additions & 4 deletions templates/httperror.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="HttpError">
<img style="float:left; margin: 6px 15px 15px 0" src="<?php echo Sledgehammer\WEBROOT.'mvc/img/'.$icon.'.png'; ?>" alt="" />
<h2><?php echo $title; ?></h2>
<p><?php echo $message; ?></p>
<br style="clear: both" />
<img style="float:left; margin: 6px 15px 15px 0" src="<?php echo Sledgehammer\WEBROOT.'mvc/img/'.$icon.'.png'; ?>" alt="" />
<h2><?php echo $title; ?></h2>
<p><?php echo $message; ?></p>
<br style="clear: both" />
</div>
12 changes: 4 additions & 8 deletions templates/doctype/html.php → templates/page.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
<?php
/**
* HTML5
*/
?><!DOCTYPE html>
<!DOCTYPE html>
<html<?php echo $htmlParameters; ?>>
<head>
<title><?php echo $title; ?></title>
<title><?php echo $title; ?></title>
<?php
foreach ($head as $html) {
echo "\t".$html."\n";
echo " ".$html."\n";
}
?>
</head>
<?php flush(); ?>
<body<?php echo $bodyParameters; ?>>

<?php render($body); ?>
<?php if ($showStatusbar) {
<?php if ($statusbar) {
include(__DIR__.'/../statusbar.php');
} ?>

Expand Down
6 changes: 3 additions & 3 deletions templates/statusbar.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

<div class="statusbar">
<a href="#" onclick="this.parentNode.style.display='none';return false" class="statusbar-close">&times;</a>
<?php \Sledgehammer\statusbar(true); ?>
<script type="text/javascript" src="<?php echo Sledgehammer\WEBROOT ?>mvc/js/statusbar.js"></script>
<a href="#" onclick="this.parentNode.style.display='none';return false" class="statusbar-close">&times;</a>
<?php \Sledgehammer\statusbar(true); ?>
<script type="text/javascript" src="<?php echo Sledgehammer\WEBROOT ?>mvc/js/statusbar.js"></script>
</div>

0 comments on commit 82c31a2

Please sign in to comment.