Esempio n. 1
0
 public static function addPost($input)
 {
     $validation = Validator::make($input, Posts::$addPostRules);
     if ($validation->fails()) {
         return Response::json(array('status' => '0', 'msg' => $validation->getMessageBag()->first()), 200);
     } else {
         $access_token = $input['token'];
         $title = $input['title'];
         $skillset = $input['skillset'];
         $place_id = $input['place_id'];
         $min_age = $input['min_age'];
         $max_age = $input['max_age'];
         $post_desc = isset($input['post_desc']) ? $input['post_desc'] : "";
         $post_image = Input::file('post_image');
         $post_video = Input::file('post_video');
         $current_time = new DateTime();
         if (is_string($skillset)) {
             $skillset = str_replace('"', "", $skillset);
             $skillset = explode(",", $skillset);
         } else {
             $skillset = json_decode($skillset, true);
         }
         $user_id = Users::getUserIdByToken($access_token);
         if ($user_id) {
             $place_data = Posts::getLatlng($place_id);
             $lat = $place_data['location']['lat'] ? $place_data['location']['lat'] : '0';
             $lng = $place_data['location']['lng'] ? $place_data['location']['lng'] : '0';
             //users with matching skillset
             $user_data = DB::table('user_skillset')->selectRaw('users.apn_id,users.reg_id,users.push_notification')->Join('users', 'users.id', '=', 'user_skillset.user_id')->whereIN('user_skillset.skillset_id', $skillset)->groupBy('users.id')->get();
             if ($post_image == "") {
                 $image = "";
             } else {
                 $image = Users::uploadPostFiles('post_image');
             }
             $notify_message = "New Post matching your skillset";
             if ($post_video == "") {
                 $video = "";
             } else {
                 $video = Users::uploadPostFiles('post_video');
             }
             $post_id = DB::table('posts')->insertGetId(array('user_id' => $user_id, 'title' => $title, 'post_desc' => $post_desc, 'post_image' => $image, 'post_video' => $video, 'min_age' => $min_age, 'max_age' => $max_age, 'post_lat' => $lat, 'post_lng' => $lng, 'created_at' => $current_time));
             if ($skillset) {
                 foreach ($skillset as $key => $value) {
                     $skill[$key] = DB::table('post_skillset')->select('*')->where('post_id', $post_id)->where('user_id', $user_id)->where('skillset_id', $value)->get();
                     if (!$skill[$key]) {
                         $post_skill = DB::table('post_skillset')->insertGetId(array('user_id' => $user_id, 'post_id' => $post_id, 'skillset_id' => $value, 'created_at' => $current_time));
                     }
                 }
             }
             $post_desc = Posts::PostDesc($user_id, $post_id);
             foreach ($user_data as $row) {
                 if ($row->push_notification) {
                     Notifications::SendPushNotification($notify_message, '', $row->apn_id, 3, $post_id, $user_id);
                     Notifications::SendPushNotification($notify_message, $row->reg_id, '', 3, $post_desc, $user_id);
                 }
             }
             if ($post_id) {
                 return Response::json(array('status' => '1', 'msg' => 'New Post Added'), 200);
             } else {
                 return Response::json(array('status' => '0', 'msg' => "Error"), 200);
             }
         } else {
             return Response::json(array('status' => '0', 'msg' => "Token Expired"), 200);
         }
     }
 }