/**
  * Store a newly created resource in storage.
  *
  * @param Request $request
  * @return array
  */
 public function store(Request $request)
 {
     $this->validate($request, ['category_id' => 'required', 'tags_id' => 'required', 'title' => 'required|max:250', 'alias' => 'required|max:250', 'description' => 'required', 'short_description' => 'required|max:1000', 'meta_description' => 'required|max:1000']);
     $article = new Articles();
     $article->fill($request->all());
     $article->user_id = Auth::user()->id;
     $article->save();
     //        $article = Auth::user()->articles()->create($request->all());
     /**
      * Check tags_id input and create tags if not number in input
      */
     $tag_ids = [];
     foreach ($request->input('tags_id') as $tag_input) {
         if (ctype_digit($tag_input)) {
             //it`s number, save to ids array
             array_push($tag_ids, $tag_input);
         } else {
             //create new tag with this input name if not exist
             $tag = Tags::where('name', $tag_input)->first();
             if (!$tag) {
                 $tag = Tags::create(['name' => $tag_input]);
             }
             array_push($tag_ids, $tag->id);
         }
     }
     $article->tags()->attach($tag_ids);
     \Flash::success('Article created');
     return redirect()->action('ArticlesController@index');
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param Request $request
  * @return Response
  */
 public function store(Request $request)
 {
     $this->validate($request, ['name' => 'required']);
     Tags::create($request->all());
     \Flash::success('Tag created');
     return redirect('/tags');
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Faker::create();
     foreach (range(1, 10) as $index) {
         Tags::create(['name' => $faker->word]);
     }
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $loop = 35;
     $faker = $this->getFaker();
     for ($i = 0; $i < $loop; $i++) {
         $name = $faker->word;
         $user = $this->getRandomUser();
         $data = ['tag' => $name, 'created_by' => $user];
         \App\Tags::create($data);
         print 'Done ' . $loop . ' entries was created';
     }
 }
 public function newTopic(Request $request)
 {
     $user = Auth::user();
     if ($user && $user->role == "editor") {
         // Als de gebruiker ingelogd is en editor is, anders niets doen
         try {
             $filename = uniqid();
             DB::beginTransaction();
             $topic = new Artefact();
             $topic->author = $user->id;
             $thread = DB::table('artefacts')->max('thread') + 1;
             $topic->thread = $thread;
             if ($request->input('topic_title')) {
                 $topic->title = $request->input('topic_title');
             } else {
                 $topic->title = 'No title';
             }
             if ($request->input('topic_copyright')) {
                 $topic->copyright = $request->input('topic_copyright');
             }
             // De eigenlijke inhoud verwerken en het type bepalen en juist zetten
             $at = null;
             switch ($request->input('topic_temp_type')) {
                 case 'text':
                     if ($request->input('topic_text')) {
                         $topic->contents = $request->input('topic_text');
                     }
                     $at = ArtefactType::where('description', 'text')->first();
                     break;
                 case 'video':
                     if ($request->input('topic_url') && $request->input('topic_url') != null && $request->input('topic_url') != '') {
                         // URL meegegeven voor video
                         $url = $request->input('topic_url');
                         if (strpos($url, 'youtube') !== false || strpos($url, 'youtu.be') !== false) {
                             // Youtube video
                             $yt = BmoocController::parseYoutube($url);
                             if ($yt && $yt != '') {
                                 $topic->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
                             $topic->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 topic of type video');
                     }
                     break;
                 case 'image':
                     if (Input::file('topic_upload') && Input::file('topic_upload')->isValid()) {
                         $extension = strtolower(Input::file('topic_upload')->getClientOriginalExtension());
                         if (in_array($extension, ['jpg', 'png', 'gif', 'jpeg'])) {
                             $destinationPath = 'uploads';
                             Input::file('topic_upload')->move($destinationPath, $filename);
                             $topic->url = $filename;
                             $at = ArtefactType::where('description', 'local_image')->first();
                         } else {
                             throw new Exception('Image should be a JPEG, PNG or GIF.');
                         }
                     } elseif ($request->input('topic_url') && $request->input('topic_url') != null && $request->input('topic_url') != '') {
                         // URL voor de afbeelding
                         if (getimagesize($request->input('topic_url'))) {
                             // De afbeelding is een echte afbeelding als dit niet false teruggeeft
                             $topic->url = $request->input('topic_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('topic_upload') && Input::file('topic_upload')->isValid()) {
                         $extension = strtolower(Input::file('topic_upload')->getClientOriginalExtension());
                         if (in_array($extension, ['pdf'])) {
                             $destinationPath = 'uploads';
                             Input::file('topic_upload')->move($destinationPath, $filename);
                             $topic->url = $filename;
                             $at = ArtefactType::where('description', 'local_pdf')->first();
                         } else {
                             throw new Exception('File should be a PDF.');
                         }
                     } elseif ($request->input('topic_url') && $request->input('topic_url') != null && $request->input('topic_url') != '') {
                         // URL voor de afbeelding
                         if (getimagesize($request->input('topic_url'))) {
                             // De afbeelding is een echte afbeelding als dit niet false teruggeeft
                             $topic->url = $request->input('topic_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/' . $topic->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/' . $topic->url;
                     $data = base64_decode(preg_replace('#^data:image/\\w+;base64,#i', '', $request->input('thumbnail_large')));
                     file_put_contents($destinationPath, $data);
                 }
             }
             if ($at) {
                 $at->artefacts()->save($topic);
             } else {
                 throw new Exception('Selected file is not a valid image or PDF.');
             }
             // Einde inhoud verwerken en type bepalen
             // Bijlage verwerken
             if (Input::file('topic_attachment') && Input::file('topic_attachment')->isValid()) {
                 $extension = strtolower(Input::file('topic_attachment')->getClientOriginalExtension());
                 if (in_array($extension, ['jpg', 'png', 'gif', 'jpeg', 'pdf'])) {
                     $destinationPath = 'uploads/attachments';
                     $filename = base64_encode(Input::file('topic_attachment')->getClientOriginalName() . time()) . '.' . $extension;
                     Input::file('topic_attachment')->move($destinationPath, $filename);
                     //$topic->url = $filename;
                     $topic->attachment = $filename;
                 } else {
                     throw new Exception('Attachment should be a JPG, PNG, GIF or PDF');
                 }
             }
             // Topic opslaan
             $topic->save();
             // Tags verwerken
             if ($request->input('topic_new_tag')) {
                 foreach ($request->input('topic_new_tag') as $newtag) {
                     if ($newtag != '') {
                         $existingtag = Tags::where('tag', strtolower($newtag))->first();
                         if ($existingtag) {
                             $existingtag->artefacts()->save($topic);
                         } else {
                             $newTag = Tags::create(['tag' => strtolower($newtag), 'times_used' => 1]);
                             $newTag->artefacts()->save($topic);
                         }
                     } else {
                         throw new Exception('Tags must not be empty!');
                     }
                 }
             }
             DB::commit();
             // add handler for Ajax requests
             if ($request->isXmlHttpRequest()) {
                 return Response::json(['status' => '200', 'url' => URL::to('/')], 200);
             } else {
                 return Redirect::back();
             }
         } catch (Exception $e) {
             DB::rollback();
             //return view('errors.topic', ['error' => $e]);
             throw $e;
         }
     }
     // End if ($user)
 }
 /**
  * Check tags_id input and create tags if not number in input
  *
  * @param array $tags
  * @return array tags_ids
  */
 public function checkTags(array $tags)
 {
     $tag_ids = [];
     foreach ($tags as $tag_input) {
         if (ctype_digit($tag_input)) {
             //it`s number, save to ids array
             array_push($tag_ids, $tag_input);
         } else {
             //create new tag with this input name if not exist
             $tag = Tags::where('name', $tag_input)->first();
             if (!$tag) {
                 $tag = Tags::create(['name' => $tag_input]);
             }
             array_push($tag_ids, $tag->id);
         }
     }
     return $tag_ids;
 }
 private function savePostsTags($data, $post_id)
 {
     $tags = explode(',', $data);
     foreach ($tags as $tag) {
         $normalized = $this->strtoupperForCN($tag);
         $tag_ref = Tags::where('normalized', $normalized)->first();
         if (is_null($tag_ref)) {
             $tag_ref = Tags::create(['name' => $tag, 'normalized' => $normalized]);
         }
         $posts_tags = PostsTags::where('post_id', $post_id)->where('tag_id', $tag_ref->id)->first();
         if (is_null($posts_tags)) {
             $posts_tags = PostsTags::create(['post_id' => $post_id, 'tag_id' => $tag_ref->id]);
         }
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \App\Http\Requests\TagsRequest $request
  * @return \Illuminate\Http\Response
  */
 public function store(TagsRequest $request)
 {
     Tags::create($request->all());
     return redirect('tags');
 }