/** * Store a newly created resource in storage. * * @return Response */ public function store() { # Config $config = Config::get('poster'); $event_types = $config['event_types']; $days = $config['days']; # Form Validation $rules = array('code' => 'required|exists:boards,code', 'program' => 'required|between:3,32', 'type' => 'required|in:' . implode(',', $event_types), 'from' => 'required|date_format:Y-m-d', 'end' => 'required|date_format:Y-m-d'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return Response::json(['success' => false, 'messages' => $validator->errors()]); } if (Input::get('from') > Input::get('end')) { $msg = 'Begin date is later than end date.'; return Response::json(['success' => false, 'messages' => $msg]); } # Poster Status Validation $board = Board::where('code', Input::get('code'))->first(); $from = Input::get('from'); $end = Input::get('end'); $days_diff = round((strtotime($end) - strtotime($from)) / 60 / 60 / 24); if ($board->getUsingStatus($from, $end)) { $msg = 'Board has been applied.'; return Response::json(['success' => false, 'messages' => $msg]); } # Days Validation if ($board->type == 'large' and $days_diff > $days['large_poster']) { $msg = "You can't applied over {$days['large_poster']} days!"; return Response::json(['success' => false, 'messages' => $msg]); } if ($days_diff > $days[Input::get('type')]) { $msg = "You can't applied over {$days[Input::get('type')]} days!"; return Response::json(['success' => false, 'messages' => $msg]); } # Times Validation $boards = Board::where('type', $board->type)->lists('id'); $records = ApplyRecord::date($from, $end)->where('user_id', Auth::id()); $amount = $records->whereIn('board_id', $boards)->count(); $quota = Config::get('poster.meanwhile_quota')[$board->type]; if ($amount >= $quota) { return Response::json(['success' => false, 'messages' => 'You can\'t apply more than ' . $quota . ' times in same time.']); } # Continuously Validation if ($board->type != 'stairs') { $cold_down = Config::get('poster.cold_down'); $from_cd = date('Y-m-d', strtotime($from . " - {$cold_down} days")); $end_cd = date('Y-m-d', strtotime($end . " + {$cold_down} days")); $record_cd = ApplyRecord::where('board_id', $board->id)->date($from_cd, $end_cd)->where('user_id', Auth::id())->count(); if ($cold_down != 0 and 0 < $record_cd) { $msg = 'You can\'t apply same board continuously.'; return Response::json(['success' => false, 'messages' => $msg]); } } # Create ApplyRecord::create(['board_id' => $board->id, 'user_id' => Auth::id(), 'event_name' => Input::get('program'), 'event_type' => Input::get('type'), 'post_from' => $from, 'post_end' => $end]); return Response::json(['success' => true]); }