Skip to content

Commit 0438aca

Browse files
Merge pull request #1 from trendyminds/1.2.0
1.2.0
2 parents d215538 + e45549e commit 0438aca

File tree

6 files changed

+81
-38
lines changed

6 files changed

+81
-38
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Release Notes for Reporter
22

3+
## 1.2.0 - 2022-03-01
4+
5+
> {warning} This update modifies the permission behavior of the plugin. If you have given permissions to users or user groups for Reporter you will need to re-enable these under the "General" section of Craft's permission utility for each user/group.
6+
7+
### Added
8+
- Process reports via the CLI using `php craft reporter/report --handle=myReport`
9+
10+
### Updated
11+
- Use `$hasCpSection` to insert Reporter navigation link in the control panel. This ensures it's ordered properly in the sidebar by Craft.
12+
- Use Craft's baked-in permissions when using `$hasCpSection` instead of custom one.
13+
314
## 1.1.0 - 2022-02-17
415

516
### Updated

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- 🏎 Reports are processed with [Query Batching](https://www.yiiframework.com/doc/api/2.0/yii-db-query#batch()-detail), making exports run quickly and without exhausting your memory limit
1010
- 🧘 Inspired by [Element API](https://github.com/craftcms/element-api), create reports with a simple and familiar structure
1111
- 📦 Reports stored using Asset volumes so you can host reports locally or on a cloud-based service like Amazon S3
12+
- 🖥 Process reports via the CLI using `php craft reporter/report --handle=myReport` where `myReport` is the key of a specific report in the `reports` array found in `config/reporter.php`.
1213

1314
## 📦 Installing
1415

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "trendyminds/craft-reporter",
33
"description": "Export Craft data as CSVs in a snap",
44
"type": "craft-plugin",
5-
"version": "1.1.0",
5+
"version": "1.2.0",
66
"keywords": ["reports", "craft", "craft cms"],
77
"license": "MIT",
88
"authors": [

src/Reporter.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44

55
use Craft;
66
use craft\base\Plugin;
7-
use craft\events\RegisterCpNavItemsEvent;
8-
use craft\web\twig\variables\Cp;
97
use craft\events\RegisterUrlRulesEvent;
10-
use craft\events\RegisterUserPermissionsEvent;
118
use craft\models\VolumeFolder;
12-
use craft\services\UserPermissions;
139
use craft\web\UrlManager;
1410
use Stringy\Stringy;
1511
use yii\base\Event;
1612

1713
class Reporter extends Plugin
1814
{
15+
public $hasCpSection = true;
16+
1917
public function init()
2018
{
2119
parent::init();
@@ -29,35 +27,6 @@ function (RegisterUrlRulesEvent $event) {
2927
$event->rules['reporter/exports'] = 'reporter/default/exports';
3028
}
3129
);
32-
33-
// Setup the control panel navigation link
34-
Event::on(
35-
Cp::class,
36-
Cp::EVENT_REGISTER_CP_NAV_ITEMS,
37-
function(RegisterCpNavItemsEvent $event) {
38-
// Add the navigation item if the user has access to Reporter
39-
if (Craft::$app->user->checkPermission('accessReporter')) {
40-
$event->navItems[] = [
41-
'url' => 'reporter',
42-
'label' => $this->getSettings()->displayName,
43-
'icon' => '@trendyminds/reporter/icon-mask.svg'
44-
];
45-
}
46-
}
47-
);
48-
49-
// Setup user permissions to access the plugin area
50-
Event::on(
51-
UserPermissions::class,
52-
UserPermissions::EVENT_REGISTER_PERMISSIONS,
53-
function(RegisterUserPermissionsEvent $event) {
54-
$event->permissions['Reporter'] = [
55-
'accessReporter' => [
56-
'label' => 'Access Reporter',
57-
],
58-
];
59-
}
60-
);
6130
}
6231

6332
/**
@@ -80,7 +49,7 @@ public function getReport(string $name)
8049
public function getExportPath()
8150
{
8251
$volume = Craft::$app->getVolumes()->getVolumeByHandle(
83-
$this->getSettings()->volume
52+
$this->getSettings()->volume ?? ''
8453
);
8554

8655
if (!$volume) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace trendyminds\reporter\console\controllers;
4+
5+
Use Craft;
6+
use craft\console\Controller;
7+
use trendyminds\reporter\jobs\ExportJob;
8+
use trendyminds\reporter\Reporter;
9+
use yii\helpers\Console;
10+
11+
class ReportController extends Controller
12+
{
13+
/**
14+
* @var string The handle of the report to export
15+
*/
16+
public $handle = null;
17+
18+
public function options($actionID)
19+
{
20+
$options = parent::options($actionID);
21+
$options[] = 'handle';
22+
23+
return $options;
24+
}
25+
26+
/**
27+
* Export a single Reporter report using --handle=myReportHandle
28+
*/
29+
public function actionIndex()
30+
{
31+
// Error if the user did not supply a --handle param
32+
if (! $this->handle) {
33+
$this->stderr("You must supply a --handle parameter to indicate which report to process:" . PHP_EOL, Console::FG_RED);
34+
$this->stderr("php craft reporter/report --handle=myReportHandle" . PHP_EOL);
35+
die();
36+
}
37+
38+
// Get the report by its handle
39+
$report = Reporter::getInstance()->getReport($this->handle);
40+
41+
// Error out if we did not find a match
42+
if(! $report) {
43+
$this->stderr("You must specify the handle of an existing report to export." . PHP_EOL, Console::FG_RED);
44+
$this->stderr("Consult `config/reporter.php` to ensure you created at least one report and properly referenced the handle." . PHP_EOL, Console::FG_RED);
45+
die();
46+
}
47+
48+
// Process the closure and reassign the output for the remaining steps
49+
$report = $report();
50+
51+
// Provide a bit of visual feedback to the user that their report is being processed
52+
$this->stdout('Added an export of the "' . $report['name'] . '" report to the queue.' . PHP_EOL, Console::FG_BLUE);
53+
54+
// Send the process to the queue
55+
Craft::$app->getQueue()->push(
56+
new ExportJob([
57+
'handle' => $this->handle,
58+
'name' => $report['name'],
59+
])
60+
);
61+
}
62+
}

src/controllers/DefaultController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DefaultController extends Controller
1717
*/
1818
public function actionIndex()
1919
{
20-
$this->requirePermission('accessReporter');
20+
$this->requirePermission('accessPlugin-reporter');
2121

2222
return $this->renderTemplate("reporter/index", [
2323
'displayName' => Reporter::getInstance()->getSettings()->displayName,
@@ -39,7 +39,7 @@ public function actionIndex()
3939
*/
4040
public function actionExports()
4141
{
42-
$this->requirePermission('accessReporter');
42+
$this->requirePermission('accessPlugin-reporter');
4343

4444
$folderVolume = Reporter::getInstance()->getExportPath();
4545

@@ -53,7 +53,7 @@ public function actionExports()
5353
*/
5454
public function actionRun()
5555
{
56-
$this->requirePermission('accessReporter');
56+
$this->requirePermission('accessPlugin-reporter');
5757
$this->requirePostRequest();
5858

5959
$reportHandle = $this->request->getRequiredBodyParam('report');

0 commit comments

Comments
 (0)