I would like to reset the properties for my ReportWidget one of two ways:
1) When the page containing the report widget is refreshed
or
2) When certain properties are changed
My ReportWidget has 3 properties: year, quarter, month
By default, I am displaying information for the current year. If a user changes the year, and then perhaps specifies a quarter or month for that year, the report displays accordingly.
Now, when they come back to the page, these settings are saved. What I would like to do is have the properties reset to their defaults when the page is reloaded or refreshed.
Furthermore, if the user changes from year to year, the other properties should reset.
I have attempted to solve this problem with this code:
public function defineProperties() {
return [
'year' => [
'title' => 'Year',
'default' => $this->getDefaultYear(),
'group' => 'Date',
'type' => 'dropdown',
'options' => $this->getYearOptions(),
],
'quarter' => [
'title' => 'Quarter',
'type' => 'dropdown',
'group' => 'Date',
'options' => $this->getQuarterOptions(),
'depends' => ['month']
],
'month' => [
'title' => 'Month',
'type' => 'dropdown',
'group' => 'Date',
'options' => $this->getMonthOptions(),
'depends' => ['year']
]
];
}
public function getYearOptions() {
$query = User::all();
$years = [];
foreach($query as $user) {
$year = date('Y', strtotime($user->created_at));
$years[$year] = $year;
}
$years = array_unique($years);
return $years;
}
public function getQuarterOptions() {
$monthCode = Request::input('month'); // Load the year property value from POST
$yearCode = Request::input('year');
if ($yearCode && $monthCode) {
return;
}
if ($yearCode) {
return [
1 => '1st',
2 => '2nd',
3 => '3rd',
4 => '4th'
];
}
}
public function getMonthOptions() {
$yearCode = Request::input('year'); // Load the year property value from POST
if ($yearCode) {
return;
}
$months = [];
for ($m=1; $m<=12; $m++) {
$month = date('m', mktime(0,0,0,$m, 1, date('Y')));
$months[$month] = date('M', mktime(0,0,0,$m, 1, date('Y')));
}
return $months;
}
So what happens here is that if the year changes, it will call the getMonthOptions() function which listens for the response and if it catches the year, it will return nothing. Now this works but obviously my month list does not contain any months. I then have to close the property box and reopen it to list the months.
Are there any ideas about how I can implement this functionality? Thank you.