/** * Returns all notification data per user_id and status using lengthAwarePagination * * @param int $status * @param string $order * @param bool $user_id_to * @param int current_page * @param boolean $count_only * @return LengthAwarePaginator */ public static function getNotificationByUserToCustomPaginate($status = CONSTANTS::NOTIFICATION_STATUS_DELETED, $order = CONSTANTS::ORDER_DESC, $user_id_to = false, $current_page = CONSTANTS::FIRST_PAGE, $count_only = false) { $notifications = self::where('status', '<=', $status); if ($user_id_to) { $notifications->where('user_id_to', $user_id_to); } $notifications = $notifications->orderBy('date_created', $order)->get(); $data = array(); $duplicate_activities = array(); $notif_count = 0; foreach ($notifications as $notification) { $type_id = $notification->type_id; $type = $notification->type; $restaurant_id = $notification->restaurant_id; if ($restaurant_id != 0 && !Restaurants::isExists($restaurant_id)) { continue; } $from_user = Users::find($notification->user_id_from); if (!$count_only) { if ($notification->status == CONSTANTS::NOTIFICATION_STATUS_NEW) { $notification->updateStatus(CONSTANTS::NOTIFICATION_STATUS_UNREAD); } } if ($from_user && isset($duplicate_activities[$type][$type_id])) { $date_diff = strtotime($duplicate_activities[$type][$type_id][KeyParser::date_created]) - strtotime($notification->date_created); if ($date_diff < CONSTANTS::DAY_SECOND_VALUE && $date_diff >= 0) { $notif_index = $duplicate_activities[$type][$type_id][KeyParser::index]; $user_fullname = $from_user->getUserFullName(); if (!array_key_exists(KeyParser::usernames_from, $data[$notif_index][KeyParser::notification]) || !in_array($user_fullname, $data[$notif_index][KeyParser::notification][KeyParser::usernames_from])) { $data[$notif_index][KeyParser::notification][KeyParser::usernames_from][] = $user_fullname; $data[$notif_index][KeyParser::notification][KeyParser::users_from][] = $from_user->toArray(); } continue; } } $data[$notif_count][KeyParser::notification] = ModelFormatter::notificationFormat($notification); $to_user = Users::find($notification->user_id_to); if ($to_user) { $data[$notif_count][KeyParser::notification] += array(KeyParser::facebook_id_to => $to_user->facebook_id); } if ($from_user) { $data[$notif_count][KeyParser::notification] += array(KeyParser::facebook_id_from => $from_user->facebook_id, KeyParser::usernames_from => array($from_user->getUserFullName()), KeyParser::users_from => array($from_user->toArray())); } $duplicate_activities[$type][$type_id] = array(KeyParser::date_created => $notification->date_created, KeyParser::index => $notif_count); $restaurant = Restaurants::find($notification->restaurant_id); if ($restaurant) { $data[$notif_count][KeyParser::notification] += array(KeyParser::restaurant_name => $restaurant->name); } $notif_count++; } if ($count_only) { return count($data); } $max_results = CONSTANTS::NOTIFICATIONS_VIEW_PAGINATION_LIMIT; $offset = $current_page * $max_results - $max_results; $notification_data = array_slice($data, $offset, $max_results); $paginated_data = new LengthAwarePaginator($notification_data, count($data), $max_results); return $paginated_data; }
public function readAction(Request $request) { $data = $request->json()->get('Notification'); if (!$data) { return showErrorResponse('Incorrect request parameters', HTTP_UNPROCESSABLE_ENTITY); } $notification = Notification::find($data[CONSTANTS::KEY_ID]); if ($notification) { $notification = ModelFormatter::notificationFormat($notification->updateStatus(CONSTANTS::NOTIFICATION_STATUS_READ)); } $json_return[KeyParser::data][KeyParser::notification] = $notification; return response()->json($json_return); }