public static function update() { self::check_logged_in(); $curruser = self::get_user_logged_in(); if (!$curruser->admin) { Redirect::to('/user/' . $curruser->id, array('warning' => 'Pääsy kielletty ilman ylläpito-oikeutta!')); } $p = $_POST; $poll = new Poll(array('id' => $p['id'], 'name' => $p['name'], 'description' => $p['description'], 'start_time' => $p['start_time'], 'end_time' => $p['end_time'])); // Search the input array for Option model attributes and build an array // out of them: $poll_options = array(); foreach ($p as $key => $value) { $matches = array(); if (preg_match("/^option_name_([0-9]+)\$/", $key, $matches)) { $poll_options[$matches[1]]['id'] = $p['option_' . $matches[1]]; $poll_options[$matches[1]]['name'] = $p[$matches[0]]; $poll_options[$matches[1]]['description'] = $p['option_description_' . $matches[1]]; } else { if (preg_match("/^option_name_new_([0-9]+)\$/", $key, $matches)) { $poll_options[$matches[1]]['name'] = $p[$matches[0]]; $poll_options[$matches[1]]['description'] = $p['option_description_new_' . $matches[1]]; } } } $errors = $poll->errors(); $polloptions = array(); if (count($errors) == 0) { $poll->update(); foreach ($poll_options as $option) { $polloption = new PollOption(array('polls_id' => $poll->id, 'name' => $option['name'], 'description' => $option['description'])); $errors = $polloption->errors(); if (count($errors) == 0) { if (isset($option['id'])) { $polloption->id = $option['id']; $polloption->update(); } else { $polloption->save(); } } else { $polloptions[] = $polloption; } } if (empty($polloptions)) { Redirect::to('/poll/' . $poll->id, array('message' => 'Tallennettiin äänestys ' . $poll->name . '.')); } else { // The poll was saved successfully but options weren't. Edit the poll. $errors[] = 'Tallennettiin äänestys ' . $poll->name . ', mutta äänestyksen vaihtoehtoja ei saatu tallennettua. Tarkista virheet'; View::make('poll/edit.html', array('errors' => $errors, 'poll' => $poll, 'polloptions' => $polloptions)); } } else { // The actual poll wasn't saved. foreach ($poll_options as $option) { $polloption = new PollOption(array('polls_id' => $poll->id, 'name' => $option['name'], 'description' => $option['description'])); if (isset($option['id'])) { $polloption->id = $option['id']; } $optionerrors = $polloption->errors(); if (!empty($optionerrors)) { $errors = array_merge($errors, $optionerrors); } $polloptions[] = $polloption; } View::make('poll/edit.html', array('errors' => $errors, 'poll' => $poll, 'polloptions' => $polloptions)); } }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function postUpdate($id) { if (Session::get('user_level') < Config::get('cms.editPolls')) { return Redirect::to(_l(URL::action('AdminHomeController@getIndex')))->with('message', Lang::get('admin.notPermitted'))->with('notif', 'warning'); } $rules = array('question' => 'Required', 'options' => 'Required'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return Redirect::to(_l(URL::action('PollController@getCreate')))->withErrors($validator)->withInput(); } else { try { $poll = Poll::findOrFail($id); if (Input::get('createdAt')) { $poll->created_at = date("Y-m-d H:i:s", strtotime(Input::get('createdAt'))); } else { $poll->created_at = date("Y-m-d H:i:s", strtotime('now')); } $poll->title = Input::get('question'); if (Input::get('endDate')) { $poll->end_date = date("Y-m-d H:i:s", strtotime(Input::get('endDate'))); } else { $poll->end_date = date("Y-m-d H:i:s", strtotime('1.1.1970')); } $poll->published = Input::get('published'); $poll->save(); $votes = Input::get('voteCount'); $options = $poll->pollOptions; $poll->pollOptions()->delete(); foreach (Input::get('options') as $key => $option) { $done = false; foreach ($options as $key2 => $option2) { if ($key == $option2->id) { $poption = new PollOption(); $poption->title = $option; $poption->poll_id = $poll->id; $poption->id = $option2->id; if (isset($votes[$key]) && $votes[$key] != 0) { $poption->vote_count = $votes[$key]; } $poption->save(); $done = true; continue; } } if (!$done) { $pollOption = new PollOption(); $pollOption->poll_id = $poll->id; $pollOption->title = $option; if (isset($votes[$key]) && $votes[$key] != 0) { $pollOption->vote_count = $votes[$key]; } else { $pollOption->vote_count = 0; } $pollOption->save(); } } //return; return Redirect::to(_l(URL::action('PollController@getEdit') . "/" . $poll->id))->with('message', Lang::get('admin.pollSaved'))->with('notif', 'success'); } catch (Exception $e) { return Redirect::to(_l(URL::action('PollController@getIndex')))->with('message', Lang::get('admin.noSuchPoll'))->with('notif', 'danger'); } } }