. * * @package NanoMvc * @author Nicolas Joseph * @copyright 2008 Nicolas Joseph * @license http://www.opensource.org/licenses/gpl-3.0.html GPL v3 * @filesource */ class Helper_Page { /** *@var string */ private $title; /** *@var array */ private $metas; /** *@var array */ private $css; /** *@var array */ private $javascripts; /** * @var Page */ private static $instance = null; private static function get_dirname () { return dirname ($_SERVER['SCRIPT_NAME']); } private static function build_path ($dir, $filename) { $path = $dir; if (substr ($path, -1) != '/') { $path .= '/'; } $path .= $filename; return $path; } private function __construct () { $this->metas = array (); } /** * Obtenir l'unique instance de la classe. * * @return Page */ public static function get_instance () { if (is_null (self::$instance)) { self::$instance = new self (); } return self::$instance; } public static function set_title ($title) { self::get_instance ()->title = $title; } public static function render_title () { echo self::get_instance ()->title; } public static function add_meta ($name, $content, $http_equiv = false) { $metas = self::get_instance ()->metas; array_push ($metas, array( 'name' => $name, 'content' => $content, 'http_equiv' => $http_equiv )); self::get_instance ()->metas = $metas; } static function render_metas () { $metas = self::get_instance ()->metas; $html = array (); if ($metas) { foreach ($metas as $meta) { if ($meta['http_equiv']) { $html[] = ''; } else { $html[] = ''; } } } echo implode ("\n", $html) ."\n"; } public static function add_css ($css, $media='screen') { $css_store = self::get_instance ()->css; $css_store[] = array ('file'=>$css, 'media'=>$media); self::get_instance()->css = $css_store; } public static function render_css () { $css_store = self::get_instance ()->css; $html = array (); if ($css_store) { foreach ($css_store as $css) { if ($css['media']) { $media = 'media="'. $css['media'] .'"'; } $uri = self::build_path (self::get_dirname (), $css['file']); $html[] = ''; } } echo implode ("\n", $html) ."\n"; } public static function add_javascript ($javascript) { $javascripts_store = self::get_instance ()->javascripts; $javascripts_store[] = $javascript; self::get_instance ()->javascripts = $javascripts_store; } public static function render_javascripts () { $javascripts_store = self::get_instance ()->javascripts; $html = array (); if ($javascripts_store) { foreach ($javascripts_store as $javascript) { $uri = self::build_path (self::get_dirname (), $javascript); $html[] = ''; } } echo implode ("\n", $html) ."\n"; } /** * Retourne l'URL d'une page en fonction de la page et de l'action souhaitee. * * @param string page: la page * @param string action: l'action * @return string */ public static function get_url ($page, $action='') { return $_SERVER['SCRIPT_NAME'] .'/'. $page .'/'. $action; } /** * Retourne l'URL d'un fichier. * * @param string file: chemin relatif du fichier. * @return string */ public static function url ($file) { return self::build_path (self::get_dirname (), $file); } public static function tpl_dir () { return self::build_path (self::get_dirname (), 'template/'); } } function url ($file) { echo Helper_Page::url ($file); }