* @category Horde
* @copyright 2012-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Core
*/
class Horde_PageOutput
{
/**
* Output code necessary to perform AJAX operations?
*
* @var boolean
*/
public $ajax = false;
/**
* Stylesheet object.
*
* @var Horde_Themes_Css
*/
public $css;
/**
* Activate debugging output.
*
* @internal
*
* @var boolean
*/
public $debug = false;
/**
* Defer loading of scripts until end of page?
*
* @var boolean
*/
public $deferScripts = true;
/**
* Output code necessary to display growler notifications?
*
* @var boolean
*/
public $growler = false;
/**
* Script list.
*
* @var Horde_Script_List
*/
public $hsl;
/**
* List of inline scripts.
*
* @var array
*/
public $inlineScript = array();
/**
* List of LINK tags to output.
*
* @var array
*/
public $linkTags = array();
/**
* List of META tags to output.
*
* @var array
*/
public $metaTags = array();
/**
* Load the sidebar in this page?
*
* @var boolean
*/
public $sidebar = true;
/**
* Smartmobile init code that needs to be output before jquery.mobile.js
* is loaded.
*
* @since 2.12.0
*
* @var array
*/
public $smartmobileInit = array();
/**
* Load the topbar in this page?
*
* @var boolean
*/
public $topbar = true;
/**
* Has PHP userspace page compression been started?
*
* @var boolean
*/
protected $_compress = false;
/**
* View mode.
*
* @var integer
*/
protected $_view = 0;
/**
* Constructor.
*/
public function __construct()
{
$this->css = new Horde_Themes_Css();
$this->hsl = new Horde_Script_List();
}
/**
* Adds a single javascript script to the output (if output has already
* started), or to the list of script files to include in the output.
*
* @param mixed $file Either a Horde_Script_File object, or the full
* javascript file name.
* @param string $app If $file is a file name, this is the application
* where the file is located. Defaults to the current
* registry application.
*
* @return Horde_Script_File Script file object.
*/
public function addScriptFile($file, $app = null)
{
$ob = is_object($file)
? $file
: new Horde_Script_File_JsDir($file, $app);
return $this->hsl->add($ob);
}
/**
* Adds a javascript package to the browser output.
*
* @param mixed $package Either a classname, basename of a
* Horde_Core_Script_Package class, or a
* Horde_Script_Package object.
*
* @return Horde_Script_Package Package object.
* @throws Horde_Exception
*/
public function addScriptPackage($package)
{
if (!is_object($package)) {
if (!class_exists($package)) {
$package = 'Horde_Core_Script_Package_' . $package;
if (!class_exists($package)) {
throw new Horde_Exception('Invalid package name provided.');
}
}
$package = new $package();
}
foreach ($package as $ob) {
$this->hsl->add($ob);
}
return $package;
}
/**
* Outputs the necessary script tags, honoring configuration choices as
* to script caching.
*
* @param boolean $full Return a full URL?
*
* @throws Horde_Exception
*/
public function includeScriptFiles($full = false)
{
global $browser, $injector;
if (!$browser->hasFeature('javascript')) {
return;
}
if (!empty($this->smartmobileInit)) {
echo Horde::wrapInlineScript(array(
'var horde_jquerymobile_init = function() {' .
implode('', $this->smartmobileInit) . '};'
));
$this->smartmobileInit = array();
}
$out = $injector->getInstance('Horde_Core_JavascriptCache')->process($this->hsl, $full);
$this->hsl->clear();
foreach ($out->script as $val) {
echo '';
}
if (($this->ajax || $this->growler) && $out->all) {
$out->jsvars['HordeCore.jsfiles'] = $out->all;
}
$this->addInlineJsVars($out->jsvars);
}
/**
* Add inline javascript to the output buffer.
*
* @param string|array $script The script text(s) to add.
* @param boolean|string $onload Load the script after the page (DOM) has
* loaded? If a string (either 'prototype'
* or 'jquery'), that JS framework's method
* is used. Defaults to Prototype. @since
* Horde_Core 2.28.0
* @param boolean $top Add script to top of stack?
*/
public function addInlineScript($script, $onload = false, $top = false)
{
$script = is_array($script)
? implode(';', array_map('trim', $script))
: trim($script);
if (!strlen($script)) {
return;
}
$onload = is_bool($onload) && $onload ? 'prototype' : $onload;
$script = rtrim($script, ';') . ';';
if ($top && isset($this->inlineScript[$onload])) {
array_unshift($this->inlineScript[$onload], $script);
} else {
$this->inlineScript[$onload][] = $script;
}
// If headers have already been sent, we need to output a
//