/** * 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; }
/** * Update the specified article status and state in storage. * * @param $type * @param $label * @param $id * @return \Illuminate\Http\Response */ public function mark($type, $label, $id) { $article = Article::findOrFail($id); $operation = 'new'; if ($type == 'status') { $article->status = $label; if ($label == 'published') { $article->created_at = Carbon::now(); if (!empty(trim($article->content_update))) { $article->content = $article->content_update; $article->content_update = ''; $operation = 'update'; } else { $operation = 'new'; } } } else { if ($type == 'state') { $article->state = $label; } else { abort(404); } } $result = $article->save(); if ($result) { if ($type == 'status' && $label == 'published') { if ($operation == 'new') { $this->sendEmailNotification($article); $articleModel = new Article(); $articleModel->broadcastArticle($article); } } return redirect(route('admin.article.index'))->with(['status' => $label == 'reject' || $label == 'general' ? 'warning' : 'success', 'message' => Lang::get('alert.article.mark', ['title' => $article->title, 'type' => $type, 'label' => $label])]); } else { return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]); } }