Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Oct 31, 2024
1 parent 7763ed6 commit 0e3a2d6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"backpack/crud": "^6.0",
"panphp/pan": "^0.1.5"
"panphp/pan": "^0.1.7"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/pan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
'hovers' => 'Hovers',
'name' => 'Name',
'analytics' => 'Analytics',
'tags' => 'Tags',
];
1 change: 1 addition & 0 deletions resources/lang/pt/pan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
'hovers' => 'Hovers',
'name' => 'Nome',
'analytics' => 'Estatisticas',
'tags' => 'Tags',
];
35 changes: 34 additions & 1 deletion src/BackpackPanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Pan\PanConfiguration;


class BackpackPanServiceProvider extends ServiceProvider
{
Expand All @@ -15,12 +17,43 @@ public function register()
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
], function () {
Route::crud(config('backpack.pan.route_prefix'), PanAnalyticsCrudController::class);
Route::crud(config('backpack.pan.panel_route_prefix'), config('backpack.pan.controller'));
});
}

public function boot()
{
$this->loadTranslationsFrom(realpath(__DIR__.'/../resources/lang'), 'backpack-pan');

$this->publishes([
__DIR__.'/config/pan.php' => config_path('backpack/pan.php'),
], 'pan-config');

PanConfiguration::allowedAnalytics([
"my-button",
"welcome-page",
"welcome-login-link",
"welcome-docs-link",
"welcome-github-link",
"welcome-contact-link",
"login-form",
"menu-item-dashboard",
"menu-item-addons",
"menu-item-petshop",
"menu-item-news",
"menu-item-auth",
"menu-item-filemanager",
"menu-item-activity-log",
"menu-item-translation-manager",
"menu-item-calendar-operation",
"menu-item-backup-manager",
"menu-item-log-manager",
"menu-item-settings",
"menu-item-page-manager",
"menu-item-menu-manager",
"menu-item-analytics",
]);

PanConfiguration::routePrefix(config('backpack.pan.events_route_prefix', 'pan'));
}
}
50 changes: 43 additions & 7 deletions src/PanAnalyticsCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PanAnalyticsCrudController extends CrudController
public function setup()
{
CRUD::setModel('Backpack\Pan\PanAnalytic');
CRUD::setRoute(config('backpack.base.route_prefix').'/'.config('backpack.pan.route_prefix'));
CRUD::setRoute(config('backpack.base.route_prefix').'/'.config('backpack.pan.panel_route_prefix'));
CRUD::setEntityNameStrings(null, trans('backpack-pan::pan.analytics'));
}

Expand All @@ -23,44 +23,80 @@ public function setupListOperation()
CRUD::column('clicks')->label(trans('backpack-pan::pan.clicks'));
CRUD::column('hovers')->label(trans('backpack-pan::pan.hovers'));

if (backpack_pro()) {
$this->setupFilters();
}
}

private function setupFilters(): void
{
if (config('backpack.pan.filters.tags')) {
CRUD::addFilter(
[
'type' => 'select2_multiple',
'name' => 'tags',
'label' => trans('backpack-pan::pan.tags'),
],
PanAnalytic::all()->pluck('name', 'id')->toArray(),
function ($values) {
CRUD::addClause('whereIn', 'id', json_decode($values, false, 512, JSON_NUMERIC_CHECK));
}
);
}

if (config('backpack.pan.filters.impressions')) {
CRUD::addFilter(
[
'type' => 'text',
'type' => 'range',
'name' => 'impressions',
'label' => trans('backpack-pan::pan.impressions'),
],
false,
function ($value) {
$this->crud->addClause('where', 'impressions', '>=', (int) $value);
$range = json_decode($value);
if(empty($range->to)) {
return $this->crud->addClause('where', 'impressions', '>=', (int) $range->from);
}
$this->crud->addClause('where', 'impressions', '>=', (int) $range->from);
$this->crud->addClause('where', 'impressions', '<=', (int) $range->to);
}
);
}

if (config('backpack.pan.filters.clicks')) {
CRUD::addFilter(
[
'type' => 'text',
'type' => 'range',
'name' => 'clicks',
'label' => trans('backpack-pan::pan.clicks'),
],
false,
function ($value) {
$this->crud->addClause('where', 'clicks', '>=', (int) $value);
$range = json_decode($value);
if(empty($range->to)) {
return $this->crud->addClause('where', 'clicks', '>=', (int) $range->from);
}
$this->crud->addClause('where', 'clicks', '>=', (int) $range->from);
$this->crud->addClause('where', 'clicks', '<=', (int) $range->to);
}
);
}

if (config('backpack.pan.filters.hovers')) {
CRUD::addFilter(
[
'type' => 'text',
'type' => 'range',
'name' => 'hovers',
'label' => trans('backpack-pan::pan.hovers'),
],
false,
function ($value) {
$this->crud->addClause('where', 'hovers', '>=', (int) $value);
$range = json_decode($value);
if(empty($range->to)) {
return $this->crud->addClause('where', 'hovers', '>=', (int) $range->from);
}
$this->crud->addClause('where', 'hovers', '>=', (int) $range->from);
$this->crud->addClause('where', 'hovers', '<=', (int) $range->to);
}
);
}
Expand Down
12 changes: 11 additions & 1 deletion src/config/pan.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<?php

return [
'route_prefix' => 'analytics',
// the prefix you use to submit events to the API
'events_route_prefix' => 'pan',

// the prefix you use to access your analytics panel
'panel_route_prefix' => 'analytics',

// to use filters in your panel you need to have backpack/pro installed. https://backpackforlaravel.com/products/pro-for-unlimited-projects
'filters' => [
'tags' => true,
'impressions' => true,
'clicks' => true,
'hovers' => true,
],

// the controller used to display the analytics panel
'controller' => \Backpack\Pan\PanAnalyticsCrudController::class,
];

0 comments on commit 0e3a2d6

Please sign in to comment.