* @license PHP License
* @package WB
* @subpackage service
*/
WBClass::load('WBService');
/**
* Deliver site's html
*
* @version 0.7.0
* @package WB
* @subpackage service
*/
class WBService_Site extends WBService
{
/**
* decide whether to select locale settings from path
* @var bool
*/
protected $useI18nFromPath = true;
/**
* Display
*
* @param int $status
* @param mixed $data to be send
* @param string $checksum
* @return bool true
*/
public function run(&$status, &$data, &$checksum = null)
{
// browser must revalidate
$this->res->setCacheControl(WBResponse::CACHECONTROL_MUSTREVALIDATE);
$this->res->setMaxAge(600);
// keep path in session
$this->sess->set('wb/service/path/site', $this->req->path);
$lt = WBClass::create('WBStatistic_LinkTracker');
$lt->add();
$rUrl = $lt->redirect();
if (!empty($rUrl)){
$this->res->addHeader('Location', $rUrl);
$data = str_replace('{URL}', $rUrl, 'Please continue at {URL}. Thank you.');
$status = 301;
return true;
}
// load page config
$params = $this->conf->get('content');
$this->processPath($params, $this->path);
$this->processPathRequest($params, $this->path);
$this->processEvents($params);
$this->addJavaScript();
$tmpl = WBClass::create('patTemplate');
/** @var $tmpl patTemplate */
$tmpl->readTemplatesFromInput($this->conf->get('skin/tmpl', 'main') . '.tmpl');
$this->cont = WBClass::create('WBContent_Composite');
$this->cont->configure( '', $params, array() );
$params = $this->cont->run();
$tmpl->addGlobalVar('area_content', $this->cont->getString());
$tmpl->addGlobalVars($this->cont->getBubbles());
$head = $this->getAlternativeLanguageURLs();
$head[] = WBHtml::get(WBHtml::AREA_HEAD);
$head = implode("\n", $head);
$tmpl->addGlobalVar('area_head', $head);
$tmpl->addGlobalVar('area_foot', WBHtml::get(WBHtml::AREA_FOOT));
$tmpl->addGlobalVar('area_js_onload', WBHtml::get(WBHtml::AREA_JS_ONLOAD));
$data = $tmpl->getParsedTemplate('page');
$status = $this->cont->getStatusCode();
$this->res->useCache($this->cont->isCachable());
// save parameters in session which allows to continue using AJAX
$paramName = md5($this->part . ':params:' . WBParam::get('wb/dir/base'));
$this->sess->set($paramName, $params);
$stat = WBClass::create('WBStatistic_View');
$stat->addPath(implode('/', $this->path));
return true;
}
/**
* Get List of Alternate Language URLs
*
* Build list of alternate URLs for HTML header and add HTTP-Link header
* for same URLs
*
* @return array
*/
private function getAlternativeLanguageURLs()
{
$alt = array();
$head = array();
if (!$this->useI18nFromPath) {
return $alt;
}
$requestPath = $this->confLocale->get('locale/languages/requestpath', array());
if (empty($requestPath)) {
return $alt;
}
$site = implode('/', $this->path);
$withXDefault = true;
foreach ($requestPath as $lang => $path) {
$lang = explode('_', strtolower($lang));
if ('__default' == $path) {
$withXDefault = false;
$alt[] = sprintf('', $lang[0], $site);
$head[] = sprintf('<[[PROTOCOL]]://[[SERVER]][[SELF_NO_LANG]]%s>; rel="alternate; hreflang="%s"', $site, $lang[0]);
continue;
}
$alt[] = sprintf('', $lang[0], $path, $site);
$head[] = sprintf('<[[PROTOCOL]]://[[SERVER]][[SELF_NO_LANG]]%s/%s>; rel="alternate; hreflang="%s"', $path, $site, $lang[0]);
}
if ($withXDefault) {
$alt[] = sprintf('', $site);
$head[] = sprintf('<[[PROTOCOL]]://[[SERVER]][[SELF_NO_LANG]]%s>; rel="alternate; hreflang="x-default"', $site);
}
$this->res->addHeader('Link', implode(', ', $head));
return $alt;
}
/**
* load prototypish javascipt files
*
* Originally Wombat used prootype.js. Hence all classes are
* base on prototype features. Still you can override this method
* to implement some custom WB-classes.
*
* This method loads the list of javascript includes from site.xml
* (depending on current part)
*
* @todo consider addeing more str_replace textes
*/
protected function addJavaScript()
{
WBClass::load('WBHtml_JS');
$javascript = $this->conf->get('javascript', array());
if (isset($javascript['include'])) {
if (!is_array($javascript['include'])) {
$javascript['include'] = array();
}
foreach ($javascript['include'] as $inc) {
WBHtml_JS::add($inc);
}
}
if (isset($javascript['onload'])) {
$search = array();
$replace = array();
if ($this->useI18n) {
$search[] = '{LANG_COMPLETE}';
$replace[] = patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE);
}
$onload = str_replace($search, $replace, $javascript['onload']);
WBHtml_JS::add($onload, WBHtml_JS::AREA_ONLOAD);
}
}
/**
* process queued events
*
* @param array $params
* @return bool always true
*/
protected function processEvents(&$params)
{
$queue = $this->getEventQueue();
if (empty($queue)) {
return true;
}
$log = array(
'process' => 'event',
'part' => $this->part,
'path' => implode('/', $this->path),
'event' => '',
);
$allowed = $this->conf->get('events', array());
if (!is_array($allowed)) {
$allowed = array($allowed);
}
$conf = WBClass::create('WBConfig');
/** @var $conf WBConfig */
$conf->setLoader($this->conf->getLoader()->getName());
foreach ($queue as $q) {
if (!isset($allowed[$q['name']])) {
continue;
}
// override
if (!isset($allowed[$q['name']]['override'])) {
continue;
}
if (!$conf->load($this->part . '/event/' . $q['name'], true)) {
continue;
}
$log['event'] = $q['name'];
$this->log->notice($log);
$page = $conf->get();
$this->overrideAreas($params, $page, array());
}
$this->flushEventQueue();
return true;
}
}