Example #1
0
 public static function FetchPosts($user_id, $other_id)
 {
     $user_latlong = Users::getUserLatLong($user_id);
     $latitude = $user_latlong->lat;
     $longitude = $user_latlong->lng;
     $vid_path = "https://s3-us-west-2.amazonaws.com/cbrealestate/connected_uploads/";
     $post_data = DB::table('posts')->selectRaw("users.id as uid,posts.*,posts.id as pid,comments.id as cid,comments.user_id as c_uid,comments.*,\n\t\t\t\t\t\t\t(select count(likes.id) from likes where likes.post_id=posts.id and likes.user_id='{$user_id}') as is_liked,\n\t\t\t\t\t\t\t(select group_concat(CONCAT('#',skillset.skill) SEPARATOR '  ') from skillset JOIN post_skillset ON post_skillset.skillset_id=skillset.id WHERE post_skillset.post_id=posts.id) as skills,\n\t\t\t\t\t\t\t(select count(likes.id) from likes where likes.post_id=posts.id) as likes_count,(select users.full_name from users where users.id=comments.user_id) as un,\n\t\t\t\t\t\t\t\t(select users.profile_pic from users where users.id=comments.user_id) as c_profile_pic,(select users.user_type from users where users.id=comments.user_id) as cutype,\n\t\t\t\t\t\t\t\tTRUNCATE(( 3961 * acos( cos( radians( " . $latitude . " ) ) * cos( radians( `lat` ) ) * cos( radians( `lng` ) - radians( " . $longitude . " ) ) + sin( radians( " . $latitude . " ) ) * sin( radians( `lat` ) ) ) ),2) AS distance,\n\t\t\t\t\t\t\tCASE \n\t\t\t\t\t\t\t  WHEN DATEDIFF(NOW(),posts.created_at) != 0 THEN CONCAT(DATEDIFF(NOW(),posts.created_at) ,'d ago')\n\t\t\t\t\t\t\t  WHEN HOUR(TIMEDIFF(NOW(),posts.created_at)) != 0 THEN CONCAT(HOUR(TIMEDIFF(NOW(),posts.created_at)) ,'h ago')\n\t\t\t\t\t\t\t  WHEN MINUTE(TIMEDIFF(NOW(),posts.created_at)) != 0 THEN CONCAT(MINUTE(TIMEDIFF(NOW(),posts.created_at)) ,'m ago')\n\t\t\t\t\t\t\t  ELSE\n\t\t\t\t\t\t\t\t CONCAT(SECOND(TIMEDIFF(NOW(),posts.created_at)) ,' s ago')\n\t\t\t\t\t\t\tEND as post_time,\n\t\t\t\t\t\t\tCASE \n\t\t\t\t\t\t\t  WHEN DATEDIFF(NOW(),comments.created_at) != 0 THEN CONCAT(DATEDIFF(NOW(),comments.created_at) ,'d ago')\n\t\t\t\t\t\t\t  WHEN HOUR(TIMEDIFF(NOW(),comments.created_at)) != 0 THEN CONCAT(HOUR(TIMEDIFF(NOW(),comments.created_at)) ,'h ago')\n\t\t\t\t\t\t\t  WHEN MINUTE(TIMEDIFF(NOW(),comments.created_at)) != 0 THEN CONCAT(MINUTE(TIMEDIFF(NOW(),comments.created_at)) ,'m ago')\n\t\t\t\t\t\t\t  ELSE\n\t\t\t\t\t\t\t\t CONCAT(SECOND(TIMEDIFF(NOW(),comments.created_at)) ,' s ago')\n\t\t\t\t\t\t\tEND as comment_time")->leftJoin('users', 'users.id', '=', 'posts.user_id')->leftJoin('comments', 'posts.id', '=', 'comments.post_id')->Where('users.id', '=', $other_id)->orderBy('posts.created_at', 'DESC')->get();
     $final = array();
     $result = array();
     if ($post_data) {
         foreach ($post_data as $key => $value) {
             if (!isset($final[$value->pid])) {
                 $final[$value->pid] = array("post_id" => $value->pid ? $value->pid : "", 'post_latitude' => $value->post_lat ? $value->post_lat : "0", 'post_longitude' => $value->post_lng ? $value->post_lng : "0", 'min_age' => $value->min_age, 'max_age' => $value->max_age, "post_title" => $value->title ? $value->title : "", "post_description" => $value->post_desc ? $value->post_desc : "", 'post_image' => $value->post_image ? Users::getFormattedImage($value->post_image) : "", 'post_video' => $value->post_video ? $vid_path . $value->post_video : "", 'post_time' => $value->post_time ? $value->post_time : "", 'likes_count' => $value->likes_count ? $value->likes_count : '0', 'skills' => $value->skills ? $value->skills : '', 'distance' => $value->distance ? $value->distance : '0', 'is_liked' => $value->is_liked ? $value->is_liked : '0', 'comments' => array());
             }
             if (!isset($final[$value->pid]['comments'][$value->cid])) {
                 if ($value->cid) {
                     $final[$value->pid]['comments'][] = array("comment_id" => $value->cid, "user_id" => $value->c_uid ? $value->c_uid : "", "name" => $value->un ? $value->un : "", "user_type" => $value->cutype ? $value->cutype : "", 'profile_pic' => $value->c_profile_pic ? Users::getFormattedImage($value->c_profile_pic) : "", 'comment' => $value->comment ? $value->comment : "", 'comment_time' => $value->comment_time ? $value->comment_time : "");
                 }
             }
         }
     }
     if ($final) {
         foreach ($final as $key => $val) {
             $data2 = array();
             $result[] = $val;
         }
     }
     return $result;
 }