Пример #1
0
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);
        }
    }
}
Пример #2
0
 function admin_update_post($f3, $params)
 {
     $token = $_SERVER['Authorization'];
     $admin = __get_auth_admin($token);
     $result = array();
     if ($admin == NULL) {
         $result['success'] = 'false';
         $result['message'] = 'You are not authorized';
     } else {
         $post_id = $params['id'];
         $post = Post::find($post_id);
         if ($post == NULL) {
             $result = array('success' => 'false', 'message' => 'No such post');
         } else {
             extract($_POST);
             if ($post->post_type != $post_type) {
                 SimilarPost::where('post_from', $post->id)->delete();
                 // if post type had been changed, similar / matching relationship would have been broken.
                 MatchingPost::where('post_from', $post->id)->delete();
             }
             $post->post_type = $post_type;
             $post->property_type = $property_type;
             $post->num_rooms = $num_rooms;
             $post->area = $area;
             $post->price = $price;
             $post->description = $description;
             if ($post->location != $location) {
                 $post->location = $location;
                 $r = getCoordinate($location);
                 $post->lat = $r->results[0]->geometry->location->lat;
                 $post->lng = $r->results[0]->geometry->location->lng;
             }
             $post->save();
             $result['success'] = 'true';
             $result['message'] = 'Updated';
             __process_post($post);
             // processes similar matches and finding matches
         }
     }
     echo json_encode($result);
 }