public function instruction($thread, $all = false)
 {
     $instruction = Instruction::with('instruction_type')->where('thread', $thread);
     if ($all) {
         $instruction = $instruction->where('active_from', '<=', date('Y-m-d H:i:s'));
     }
     $instruction = $instruction->get();
     return response()->json($instruction);
 }
 public function newInstruction(Request $request)
 {
     $user = Auth::user();
     if ($user && $user->role == "editor") {
         // Als de gebruiker ingelogd is en editor is, anders niets doen
         $filename = uniqid();
         try {
             DB::beginTransaction();
             $instruction = new Instruction();
             $instruction->author = $user->id;
             $instruction->active_from = Carbon::now();
             if ($request->input('instruction_title')) {
                 $instruction->title = $request->input('instruction_title');
             } else {
                 $instruction->title = 'No title';
             }
             $at = null;
             switch ($request->input('instruction_temp_type')) {
                 case 'text':
                     if ($request->input('instruction_text')) {
                         $instruction->contents = $request->input('instruction_text');
                     }
                     $at = ArtefactType::where('description', 'text')->first();
                     break;
                 case 'video':
                     if ($request->input('instruction_url') && $request->input('instruction_url') != null && $request->input('instruction_url') != '') {
                         // URL meegegeven voor video
                         $url = $request->input('instruction_url');
                         if (strpos($url, 'youtube') !== false || strpos($url, 'youtu.be') !== false) {
                             // Youtube video
                             $yt = BmoocController::parseYoutube($url);
                             if ($yt && $yt != '') {
                                 $instruction->url = 'http://www.youtube.com/embed/' . $yt;
                                 $at = ArtefactType::where('description', 'video_youtube')->first();
                             } else {
                                 throw new Exception('The URL you entered is not a valid link to a YouTube video');
                             }
                         } elseif (strpos($url, 'vimeo.com') !== false) {
                             // Vimeo video
                             $instruction->url = '//player.vimeo.com/video/' . substr($url, strpos($url, 'vimeo.com/') + 10);
                             $at = ArtefactType::where('description', 'video_vimeo')->first();
                         } else {
                             throw new Exception('The URL you entered is not a valid link to a YouTube or Vimeo video.');
                         }
                     } else {
                         // Kan niet voorkomen, maar voor de veiligheid wel fout opwerpen
                         //$topic->url = 'https://www.youtube.com/embed/YecyKnQUcBY'; // Dummy video
                         throw new Exception('No video URL provided for new instruction of type video');
                     }
                     break;
                 case 'image':
                     if (Input::file('instruction_upload') && Input::file('instruction_upload')->isValid()) {
                         $extension = strtolower(Input::file('instruction_upload')->getClientOriginalExtension());
                         if (in_array($extension, ['jpg', 'png', 'gif', 'jpeg'])) {
                             $destinationPath = 'uploads';
                             Input::file('instruction_upload')->move($destinationPath, $filename);
                             $instruction->url = $filename;
                             $at = ArtefactType::where('description', 'local_image')->first();
                         } else {
                             throw new Exception('Wrong file uploaded for new instruction');
                         }
                     } elseif ($request->input('instruction_url') && $request->input('instruction_url') != null && $request->input('instruction_url') != '') {
                         // URL voor de afbeelding
                         if (getimagesize($request->input('instruction_url'))) {
                             // De afbeelding is een echte afbeelding als dit niet false teruggeeft
                             $instruction->url = $request->input('instruction_url');
                             $at = ArtefactType::where('description', 'remote_image')->first();
                         } else {
                             throw new Exception('The document in the url is not an image');
                         }
                     }
                     break;
                 case 'file':
                     if (Input::file('instruction_upload') && Input::file('instruction_upload')->isValid()) {
                         $extension = strtolower(Input::file('instruction_upload')->getClientOriginalExtension());
                         if (in_array($extension, ['pdf'])) {
                             $destinationPath = 'uploads';
                             Input::file('instruction_upload')->move($destinationPath, $filename);
                             $instruction->url = $filename;
                             $at = ArtefactType::where('description', 'local_pdf')->first();
                         } else {
                             throw new Exception('Wrong file uploaded for new topic');
                         }
                     } elseif ($request->input('instruction_url') && $request->input('instruction_url') != null && $request->input('instruction_url') != '') {
                         // URL voor de afbeelding
                         if (getimagesize($request->input('instruction_url'))) {
                             // De afbeelding is een echte afbeelding als dit niet false teruggeeft
                             $instruction->url = $request->input('instruction_url');
                             $at = ArtefactType::where('description', 'remote_pdf')->first();
                         }
                     }
                     break;
             }
             // Thumbnails opslaan
             if (isset($topic->url)) {
                 // small
                 if ($request->input('thumbnail_small') && $request->input('thumbnail_small') != null && $request->input('thumbnail_small') != '') {
                     $destinationPath = 'uploads/thumbnails/small/' . $instruction->url;
                     $data = base64_decode(preg_replace('#^data:image/\\w+;base64,#i', '', $request->input('thumbnail_small')));
                     file_put_contents($destinationPath, $data);
                 }
                 // large
                 if ($request->input('thumbnail_large') && $request->input('thumbnail_large') != null && $request->input('thumbnail_large') != '') {
                     $destinationPath = 'uploads/thumbnails/large/' . $instruction->url;
                     $data = base64_decode(preg_replace('#^data:image/\\w+;base64,#i', '', $request->input('thumbnail_large')));
                     file_put_contents($destinationPath, $data);
                 }
             }
             if ($at) {
                 $at->instructions()->save($instruction);
             } else {
                 throw new Exception('Error creating new instruction (wrong type provided)');
             }
             // Einde inhoud verwerken en type bepalen
             // Set the thread of the instruction
             if ($request->input('instruction_parent')) {
                 $instruction->thread = $request->input('instruction_parent');
             }
             // Disable the previous instruction for the thread
             $previous = Instruction::getCurrent($instruction->thread);
             if ($previous) {
                 $previous->active_until = $instruction->active_from;
                 $previous->save();
             }
             $instruction->save();
             // Set the available artefact types for the thread
             if ($request->input('instruction_types')) {
                 foreach ($request->input('instruction_types') as $instructiontype) {
                     switch ($instructiontype) {
                         case 'text':
                             $it = ArtefactType::where('description', 'text')->first();
                             if ($it) {
                                 $instruction->available_types()->attach($it->id);
                             }
                             break;
                         case 'image':
                             $it = ArtefactType::where('description', 'local_image')->first();
                             if ($it) {
                                 $instruction->available_types()->attach($it->id);
                             }
                             $it = ArtefactType::where('description', 'remote_image')->first();
                             if ($it) {
                                 $instruction->available_types()->attach($it->id);
                             }
                             break;
                         case 'video':
                             $it = ArtefactType::where('description', 'video_youtube')->first();
                             if ($it) {
                                 $instruction->available_types()->attach($it->id);
                             }
                             $it = ArtefactType::where('description', 'video_vimeo')->first();
                             if ($it) {
                                 $instruction->available_types()->attach($it->id);
                             }
                             break;
                         case 'file':
                             $it = ArtefactType::where('description', 'local_pdf')->first();
                             if ($it) {
                                 $instruction->available_types()->attach($it->id);
                             }
                             $it = ArtefactType::where('description', 'remote_pdf')->first();
                             if ($it) {
                                 $instruction->available_types()->attach($it->id);
                             }
                             break;
                     }
                 }
             }
             // endif set the available artefact types for the thread
             DB::commit();
             // add handler for Ajax requests
             if ($request->isXmlHttpRequest()) {
                 return Response::json(['status' => '200', 'refresh' => 'refresh', 'url' => URL::to('/')], 200);
             } else {
                 return Redirect::back();
             }
         } catch (Exception $e) {
             DB::rollback();
             //return view('errors.topic', ['error' => $e]);
             throw $e;
         }
     }
     // End if ($user)
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $object = Model::find($id);
     if ($object) {
         $object->delete();
     }
     return redirect()->back();
 }
 public function index(Model $objects)
 {
     return view('faq.instruction', ['categories' => $this->categories, 'objects' => $objects->paginate(6), 'title' => LANG . '_title']);
 }