function __process_post($post) { // finding similar matches $areaRange = $post->area / 5; $priceRange = $post->price / 5; $ptype = $post->post_type == 1 ? 2 : 1; $similars = Post::where('property_type', $post->property_type)->whereRaw('abs(' . $post->area . ' - area) <= ' . $areaRange)->whereRaw('abs(' . $post->price . ' - price) <= ' . $priceRange)->where('num_rooms', '>=', $post->num_rooms)->where('post_type', $post->post_type)->where('id', '<>', $post->id)->where('user_id', '<>', $post->user_id)->get(); foreach ($similars as $s) { $dist = distance($post->lat, $post->lng, $s->lat, $s->lng, 'K'); $sp = new SimilarPost(); $sp->post_from = $post->id; $sp->post_to = $s->id; $sp->dist = $dist; $sp->state = 0; $sp->save(); if ($post->num_rooms == $s->num_rooms) { SimilarPost::where('post_to', $post->id)->where('post_from', $s->id)->delete(); $ssp = new SimilarPost(); $ssp->post_to = $post->id; $ssp->post_from = $s->id; $ssp->dist = $dist; $ssp->state = 0; $ssp->save(); } } // finding matchings $matchings = Post::where('property_type', $post->property_type)->whereRaw('abs(' . $post->area . ' - area) <= ' . $areaRange)->whereRaw('abs(' . $post->price . ' - price) <= ' . $priceRange)->where('num_rooms', '>=', $post->num_rooms)->where('post_type', $ptype)->where('id', '<>', $post->id)->where('user_id', '<>', $post->user_id)->get(); $devices = array(); foreach ($matchings as $m) { $dist = distance($post->lat, $post->lng, $m->lat, $m->lng, 'K'); $match = MatchingPost::where('post_from', $post->id)->where('post_to', $m->id)->first(); if ($match == NULL) { $match = new MatchingPost(); $match->post_from = $post->id; $match->post_to = $m->id; $match->dist = $dist; $match->state = 0; $match->save(); } else { $nmatch = new MatchingPost(); $nmatch->post_from = $post->id; $nmatch->post_to = $m->id; $nmatch->dist = $dist; $nmatch->state = $match->state; $nmatch->save(); $match->delete(); $match = $nmatch; } if ($post->num_rooms == $m->num_rooms) { $amatch = MatchingPost::where('post_from', $m->id)->where('post_to', $post->id)->first(); if ($amatch == NULL) { $amatch = new MatchingPost(); $amatch->post_from = $m->id; $amatch->post_to = $post->id; $amatch->dist = $dist; $amatch->state = 0; $amatch->save(); } else { $anmatch = new MatchingPost(); $anmatch->post_from = $m->id; $anmatch->post_to = $post->id; $anmatch->dist = $dist; $anmatch->state = $amatch->state; $anmatch->save(); $amatch->delete(); } } if ($match->state == 0 && $dist < 5) { // send notification to post owners within 5 km (distance between post and post) $user = $m->user; // unset($devices); // $devices = array(); // foreach ($user->logins as $login){ // if ($login->push_type == 2){ // if ($login->push_token == NULL || strlen($login->push_token) < 10){ // continue; // } // $devices[] = $login->push_token; // } // } $match->state = 1; $match->save(); $message = array('message' => $post->user->full_name . ' has just created a post that matches your post', 'post_id' => $match->post_to, 'post_from' => $match->post_from); // sendGCMMessage($devices, $message); sendPush($user->logins, $message); } } }