-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(NotificationReport) Added a report for selecting a date range whe…
…n exporting internal notifications
- Loading branch information
Showing
2 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ | |
"images" | ||
], | ||
"branch-alias": { | ||
"dev-master": "4.3.x-dev" | ||
"dev-master": "4.4.x-dev" | ||
} | ||
}, | ||
"suggest": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Symbiote\Notifications\Report; | ||
|
||
use Exception; | ||
use SilverStripe\Control\Controller; | ||
use SilverStripe\Forms\DateField; | ||
use SilverStripe\Forms\DropdownField; | ||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\ORM\ArrayList; | ||
use SilverStripe\ORM\DataList; | ||
use SilverStripe\Reports\Report; | ||
use Symbiote\Notifications\Model\BroadcastNotification; | ||
use Symbiote\Notifications\Model\InternalNotification; | ||
|
||
class NotificationReport extends Report | ||
{ | ||
|
||
private static $notification_types = [ | ||
InternalNotification::class => 'Internal message', | ||
]; | ||
|
||
public function title() | ||
{ | ||
return _t(__CLASS__ . '.NOTIFICATION_REPORT', 'Notifications'); | ||
} | ||
|
||
public function group() | ||
{ | ||
return _t(__CLASS__ . '.NOTIFICATION_REPORT_TITLE', "Notification reports"); | ||
} | ||
|
||
public function sourceRecords($params, $sort, $limit) | ||
{ | ||
$type = $this->getReportType($params); | ||
|
||
$list = DataList::create($type); | ||
|
||
$fromDate = $params['From'] ?? date('Y-m-d', strtotime('-1 month')); | ||
$toDate = $params['To'] ?? date('Y-m-d'); | ||
|
||
$list = $list->filter([ | ||
'Created:GreaterThan' => $fromDate, | ||
'Created:LessThanOrEqual' => $toDate | ||
]); | ||
return $list; | ||
} | ||
|
||
public function columns() | ||
{ | ||
$ctrl = Controller::curr(); | ||
$params = $ctrl ? $ctrl->getRequest()->getVar('filter') : []; | ||
|
||
$type = $this->getReportType($params); | ||
return $type::config()->summary_fields; | ||
} | ||
|
||
protected function getReportType($params) | ||
{ | ||
$type = $params['Type'] ?? InternalNotification::class; | ||
if (!isset(self::config()->notification_types[$type])) { | ||
throw new Exception("Invalid type"); | ||
} | ||
return $type; | ||
} | ||
|
||
public function parameterFields() | ||
{ | ||
$ctrl = Controller::curr(); | ||
$params = $ctrl ? $ctrl->getRequest()->getVar('filter') : []; | ||
|
||
$fields = FieldList::create( | ||
DropdownField::create('Type', 'Notification type', self::config()->notification_types), | ||
$from = DateField::create('From'), | ||
$to = DateField::create('To') | ||
); | ||
|
||
if (!isset($params['From'])) { | ||
$from->setValue(date('Y-m-d', strtotime('-1 month'))); | ||
} | ||
|
||
return $fields; | ||
} | ||
} |