/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     // create the validator
     $validator = Validator::make(Input::all(), Dog::$rules);
     // attempt validation
     if ($validator->fails()) {
         Log::info('Validator failed', Input::all());
         Session::flash('errorMessage', 'Something went wrong please refer to the errors below:');
         // validation failed, redirect to the dog create page with validation errors and old inputs
         return Redirect::back()->withInput()->withErrors($validator);
     } else {
         $file = Input::file('img_url');
         $destinationPath = public_path() . '/img/dogs';
         $filename = $file->getClientOriginalName();
         Input::file('img_url')->move($destinationPath, $filename);
         if (Input::file('img_url2') != '') {
             $file2 = Input::file('img_url2');
             $destinationPath = public_path() . '/img/dogs';
             $filename2 = $file2->getClientOriginalName();
             Input::file('img_url2')->move($destinationPath, $filename2);
             $dog = new Dog();
             $dog->name = Input::get('name');
             $dog->comment = Input::get('comment');
             $dog->gender = Input::get('gender');
             $dog->banner = Input::get('banner');
             $dog->retired = Input::get('retired');
             $dog->puppy = Input::get('puppy');
             $dog->past = Input::get('past');
             $dog->sold = Input::get('sold');
             $dog->mom = Input::get('mom');
             $dog->dad = Input::get('dad');
             $dog->fun = Input::get('fun');
             $dog->img_url = $filename;
             $dog->img_url2 = $filename2;
             $dog->user_id = Auth::id();
             $dog->save();
             Session::flash('successMessage', 'Your dog was saved successfully');
             return Redirect::action('DogsController@create');
         }
         $dog = new Dog();
         $dog->name = Input::get('name');
         $dog->comment = Input::get('comment');
         $dog->gender = Input::get('gender');
         $dog->banner = Input::get('banner');
         $dog->retired = Input::get('retired');
         $dog->puppy = Input::get('puppy');
         $dog->past = Input::get('past');
         $dog->sold = Input::get('sold');
         $dog->mom = Input::get('mom');
         $dog->dad = Input::get('dad');
         $dog->fun = Input::get('fun');
         $dog->img_url = $filename;
         $dog->user_id = Auth::id();
         $dog->save();
         Session::flash('successMessage', 'Your dog was saved successfully');
         return Redirect::action('DogsController@create');
     }
 }
 public function create()
 {
     if (Request::isMethod('post')) {
         $name = Input::get('name');
         $age = Input::get('age');
         $breed = Input::get('type');
         $likes = Input::get('notes');
         $token = Input::get('token');
         $owner_id = Input::get('id');
         $picture = Input::file('picture');
         $validator = Validator::make(array('name' => $name, 'age' => $age, 'breed' => $breed, 'token' => $token, 'owner_id' => $owner_id, 'picture' => $picture), array('name' => 'required', 'age' => 'required|integer', 'breed' => 'required', 'token' => 'required', 'owner_id' => 'required|integer', 'picture' => 'required|mimes:jpeg,bmp,png'));
         if ($validator->fails()) {
             $error_messages = $validator->messages()->all();
             $response_array = array('success' => false, 'error' => 'Invalid Input', 'error_code' => 401, 'error_messages' => $error_messages);
             $response_code = 200;
         } else {
             $is_admin = $this->isAdmin($token);
             if ($owner_data = $this->getOwnerData($owner_id, $token, $is_admin)) {
                 // check for token validity
                 if (is_token_active($owner_data->token_expiry) || $is_admin) {
                     // Do necessary operations
                     // check if there's already a dog
                     $dog = Dog::where('owner_id', $owner_id)->first();
                     if ($dog === null) {
                         $dog = new Dog();
                     }
                     $dog->name = $name;
                     $dog->age = $age;
                     $dog->breed = $breed;
                     $dog->likes = $likes;
                     $dog->owner_id = $owner_data->id;
                     // Upload File
                     $file_name = time();
                     $file_name .= rand();
                     $ext = Input::file('picture')->getClientOriginalExtension();
                     Input::file('picture')->move(public_path() . "/uploads", $file_name . "." . $ext);
                     $local_url = $file_name . "." . $ext;
                     // Upload to S3
                     if (Config::get('app.s3_bucket') != "") {
                         $s3 = App::make('aws')->get('s3');
                         $pic = $s3->putObject(array('Bucket' => Config::get('app.s3_bucket'), 'Key' => $file_name, 'SourceFile' => public_path() . "/uploads/" . $local_url));
                         $s3->putObjectAcl(array('Bucket' => Config::get('app.s3_bucket'), 'Key' => $file_name, 'ACL' => 'public-read'));
                         $s3_url = $s3->getObjectUrl(Config::get('app.s3_bucket'), $file_name);
                     } else {
                         $s3_url = asset_url() . '/uploads/' . $local_url;
                     }
                     $dog->image_url = $s3_url;
                     $dog->save();
                     $owner = Owner::find($owner_data->id);
                     $owner->dog_id = $dog->id;
                     $owner->save();
                     $response_array = array('success' => true);
                     $response_code = 200;
                 } else {
                     $response_array = array('success' => false, 'error' => 'Token Expired', 'error_code' => 405);
                     $response_code = 200;
                 }
             } else {
                 if ($is_admin) {
                     $response_array = array('success' => false, 'error' => 'Owner ID not Found', 'error_code' => 410);
                 } else {
                     $response_array = array('success' => false, 'error' => 'Not a valid token', 'error_code' => 406);
                 }
                 $response_code = 200;
             }
         }
     } else {
         //handles get request
         $token = Input::get('token');
         $owner_id = Input::get('id');
         $validator = Validator::make(array('token' => $token, 'owner_id' => $owner_id), array('token' => 'required', 'owner_id' => 'required|integer'));
         if ($validator->fails()) {
             $error_messages = $validator->messages()->all();
             $response_array = array('success' => false, 'error' => 'Invalid Input', 'error_code' => 401, 'error_messages' => $error_messages);
             $response_code = 200;
         } else {
             $is_admin = $this->isAdmin($token);
             if ($owner_data = $this->getOwnerData($owner_id, $token, $is_admin)) {
                 // check for token validity
                 if (is_token_active($owner_data->token_expiry) || $is_admin) {
                     $dog = Dog::find($owner_data->dog_id);
                     if ($dog) {
                         $response_array = array('success' => true, 'thing_id' => $dog->id, 'age' => $dog->age, 'type' => $dog->breed, 'notes' => $dog->likes, 'image_url' => $dog->image_url);
                         $response_code = 200;
                     } else {
                         $response_array = array('success' => false, 'error' => 'No Dogs Found', 'error_code' => 445);
                         $response_code = 200;
                     }
                 } else {
                     $response_array = array('success' => false, 'error' => 'Token Expired', 'error_code' => 405);
                     $response_code = 200;
                 }
             } else {
                 if ($is_admin) {
                     $response_array = array('success' => false, 'error' => 'Owner ID not Found', 'error_code' => 410);
                 } else {
                     $response_array = array('success' => false, 'error' => 'Not a valid token', 'error_code' => 406);
                 }
                 $response_code = 200;
             }
         }
     }
     $response = Response::json($response_array, $response_code);
     return $response;
 }
 protected function createEnvDog()
 {
     $dog = new Dog();
     $dog->name = 'Crush';
     $dog->comment = 'this is crush';
     $dog->gender = 'Male';
     $dog->img_url = 'crushBanner.jpg';
     $dog->user_id = '1';
     $dog->banner = 1;
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Ariat';
     $dog->comment = 'this is Ariat';
     $dog->gender = 'Female';
     $dog->img_url = 'ariatBanner.jpg';
     $dog->user_id = '1';
     $dog->banner = 1;
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Crush';
     $dog->comment = 'this is crush';
     $dog->gender = 'Male';
     $dog->img_url = '1.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 1;
     $dog->past = 1;
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Hay';
     $dog->comment = 'this is hay';
     $dog->gender = 'Male';
     $dog->img_url = 'hay1.jpg';
     $dog->img_url2 = 'hay2.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 0;
     $dog->past = 0;
     $dog->dad = 'Crush';
     $dog->mom = 'Ariat';
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Maple';
     $dog->comment = 'this is maple';
     $dog->gender = 'Female';
     $dog->img_url = 'maple1a.jpg';
     $dog->img_url2 = 'maple1b.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 0;
     $dog->past = 0;
     $dog->dad = 'Crush';
     $dog->mom = 'Ariat';
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Pumpkin';
     $dog->comment = 'this is pumpkin';
     $dog->gender = 'Female';
     $dog->img_url = 'pumpkin1a.jpg';
     $dog->img_url2 = 'pumpkin1b.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 1;
     $dog->past = 0;
     $dog->dad = 'Crush';
     $dog->mom = 'Ariat';
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Pecan';
     $dog->comment = 'this is Pecan';
     $dog->gender = 'Male';
     $dog->img_url = 'pecan1a.jpg';
     $dog->img_url2 = 'pecan1b.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 1;
     $dog->past = 0;
     $dog->dad = 'Crush';
     $dog->mom = 'Ariat';
     $dog->save();
     $dog = new Dog();
     $dog->name = 'dog';
     $dog->comment = 'this is dog';
     $dog->gender = 'Female';
     $dog->img_url = 'dogs.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 1;
     $dog->past = 1;
     $dog->fun = 1;
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Nehi';
     $dog->comment = 'this is nehi';
     $dog->gender = 'Female';
     $dog->img_url = '2.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 1;
     $dog->past = 1;
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Nehi';
     $dog->comment = 'this is nehi';
     $dog->gender = 'Female';
     $dog->img_url = '3.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 1;
     $dog->past = 1;
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Shasta';
     $dog->comment = 'this is Shasta';
     $dog->gender = 'Female';
     $dog->img_url = '4.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 1;
     $dog->past = 1;
     $dog->save();
     $dog = new Dog();
     $dog->name = 'Fanta';
     $dog->comment = 'this is Fanta';
     $dog->gender = 'Female';
     $dog->img_url = '5.jpg';
     $dog->user_id = '1';
     $dog->banner = 0;
     $dog->puppy = 1;
     $dog->sold = 1;
     $dog->past = 1;
     $dog->fun = 1;
     $dog->save();
     $dog2 = new Dog();
     $dog2->name = 'Jefe';
     $dog2->comment = 'this is Jefe';
     $dog2->gender = 'Male';
     $dog2->img_url = 'jefeBanner.jpg';
     $dog2->user_id = '1';
     $dog2->banner = 1;
     $dog2->save();
     $dog3 = new Dog();
     $dog3->name = 'Trooper';
     $dog3->comment = 'this is Trooper';
     $dog3->gender = 'Male';
     $dog3->img_url = 'trooperBanner.jpg';
     $dog3->user_id = '1';
     $dog3->banner = 1;
     $dog3->retired = 1;
     $dog3->save();
     $dog1 = new Dog();
     $dog1->name = 'Crush';
     $dog1->comment = 'this is crush';
     $dog1->gender = 'Male';
     $dog1->img_url = 'crush7.jpg';
     $dog1->user_id = '1';
     $dog1->banner = 0;
     $dog1->save();
     $dog1 = new Dog();
     $dog1->name = 'Crush';
     $dog1->comment = 'this is crush';
     $dog1->gender = 'Male';
     $dog1->img_url = 'crushHead.jpg';
     $dog1->user_id = '1';
     $dog1->banner = 0;
     $dog1->save();
     $dog1 = new Dog();
     $dog1->name = 'Crush';
     $dog1->comment = 'this is crush';
     $dog1->gender = 'Male';
     $dog1->img_url = 'nc.jpg';
     $dog1->user_id = '1';
     $dog1->banner = 0;
     $dog1->save();
     $dog4 = new Dog();
     $dog4->name = 'Nehi';
     $dog4->comment = 'this is Nehi';
     $dog4->gender = 'Female';
     $dog4->img_url = 'nehiBanner.jpg';
     $dog4->user_id = '1';
     $dog4->banner = 1;
     $dog4->retired = 1;
     $dog4->save();
     $dog5 = new Dog();
     $dog5->name = 'Fanta';
     $dog5->comment = 'this is Fanta';
     $dog5->gender = 'Female';
     $dog5->img_url = 'fantaBanner.jpg';
     $dog5->user_id = '1';
     $dog5->banner = 1;
     $dog5->save();
     $dog6 = new Dog();
     $dog6->name = 'Nike';
     $dog6->comment = 'this is Nike';
     $dog6->gender = 'Female';
     $dog6->img_url = 'nikeBanner.jpg';
     $dog6->user_id = '1';
     $dog6->banner = 1;
     $dog6->save();
     $dog6 = new Dog();
     $dog6->name = 'Cinch';
     $dog6->comment = 'this is Cinch';
     $dog6->gender = 'Female';
     $dog6->img_url = 'cinchBanner.jpg';
     $dog6->user_id = '1';
     $dog6->banner = 1;
     $dog6->save();
 }