/** * Store a newly created resource in storage. * * @return Response */ public function store() { // Is the user a student or moderator or admin? if (Auth::check() && Auth::user()->hasOnePrivelege(['Student', 'Moderator', 'Administrator'])) { // Get all post data $input = Input::all(); // Create an artwork $artwork = new Artwork(); // Set the id one higher than the amount of artworks $artwork->id = Artwork::max('id') + 1; // Set the title to the input $artwork->title = Input::get('title'); // Set the description to a trimmed version of the input (removing whitespaces) $artwork->description = trim(Input::get('description')); // Set the reserved to 0 $artwork->reserved = 0; $artwork->artist = Input::get('artist'); $artwork->technique = Input::get('technique'); $artwork->category = Input::get('category'); $artwork->genre = Input::get('genre'); $artwork->size = Input::get('size'); $artwork->price = Input::get('price'); $artwork->colour = Input::get('colour'); $artwork->material = Input::get('material'); // get the tags [test,mark] <-- format and split them by a , to an array if (!empty($input['tags'])) { $tags = explode(',', $input['tags']); } // Is the user a moderator or admin if (Auth::user()->hasOnePrivelege(['Moderator', 'Administrator'])) { // If the publish checkbox was checked set publish to true $artwork->state = Input::get('publish') == "true" ? 0 : 1; } else { // Else make it archived $artwork->state = 1; } // Create a slug from the title // replace all spaces by a - $slug = strtolower(implode('-', explode(' ', Input::get('title')))); // replace all ?, /, \\ by nothing $slug = str_replace('?', '', $slug); $slug = str_replace('/', '', $slug); $slug = str_replace('\\', '', $slug); // check if the slug already exist. if (Artwork::where('slug', $slug)->first()) { // Tell the user this title is already being used return Response::json([0 => 'Deze titel is al gebruikt bij een ander kunstwerk.'], HttpCode::Conflict); } // Set the slug to the slug we created $artwork->slug = $slug; $image = Image::canvas(800, 600); $img = Image::make(Input::get('image-data-url'))->resize(800, 600, function ($c) { $c->aspectRatio(); $c->upsize(); }); $image->insert($img, 'center'); // Retrieve the image $image = Image::make(Input::get('image-data-url')); // Get the image extension ex: png, jpg $imageExtension = substr($image->mime(), 6); $artwork->file = 'images/artworks/' . $artwork->id . '.jpeg'; // set the image file to images/artworks/artwork number.extension $artwork->file = 'images/artworks/' . $artwork->id . '.' . $imageExtension; /** * @todo add middleware to check if logged in. */ $artwork->user_id = Auth::user()->id; $image->save('images/artworks/' . $artwork->id . '.jpeg'); // save it at the files place $image->save($artwork->file); // tag the artwork with all the tags if (!empty($tags)) { foreach ($tags as $tag) { $artwork->tag($tag); } } if (Auth::check() && Auth::user()->hasOnePrivelege(['Student'])) { Artwork::mailArtworkRequest($artwork->slug); } // save the artwork data in the database $artwork->save(); // Success return Response::json([0 => 'Het kunstwerk is aangemaakt klik <a href="/artworks/' . $artwork->slug . '">hier</a> om het the bekijken', 1 => 'of klik <a href="/gallery"> hier </a> om terug te keren naar de gallerij'], 200); } else { // Unauthorized error return Response::json([0 => 'Je bent niet geautoriseerd.'], 401); } }