Skip to content

Instantly share code, notes, and snippets.

@iammateus
Created December 11, 2018 14:11
Show Gist options
  • Save iammateus/e4950473416991d1952e92eba62ae025 to your computer and use it in GitHub Desktop.
Save iammateus/e4950473416991d1952e92eba62ae025 to your computer and use it in GitHub Desktop.
public function searchBuilder(Request $request)
{
try {
$sort_by = null;
$search_type = $request->has("search_type") ? $request->get("search_type") : "";
$search = (new Volunteers)->with('businessesVolunteers', 'peopleVolunteers', 'demands', 'events', 'actingAreas', 'serviceTypes')->newQuery();
if ($request->has('selecting_volunteers')) {
$search->where('settings->enabled_for_search', '1');
$search->where('settings->enabled_for_notifications', '1');
}
if ($request->has('settings') && !empty($request->get('settings')['enabled_for_search'])) {
$search->where('settings->enabled_for_search', '0');
}
if ($request->has('q') && !empty($request->get('q'))) {
$search->where(function ($query) use ($request) {
$query->whereHas('businessesVolunteers', function ($subquery) use ($request) {
$subquery->where('businesses_volunteers.business_name', 'like', '%' . $request->get('q') . '%');
$subquery->orWhere('businesses_volunteers.contact_emails', 'like', '%' . $request->get('q') . '%');
});
$query->orWhereHas('peopleVolunteers', function ($subquery) use ($request) {
$subquery->where('people_volunteers.name', 'like', '%' . $request->get('q') . '%');
});
});
$search->orWhere('emails', 'like', '%' . $request->get( 'q' ) . '%');
}
//@todo test it (it seems work fine)
$search_type_plural = $search_type.'s';//it will be 'events' or 'demands'
if ($request->has('status') && !empty($request->get('status')[0]) && !empty($search_type)){
if (!in_array('not_invited', $request->get('status'))) :
$search->whereHas($search_type_plural, function ($subquery) use ($request , $search_type_plural, $search_type) {
$subquery->whereIn($search_type_plural.'_candidates.status', $request->get('status'));
if ($request->has($search_type) && !empty($request->get($search_type))) {
$subquery->where($search_type_plural.'_candidates.'.$search_type_plural.'_id', $request->get($search_type));
}
});
else :
$search->doesntHave($search_type_plural);
endif;
}
if ($request->has('name') && !empty($request->get('name'))) {
$search->whereHas('businessesVolunteers', function ($subquery) use ($request) {
$subquery->where('businesses_volunteers.business_name', 'like', '%' . $request->get('name') . '%');
});
$search->orWhereHas('peopleVolunteers', function ($subquery) use ($request) {
$subquery->where('people_volunteers.name', 'like', '%' . $request->get('name') . '%');
});
}
if ($request->has('service_type') && !empty($request->get('service_type')[0])) {
$search->whereHas('serviceTypes', function ($query) use ($request) {
$query->whereIn('service_types.id', $request->get('service_type'));
});
}
if ($request->has('acting_area') && !empty($request->get('acting_area')[0])) {
$search->whereHas('actingAreas', function ($query) use ($request) {
$query->whereIn('acting_areas.id', $request->get('acting_area'));
});
}
# (A) and (B) : Item 38 QA-10092018 For default, this search looks only for business and lawyer accounts,
# students will be ignored until they are specified into filters at sidebar
if ($request->has('type') && !empty($request->get('type')[0])) {
if (in_array('business', $request->get('type'))) {
$search->where('type', $request->get('type'));
} else {
$search->whereHas('peopleVolunteers', function ($query) use ($request) {
$query->where('people_volunteers.subtype', $request->get('type'));
# (A) ignoring when not specified into filter
if (!in_array('student', $request->get('type')) && $request->has('selecting_volunteers')) {
$query->where('people_volunteers.subtype', '<>', 'student');
}
});
}
} else {
# (B) ignoring by default
$search->where(function ($q) use ($request) {
$q->where('type', 'business')->orWhereHas('peopleVolunteers', function ($query) use ($request) {
if ($request->has('selecting_volunteers')) {
$query->where('people_volunteers.subtype', '<>', 'student');
}
});
});
}
if ($request->has('city') && !empty($request->get('city'))) {
$search->where('city', 'like', '%' . $request->get('city') . '%');
}
if ($request->has('state') && !empty($request->get('state'))) {
$search->where(function ($query) use ($request) {
$query->where('state', $request->get('state'));
$query->orWhereHas('businessesVolunteers', function ($subquery) use ($request) {
$subquery->whereRaw(
'JSON_CONTAINS(states_with_offices, \'["' . $request->get('state') . '"]\')'
);
});
});
}
//@todo fix it
if (!empty($search_type) && $request->has('worked_hours') && (!empty($request->get('worked_hours')[0]) || !empty($request->get('worked_hours')[1]))) {
$alternative_search = $search;
$volunteers_found = $alternative_search->whereHas($search_type_plural, function ($subquery) use ($request, $search_type_plural) {
$subquery->where($search_type_plural.'_candidates.status', 'allocated');
})->orderBy('created_at', 'desc')->get();
if (!empty($volunteers_found)) {
$filtered_by_worked_hours_ids = [];
$min_worked_hours = (int)$request->get('worked_hours')[0];
$max_worked_hours = (int)$request->get('worked_hours')[1];
foreach ($volunteers_found as $volunteer) {
$his_demands_or_events = $volunteer->$search_type_plural()->get();
$his_worked_hours = $his_demands_or_events->sum('work_hours');
# bypass for default
$minimum_compare = true;
$maximum_compare = true;
if (!empty($min_worked_hours)) :
$minimum_compare = $his_worked_hours >= $min_worked_hours;
endif;
if (!empty($max_worked_hours)) :
$maximum_compare = $his_worked_hours <= $max_worked_hours;
endif;
if ($minimum_compare && $maximum_compare) {
$filtered_by_worked_hours_ids[] = $volunteer->id;
}
}
$search->whereIn('volunteers.id', $filtered_by_worked_hours_ids);
}
}
# modifying pagination amount
if ($request->has('items_per_page') && !empty($request->get('items_per_page'))) {
$this->items_per_page = $request->get('items_per_page');
}
return $search
->when(
$sort_by,
function ($subquery, $sort_by) {
return $subquery->orderBy($sort_by);
},
function ($subquery) {
return $subquery->orderBy('created_at', 'desc');
}
)->paginate($this->items_per_page);
} catch (\Exception $e) {
Log::debug($e);
return response([
'error' => 1,
'description' => 'Não foi possível processar sua requisição, tente novamente mais tarde.'
], 400)->header('Content-Type', 'application/json');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment