update Piwik to version 2.16 (fixes #91)
This commit is contained in:
parent
296343bf3b
commit
d885a4baa9
5833 changed files with 418860 additions and 226988 deletions
217
www/analytics/core/Twig.php
Normal file → Executable file
217
www/analytics/core/Twig.php
Normal file → Executable file
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* Piwik - Open source web analytics
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
|
|
@ -9,16 +9,19 @@
|
|||
namespace Piwik;
|
||||
|
||||
use Exception;
|
||||
use Piwik\Period\Range;
|
||||
use Piwik\Translate;
|
||||
use Piwik\Visualization\Sparkline;
|
||||
use Piwik\Container\StaticContainer;
|
||||
use Piwik\DataTable\Filter\SafeDecodeLabel;
|
||||
use Piwik\Metrics\Formatter;
|
||||
use Piwik\Tracker\GoalManager;
|
||||
use Piwik\View\RenderTokenParser;
|
||||
use Piwik\Visualization\Sparkline;
|
||||
use Twig_Environment;
|
||||
use Twig_Extension_Debug;
|
||||
use Twig_Loader_Chain;
|
||||
use Twig_Loader_Filesystem;
|
||||
use Twig_SimpleFilter;
|
||||
use Twig_SimpleFunction;
|
||||
use Twig_SimpleTest;
|
||||
|
||||
/**
|
||||
* Twig class
|
||||
|
|
@ -34,18 +37,36 @@ class Twig
|
|||
*/
|
||||
private $twig;
|
||||
|
||||
private $formatter;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$loader = $this->getDefaultThemeLoader();
|
||||
|
||||
$this->addPluginNamespaces($loader);
|
||||
|
||||
// If theme != default we need to chain
|
||||
$chainLoader = new Twig_Loader_Chain(array($loader));
|
||||
//get current theme
|
||||
$manager = Plugin\Manager::getInstance();
|
||||
$theme = $manager->getThemeEnabled();
|
||||
$loaders = array();
|
||||
|
||||
$this->formatter = new Formatter();
|
||||
|
||||
//create loader for custom theme to overwrite twig templates
|
||||
if ($theme && $theme->getPluginName() != \Piwik\Plugin\Manager::DEFAULT_THEME) {
|
||||
$customLoader = $this->getCustomThemeLoader($theme);
|
||||
if ($customLoader) {
|
||||
//make it possible to overwrite plugin templates
|
||||
$this->addCustomPluginNamespaces($customLoader, $theme->getPluginName());
|
||||
$loaders[] = $customLoader;
|
||||
}
|
||||
}
|
||||
|
||||
$loaders[] = $loader;
|
||||
|
||||
$chainLoader = new Twig_Loader_Chain($loaders);
|
||||
|
||||
// Create new Twig Environment and set cache dir
|
||||
$templatesCompiledPath = PIWIK_USER_PATH . '/tmp/templates_c';
|
||||
$templatesCompiledPath = SettingsPiwik::rewriteTmpPathWithHostname($templatesCompiledPath);
|
||||
$templatesCompiledPath = StaticContainer::get('path.tmp') . '/templates_c';
|
||||
|
||||
$this->twig = new Twig_Environment($chainLoader,
|
||||
array(
|
||||
|
|
@ -62,11 +83,19 @@ class Twig
|
|||
$this->addFilter_sumTime();
|
||||
$this->addFilter_money();
|
||||
$this->addFilter_truncate();
|
||||
$this->addFilter_notificiation();
|
||||
$this->addFilter_notification();
|
||||
$this->addFilter_percentage();
|
||||
$this->addFilter_percent();
|
||||
$this->addFilter_percentEvolution();
|
||||
$this->addFilter_piwikProAdLink();
|
||||
$this->addFilter_piwikProOnPremisesAdLink();
|
||||
$this->addFilter_piwikProCloudAdLink();
|
||||
$this->addFilter_prettyDate();
|
||||
$this->addFilter_safeDecodeRaw();
|
||||
$this->addFilter_number();
|
||||
$this->twig->addFilter(new Twig_SimpleFilter('implode', 'implode'));
|
||||
$this->twig->addFilter(new Twig_SimpleFilter('ucwords', 'ucwords'));
|
||||
$this->twig->addFilter(new Twig_SimpleFilter('lcfirst', 'lcfirst'));
|
||||
|
||||
$this->addFunction_includeAssets();
|
||||
$this->addFunction_linkTo();
|
||||
|
|
@ -76,6 +105,43 @@ class Twig
|
|||
$this->addFunction_getJavascriptTranslations();
|
||||
|
||||
$this->twig->addTokenParser(new RenderTokenParser());
|
||||
|
||||
$this->addTest_false();
|
||||
$this->addTest_true();
|
||||
$this->addTest_emptyString();
|
||||
}
|
||||
|
||||
private function addTest_false()
|
||||
{
|
||||
$test = new Twig_SimpleTest(
|
||||
'false',
|
||||
function ($value) {
|
||||
return false === $value;
|
||||
}
|
||||
);
|
||||
$this->twig->addTest($test);
|
||||
}
|
||||
|
||||
private function addTest_true()
|
||||
{
|
||||
$test = new Twig_SimpleTest(
|
||||
'true',
|
||||
function ($value) {
|
||||
return true === $value;
|
||||
}
|
||||
);
|
||||
$this->twig->addTest($test);
|
||||
}
|
||||
|
||||
private function addTest_emptyString()
|
||||
{
|
||||
$test = new Twig_SimpleTest(
|
||||
'emptyString',
|
||||
function ($value) {
|
||||
return '' === $value;
|
||||
}
|
||||
);
|
||||
$this->twig->addTest($test);
|
||||
}
|
||||
|
||||
protected function addFunction_getJavascriptTranslations()
|
||||
|
|
@ -126,7 +192,7 @@ class Twig
|
|||
// make the first value the string that will get output in the template
|
||||
// plugins can modify this string
|
||||
$str = '';
|
||||
$params = array_merge( array( &$str ), $params);
|
||||
$params = array_merge(array( &$str ), $params);
|
||||
|
||||
Piwik::postEvent($eventName, $params);
|
||||
return $str;
|
||||
|
|
@ -164,12 +230,29 @@ class Twig
|
|||
return $themeLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* create template loader for a custom theme
|
||||
* @param \Piwik\Plugin $theme
|
||||
* @return \Twig_Loader_Filesystem
|
||||
*/
|
||||
protected function getCustomThemeLoader(Plugin $theme)
|
||||
{
|
||||
if (!file_exists(sprintf("%s/plugins/%s/templates/", PIWIK_INCLUDE_PATH, $theme->getPluginName()))) {
|
||||
return false;
|
||||
}
|
||||
$themeLoader = new Twig_Loader_Filesystem(array(
|
||||
sprintf("%s/plugins/%s/templates/", PIWIK_INCLUDE_PATH, $theme->getPluginName())
|
||||
));
|
||||
|
||||
return $themeLoader;
|
||||
}
|
||||
|
||||
public function getTwigEnvironment()
|
||||
{
|
||||
return $this->twig;
|
||||
}
|
||||
|
||||
protected function addFilter_notificiation()
|
||||
protected function addFilter_notification()
|
||||
{
|
||||
$twigEnv = $this->getTwigEnvironment();
|
||||
$notificationFunction = new Twig_SimpleFilter('notification', function ($message, $options) use ($twigEnv) {
|
||||
|
|
@ -198,10 +281,22 @@ class Twig
|
|||
$this->twig->addFilter($notificationFunction);
|
||||
}
|
||||
|
||||
protected function addFilter_safeDecodeRaw()
|
||||
{
|
||||
$rawSafeDecoded = new Twig_SimpleFilter('rawSafeDecoded', function ($string) {
|
||||
$string = str_replace('+', '%2B', $string);
|
||||
$string = str_replace(' ', html_entity_decode(' '), $string);
|
||||
|
||||
return SafeDecodeLabel::decodeLabelSafe($string);
|
||||
|
||||
}, array('is_safe' => array('all')));
|
||||
$this->twig->addFilter($rawSafeDecoded);
|
||||
}
|
||||
|
||||
protected function addFilter_prettyDate()
|
||||
{
|
||||
$prettyDate = new Twig_SimpleFilter('prettyDate', function ($dateString, $period) {
|
||||
return Range::factory($period, $dateString)->getLocalizedShortString();
|
||||
return Period\Factory::build($period, $dateString)->getLocalizedShortString();
|
||||
});
|
||||
$this->twig->addFilter($prettyDate);
|
||||
}
|
||||
|
|
@ -209,11 +304,78 @@ class Twig
|
|||
protected function addFilter_percentage()
|
||||
{
|
||||
$percentage = new Twig_SimpleFilter('percentage', function ($string, $totalValue, $precision = 1) {
|
||||
return Piwik::getPercentageSafe($string, $totalValue, $precision) . '%';
|
||||
return NumberFormatter::getInstance()->formatPercent(Piwik::getPercentageSafe($string, $totalValue, $precision), $precision);
|
||||
});
|
||||
$this->twig->addFilter($percentage);
|
||||
}
|
||||
|
||||
protected function addFilter_percent()
|
||||
{
|
||||
$percentage = new Twig_SimpleFilter('percent', function ($string, $precision = 1) {
|
||||
return NumberFormatter::getInstance()->formatPercent($string, $precision);
|
||||
});
|
||||
$this->twig->addFilter($percentage);
|
||||
}
|
||||
|
||||
protected function addFilter_percentEvolution()
|
||||
{
|
||||
$percentage = new Twig_SimpleFilter('percentEvolution', function ($string) {
|
||||
return NumberFormatter::getInstance()->formatPercentEvolution($string);
|
||||
});
|
||||
$this->twig->addFilter($percentage);
|
||||
}
|
||||
|
||||
protected function addFilter_piwikProAdLink()
|
||||
{
|
||||
$ads = $this->getPiwikProAdvertising();
|
||||
$piwikProAd = new Twig_SimpleFilter('piwikProCampaignParameters', function ($url, $campaignName, $campaignMedium, $campaignContent = '') use ($ads) {
|
||||
$url = $ads->addPromoCampaignParametersToUrl($url, $campaignName, $campaignMedium, $campaignContent);
|
||||
return $url;
|
||||
});
|
||||
$this->twig->addFilter($piwikProAd);
|
||||
}
|
||||
|
||||
protected function addFilter_piwikProOnPremisesAdLink()
|
||||
{
|
||||
$twigEnv = $this->getTwigEnvironment();
|
||||
$ads = $this->getPiwikProAdvertising();
|
||||
$piwikProAd = new Twig_SimpleFilter('piwikProOnPremisesPromoUrl', function ($medium, $content = '') use ($twigEnv, $ads) {
|
||||
|
||||
$url = $ads->getPromoUrlForOnPremises($medium, $content);
|
||||
|
||||
return twig_escape_filter($twigEnv, $url, 'html_attr');
|
||||
|
||||
}, array('is_safe' => array('html_attr')));
|
||||
$this->twig->addFilter($piwikProAd);
|
||||
}
|
||||
|
||||
protected function addFilter_piwikProCloudAdLink()
|
||||
{
|
||||
$twigEnv = $this->getTwigEnvironment();
|
||||
$ads = $this->getPiwikProAdvertising();
|
||||
$piwikProAd = new Twig_SimpleFilter('piwikProCloudPromoUrl', function ($medium, $content = '') use ($twigEnv, $ads) {
|
||||
|
||||
$url = $ads->getPromoUrlForCloud($medium, $content);
|
||||
|
||||
return twig_escape_filter($twigEnv, $url, 'html_attr');
|
||||
|
||||
}, array('is_safe' => array('html_attr')));
|
||||
$this->twig->addFilter($piwikProAd);
|
||||
}
|
||||
|
||||
private function getPiwikProAdvertising()
|
||||
{
|
||||
return StaticContainer::get('Piwik\PiwikPro\Advertising');
|
||||
}
|
||||
|
||||
protected function addFilter_number()
|
||||
{
|
||||
$formatter = new Twig_SimpleFilter('number', function ($string, $minFractionDigits = 0, $maxFractionDigits = 0) {
|
||||
return NumberFormatter::getInstance()->format($string, $minFractionDigits, $maxFractionDigits);
|
||||
});
|
||||
$this->twig->addFilter($formatter);
|
||||
}
|
||||
|
||||
protected function addFilter_truncate()
|
||||
{
|
||||
$truncateFilter = new Twig_SimpleFilter('truncate', function ($string, $size) {
|
||||
|
|
@ -229,21 +391,24 @@ class Twig
|
|||
|
||||
protected function addFilter_money()
|
||||
{
|
||||
$moneyFilter = new Twig_SimpleFilter('money', function ($amount) {
|
||||
$formatter = $this->formatter;
|
||||
$moneyFilter = new Twig_SimpleFilter('money', function ($amount) use ($formatter) {
|
||||
if (func_num_args() != 2) {
|
||||
throw new Exception('the money modifier expects one parameter: the idSite.');
|
||||
}
|
||||
$idSite = func_get_args();
|
||||
$idSite = $idSite[1];
|
||||
return MetricsFormatter::getPrettyMoney($amount, $idSite);
|
||||
$currencySymbol = Site::getCurrencySymbolFor($idSite);
|
||||
return NumberFormatter::getInstance()->formatCurrency($amount, $currencySymbol, GoalManager::REVENUE_PRECISION);
|
||||
});
|
||||
$this->twig->addFilter($moneyFilter);
|
||||
}
|
||||
|
||||
protected function addFilter_sumTime()
|
||||
{
|
||||
$sumtimeFilter = new Twig_SimpleFilter('sumtime', function ($numberOfSeconds) {
|
||||
return MetricsFormatter::getPrettyTimeFromSeconds($numberOfSeconds);
|
||||
$formatter = $this->formatter;
|
||||
$sumtimeFilter = new Twig_SimpleFilter('sumtime', function ($numberOfSeconds) use ($formatter) {
|
||||
return $formatter->getPrettyTimeFromSeconds($numberOfSeconds, true);
|
||||
});
|
||||
$this->twig->addFilter($sumtimeFilter);
|
||||
}
|
||||
|
|
@ -289,6 +454,22 @@ class Twig
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Plugin-Templates can be overwritten by putting identically named templates in plugins/[theme]/templates/plugins/[plugin]/
|
||||
*
|
||||
*/
|
||||
private function addCustomPluginNamespaces(Twig_Loader_Filesystem $loader, $pluginName)
|
||||
{
|
||||
$plugins = \Piwik\Plugin\Manager::getInstance()->getAllPluginsNames();
|
||||
foreach ($plugins as $name) {
|
||||
$path = sprintf("%s/plugins/%s/templates/plugins/%s/", PIWIK_INCLUDE_PATH, $pluginName, $name);
|
||||
if (is_dir($path)) {
|
||||
$loader->addPath(PIWIK_INCLUDE_PATH . '/plugins/' . $pluginName . '/templates/plugins/'. $name, $name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepend relative paths with absolute Piwik path
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue