예제 #1
0
 public function storeDvd()
 {
     $input = $this->request->request->all();
     // Validate the data
     if (($validator = Dvd::validate($input)) !== true) {
         session()->addErrors($validator);
         session()->flashInput($input);
         return redirect(l('admin/dvd/add'));
     }
     // Validate the image
     if (($imageValidator = Dvd::validateImage()) !== true) {
         session()->addErrors($imageValidator);
         session()->flashInput($input);
         return redirect(l('admin/dvd/add'));
     }
     // Save file with random name
     $image = $_FILES['image'];
     $name = $image['type'] == 'image/jpeg' ? uniqid('dvd_') . '.jpg' : uniqid('dvd_') . '.png';
     if (!move_uploaded_file($_FILES['image']['tmp_name'], PUBLIC_PATH . 'uploads/' . $name)) {
         session()->addErrors(new Message(['Image was not uploaded - please try again']));
         session()->flashInput($input);
         return redirect(l('admin/dvd/add'));
     }
     // Insert into database
     $dvd = new Dvd();
     $dvd->create(['title' => $input['title'], 'genre_id' => $input['genre'], 'price_band' => $input['price'], 'year' => $input['year'], 'synopsis' => $input['synopsis'], 'director' => $input['director'], 'cast' => $input['cast'], 'image' => $name]);
     session()->addSuccess('Dvd successfully added');
     return redirect(l('admin/dvds'));
 }