Browse Source

Count views and clicks by ajax request on banner

master
Egorka 6 years ago
parent
commit
b8065b9c8c
  1. 5
      common/modules/banners/BannersModule.php
  2. 49
      common/modules/banners/controllers/BannerController.php
  3. 12
      common/modules/banners/services/BannerManageService.php
  4. 23
      common/modules/banners/widgets/views/banner.php

5
common/modules/banners/BannersModule.php

@ -35,6 +35,11 @@ class BannersModule extends \yii\base\Module implements ModuleInterface
$app->params['search_rules'][] = "SELECT title, title as content, CONCAT('/banners/manage/banner/view/', id) AS url FROM {{banners}}";
$app->getUrlManager()->addRules([
'banner/view' => 'banners/banner/view',
'banner/click' => 'banners/banner/click',
]);
$app->getUrlManager()->addRules([
'banners/manage/banner/view/<id:\d+>' => 'banners/manage/banner/view',
]);

49
common/modules/banners/controllers/BannerController.php

@ -0,0 +1,49 @@
<?php
/**
* Created by Error202
* Date: 22.08.2018
*/
namespace common\modules\banners\controllers;
use common\modules\banners\entities\Banner;
use common\modules\banners\services\BannerManageService;
use frontend\components\FrontendController;
use yii\web\NotFoundHttpException;
class BannerController extends FrontendController
{
private $service;
public function __construct( string $id, $module, BannerManageService $service, array $config = [] ) {
parent::__construct( $id, $module, $config );
$this->service = $service;
}
public function actionView()
{
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if (\Yii::$app->request->isAjax && $id) {
$banner = $this->findModel( $id );
$this->service->addView( $banner );
}
}
public function actionClick()
{
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if (\Yii::$app->request->isAjax && $id) {
$banner = $this->findModel( $id );
$this->service->addClick( $banner );
}
}
protected function findModel($id): Banner
{
if (($model = Banner::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested banner does not exist.');
}
}

12
common/modules/banners/services/BannerManageService.php

@ -79,4 +79,16 @@ class BannerManageService
$banner = $this->repository->get($id);
$this->repository->remove($banner);
}
public function addView(Banner $banner)
{
$banner->views++;
$this->repository->save($banner);
}
public function addClick(Banner $banner)
{
$banner->clicks++;
$this->repository->save($banner);
}
}

23
common/modules/banners/widgets/views/banner.php

@ -5,14 +5,35 @@
*/
use yii\helpers\Html;
use yii\helpers\Url;
/**
* @var $this \yii\web\View
* @var $banner \common\modules\banners\entities\Banner;
*/
$banner_id = 'banner_' . $banner->id;
$view_url = Url::to(['/banner/view']);
$click_url = Url::to(['/banner/click']);
$js = <<<JS
$("#{$banner_id}").on('click', function(){
$.ajax({
method: "POST",
url: "{$click_url}",
data: { id: "{$banner->id}" }
});
});
$.ajax({
method: "POST",
url: "{$view_url}",
data: { id: "{$banner->id}" }
});
JS;
$this->registerJs($js, $this::POS_READY);
?>
<?= Html::a(Html::img('@static/origin/banners/' . $banner->image), $banner->url, [
'target' => $banner->target
'target' => $banner->target,
'id' => $banner_id,
]) ?>

Loading…
Cancel
Save