Files
DomFramework/renderer.php

122 lines
3.7 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
/** Display the datas 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 (!@require_once ("output$this->output.php"))
throw new Exception ("Renderer '$this->output' not found", 500);
$class = "output$this->output";
$reflection = new ReflectionMethod ($class, "out");
$res = $reflection->invokeArgs (new $class,
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);
}
}
}
/** @param mixed $data The data to display.*/
static function html ($viewclass, $viewmethod, $data,
$layoutfile = "views/layout.html", $route = null)
{
if ($layoutfile !== false && ! file_exists ($layoutfile))
throw new Exception ("Layout file $layoutfile not found", 500);
require_once ("domframework/outputhtml.php");
if ($route === null)
$route = new route ();
$html = new outputhtml ();
$replacement = array ("{baseurl}"=>$route->baseURL (),
"{baseurlresource}"=>$route->baseURLresource ());
echo $html->out ($data,
FALSE,
$viewclass, $viewmethod,
$layoutfile,
$replacement);
}
}