public function sendToAPNS() { $user_to = Users::find($this->user_id_to); $device_id = $user_to->device_id; // Build payload $aps = array(KeyParser::alert => $this->buildMessage(), KeyParser::sound => CONSTANTS::APNS_SOUND_DEFAULT, KeyParser::badge => $this->getNotificationByUserTo($this->user_id_to, CONSTANTS::NOTIFICATION_STATUS_NEW, true)); $custom = array(KeyParser::id => $this->id, KeyParser::type => $this->type, KeyParser::type_id => $this->type_id, KeyParser::restaurant_id => $this->restaurant_id, KeyParser::user_id_from => $this->user_id_from, KeyParser::user_id_to => $this->user_id_to); $body = array('aps' => $aps, 'custom' => $custom); $payload = json_encode($body); $json_size = strlen($payload); $pem_file = Config::get('apns.apns_pem_file'); $host = Config::get('apns.apns_host'); $passphrase = Config::get('apns.apns_passphrase'); // Create socket $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $pem_file); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); try { $fp = stream_socket_client($host, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); } catch (\Exception $e) { return array(KeyParser::status => CONSTANTS::FAIL, KeyParser::message => CONSTANTS::NOTIFICATION_SERVER_FAILURE, KeyParser::host => $host, KeyParser::pem_file => $pem_file, KeyParser::error => $e->getMessage()); } if (!$fp) { return array(KeyParser::status => CONSTANTS::FAIL, KeyParser::message => CONSTANTS::NOTIFICATION_SERVER_FAILURE, KeyParser::host => $host, KeyParser::pem_file => $pem_file, KeyParser::error => CONSTANTS::NOTIFICATION_SERVER_FAILURE); } // Build send data try { $send_data = chr(0) . chr(0) . chr(32) . pack('H*', $device_id) . chr(0) . chr($json_size) . $payload; } catch (\ErrorException $e) { return array(KeyParser::status => CONSTANTS::FAIL, KeyParser::message => CONSTANTS::NOTIFICATION_INVALID_DEVICE_ID); } // Send $result = fwrite($fp, $send_data); if ($result !== false) { $result = $body; } fclose($fp); return $result; }
/** * Construct the needed Array for Reviews User and Restaurant * action: userAction, restaurantAction * * @param $reviews * @return mixed */ public static function reviewsQueries($reviews) { $reviews_array = array(); if (!$reviews) { return $reviews_array; } foreach ($reviews as $review) { $restaurant = Restaurants::find($review->restaurant_id); if (!$restaurant) { continue; } $user = Users::find($review->user_id); $photos = Photos::getByType(CONSTANTS::REVIEW, $review->id); $photos_array = Photos::convertPhotosToArray($photos); if ($user) { $reviews_array[] = array(KeyParser::review => ModelFormatter::reviewFormat($review), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array); } //end of check user unset($restaurant); unset($user); unset($photos); unset($photos_array); } //end foreach return $reviews_array; }
function checkTypeId($type, $type_id) { switch ($type) { case CONSTANTS::REVIEW: $object = Reviews::find($type_id); $message = "Review does not exist"; $error_code = CONSTANTS::ERROR_CODE_REVIEW_MISSING; break; case CONSTANTS::CHECKIN: $object = CheckIns::find($type_id); $message = "Checkin does not exist"; $error_code = CONSTANTS::ERROR_CODE_CHECKIN_MISSING; break; case CONSTANTS::BOOKMARK: $object = Bookmarks::find($type_id); $message = "Bookmark does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case CONSTANTS::COMMENT: $object = Comments::find($type_id); $message = "Comment does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case CONSTANTS::PHOTO: $object = Photos::find($type_id); $message = "Photo does not exist"; $error_code = CONSTANTS::ERROR_CODE_PHOTO_MISSING; break; case CONSTANTS::RESTAURANT: $object = Restaurants::find($type_id); $message = "Restaurant does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case 'user': $object = Users::find($type_id); $message = "User does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; default: return showErrorResponse('Invalid type', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_INVALID_TYPE); } if (!$object) { return showErrorResponse($message, HTTP_ACCEPTED, $error_code); } return false; }
/** * @param Request $request * @param $id * @return Response * @throws Exception */ public function editAction(Request $request, $id) { /* Multipart Procedure $data = $request->only('json'); $data_json = json_decode(file_get_contents($data['json']), true); */ $data_json['CheckIn'] = $request->json()->get('CheckIn'); if (!isset($data_json['CheckIn']) || !isset($data_json['CheckIn']['message'])) { $message = "Format should be: {'CheckIn': 'message': <string>}}"; return showErrorResponse($message, HTTP_UNPROCESSABLE_ENTITY); } // check fo valid data // Check Ng Words $ng_words = NgWord::ngword_filter($data_json['CheckIn']['message']); if ($ng_words) { $message = "Bad word(s) found: " . implode(',', $ng_words); return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND); } // check of Ng Words try { // Edit Review Data $checkin = new Checkins(); $edit_checkin = $checkin->editCheckin($id, $data_json['CheckIn']); $photos = Photos::getByType(CONSTANTS::CHECKIN, $edit_checkin->id); $photos_array = Photos::convertPhotosToArray($photos); $restaurant = Restaurants::find($edit_checkin->restaurant_id); if (!$restaurant) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } $user = Users::find($edit_checkin->user_id); $json_return[KeyParser::data] = array(KeyParser::checkin => ModelFormatter::checkinFormat($edit_checkin), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array); return response()->json($json_return); } catch (\Exception $e) { return showErrorResponse($e->getMessage()); } // end catch }
/** * Construct the needed Array for Checkins User and Restaurant * action: * * @param $checkins * @return mixed */ public static function checkinsQueries($checkins) { $checkins_array = array(); if (!$checkins) { return $checkins_array; } foreach ($checkins as $checkin) { $restaurant = Restaurants::find($checkin->restaurant_id); if (!$restaurant) { continue; } $user = Users::find($checkin->user_id); $photos = Photos::getByType(CONSTANTS::CHECKIN, $checkin->id); $photos_array = Photos::convertPhotosToArray($photos); $checkins_array[] = array(KeyParser::checkin => ModelFormatter::checkinFormat($checkin), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array); unset($restaurant); unset($user); unset($photos); unset($photos_array); } //end foreach return $checkins_array; }
/** * Get the list of users that likes the activity * * @param request $request * @return Response */ public function likerListAction(Request $request) { $viewer_id = $request->viewer_id; $type = $request->type; $type_id = $request->type_id; $error_msg = checkTypeId($type, $type_id); if ($error_msg) { return $error_msg; } $liker_list = Like::getLikerList($type, $type_id); $json_return[KeyParser::data] = array(); if ($liker_list) { foreach ($liker_list as $index => $liker) { $user_id = $liker->user_id; $user_data = Users::find($user_id); if ($user_data) { $array = ModelFormatter::userLongFormat($user_data); $array += array(KeyParser::follower_count => Follow::getCountByUserId($user_id, CONSTANTS::FOLLOW_FOLLOWER), KeyParser::review_count => Reviews::getCountByUserId($user_id), KeyParser::is_followed_by_viewer => Follow::isFollowed($viewer_id, $user_id)); $json_return[KeyParser::data][] = $array; unset($array); } // end of check $user_date } $json_return[KeyParser::like_count] = Like::getCount($type, $type_id); } $json_return[KeyParser::page] = array(KeyParser::current => $liker_list->currentPage(), KeyParser::number => $liker_list->lastPage()); return response()->json($json_return); }
/** * Update comment * * @param Request $request * @return Response */ public function editCommentAction(Request $request) { $data = $request->json()->get('Comment'); if (!isset($data['id']) || !isset($data['text']) || !isset($data['user_id'])) { $message = "Format should be: {'Comment': {'id': <int>, 'text': <string>, 'user_id': <int>}}"; return showErrorResponse($message, HTTP_UNPROCESSABLE_ENTITY); } // Check Ng Words $ng_words = NgWord::ngword_filter($data['text']); if ($ng_words) { $message = "Bad word(s) found: " . implode(',', $ng_words); return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND); } // check of Ng Words try { $comment = new Comments(); $comment = $comment->editComment($data['id'], $data['user_id'], $data['text']); $comment = ModelFormatter::commentFormat($comment); $user = Users::find($data['user_id']); $user = ModelFormatter::userLongFormat($user); $json_return[KeyParser::data] = array(KeyParser::comment => $comment, KeyParser::user => $user); } catch (\Exception $e) { return showErrorResponse('Error Editing Comment'); } return response()->json($json_return); }
/** * Follow multiple users * Used in registration when a user logs in to FB for the first time * * @param Request $request * @return Response */ public function followManyAction(Request $request) { $json_return[KeyParser::data] = array(KeyParser::users => array(), KeyParser::failed_users => array()); $data = $request->json()->get('Follow'); if (!isset($data['follower_id']) && !isset($data['follower_fb_id']) || !isset($data['following_ids']) && !isset($data['following_fb_ids'])) { return showErrorResponse("Format should be: {'Follow': {'follower_fb_id': <int>, 'following_fb_ids: <array:int>}}", HTTP_UNPROCESSABLE_ENTITY); } if (isset($data['follower_id'])) { $follower_id = $data['follower_id']; } else { $follower_id = convertFbIdToId($data['follower_fb_id']); } if (isset($data['following_ids'])) { $following_ids = $data['following_ids']; if (!is_array($following_ids)) { $following_ids = array($following_ids); } } else { $following_fb_ids = $data['following_fb_ids']; if (!is_array($following_fb_ids)) { $following_fb_ids = array($following_fb_ids); } $following_ids = array(); foreach ($following_fb_ids as $following_fb_id) { $following_id = convertFbIdToId($following_fb_id); if ($following_id) { $following_ids[] = $following_id; } else { $json_return[KeyParser::data][KeyParser::failed_users][] = $following_fb_id; } } } foreach ($following_ids as $following_id) { if (isSameUser($follower_id, $following_id)) { $json_return[KeyParser::data][KeyParser::failed_users][] = $following_id; continue; } try { $follow_data = new Follow(); $follow_data->addFollow($follower_id, $following_id); } catch (\Exception $e) { if ($e->getCode() == INTEGRITY_CONSTRAINT_VIOLATION) { $json_return[KeyParser::data][KeyParser::failed_users][] = $following_id; continue; } else { return showErrorResponse($e->getMessage()); } } $user_data = Users::find($following_id); $json_return[KeyParser::data][KeyParser::users][] = array(KeyParser::id => $following_id, KeyParser::facebook_id => $user_data->facebook_id, KeyParser::follower_count => Follow::getCountByUserId($user_data->id, CONSTANTS::FOLLOW_FOLLOWER), KeyParser::following_count => Follow::getCountByUserId($user_data->id, CONSTANTS::FOLLOW_FOLLOWED)); } return response()->json($json_return); }
/** * Upload Photo Routine for Restaurant * route: photos/upload/restaurant * * @param Request $request * @return Mixed */ public function photoUploadRestaurantAction(Request $request) { $data = $request->only('json', 'fileField'); $data_photos = $data['fileField']; $data_json = json_decode(file_get_contents($data['json']), true); try { DB::beginTransaction(); foreach ($data_json['Photos'] as $data_json_photo) { foreach ($data_photos as $key => $data_photo) { $photo_text = ""; if (isset($data_json_photo['Photo']['text'])) { $photo_text = $data_json_photo['Photo']['text']; // Check Ng Words $ng_words = NgWord::ngword_filter($photo_text); if ($ng_words) { $message = "Bad word(s) found: " . implode(' ', $ng_words); return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND); } // check of Ng Words } $data_photos[$key]->text = $photo_text; } } $restaurant_id = $data_json['Restaurant']['restaurant_id']; $user_id = $data_json['Restaurant']['user_id']; // Save and Upload Photo $photos_upload = new Photos(); $success_photo = $photos_upload->saveUploadedPhotos($data_photos, $data_json['Restaurant'], CONSTANTS::RESTAURANT, $restaurant_id); $followers = Follow::getFollowerUsersAll($user_id); $notification = new Notification(); //Add Activity foreach ($success_photo as $photo) { $activity = new Activities(); $activity->addActivity(CONSTANTS::PHOTO_UPLOAD_RESTAURANT, $photo->id, $user_id, $restaurant_id); foreach ($followers as $follower) { $notification->addNotificationNewPhoto($user_id, $follower->follower_user_id, $photo->id, $restaurant_id); } } $photos_array = Photos::convertPhotosToArray($success_photo); $restaurant = Restaurants::find($restaurant_id); $user = Users::find($user_id); $json_return[KeyParser::data] = array(KeyParser::activity => ModelFormatter::activityFormat($activity), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array); DB::commit(); return response()->json($json_return); } catch (\Exception $e) { return showErrorResponse($e->getMessage()); } // end catch }
/** * Disable user's notification * * @param Request $request * @return response */ public function disableNotificationAction(Request $request) { $data = $request->json()->get('User'); $user = Users::find($data[CONSTANTS::KEY_USER_ID]); if (!$user) { return showErrorResponse('User not found'); } $old_device_id = $user->device_id; $new_device_id = $data[CONSTANTS::KEY_DEVICE_ID]; if (!$old_device_id && $old_device_id != $new_device_id) { return showErrorResponse('The user is registered on an old device', HTTP_ACCEPTED); } $user->disableNotification(); $json_return[KeyParser::data][KeyParser::user] = Users::getStatistics($user->id); return response()->json($json_return); }
/** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { return view('admin.user')->withUser(Users::find($id)); }
/** * Query the users, restaurant, bookmarks, checkins, reviews * * @param int activities * @return mixed */ public static function activitiesQueries($activities) { $activities_array = array(); if ($activities->count()) { foreach ($activities as $activity) { $data = array(); $data[KeyParser::activity] = ModelFormatter::activityFormat($activity); //GetUserInfo $user = Users::find($activity->user_id); if ($user) { $data[KeyParser::user] = ModelFormatter::userFormat($user); } else { $data[KeyParser::user][KeyParser::error] = "No Information"; } //end check user //GetRestaurantInfo $restaurant = Restaurants::where('status_verify', CONSTANTS::STATUS_VERIFIED)->find($activity->restaurant_id); if ($restaurant) { $data[KeyParser::restaurant] = ModelFormatter::restaurantFormat($restaurant); } else { $data[KeyParser::restaurant][KeyParser::error] = "No Information"; } // end check restaurant $data += self::getActivityType($activity->type, $activity->type_id); $activities_array[] = $data; unset($data); } //end foreach } //end count return $activities_array; }
/** * get all of the bookmarks of a user * /bookmarks/user/{user_id} * * @param $user_id * @return Response */ public function userBookmarkListAction($user_id) { if (!is_numeric($user_id)) { return showErrorResponse('Incorrect User ID format'); } $bookmark_list = Bookmarks::getBookmarkByUserId($user_id); $user_data = Users::find($user_id); $json_return[KeyParser::data] = array(); if ($bookmark_list) { foreach ($bookmark_list as $bookmark) { $restaurant_data = Restaurants::find($bookmark->restaurant_id); if ($restaurant_data) { $json_return[KeyParser::data][] = array(KeyParser::bookmark => ModelFormatter::bookmarkFormat($bookmark), KeyParser::user => ModelFormatter::userFormat($user_data), KeyParser::restaurant => ModelFormatter::restaurantBookmarkListViewFormat($restaurant_data), KeyParser::categories => Categories::getFormattedRestaurantCategories($bookmark->restaurant_id)); } } } $json_return[KeyParser::page] = array(KeyParser::current => $bookmark_list->currentPage(), KeyParser::number => $bookmark_list->lastPage()); return response()->json($json_return); }
/** * Review View * route: reviews/view{id} * * @param $id * @optional ?viewer_id * @return Response */ public function viewAction($id) { $json_return = array(); $review = Reviews::find($id); if (!$review) { return showErrorResponse('Review not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_REVIEW_MISSING); } $restaurant = Restaurants::find($review->restaurant_id); if (!$restaurant) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } $user = Users::find($review->user_id); $photos = Photos::getByType(CONSTANTS::REVIEW, $review->id); $photos_array = Photos::convertPhotosToArray($photos); $comments = Comments::getByType(CONSTANTS::REVIEW, $review->id); $comments_array = array(); if ($comments) { foreach ($comments as $comment) { $comments_array[] = ModelFormatter::commentFormat($comment); } } $json_return[KeyParser::data] = array(KeyParser::review => ModelFormatter::reviewFormat($review), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array, KeyParser::comments => $comments_array); return response()->json($json_return); }