| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830 |
- <?php
- App::uses('AppHelper', 'View/Helper');
- /**
- * All site-wide neccessary stuff for the view layer
- */
- class CommonHelper extends AppHelper {
- public $helpers = array ('Session', 'Html');
- public $packages = array(
- 'Tools.Jquery' //Used by showDebug
- );
- /* Layout Stuff */
- /**
- * convinience function for clean ROBOTS allowance
- * @param STRING private/public
- * 2008-12-08 ms
- */
- public function metaRobots($type = null) {
- if (($meta = Configure::read('Config.robots')) !== null) {
- $type = $meta;
- }
- $content = array();
- if ($type == 'public') {
- $this->privatePage = false;
- $content['robots']= array('index','follow','noarchive');
- } else {
- $this->privatePage = true;
- $content['robots']= array('noindex','nofollow','noarchive');
- }
- $return = '<meta name="robots" content="'.implode(',',$content['robots']).'" />';
- return $return;
- }
- /**
- * convinience function for clean meta name tags
- * @param STRING $name: author, date, generator, revisit-after, language
- * @param MIXED $content: if array, it will be seperated by commas
- * 2009-07-07 ms
- */
- public function metaName($name = null, $content = null) {
- if (empty($name) || empty($content)) {
- return '';
- }
- if (!is_array($content)) {
- $content = (array)$content;
- }
- $return = '<meta name="'.$name.'" content="'.implode(', ',$content).'" />';
- return $return;
- }
- public function metaDescription($content, $language = null, $options = array()) {
- if (!empty($language)) {
- $options['lang'] = mb_strtolower($language);
- } elseif ($language !== false) {
- $options['lang'] = Configure::read('Config.locale'); // DEFAULT_LANGUAGE
- }
- return $this->Html->meta('description', $content, $options);
- }
- public function metaKeywords($keywords = null, $language = null, $escape = true) {
- if ($keywords === null) {
- $keywords = Configure::read('Config.keywords');
- }
- if (is_array($keywords)) {
- $keywords = implode(', ', $keywords);
- }
- if ($escape) {
- $keywords = h($keywords);
- }
- if (!empty($language)) {
- $options['lang'] = mb_strtolower($language);
- } elseif ($language !== false) {
- $options['lang'] = Configure::read('Config.locale'); // DEFAULT_LANGUAGE
- }
- return $this->Html->meta('keywords', $keywords, $options);
- }
- /**
- * convinience function for "canonical" SEO links
- * 2010-03-03 ms
- */
- public function metaCanonical($url = null, $full = false) {
- $canonical = $this->Html->url($url, $full);
- //return $this->Html->meta('canonical', $canonical, array('rel'=>'canonical', 'type'=>null, 'title'=>null));
- return '<link rel="canonical" href="'.$canonical.'" />';
- }
- /**
- * convinience function for "alternate" SEO links
- * @param mixed $url
- * @param mixed $lang (lang(iso2) or array of langs)
- * lang: language (in ISO 6391-1 format) + optionally the region (in ISO 3166-1 Alpha 2 format)
- * - de
- * - de-ch
- * etc
- * 2011-12-12 ms
- */
- public function metaAlternate($url, $lang, $full = false) {
- $canonical = $this->Html->url($url, $full);
- //return $this->Html->meta('canonical', $canonical, array('rel'=>'canonical', 'type'=>null, 'title'=>null));
- $lang = (array)$lang;
- $res = array();
- foreach ($lang as $language => $countries) {
- if (is_numeric($language)) {
- $language = '';
- } else {
- $language .= '-';
- }
- $countries = (array)$countries;
- foreach ($countries as $country) {
- $l = $language.$country;
- $res[] = $this->Html->meta('alternate', $url, array('rel'=>'alternate', 'hreflang'=>$l, 'type'=>null, 'title'=>null)).PHP_EOL;
- }
- }
- return implode('', $res);
- }
- /**
- * convinience function for META Tags
- * @param STRING type
- * @param STRING content
- * 2008-12-08 ms
- */
- public function metaRss($url = null, $title = null) {
- $tags = array(
- 'meta' => '<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />',
- );
- $content = array();
- if (empty($url)) { return ''; }
- if (empty($title)) {
- $title = 'Diesen Feed abonnieren';
- }
- return sprintf($tags['meta'], $title, $this->url($url));
- }
- /**
- * convinience function for META Tags
- * @param STRING type
- * @param STRING content
- * 2008-12-08 ms
- */
- public function metaEquiv($type = null, $value = null, $escape = true) {
- $tags = array(
- 'meta' => '<meta http-equiv="%s"%s />',
- );
- if (empty($value)) {
- return '';
- }
- if ($escape) {
- $value = h($value);
- }
- if ($type == 'language') {
- return sprintf($tags['meta'],'language',' content="'.$value.'"');
- } elseif ($type == 'pragma') {
- return sprintf($tags['meta'],'pragma',' content="'.$value.'"');
- } elseif ($type == 'expires') {
- return sprintf($tags['meta'],'expires',' content="'.$value.'"');
- } elseif ($type == 'cache-control') {
- return sprintf($tags['meta'],'cache-control',' content="'.$value.'"');
- } elseif ($type == 'refresh') {
- return sprintf($tags['meta'],'refresh',' content="'.$value.'"');
- }
- return '';
- }
- /**
- * (example): array(x, Tools|y, Tools.Jquery|jquery/sub/z)
- * => x is in webroot/
- * => y is in plugins/tools/webroot/
- * => z is in plugins/tools/packages/jquery/files/jquery/sub/
- * 2011-03-23 ms
- */
- public function css($files = array(), $rel = null, $options = array()) {
- $files = (array)$files;
- $pieces = array();
- foreach ($files as $file) {
- $pieces[] = 'file='.$file;
- }
- if ($v = Configure::read('Config.layout_v')) {
- $pieces[] = 'v='.$v;
- }
- $string = implode('&', $pieces);
- return $this->Html->css('/css.php?'.$string, $rel, $options);
- }
- /**
- * (example): array(x, Tools|y, Tools.Jquery|jquery/sub/z)
- * => x is in webroot/
- * => y is in plugins/tools/webroot/
- * => z is in plugins/tools/packages/jquery/files/jquery/sub/
- * 2011-03-23 ms
- */
- public function script($files = array(), $options = array()) {
- $files = (array)$files;
- foreach ($files as $file) {
- $pieces[] = 'file='.$file;
- }
- if ($v = Configure::read('Config.layout_v')) {
- $pieces[] = 'v='.$v;
- }
- $string = implode('&', $pieces);
- return $this->Html->script('/js.php?'.$string, $options);
- }
- /**
- * special css tag generator with the option to add '?...' to the link (for caching prevention)
- * IN USAGE
- * needs manual adjustment, but still better than the core one!
- * @example needs Asset.cssversion => xyz (going up with counter)
- * 2008-12-08 ms
- */
- public function cssDyn($path, $rel = null, $htmlAttributes = array(), $return = true) {
- $v = (int)Configure::read('Asset.version');
- return $this->Html->css($path.'.css?'.$v, $rel, $htmlAttributes, $return);
- }
- /**
- * NOT IN USAGE
- * but better than the core one!
- * @example needs Asset.timestamp => force
- * 2008-12-08 ms
- */
- public function cssAuto($path, $rel = null, $htmlAttributes = array(), $return = true) {
- define('COMPRESS_CSS',true);
- $time = date('YmdHis', filemtime(APP . 'webroot' . DS . CSS_URL . $path . '.css'));
- $url = "{$this->request->webroot}" . (COMPRESS_CSS ? 'c' : '') . CSS_URL . $this->themeWeb . $path . ".css?" . $time;
- return $url;
- }
- /* Content Stuff */
- public function displayErrors($fields = null) {
- $res = '';
- if (!empty($this->validationErrors)) {
- if ($fields === null) { # catch ALL
- foreach ($this->validationErrors as $alias => $error) {
- list($alias, $fieldname) = explode('.', $error);
- $this->validationErrors[$alias][$fieldname];
- }
- } elseif (!empty($fields)) {
- foreach ($fields as $field) {
- list($alias, $fieldname) = explode('.', $field);
- if (!empty($this->validationErrors[$alias][$fieldname])) {
- $res .= $this->_renderError($this->validationErrors[$alias][$fieldname]);
- }
- }
- }
- /*
- if (!empty($catched)) {
- foreach ($catched as $c) {
- }
- }
- */
- }
- return $res;
- }
- public function _renderError($error, $escape = true) {
- if ($escape !== false) {
- $error = h($error);
- }
- return '<div class="error-message">'.$error.'</div>';
- }
- /**
- * Alternates between two or more strings.
- *
- * echo CommonHelper::alternate('one', 'two'); // "one"
- * echo CommonHelper::alternate('one', 'two'); // "two"
- * echo CommonHelper::alternate('one', 'two'); // "one"
- *
- * Note that using multiple iterations of different strings may produce
- * unexpected results.
- * TODO: move to booststrap!!!
- *
- * @param string strings to alternate between
- * @return string
- */
- public static function alternate() {
- static $i;
- if (func_num_args() === 0) {
- $i = 0;
- return '';
- }
- $args = func_get_args();
- return $args[($i++ % count($args))];
- }
- /**
- * check if session works due to allowed cookies
- * 2009-06-29 ms
- */
- public function sessionCheck() {
- return !CommonComponent::cookiesDisabled();
- /*
- if (!empty($_COOKIE) && !empty($_COOKIE[Configure::read('Session.cookie')])) {
- return true;
- }
- return false;
- */
- }
- /**
- * display warning if cookies are disallowed (and session won't work)
- * 2009-06-29 ms
- */
- public function sessionCheckAlert() {
- if (!$this->sessionCheck()) {
- return '<div class="cookieWarning">'.__('Please enable cookies').'</div>';
- }
- return '';
- }
- /**
- * //TODO: move boostrap
- * auto-pluralizing a word using the Inflection class
- * @param string $s = the string to be pl.
- * @param int $c = the count
- * @return $string "member" or "members" OR "Mitglied"/"Mitglieder" if autoTranslate TRUE
- * 2009-07-23 ms
- */
- public function asp($s, $c, $autoTranslate = false) {
- if ((int)$c != 1) {
- $p = Inflector::pluralize($s);
- } else {
- $p = null; # no pluralization neccessary
- }
- return $this->sp($s, $p, $c, $autoTranslate);
- }
- /**
- * //TODO: move boostrap
- * manual pluralizing a word using the Inflection class
- * 2009-07-23 ms
- */
- public function sp($s, $p, $c, $autoTranslate = false) {
- if ((int)$c != 1) {
- $ret = $p;
- } else {
- $ret = $s;
- }
- if ($autoTranslate) {
- $ret = __($ret);
- }
- return $ret;
- }
- /**
- * @deprecated
- */
- public function showDebug() {
- $output = '';
- $groupout = '';
- foreach (debugTab::$content as $group => $debug) {
- if (is_int($group)) {
- $output .= '<div class="common-debug">';
- $output .= "<span style=\"cursor:pointer\" onclick=\"$(this).parent().children('pre').slideToggle('fast');\"><strong>" . h($debug['file']) . '</strong>';
- $output .= ' (line <strong>' . $debug['line'] . '</strong>)</span>';
- if ($debug['display'])
- $debug['display'] = 'block';
- else
- $debug['display'] = 'none';
- $output .= "\n<pre style=\"display:" . $debug['display'] . "\" class=\"cake-debug\">\n";
- $output .= h($debug['debug']);
- $output .= "\n</pre>\n</div>";
- }
- }
- foreach (debugTab::$groups as $group => $data) {
- $groupout .= '<div class="common-debug">';
- $groupout .= "<span style=\"cursor:pointer\" onclick=\"$(this).parent().children('div').slideToggle('fast');\"><strong>" . h($group) . '</strong></span>';
- foreach ($data as $debug) {
- $groupout .= "<div style=\"display:none\"><br/><span style=\"cursor:pointer\" onclick=\"$(this).parent().children('pre').slideToggle('fast');\"><strong>" . h($debug['file']) . '</strong></span>';
- $groupout .= ' (line <strong>' . h($debug['line']) . '</strong>)</span>';
- if ($debug['display'])
- $debug['display'] = 'block';
- else
- $debug['display'] = 'none';
- $groupout .= "\n<pre style=\"display:" . $debug['display'] . "\" class=\"cake-debug\">\n";
- $groupout .= h($debug['debug']);
- $groupout .= "\n</pre>\n</div>";
- }
- $groupout .= "\n</div>";
- }
- return $groupout . $output;
- }
- /**
- * Show FlashMessages
- * @param boolean unsorted true/false [default:FALSE = sorted by priority]
- * TODO: export div wrapping method (for static messaging on a page)
- * TODO: sorting
- * 2010-11-22 ms
- */
- public function flash($unsorted = false, $backwardsComp = true) {
- // Get the messages from the session
- $messages = (array)$this->Session->read('messages');
- $cMessages = (array)Configure::read('messages');
- if (!empty($cMessages)) {
- $messages = (array)Set::merge($messages, $cMessages);
- }
- $html='';
- if (!empty($messages)) {
- $html = '<div class="flashMessages">';
- if ($unsorted !== true) {
- // Add a div for each message using the type as the class
- foreach ($messages as $type => $msgs) {
- foreach ((array)$msgs as $msg) {
- $html .= $this->_message($msg, $type);
- }
- }
- } else {
- foreach ($messages as $type) {
- //
- }
- }
- $html .= '</div>';
- if (method_exists($this->Session, 'delete')) {
- $this->Session->delete('messages');
- } else {
- CakeSession::delete('messages');
- }
- }
- return $html;
- }
- /**
- * output a single flashMessage
- * 2010-11-22 ms
- */
- public function flashMessage($msg, $type = 'info', $escape = true) {
- $html = '<div class="flashMessages">';
- if ($escape) {
- $msg = h($msg);
- }
- $html .= $this->_message($msg, $type);
- $html .= '</div>';
- return $html;
- }
- protected function _message($msg, $type) {
- if (!empty($msg)) {
- return '<div class="message'.(!empty($type) ? ' '.$type : '').'">'.$msg.'</div>';
- }
- return '';
- }
- /**
- * add a message on the fly
- * 2011-05-25 ms
- */
- public function transientFlashMessage($msg, $class = null) {
- return CommonComponent::transientFlashMessage($msg, $class);
- }
- /**
- * SINGLE ROLES function
- * currently: isRole('admin'), isRole('user')
- * 2009-07-06 ms
- */
- public function isRole($role) {
- $sessionRole = $this->Session->read('Auth.User.role_id');
- $roles = array(
- ROLE_USER => 'user',
- ROLE_ADMIN => 'admin',
- ROLE_SUPERADMIN => 'superadmin',
- ROLE_GUEST => 'guest',
- );
- if (!empty($roles[$sessionRole]) && $role = $roles[$sessionRole]) {
- return true;
- }
- return false;
- }
- /**
- * Checks if a role is in the current users session
- * @param neccessary right(s) as array - or a single one as string possible
- * Note: all of them need to be in the user roles to return true by default
- */
- public function roleNames($sessionRoles = null) {
- $tmp = array();
- if ($sessionRoles === null) {
- $sessionRoles = $this->Session->read('Auth.User.Role');
- }
- $roles = Cache::read('User.Role');
- if (empty($roles) || !is_array($roles)) {
- $Role = ClassRegistry::init('Role');
- /*
- if ($Role->useDbConfig == 'test_suite') {
- return array();
- }
- */
- $roles = $Role->getActive('list');
- Cache::write('User.Role', $roles);
- }
- //$roleKeys = Set::combine($roles, '/Role/id','/Role/name'); // on find(all)
- if (!empty($sessionRoles)) {
- if (is_array($sessionRoles)) {
- foreach ($sessionRoles as $sessionRole) {
- if (!$sessionRole) {
- continue;
- }
- if (array_key_exists((int)$sessionRole, $roles)) {
- $tmp[$sessionRole] = $roles[(int)$sessionRole];
- }
- }
- } else {
- if (array_key_exists($sessionRoles, $roles)) {
- $tmp[$sessionRoles] = $roles[$sessionRoles];
- }
- }
- }
- return $tmp;
- }
- /**
- * Display Roles separated by Commas
- * 2009-07-17 ms
- */
- public function displayRoles($sessionRoles = null, $placeHolder = '---') {
- $roles = $this->roleNames($sessionRoles);
- if (!empty($roles)) {
- return implode(', ',$roles);
- }
- return $placeHolder;
- }
- /**
- * escape text
- * @param string $text
- * @param options
- * - nl2br: true/false (defaults to true)
- * - escape: false prevents h() and space transformation (defaults to true)
- * - tabsToSpaces: int (defaults to 4)
- * 2010-11-20 ms
- */
- public function esc($text, $options = array()) {
- if (!isset($options['escape']) || $options['escape'] !== false) {
- //$text = str_replace(' ', ' ', h($text));
- $text = h($text);
- # try to fix indends made out of spaces
- $text = explode(NL, $text);
- foreach ($text as $key => $t) {
- $i = 0;
- while (!empty($t[$i]) && $t[$i] === ' ') {
- $i++;
- }
- if ($i > 0) {
- $t = str_repeat(' ', $i) . substr($t, $i);
- $text[$key] = $t;
- }
- }
- $text = implode(NL, $text);
- $esc = true;
- }
- if (!isset($options['nl2br']) || $options['nl2br'] !== false) {
- $text = nl2br($text);
- }
- if (!isset($options['tabsToSpaces'])) {
- $options['tabsToSpaces'] = 4;
- }
- if (!empty($options['tabsToSpaces'])) {
- $text = str_replace(TB, str_repeat(!empty($esc) ? ' ' : ' ', $options['tabsToSpaces']), $text);
- }
- return $text;
- }
- /**
- * takes int / array(int) and finds the role name to it
- * @return array roles
- */
- public function roleNamesTranslated($value) {
- if (empty($value)) { return array(); }
- $ret = array();
- $translate = (array)Configure::read('Role');
- if (is_array($value)) {
- foreach ($value as $k => $v) {
- $ret[$v] = __($translate[$v]);
- }
- } else {
- $ret[$value] = __($translate[$value]);
- }
- return $ret;
- }
- /**
- * //TODO: move to TextExt?
- * minimizes the given url to a maximum length
- * @param string $url the url
- * @param int $max the maximum length
- * @param options
- * - placeholder
- * @return string the manipulated url (+ eventuell ...)
- */
- public function minimizeUrl($url = null, $max = null, $options = array()) {
- // check if there is nothing to do
- if (empty($url) || mb_strlen($url) <= (int)$max)
- return (string)$url;
- // http:// has not to be displayed, so
- if (mb_substr($url,0,7) == 'http://')
- $url = mb_substr($url,7);
- // cut the parameters
- if (mb_strpos($url,'/') !== false)
- $url = strtok($url,'/');
- // return if the url is short enough
- if (mb_strlen($url) <= (int)$max)
- return $url;
- // otherwise cut a part in the middle (but only if long enough!!!)
- # TODO: more dynamically
- $placeholder = CHAR_HELLIP;
- if (!empty($options['placeholder'])) {
- $placeholder = $options['placeholder'];
- }
- $end = mb_substr($url,-5,5);
- $front = mb_substr($url,0,(int)$max - 8);
- return $front.$placeholder.$end;
- }
- /** should be in format END **/
- /**
- * prevents site being opened/included by others/websites inside frames
- * 2009-01-08 ms
- */
- public function framebuster() {
- return $this->Html->scriptBlock('
- if (top!=self) top.location.ref=self.location.href;
- ');
- }
- /**
- * currenctly only alerts on IE6/IE7
- * options
- * - engine (js, jquery)
- * - escape
- * needs the id element to be a present (div) container in the layout
- * 2009-12-26 ms
- **/
- public function browserAlert($id, $message, $options = array()) {
- $engine = 'js';
- if (!isset($options['escape']) || $options['escape'] !== false) {
- $message = h($message);
- }
- return $this->Html->scriptBlock('
- // Returns the version of Internet Explorer or a -1
- function getInternetExplorerVersion() {
- var rv = -1; // Return value assumes failure.
- if (navigator.appName == "Microsoft Internet Explorer") {
- var ua = navigator.userAgent;
- var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
- if (re.exec(ua) != null)
- rv = parseFloat( RegExp.$1 );
- }
- return rv;
- }
- if ((document.all) && (navigator.appVersion.indexOf("MSIE 7.") != -1) || typeof document.body.style.maxHeight === "undefined") {
- document.getElementById(\''.$id.'\').innerHTML = \''.$message.'\';
- }
- /*
- jQuery(document).ready(function() {
- if ($.browser.msie && $.browser.version.substring(0,1) < 8) {
- document.getElementById(\''.$id.'\').innerHTML = \''.$message.'\';
- }
- });
- */
- ');
- /*
- if ($.browser.msie) {
- var version = $.browser.version.substring(0,1);
- }
- */
- }
- /**
- * in noscript tags:
- * - link which should not be followed by bots!
- * - "pseudo"image which triggers log
- * 2009-12-28 ms
- */
- public function honeypot($noFollowUrl, $noscriptUrl = array()) {
- $res = '<div class="invisible" style="display:none"><noscript>';
- $res .= $this->Html->defaultLink('Email', $noFollowUrl, array('rel'=>'nofollow'));
- if (!empty($noscriptUrl)) {
- $res .= BR.$this->Html->image($this->Html->defaultUrl($noscriptUrl, true)); //$this->Html->link($noscriptUrl);
- }
- $res .= '</noscript></div>';
- return $res;
- }
- /**
- * Creates an HTML link.
- *
- * If $url starts with "http://" this is treated as an external link. Else,
- * it is treated as a path to controller/action and parsed with the
- * HtmlHelper::url() method.
- *
- * If the $url is empty, $title is used instead.
- *
- * @param string $title The content to be wrapped by <a> tags.
- * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
- * @param array $htmlAttributes Array of HTML attributes.
- * @param string $confirmMessage JavaScript confirmation message.
- * @param boolean $escapeTitle Whether or not $title should be HTML escaped.
- * @return string An <a /> element.
- * // core-hack! $rel = null | !!!!!!!!! Somehow causes trouble with routing functionality of this helper function... careful!
- */
- public function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true, $rel = null) {
- if ($url !== null) {
- /** core-hack $rel (relative to current position/routing) **/
- if ($rel === true || !is_array($url)) {
- // leave it as it is
- } else {
- $defaultArray = array('admin'=>false, 'prefix'=>0);
- $url = array_merge($defaultArray,$url);
- }
- /** core-hack END **/
- return $this->Html->link($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle);
- }
- }
- /** Stats **/
- /**
- * print js-visit-stats-link to layout
- * uses Piwik open source statistics framework
- * 2009-04-15 ms
- */
- public function visitStats($viewPath = null) {
- $res = '';
- if (!defined('HTTP_HOST_LIVESERVER')) {
- return '';
- }
- if (HTTP_HOST == HTTP_HOST_LIVESERVER && (int)Configure::read('Config.tracking') === 1) {
- $trackingUrl = Configure::read('Config.tracking_url');
- if (empty($trackingUrl)) {
- $trackingUrl = 'visit_stats';
- }
- $error = false;
- if (!empty($viewPath) && $viewPath == 'errors') {
- $error = true;
- }
- $res .= '
- <script type="text/javascript">
- var pkBaseURL = (("https:" == document.location.protocol) ? "https://'.HTTP_HOST.'/'.$trackingUrl.'/" : "http://'.HTTP_HOST.'/'.$trackingUrl.'/");
- document.write(unescape("%3Cscript src=\'" + pkBaseURL + "piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));
- </script>
- <script type="text/javascript">
- try {
- var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
- piwikTracker.trackPageView();
- piwikTracker.enableLinkTracking();
- '.($error?'piwikTracker.setDocumentTitle(\'404/URL = \'+encodeURIComponent(document.location.pathname+document.location.search) + \'/From = \' + encodeURIComponent(document.referrer));':'').'
- } catch( err ) {}
- </script>
- <noscript><p>'.$this->visitStatsImg().'</p></noscript>
- ';
- }
- return $res;
- }
- /**
- * non js browsers
- * 2009-09-07 ms
- */
- public function visitStatsImg($trackingUrl = null) {
- if (empty($trackingUrl)) {
- $trackingUrl = Configure::read('Config.tracking_url');
- }
- if (empty($trackingUrl)) {
- $trackingUrl = 'visit_stats';
- }
- return '<img src="'.Router::url('/').$trackingUrl.'/piwik.php?idsite=1" style="border:0" alt=""/>';
- }
- }
|