/** * Store a newly created article in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $validator = Validator::make($request->all(), ['featured' => 'mimes:jpg,jpeg,gif,png|max:1000']); if ($validator->fails()) { return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => "Featured must image and less than 1MB", 'timestamp' => Carbon::now()], 400); } $exist = Article::whereSlug($request->input('slug'))->count(); if ($exist) { return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => 'Unique slug has taken', 'timestamp' => Carbon::now()], 400); } $articleController = $this; $result = DB::transaction(function () use($request, $articleController) { try { /* * -------------------------------------------------------------------------- * Populate tags * -------------------------------------------------------------------------- * Sync tags and article, extract tags from request, break down between new * tags and tags that available in database, collect available tags first * then insert new tags and merge new inserted tag id with available tags id * finally store them all into article_tags table. */ $availableTags = Tag::whereIn('tag', explode(',', $request->get('tags'))); $availableTagsId = $availableTags->pluck('id')->toArray(); $availableTagsName = $availableTags->pluck('tag')->toArray(); $newTags = array_diff(explode(',', $request->get('tags')), $availableTagsName); foreach ($newTags as $tagLabel) { $newTag = new Tag(); $newTag->tag = $tagLabel; $newTag->save(); array_push($availableTagsId, $newTag->id); } /* * -------------------------------------------------------------------------- * Populate article data * -------------------------------------------------------------------------- * Retrieve all data from request and populate into article object model * then store it into database, check if featured is available then attempt * to upload to asset directory. */ $autoApprove = Setting::whereKey('Auto Approve')->first(); $status = $request->input('status'); if ($autoApprove->value) { if ($status == 'pending') { $status = 'published'; } } $contributor = Contributor::findOrFail($request->input('contributor_id')); $article = new Article(); $article->contributor_id = $contributor->id; $article->subcategory_id = $request->input('subcategory_id'); $article->title = $request->input('title'); $article->slug = $request->input('slug'); $article->content = $request->input('content'); $article->excerpt = $request->input('excerpt'); $article->status = $status; $image = new Uploader(); if ($image->upload($request, 'featured', base_path('public/images/featured/'), 'featured_' . uniqid())) { $article->featured = $request->input('featured'); } $article->save(); Article::find($article->id)->tags()->attach($availableTagsId); Activity::create(['contributor_id' => $contributor->id, 'activity' => Activity::createArticleActivity($contributor->username, $article->title, $article->slug)]); if ($autoApprove->value) { $articleController->sendFollowerEmailNotification($article); } else { $articleController->sendAdminArticleNotification($contributor, $article); } return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => 'Article was created', 'auto_approve' => $autoApprove->value, 'timestamp' => Carbon::now()]); } catch (\Exception $e) { return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'message' => Lang::get('alert.error.transaction'), 'timestamp' => Carbon::now()], 500); } }); return $result; }
/** * Store a newly created article in storage. * * @param CreateArticleRequest $request * @return \Illuminate\Http\Response */ public function store(CreateArticleRequest $request) { /* * -------------------------------------------------------------------------- * Populate tags * -------------------------------------------------------------------------- * tags [many-to] -- article_tags -- [many] articles * * Sync tags and article, extract tags from request, break down between new * tags and tags that available in database, collect available tags first * then insert new tags and merge new inserted tag id with available tags id * finally store them all into article_tags table. */ $articleController = $this; $result = DB::transaction(function () use($request, $articleController) { try { // get all tags which ALREADY EXIST by tags are given eg: // request tags = [angga, ari, entertainment, happy, trend, 2016] // all database tags = [1 => news, 2 => happy, 3 => angga, 4 => love, 5 => 2016] // tags where in = [3 => angga, 2 => happy, 5 => 2016] $availableTags = Tag::whereIn('tag', explode(',', $request->get('tags'))); // collect tags which existed into tags_id and leave them for a while // $tags_id [3, 2, 5] $availableTagsId = $availableTags->pluck('id')->toArray(); // retrieve tags label (name) which already exist to compare with given array // tags label [angga, happy, 2016] $availableTagsName = $availableTags->pluck('tag')->toArray(); // new tags need to insert into tags table // $new_tags = [ari, entertainment, trend] $newTags = array_diff(explode(',', $request->get('tags')), $availableTagsName); // loop through new tags and retrieve last inserted id to merge with tags_id that need to insert later // new tags will have id [6 => ari, 7 => entertainment, 8 => trend] // $tags_id will be [3, 2, 5, 6, 7, 8] foreach ($newTags as $tagLabel) { $newTag = new Tag(); $newTag->tag = $tagLabel; $newTag->save(); array_push($availableTagsId, $newTag->id); } /* * -------------------------------------------------------------------------- * Populate article data * -------------------------------------------------------------------------- * Retrieve all data from request and populate into article object model * then store it into database, check if featured is available then attempt * to upload to asset directory. */ $autoApprove = Setting::whereKey('Auto Approve')->first(); $status = $request->input('status'); if ($autoApprove->value) { if ($status == 'pending') { $status = 'published'; } } $article = new Article(); $article->contributor_id = Auth::user()->id; $article->subcategory_id = $request->input('subcategory'); $article->title = $request->input('title'); $article->slug = $request->input('slug'); $article->type = $request->input('type'); $article->content = $request->input('content'); $article->excerpt = $request->input('excerpt'); $article->status = $status; $image = new Uploader(); if ($image->upload($request, 'featured', base_path('public/images/featured/'), 'featured_' . uniqid())) { $article->featured = $request->input('featured'); } $article->save(); if ($article->id != null && $article->id >= 0) { if ($autoApprove->value) { $articleModel = new Article(); $articleModel->broadcastArticle($article); } } /* * -------------------------------------------------------------------------- * Sync and Attach * -------------------------------------------------------------------------- * We have all tag ids that need to insert on article_tags [3, 2, 5, 6, 7, 8] * use attach method we insert them through article model related by article * id so everything should be working perfectly. */ Article::find($article->id)->tags()->attach($availableTagsId); /* * -------------------------------------------------------------------------- * Create article activity * -------------------------------------------------------------------------- * Create new instance of Activity and insert create article activity. */ Activity::create(['contributor_id' => Auth::user()->id, 'activity' => Activity::createArticleActivity(Auth::user()->username, $article->title, $article->slug)]); if (!$autoApprove->value) { $articleController->sendAdminArticleNotification(Auth::user(), $article); } return redirect(route('account.article.index'))->with(['status' => 'success', 'message' => Lang::get('alert.article.create', ['title' => $article->title])]); } catch (\Exception $e) { return redirect()->back()->withErrors(['error' => Lang::get('alert.error.transaction')])->withInput(); } }); return $result; }