From b5327bf993cb642e088b5a904851b4a78b06abc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 20 Jul 2014 21:29:07 +0200 Subject: [PATCH] Added custom Hitcounter component --- www/protected/components/CHitCounter.php | 122 ++++++++++++++++++++++ www/protected/components/MSController.php | 7 ++ www/protected/config/main.php | 8 ++ www/protected/models/Program.php | 5 +- www/protected/views/msmain/admin.php | 48 ++++++++- 5 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 www/protected/components/CHitCounter.php diff --git a/www/protected/components/CHitCounter.php b/www/protected/components/CHitCounter.php new file mode 100644 index 0000000..36923c1 --- /dev/null +++ b/www/protected/components/CHitCounter.php @@ -0,0 +1,122 @@ +isBot()) // Do not track bots + return; + + $this->tryUpdateStats(); + + $connection=Yii::app()->db; + + $date_fmt = (new DateTime())->format('Y-m-d'); + $ipaddr = $_SERVER['REMOTE_ADDR']; + + $unique = $connection->createCommand("SELECT COUNT(*) FROM $this->table_today WHERE ipaddress = '$ipaddr'")->queryScalar() == 0; + + if ($unique) + { + $connection->createCommand("INSERT INTO $this->table_today (date, ipaddress) VALUES ('$date_fmt', '$ipaddr');")->execute(); + } + + return $unique; + } + + public function getTotalCount() + { + $count = Yii::app()->db->createCommand("SELECT SUM(count) FROM $this->table_stats")->queryScalar(); + + $count = (($count) ? $count : 0) + $this->getTodayCount(); + + return $count; + } + + public function getTodayCount() + { + $this->tryUpdateStats(); + + return Yii::app()->db->createCommand("SELECT COUNT(*) FROM $this->table_today")->queryScalar(); + } + + /** + * @param DateTime $day + * + * @return int + */ + public function getCountForDay($day) + { + if ((new DateTime())->format('Y-m-d') == $day->format('Y-m-d')) + return $this->getTodayCount(); + + $fmt = $day->format('Y-m-d'); + + $count = Yii::app()->db->createCommand("SELECT count FROM ms4_hc_stats WHERE date = '$fmt'")->queryScalar(); + + return ($count) ? $count : 0; + } + + //############################################################# + + /** + * @return bool + */ + private function isBot() + { + return (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])); + } + + private function tryUpdateStats() + { + if ($this->updated) + return; // Only update once per instance + $this->updated = true; + + + $connection=Yii::app()->db; + + $row = $connection->createCommand("SELECT * FROM $this->table_today")->queryRow(); + + if (! $row) + return false; // nothing to update + + $last_date = new DateTime($row['date']); + + if ($last_date->format('Y-m-d') != (new DateTime())->format('Y-m-d')) + { + // Get last count + $lastday_count = $connection->createCommand("SELECT COUNT(*) FROM $this->table_today")->queryScalar(); + + // Insert into stats + $last_date_fmt = $last_date->format('Y-m-d'); + $connection->createCommand("INSERT INTO $this->table_stats (date, count) VALUES ('$last_date_fmt', $lastday_count)")->execute(); + + // Delete today table + $connection->createCommand("DELETE FROM $this->table_today")->execute(); + return true; + } + else + { + return false; + } + } +} \ No newline at end of file diff --git a/www/protected/components/MSController.php b/www/protected/components/MSController.php index ae2e78e..b3ffe01 100644 --- a/www/protected/components/MSController.php +++ b/www/protected/components/MSController.php @@ -14,4 +14,11 @@ class MSController extends CController public $js_scripts = array(); public $title = null; + + + public function beforeAction($e){ + Yii::app()->hitcounter->increment(); + + return parent::beforeAction($e); + } } \ No newline at end of file diff --git a/www/protected/config/main.php b/www/protected/config/main.php index bd9c00e..cb3e471 100644 --- a/www/protected/config/main.php +++ b/www/protected/config/main.php @@ -48,6 +48,14 @@ return ArrayX::merge( // application components 'components' => [ + 'hitcounter' => + [ + 'class' => 'CHitCounter', + + 'table_stats' => '{{hc_stats}}', + 'table_today' => '{{hc_today}}', + ], + 'bootstrap' => [ 'class' => 'bootstrap.components.TbApi', diff --git a/www/protected/models/Program.php b/www/protected/models/Program.php index 6d04606..65558c6 100644 --- a/www/protected/models/Program.php +++ b/www/protected/models/Program.php @@ -187,7 +187,10 @@ class Program extends CActiveRecord * @return string */ public function getDirectDownloadLink() { - return '/data/programs/' . $this->Name . '.zip' ; + if ($this->download_url == 'direkt' || is_null($this->download_url) || empty($this->download_url)) + return '/data/programs/' . $this->Name . '.zip'; + else + return $this->download_url; } /** diff --git a/www/protected/views/msmain/admin.php b/www/protected/views/msmain/admin.php index bf86f94..facf87c 100644 --- a/www/protected/views/msmain/admin.php +++ b/www/protected/views/msmain/admin.php @@ -88,12 +88,10 @@ $this->breadcrumbs =
-

Program of the day


breadcrumbs = ); ?>
+ +
+

Hit counter

+ +
+ + hitcounter; + ?> + + Hits (today): getTodayCount(); ?>
+ Hits (total): getTotalCount(); ?>
+ +
+ + $now->format('d.m.Y :: D'), + 'Count' => $hc->getCountForDay($now), + ]; + + $now->modify('-1 day'); + } + + $this->widget('bootstrap.widgets.TbGridView', + [ + 'type' => TbHtml::GRID_TYPE_CONDENSED, + 'dataProvider' => new CArrayDataProvider($data, + [ + 'keyField' => 'Date', + 'Pagination' => + [ + 'PageSize' => 100, + ] + ]), + ] + ); ?> +
\ No newline at end of file