Ejemplo n.º 1
0
 /**
  * Adding a comicbook series into the database
  * GET /content/store
  *
  * @return Response
  */
 public function store()
 {
     //Get string inputs
     $inputs = Input::only('book_name', 'publisher_name', 'author_name', 'artist_name');
     //Trim string inputs
     Input::merge(array_map('trim', $inputs));
     //Create session
     $this->createSession();
     //Set validation rules
     $rules = array('book_name' => 'required|alpha_num|unique:comicdb_books', 'publisher_name' => 'required|alpha_num|min:1', 'book_description' => 'max:2000', 'genres' => 'min:1', 'author_name' => 'required|alpha_num|min:1', 'artist_name' => 'required|alpha_num|min:1', 'published_date' => 'required|date_format:yy-m-d', 'cover_image' => 'required|image', 'characters' => 'min:1', 'issue_summary' => 'max:2000');
     //Laravel Validation class and make method takes the inputs in the first argument
     //then the rules on the data in the second
     $validator = Validator::make(Input::all(), $rules);
     //Validator instance use the pass method to continue
     if ($validator->passes()) {
         dd($validator->passes());
         //Instance of Comicbook model
         $comic = new Comicbooks();
         //Instance of Publisher model
         $publishers = new Publishers();
         //Setting variables
         $publisher = strtolower(Input::get('publisher_name'));
         $author = strtolower(Input::get('author_name'));
         $artist = strtolower(Input::get('artist_name'));
         $publisherExists = $publishers->where('publisher_name', $publisher)->select('id')->first();
         $authorExists = Authors::where('author_name', $author)->select('id')->first();
         $artistExists = Artists::where('artist_name', $artist)->select('id')->first();
         //Check if publisher already exist in the database
         if (isset($publisherExists->id)) {
             //if it does get the id
             $publisher_id = $publisherExists->id;
         } else {
             //else create it in the Publisher table using the instance of publisher model
             $publisher_id = $publishers->insertGetId(array('publisher_name' => $publisher));
         }
         //Check if author already exist in the database
         if (isset($authorExists)) {
             //if they do get the id
             $author_id = $authorExists->id;
         } else {
             //else create it in the Authors table using the instance of author model
             $author_id = Authors::insertGetId(array('author_name' => $author));
         }
         //Check if artist already exist in the database
         if (isset($artistExists)) {
             //if they do get the id
             $artist_id = $artistExists->id;
         } else {
             //else create it in the Artists table using the instance of artist model
             $artist_id = Artists::insertGetId(array('artist_name' => $artist));
         }
         //Add book series information to comicdb_books
         $comic->book_name = strtolower(Input::get('book_name'));
         $comic->book_description = Input::get('book_description');
         $comic->publisher_id_FK = $publisher_id;
         $comic->save();
         //Add genre and book ids in the comicdb_genrebook using Query Builder
         foreach (Input::get('genres') as $key => $genre) {
             DB::table('comicdb_genrebook')->insert(array('book_id_FK' => $comic->id, 'genre_id_FK' => $genre));
         }
         //Add cover image to local file and set location string into database
         if (Input::hasFile('cover_image')) {
             $fileName = strtolower(Input::get('book_name')) . '01_Cov_' . Str::random(10) . '.' . Input::file('cover_image')->getClientOriginalExtension();
             $cover_image = Input::file('cover_image')->move('public/img/comic_covers/', $fileName);
         }
         //Add issue character information into the comicdb_character table and keys into the comicdb_characterbook table using Query Builder
         foreach (Input::get('characters') as $key => $character) {
             $character_id = Characters::insertGetId(array('character_name' => $character));
             DB::table('comicdb_characterbook')->insert(array('book_id_FK' => $comic->id, 'character_id_FK' => $character_id));
         }
         //Add issues information to comicdb_issues
         Comicissues::insert(array('book_id' => $comic->id, 'issue_id' => 1, 'artist_id_FK' => $artist_id, 'author_id_FK' => $author_id, 'summary' => Input::get('issue_summary'), 'published_date' => Input::get('published_date'), 'cover_image' => 'img/comic_covers/' . $fileName, 'created_at' => date('Y-m-d H:i:s', time())));
         $this->destorySession();
         return Redirect::to('browse')->with('postMsg', 'Thanks for submiting!');
     } else {
         return Redirect::to('content/series')->with('postMsg', 'Whoops! Looks like you got some errors.')->withErrors($validator);
     }
 }