Exemple #1
0
 /**
  * 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]);
 }
Exemple #2
0
 /**
  * Change seq_no of a file 
  */
 public function move($item_id, $file_id, $direction)
 {
     $item = Item::with('files')->find($item_id);
     if ($item) {
         foreach ($item->files as $file) {
             if ($file->id == $file_id) {
                 $k = $file->pivot->seq_no;
                 if ($direction == 'up') {
                     $file->pivot->update(['seq_no' => $k - 1.1]);
                 }
                 if ($direction == 'down') {
                     $file->pivot->update(['seq_no' => $k + 1.1]);
                 }
             }
         }
         $item->save();
         // make sure all files atteched to this item have the correct seq no now
         correctFileSequence($item_id);
         return \Redirect::route('cspot.items.edit', [$item->plan_id, $item->id]);
     } else {
         flash('Error! Item with ID "' . $item_id . '" not found');
     }
     return \Redirect::back();
 }