From 49b7368f44a283ccb77fb4298a0410d1a9052b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 19 Jul 2014 19:35:53 +0200 Subject: [PATCH] Added Download counter --- www/protected/config/main.php | 1 + .../controllers/ProgramsController.php | 30 ++++++++++++++++++- www/protected/models/Program.php | 14 +++++++++ www/protected/views/programs/view.php | 3 +- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/www/protected/config/main.php b/www/protected/config/main.php index 14a031c..37f6a77 100644 --- a/www/protected/config/main.php +++ b/www/protected/config/main.php @@ -70,6 +70,7 @@ return ArrayX::merge( 'programs/' => ['programs/index', 'defaultParams' => ['categoryfilter' => '']], 'programs/cat/' => ['programs/index', 'defaultParams' => ['categoryfilter' => '']], 'programs/view/' => 'programs/view', + 'programs/download/' => 'programs/download', 'log/' => ['log/index', 'defaultParams' => ['logid' => '-1']], 'log/' => ['log/index', 'defaultParams' => ['logid' => '-1']], diff --git a/www/protected/controllers/ProgramsController.php b/www/protected/controllers/ProgramsController.php index 4a78119..5137b6b 100644 --- a/www/protected/controllers/ProgramsController.php +++ b/www/protected/controllers/ProgramsController.php @@ -28,7 +28,7 @@ class ProgramsController extends MSController { return array( array('allow', // allow all users to perform 'index' and 'view' actions - 'actions'=>array('index','view'), + 'actions'=>array('index','view', 'download'), 'users'=>array('*'), ), array('allow', // allow authenticated user to perform 'create' and 'update' actions @@ -72,6 +72,34 @@ class ProgramsController extends MSController )); } + /** + * Displays a particular model. + * @param integer $id the ID of the model to be displayed + * @throws CHttpException when $id is integer + */ + public function actionDownload($id) + { + $this->layout = '//layouts/main'; + + if (is_numeric($id)) + { + if (Yii::app()->user->name == 'admin') { + $model = $this->loadModelByID($id); + } else { + throw new CHttpException(400, "You can't access a program by ID"); + } + } + else + { + $model = $this->loadModelByName($id); + } + + $model->Downloads++; + $model->save(); + + $this->redirect($model->getDirectDownloadLink()); + } + /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. diff --git a/www/protected/models/Program.php b/www/protected/models/Program.php index 6f8659f..b468983 100644 --- a/www/protected/models/Program.php +++ b/www/protected/models/Program.php @@ -176,6 +176,20 @@ class Program extends CActiveRecord return '/programs/view/' . $this->Name; } + /** + * @return string + */ + public function getDownloadLink() { + return '/programs/download/' . $this->Name; + } + + /** + * @return string + */ + public function getDirectDownloadLink() { + return '/data/programs/' . $this->Name . '.zip' ; + } + /** * @return string[] */ diff --git a/www/protected/views/programs/view.php b/www/protected/views/programs/view.php index 155f905..9c94f97 100644 --- a/www/protected/views/programs/view.php +++ b/www/protected/views/programs/view.php @@ -93,7 +93,8 @@ if (!$model->visible && Yii::app()->user->name != 'admin') { 'block' => true, 'color' => TbHtml::BUTTON_COLOR_PRIMARY, 'size' => TbHtml::BUTTON_SIZE_DEFAULT, - 'url' => '#', //TODO-MS Add Download link + 'Content' => 'nofollow', + 'url' => $model->getDownloadLink(), //TODO-MS Add Download link ]); ?>