/** * Update the specified ITEM in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id, $seq_no = null) { $item = Item::find($id); if (!$item) { flashError('Error! Item with ID "' . $id . '" not found! (F:update)'); return \Redirect::back(); } $plan_id = $item->plan_id; $plan = Plan::find($plan_id); // searching for a song? // (but not when changing the seq.no) if ($request->has('search') && $seq_no == null) { $songs = songSearch('%' . $request->search . '%'); if (!count($songs)) { flash('No songs found for ' . $request->search); return redirect()->back()->withInput(); } if (count($songs) == 1) { $request->song_id = $songs[0]->id; } else { // as we found several songs, user must select one return view('cspot.item_select_song', ['songs' => $songs, 'plan' => $plan, 'item_id' => $id, 'seq_no' => $request->seq_no, 'moreItems' => 'N']); } } // check user rights (teachers and leaders can edit items of their own plan) if (!checkRights($plan)) { return redirect()->back(); } // handle file uplaods if ($request->hasFile('file')) { // file category is mandatory if (!$request->has('file_category_id')) { flashError('You must select a category for this file!'); return \Redirect::route('items.edit', [$plan_id, $id]); } // only accept valid categories $cats = DB::table('file_categories')->find($request->file_category_id); if (!$cats) { flashError('No valid category selected for this file!'); return \Redirect::route('items.edit', [$plan_id, $id]); } // check if it is a valid file if ($request->file('file')->isValid()) { // use the helper function $file = saveUploadedFile($request); // add the file as a relationship to the song $item->files()->save($file); // correct seq_no of attached files (if more than one) correctFileSequence($id); // notify the view about the newly added file $request->session()->flash('newFileAdded', $file->id); } else { flashError('Uploaded file could not be validated!'); return \Redirect::route('items.edit', [$plan_id, $id]); } } if ($seq_no == null) { // get all item fields from the request $fields = $request->except('_token', '_method'); // if a single song was selected above, add it to the array if (isset($songs)) { $fields['song_id'] = $songs[0]->id; } $item->update($fields); } else { // only update the sequence number $item->seq_no = $seq_no; $item->save(); $newItem = moveItem($item->id, 'static'); // back to full plan view return \Redirect::route('plans.edit', $plan_id); } // notify event listener that an item was updated event(new CspotItemUpdated($item)); // redirect back to the item return redirect()->action('Cspot\\ItemController@edit', ['plan_id' => $plan_id, 'item_id' => $id]); }
/** * Search in the Song Database * * - - RESTful API request - - * * */ public function searchSong(Request $request) { $result = false; // song was already selected if (isset($request->song_id) && intval($request->song_id) > 0) { $found = Song::find($request->song_id); if ($found) { $found->plans = $found->plansUsingThisSong(); $result[0] = $found; } } elseif (isset($request->search)) { // search $result = songSearch('%' . $request->search . '%'); // get usage statistics foreach ($result as $song) { # get list of plans $song->plans = $song->plansUsingThisSong(); } } if (count($result)) { // return to sender return response()->json(['status' => 200, 'data' => json_encode($result)]); } return response()->json(['status' => 404, 'data' => 'Not found'], 404); }