Exemplo 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);
     }
 }
Exemplo n.º 2
0
 /**
  * Update an issue in the database
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     //Trim inputs
     Input::merge(array_map('trim', Input::all()));
     //Set rules
     $rules = array('author_name' => 'required|min:5', 'artist_name' => 'required|min:5', 'published_date' => 'required|date_format:yy-m-d', 'issue_summary' => 'max:2000');
     //Create session
     $this->createSession();
     //Set validator
     $validator = Validator::make(Input::all(), $rules);
     //Set variables
     $data['issue_id'] = $id;
     $comic_title = Comicbooks::find(Input::get('id'));
     $comic_issues = Comicissues::issues($comic_title->book_name, $id)->select('book_id', 'issue_id')->first();
     //If comicbook issue exists
     if (!is_null($comic_issues)) {
         //Validation check
         if ($validator->passes()) {
             //Instance of Comicissue model
             $comic_issues = new Comicissues();
             //Set variables
             $author = Str::lower(Input::get('author_name'));
             $artist = Str::lower(Input::get('artist_name'));
             $authorExists = Authors::where('author_name', $author)->select('id')->first();
             $artistExists = Artists::where('artist_name', $artist)->select('id')->first();
             //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));
             }
             //Set an array of update variables
             $update_array = array('author_id_FK' => $author_id, 'artist_id_FK' => $artist_id, 'summary' => Input::get('issue_summary'), 'published_date' => Input::get('published_date'), 'updated_at' => date('Y-m-d H:i:s', time()));
             //Add cover image to local file and set location string into database
             if (Input::hasFile('cover_image')) {
                 $fileName = $comic_title->book_name . $id . '_Cov_' . Str::random(10) . '.' . Input::file('cover_image')->getClientOriginalExtension();
                 $cover_image = Input::file('cover_image')->move('public/img/comic_covers/', $fileName);
                 $update_array['cover_image'] = 'img/comic_covers/' . $fileName;
             }
             //Add issue information to comicdb_books
             Comicissues::where('book_id', Input::get('id'))->where('issue_id', $id)->update($update_array);
             //Destroy session data
             $this->destorySession();
             return Redirect::to('browse/series/' . $comic_title->book_name)->with('postMsg', 'Thanks for submiting!');
         } else {
             return Redirect::to(URL::previous())->with('postMsg', 'Whoops! Looks like you got some errors.')->withErrors($validator)->withInput();
         }
     }
     $this->destorySession();
     return Redirect::to(URL::previous())->with('postMsg', 'Looks like that issue already exists!');
 }