Ejemplo n.º 1
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!');
 }