public function getEventsGuest(GetEventsGuestRequest $request)
{
$attr = $request->validated();
$event = Event::where('events.id', '>', 0);
$add_view = null;
$has_orderBy = false;
$event = $event->where('is_deleted', 0);
$event = $event->where('is_banned', 0);
if (!array_key_exists('evt_id', $attr)) {
if (array_key_exists('is_archive', $attr) && $attr['is_archive'] == true) {
$event = $event->where('is_archive', true);
} else {
$event = $event->where('is_archive', false);
}
}
// get-event-by-id
if (array_key_exists('evt_id', $attr)) {
$add_view = $attr['evt_id'];
$event = $event->where('id', $attr['evt_id']);
}
// get-events-by-lat-long
if (
array_key_exists('lt_lat', $attr) &&
array_key_exists('rb_lat', $attr) &&
array_key_exists('lt_long', $attr) &&
array_key_exists('rb_long', $attr)
) {
$event = $event->where('evt_lat', '<=', $attr['lt_lat'])
->where('evt_lat', '>=', $attr['rb_lat'])
->where('evt_long', '>=', $attr['lt_long'])
->where('evt_long', '<=', $attr['rb_long']);
}
// get-events-by-category
if (array_key_exists('evt_ctgy_id', $attr)) {
// All children (one level deep)
$cat_ids = EventCategory::where('parent_id', $attr['evt_ctgy_id'])->pluck('id', 'id')->toArray();
// And main
$cat_ids = array_merge($cat_ids, $attr['evt_ctgy_id']);
$event = $event->whereIn('evt_ctgy_id', $cat_ids);
}
if (array_key_exists('pet_type', $attr)) {
$event = $event->whereHas('pet', function ($query) use ($request) {
$query->where('type_id', $request->get('pet_type'));
});
}
// get-events-by-date-time
if (
array_key_exists('date', $attr) &&
array_key_exists('duration', $attr)
) {
$event = $event->where('evt_dt_added', '>=', $attr['date'] - $attr['duration'])
->where('evt_dt_added', '<=', $attr['date']);
}
// get-events-by-radius
if (
array_key_exists('center_lat', $attr) &&
array_key_exists('center_long', $attr) &&
array_key_exists('distance', $attr)
) {
$event = $event->selectRaw("* ,
( 6371 * acos( cos( radians(?) ) *
cos( radians( evt_lat ) )
* cos( radians( evt_long ) - radians(?)
) + sin( radians(?) ) *
sin( radians( evt_lat ) ) )
) AS distance", [
$attr['center_lat'],
$attr['center_long'],
$attr['center_lat']
])
->having("distance", "<", $attr['distance'])
->orderBy("distance", 'asc');
$has_orderBy = true;
// dd($event->first());
}
if ($request->has('type')) {
$event->where('type', $request->type);
}
if (!$has_orderBy) {
$event->latest();
}
$limit = (int)$request->get('limit', 30);
$events = $event
->with('media')
->with('pet')
->with('pet.media')
->with('user')
->with('user.services')
->with('user.occupations')
->with('last_tree_likes')
->with('last_tree_likes.user')
// ->with('user.media')
->paginate($limit);
return response([
'status' => 'success',
'events' => EventGuestResource::collectionFull($events, $add_view)
], 201);
}
public function getEvents(GetEventsRequest $request)
{
$attr = $request->validated();
// ))))) lol
$event = Event::where('events.id', '>', 0);
$blocked_users_ids = BlockedUser::getAuthUserIds();
$event = $event->whereNotIn('user_id', $blocked_users_ids);
$add_view = null;
$has_orderBy = false;
$event = $event->where('is_deleted', (bool)$request->get('is_deleted'));
$event = $event->where('is_banned', (bool)$request->get('is_banned'));
//dd($attr);
// is archive
//7.28.22 exclude is_archive param if we are directly getting the event with evt_id
if (!array_key_exists('evt_id', $attr)) {
if (array_key_exists('is_archive', $attr) && $attr['is_archive'] == true) {
$event = $event->where('is_archive', true);
} else {
$event = $event->where('is_archive', false);
}
}
if (array_key_exists('pet_type', $attr)) {
$event = $event->whereHas('pet', function ($query) use ($request) {
$query->where('type_id', $request->get('pet_type'));
});
}
// get-event-by-id
if (array_key_exists('evt_id', $attr)) {
$add_view = $attr['evt_id'];
$event = $event->where('id', $attr['evt_id']);
EventViewHistory::addAuth(intval($attr['evt_id']));
}
// get-events-by-lat-long
if (
array_key_exists('lt_lat', $attr) &&
array_key_exists('rb_lat', $attr) &&
array_key_exists('lt_long', $attr) &&
array_key_exists('rb_long', $attr)
) {
$event = $event->where('evt_lat', '<=', $attr['lt_lat'])
->where('evt_lat', '>=', $attr['rb_lat'])
->where('evt_long', '>=', $attr['lt_long'])
->where('evt_long', '<=', $attr['rb_long']);
}
// get-events-by-category
if (array_key_exists('evt_ctgy_id', $attr)) {
//dd($attr);
if ($attr['evt_ctgy_id'][0] == 39) {
//todo: add flag/logic in db
$cat_ids = EventCategory::where('parent_id', 3)->pluck('id', 'id')->toArray();
} else {
// All children (one level deep)
$cat_ids = EventCategory::where('parent_id', $attr['evt_ctgy_id'])->pluck('id', 'id')->toArray();
// And main
}
$cat_ids = array_merge($cat_ids, $attr['evt_ctgy_id']);
$event = $event->whereIn('evt_ctgy_id', $cat_ids);
}
// get-events-by-topic
if (array_key_exists('topic', $attr)) {
$event = $event->where('evt_topic', 'like', '%' . $attr['topic'] . '%');
}
// get-events-by-user
if (array_key_exists('user_id', $attr)) {
// $event = Event::query();
$event = $event->where('events.user_id', $attr['user_id']);
if ($request->has('with_comments') && $request->with_comments) {
$has_orderBy = true;
// dd(EventComment::
// where('user_id', $attr['user_id'])
//// ->whereColumn('event_comments.event_id', 'events.id')
// ->latest()
// ->take(1)->get());
$event = $event->whereHas('comments', function ($query) use ($request) {
$query->where('user_id', $request->get('user_id'))->take(1);
});
// dd($event->get());
$event = $event->orderByDesc(EventComment::select('created_at')
->where('event_comments.user_id', $attr['user_id'])
->whereColumn('event_comments.event_id', 'events.id')
// ->latest()
->take(1)
);
$event = $event->with('comments');
// dd($event->toSql());
}
}
// get-events-by-description
if (array_key_exists('description', $attr)) {
$event = $event->where('description', 'like', '%' . $attr['description'] . '%');
}
// get-events-by-date-time
if (
array_key_exists('date', $attr) &&
array_key_exists('duration', $attr)
) {
$event = $event->where('evt_dt_added', '>=', $attr['date'] - $attr['duration'])
->where('evt_dt_added', '<=', $attr['date']);
}
// get-events-by-search
if (array_key_exists('keyword', $attr)) {
SearchKeyword::addEventSearch($attr['keyword']);
$event = $event->where(function ($q) use ($attr) {
return $q->where('evt_address', 'like', '%' . $attr['keyword'] . '%')
->orWhere('evt_topic', 'like', '%' . $attr['keyword'] . '%')
->orWhere('description', 'like', '%' . $attr['keyword'] . '%');
});
}
// get-events-by-radius
if (
array_key_exists('center_lat', $attr) &&
array_key_exists('center_long', $attr) &&
array_key_exists('distance', $attr)
) {
$event = $event->selectRaw("* ,
( 6371 * acos( cos( radians(?) ) *
cos( radians( evt_lat ) )
* cos( radians( evt_long ) - radians(?)
) + sin( radians(?) ) *
sin( radians( evt_lat ) ) )
) AS distance", [
$attr['center_lat'],
$attr['center_long'],
$attr['center_lat']
])
->having("distance", "<", $attr['distance'])
->orderBy("distance", 'asc');
$has_orderBy = true;
// dd($event->first());
}
$debug = "";
if ($request->has('order_by_user_distance')) {
$distance_direction = $request->has('order_by_user_distance_direction') &&
$request->order_by_user_distance_direction == 'desc' ? 'desc' : 'asc';
$distance_radius = $request->get('order_by_user_distance_radius', 40000);
$user_lat = auth()->user()->lat;
$user_long = auth()->user()->long;
$debug = $user_lat.' '.$user_long;
if ($user_lat && $user_long) {
$event->geofence($user_lat, $user_long, 0, $distance_radius)->orderBy(Event::DISTANCE_COLUMN, $distance_direction);
$has_orderBy = true;
} else {
Log::error('You cannot sort event by distance for user without geolocation');
}
}
// if ($request->has('limit')) {
// $event->take($request->limit);
// }
if ($request->has('search')) {
$event->orWhere(function (Builder $query) use ($request) {
$query->orWhere('evt_topic', 'LIKE', '%' . $request->search . '%')
->orWhere('description', 'LIKE', '%' . $request->search . '%');
});
}
if ($request->has('type')) {
$event->where('type', $request->type);
}
if ($request->has('da_data_region_id')) {
$event->where('da_data_region_id', $request->da_data_region_id);
}
if ($request->has('da_data_location_fias_id')) {
$event->where('da_data_fias_id', $request->da_data_location_fias_id);
}
if ($request->has('da_data_country_id')) {
$event->where('da_data_country_id', $request->da_data_country_id);
}
if ($request->has('region_query_text')) {
$region_ids = DaDataRegion::where('name', 'LIKE', '%' . $request->region_query_text .
'%')->pluck('id')->toArray();
$event->whereIn('da_data_region_id', $region_ids);
}
if ($request->has('location_query_text')) {
$region_like_query = '%' . $request->location_query_text . '%';
$location_fias_ids = DaDataCity::query()
->orWhere('address', 'LIKE', $region_like_query)
->orWhere('region', 'LIKE', $region_like_query)
->orWhere('city', 'LIKE', $region_like_query)
->pluck('fias_id')->toArray();
$event->whereIn('da_data_fias_id', $location_fias_ids);
}
if (
$request->has('da_data_region_id') ||
$request->has('da_data_location_fias_id') ||
$request->has('da_data_country_id') ||
$request->has('region_query_text') ||
$request->has('location_query_text')
) {
$event->with('daDataRegion')->with('daDataCity')->with('daDataCountry');
}
if ($request->has('is_emergency')) {
$event->where('is_emergency', (bool)$request->is_emergency);
}
if (!$has_orderBy) {
$event->latest();
}
if (array_key_exists('nolimit', $attr)) {
$limit = 1000;
} else {
$limit = (int)$request->get('limit', 20);
}
$is_map = (bool)$request->get('is_map');
if ($is_map) {
$events = $event->with('media')->paginate($limit);
return response([
'status' => 'success',
'events' => EventResourceMapVersion::collectionFull($events, $add_view)
], 201);
}
$events = $event
->withCount('comments')
->with('media')
->with('pet')
->with('pet.media')
->with('user')
->with('user.services')
->with('user.occupations')
->with('last_tree_likes')
->with('last_tree_likes.user')
// ->with('user.media')
->paginate($limit);
// dd($events->first()->comments_count);
return response([
'status' => 'success',
'events' => EventResource::collectionFull($events, $add_view),
// 'debug' => $debug
], 201);
}