Files
DomFramework/src/Renderer.php

175 lines
5.5 KiB
PHP

<?php
/** DomFramework
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework;
/** Display the data in HTML with a FLASH method to display the errors
*/
class Renderer
{
/** The result in construction */
public $result = NULL;
/** The output type */
public $output = NULL;
/** Title by default : space to be compatible with HTML5 */
public $title = " ";
/** Filename of class containing the presentation layer */
public $viewClass = FALSE;
/** Method apply to class object to display the $result */
public $viewMethod = FALSE;
/** Filename in views containing the HTML layout. Without .html at the end */
public $layout = FALSE;
/** Array to search/replace */
public $replacement = array();
/** Array to variable definition */
public $variable = array ();
/** Display the $this->result result in the output model defined by
$this->output */
public function run ()
{
// If the output is not defined in domframework, it will not be loaded but
// without error, it can be loaded by the autoloader
//@require_once ("domframework/output$this->output.php");
$class = __NAMESPACE__."\\Output$this->output";
$obj = new $class ();
$res = call_user_func_array (array ($obj, "out"),
array ($this->result, $this->title,
$this->viewClass, $this->viewMethod, $this->layout,
$this->replacement, $this->variable));
echo $res;
}
/** Add a flash message to the user, on the next HTML page.
In CLI, display the message immediately.
Message must be translated before sending to the function
@param integer|string $priority Priority 1:success, 2:info, 3:warning,
4:error or textual : SUCCESS, INFO, WARNING, ERROR
@param string $message Message to display */
static public function flash ($priority, $message)
{
if (php_sapi_name () === "cli")
{
switch ($priority)
{
case 1:
case "SUCCESS":
echo "SUCCESS: $message\n";
break;
case 2:
case "INFO":
echo "INFO: $message\n";
break;
case 3:
case "WARNING":
echo "WARNING: $message\n";
break;
case 4:
case "ERROR":
echo "ERROR: $message\n";
break;
default:
throw new \Exception ("Unknown flash priority '$priority'", 500);
}
}
else
{
if (! isset ($_SESSION))
throw new \Exception ("No session available to store flash", 500);
switch ($priority)
{
case 1:
case "SUCCESS":
$_SESSION["renderer"]["flash"][] = array (1, $message);
break;
case 2:
case "INFO":
$_SESSION["renderer"]["flash"][] = array (2, $message);
break;
case 3:
case "WARNING":
$_SESSION["renderer"]["flash"][] = array (3, $message);
break;
case 4:
case "ERROR":
$_SESSION["renderer"]["flash"][] = array (4, $message);
break;
default:
throw new \Exception ("Unknown flash priority '$priority'", 500);
}
}
}
/** Display the HTML page with the alert box if needed
* @param string $viewclass The view class to use to create the output
* @param string $viewmethod The view method to use to create the output
* @param mixed $data The data to display.
* @param array|null $replacement Replace some values by some others in the
* created HTML page
* @param string|null $layoutfile The layout page to use as base
* @param object|null $route A previous defined "route" object
*/
static function html ($viewclass, $viewmethod, $data, $replacement = array(),
$layoutfile = "views/layout.html", $route = null)
{
if ($layoutfile !== false && ! file_exists ($layoutfile))
throw new \Exception ("Layout file $layoutfile not found", 500);
if ($route === null)
$route = new Route ();
// Return a $dataflash with the displayed flash in Bootstrap
$dataflash = "";
if (isset ($_SESSION["renderer"]["flash"]))
{
foreach ($_SESSION["renderer"]["flash"] as $flash)
{
$dataflash .= "<div class='alert ";
switch ($flash[0])
{
case 4:
$dataflash .= "alert-danger";
$alert = dgettext ("domframework", "Error!");
break;
case 3:
$dataflash .= "alert-warning";
$alert = dgettext ("domframework", "Warning!");
break;
case 2:
$dataflash .= "alert-info";
$alert = dgettext ("domframework", "Info :");
break;
case 1:
$dataflash .= "alert-success";
$alert = dgettext ("domframework", "Success : ");
break;
}
$dataflash .= " alert-dismissable'>\n";
$dataflash .= "<button type='button' class='close' ".
"data-dismiss='alert'";
$dataflash .= " aria-hidden='true'>&times;</button>\n";
$dataflash .= "<strong>$alert</strong> ".$flash[1]."\n";
$dataflash .= "</div>\n";
}
unset ($_SESSION["renderer"]["flash"]);
}
$html = new Outputhtml ();
$replacement = array_merge ($replacement,
array ("{baseurl}"=>$route->baseURL (),
"{baseurlresource}"=>$route->baseURLresource (),
"{flash}"=>$dataflash));
echo $html->out ($data,
FALSE,
$viewclass, $viewmethod,
$layoutfile,
$replacement);
}
}