Esempio n. 1
0
 /**
  * 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
 }
Esempio n. 2
0
 /**
  * @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
 }
Esempio n. 3
0
 /**
  * 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);
 }
Esempio n. 4
0
 /**
  * Convert tsv file to menu data
  *
  * @param Request $request
  * @return array|\Illuminate\View\View|mixed
  */
 public function convertAction(Request $request)
 {
     $errors = [];
     $success = [];
     if ($request->isMethod('post')) {
         $connection = null;
         try {
             $uploaded_files = \Input::file('convert');
             $columns = Schema::getColumnListing('menu');
             array_push($columns, 'category_id', 'cuisine1', 'cuisine2', 'cuisine3');
             $required_columns = ['name', 'category'];
             foreach ($uploaded_files as $uploaded_file) {
                 $filename = explode('.', $uploaded_file->getClientOriginalName());
                 $restaurant_id = $filename[0];
                 $extension = $filename[count($filename) - 1];
                 if (strtolower($extension) != 'tsv') {
                     throw new \Exception('Invalid file format. Must be TSV.');
                 }
                 $file_content = file_get_contents($uploaded_file->getPathname());
                 $file_content = explode("\n", $file_content);
                 $file_content = array_map('trim', $file_content);
                 foreach ($file_content as $fc) {
                     $fc = explode("\t", $fc);
                     if ($fc) {
                         $fc_data[] = $fc;
                     }
                 }
                 $keys = array_shift($fc_data);
                 foreach ($keys as $key => $value) {
                     $keys[$key] = str_replace('"', '', $keys[$key]);
                 }
                 foreach ($fc_data as $key => $value) {
                     if (!$value) {
                         continue;
                     }
                     $current_id = $value[0];
                     $fc_data[$key] = array_combine($keys, $value);
                     $fc_data[$key]['restaurant_id'] = $restaurant_id;
                     $fc_data[$key]['name'] = str_replace('"', '', $fc_data[$key]['name']);
                     $fc_data[$key]['description'] = str_replace('"', '', $fc_data[$key]['description']);
                     $fc_data[$key]['price'] = str_replace('"', '', $fc_data[$key]['price']);
                     $fc_data[$key]['serving'] = str_replace('"', '', $fc_data[$key]['serving']);
                     $fc_data[$key]['category'] = str_replace('"', '', $fc_data[$key]['category']);
                     foreach ($fc_data[$key] as $k => $v) {
                         if (in_array($k, $required_columns) && $v === '') {
                             $errors[] = $k . ' column is required [ID: ' . $current_id . ']';
                         }
                         if (!in_array($k, $columns)) {
                             unset($fc_data[$key][$k]);
                         }
                     }
                     // Check Ng Words
                     $ng_words = NgWord::ngword_filter($fc_data[$key]['name'] . ' ' . $fc_data[$key]['description'] . ' ' . $fc_data[$key]['serving'] . ' ' . $fc_data[$key]['category']);
                     if ($ng_words) {
                         $errors[] = "Bad word(s) found: " . implode(',', $ng_words);
                     }
                 }
                 $data[] = $fc_data;
                 $fc_data = [];
             }
             if ($errors) {
                 $data = ['errors' => $errors, 'success' => $success];
                 return view('cms.menu.convert', $data);
             }
             $connection = DB::connection();
             $connection->beginTransaction();
             foreach ($data as $key => $value) {
                 foreach ($value as $v) {
                     $menu = new MenuCms();
                     $menu->addMenu($v['restaurant_id'], $v['name'], $v['category'], $v['serving'], $v['price'], $v['description']);
                 }
             }
             $connection->commit();
             $success[] = 'Data successfully added';
         } catch (\Exception $e) {
             if ($connection) {
                 $connection->rollBack();
             }
             $errors[] = $e->getMessage();
             $errors[] = 'Error occured at ID ' . $current_id . ' of TSV file.';
         } catch (\PDOException $pe) {
             if ($connection) {
                 $connection->rollBack();
             }
             $errors[] = 'Error trying to add data';
         }
     }
     $data = array('success' => $success, 'errors' => $errors);
     return view('cms.menu.convert', $data);
 }
Esempio n. 5
0
 /**
  * Add new restaurant suggest and return JSON data
  * route: /restaurants/suggest
  *
  * @param Request $request
  * @return Response
  */
 public function suggestAction(Request $request)
 {
     $data = $request->json()->get('restaurant');
     if (!isset($data['name']) || !isset($data['address']) || !isset($data['latitude']) || !isset($data['longitude']) || !isset($data['user_id'])) {
         $message = "Format should be: {'restaurant': {'name': <string>, 'address': <string>, 'latitude': <double>, 'longitude': <double>, 'user_id': <int>}}";
         return showErrorResponse($message, HTTP_UNPROCESSABLE_ENTITY);
     }
     // Check Ng Words
     $ng_words = NgWord::ngword_filter($data['name'] . ' ' . $data['telephone'] . ' ' . $data['address'] . ' ' . $data['cuisines'] . ' ' . $data['other_details']);
     if ($ng_words) {
         $message = "Bad words found: " . implode(', ', $ng_words);
         return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND);
     }
     try {
         $restaurant_suggest = new RestaurantsSuggest();
         $restaurant_suggest->addRestaurantSuggest($data);
         $json_return[KeyParser::data] = ModelFormatter::restaurantSuggestFormat($restaurant_suggest);
     } catch (\Exception $e) {
         return showErrorResponse($e->getMessage());
     }
     return response()->json($json_return);
 }
Esempio n. 6
0
 /**
  * Review Edit
  * route: reviews/edit
  *
  * @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['Review'] = $request->json()->get('Review');
     if (!isset($data_json['Review']) || !isset($data_json['Review']['user_id']) || !isset($data_json['Review']['restaurant_id']) || !isset($data_json['Review']['title']) || !isset($data_json['Review']['text']) || !isset($data_json['Review']['rating'])) {
         $message = "Format {'Review': {'user_id': <int>, 'title': <string>, 'text': <string>, 'price': <int>, 'date_visited': <string>, 'rating': <double>, 'restaurant_id': <int>}}";
         return showErrorResponse($message);
     }
     // check fo valid data
     if (!Restaurants::isExists($data_json['Review']['restaurant_id'])) {
         return showErrorResponse("Restaurant data not found", HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL);
     }
     if (!in_array($data_json['Review']['rating'], array('0.5', '1.0', '1', '1.5', '2.0', '2', '2.5', '3.0', '3', '3.5', '4.0', '4', '4.5', '5.0', '5'))) {
         $message = "Rating must not be 0. Any of 0.5, 1.0, 1.5, ..., 5.0";
         return showErrorResponse($message);
     }
     // check for rating value
     // Check Ng Words
     $ng_words = NgWord::ngword_filter($data_json['Review']['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 {
         // Edit Review Data
         $review = new Reviews();
         $edit_review = $review->editReview($id, $data_json['Review']);
         $photos = Photos::getByType(CONSTANTS::REVIEW, $edit_review->id);
         $photos_array = Photos::convertPhotosToArray($photos);
         $restaurant = Restaurants::find($edit_review->restaurant_id);
         $user = Users::find($edit_review->user_id);
         $json_return[KeyParser::data] = array(KeyParser::review => ModelFormatter::reviewFormat($edit_review), 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
 }