| Current Path : /home/forge/stage.sksb.smartcon-survey.com/app/Livewire/ |
| Current File : /home/forge/stage.sksb.smartcon-survey.com/app/Livewire/Topics.php |
<?php
namespace App\Livewire;
use App\Helpers\SurveyJSHelper;
use App\Livewire\Filter\QuarterFilter;
use App\Models\Department;
use App\Models\Topic;
use Livewire\Component;
class Topics extends Component
{
public $topics;
public $departments;
public $quarters;
// Selected filters
public $selectedDepartments = [];
public $selectedAges = [];
public $leadingPosition;
public $selectedGender;
public $selectedQuarters = [];
public function mount()
{
$this->topics = Topic::with(['interviews' => function ($query) {
$query->where('is_test', '0');
return $query;
}])->where('id', '!=', 24)->get();
$this->quarters = (new QuarterFilter)->quarters();
$this->departments = Department::all();
$sh = new SurveyJSHelper;
$this->topics = $this->topics->map(function ($topic) use ($sh) {
$topic['results'] = $sh->getResults($topic, $topic->interviews);
return $topic;
});
}
public function clearFilter()
{
$this->selectedDepartments = [];
$this->selectedAges = [];
$this->leadingPosition = null;
$this->selectedGender = null;
$this->selectedQuarters = [];
}
public function filter()
{
$this->topics = $this->topics->map(function ($topic) {
$interviews = $topic->interviews;
// department
if (count($this->selectedDepartments) > 0) {
$departments = [];
foreach ($this->selectedDepartments as $dep) {
$index = array_search($dep, array_column($this->departments, 'id'));
$departments = array_merge($departments, $this->departments[$index]['filters']);
}
$interviews = $interviews->filter(function ($int) use ($departments) {
return in_array($int->department, $departments);
});
}
if ($this->leadingPosition != null) {
$interviews = $interviews->filter(function ($int) {
return $int->leading_pos == $this->leadingPosition;
});
}
if ($this->selectedGender != null) {
$interviews = $interviews->filter(function ($int) {
return $int->gender == $this->selectedGender;
});
}
if (count($this->selectedAges) > 0) {
$ages = [];
foreach ($this->selectedAges as $ageFilter) {
// Split string (e.g. 18_25) and add all ages to the list
$ageGroup = range(explode('_', $ageFilter)[0], explode('_', $ageFilter)[1]);
$ages = array_merge($ages, $ageGroup);
}
sort($ages); // [18, 19, ..., 55]
$interviews = $interviews->filter(function ($int) use ($ages) {
return in_array($int->age, $ages);
});
}
if (count($this->selectedQuarters) > 0) {
$interviews = $interviews->filter(function ($interview) {
$quarters = array_map(function ($date) {
return (int) explode('-', $date)[0];
}, $this->selectedQuarters);
$years = array_map(function ($date) {
return (int) explode('-', $date)[1];
}, $this->selectedQuarters);
$interviewTime = $interview->created_at;
return in_array($interviewTime->quarter, $quarters) && in_array($interviewTime->year, $years);
});
}
$topic->interviews = $interviews;
return $topic;
});
}
public function render()
{
$sh = new SurveyJSHelper;
$this->topics = $this->topics->map(function ($topic) use ($sh) {
$topic['results'] = $sh->getResults($topic, $topic->interviews);
return $topic;
});
return view('livewire.topics');
}
}