/** * Stop following specified contributor. * * @param Request $request * @return \Illuminate\Http\Response */ public function unfollow(Request $request) { $contributor_id = $request->input('contributor_id'); $following_id = $request->input('following_id'); $follower = Follower::whereContributorId($contributor_id)->whereFollowing($following_id)->first(); if (count($follower) > 0) { $contributor = Contributor::findOrFail($contributor_id)->name; $following = Contributor::findOrFail($following_id)->name; if ($follower->delete()) { return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => $contributor . ' is stop following ' . $following, 'timestamp' => Carbon::now()]); } return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'message' => Lang::get('alert.database.generic'), 'timestamp' => Carbon::now()], 500); } return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => 'You has not followed this contributor before', 'timestamp' => Carbon::now()], 400); }
/** * Update transaction status and send notification to contributor. * * @param Request $request * @param $status * @return \Illuminate\Http\RedirectResponse */ public function update(Request $request, $status) { $result = DB::transaction(function () use($request, $status) { $contributor_id = $request->input('contributor_id'); $transaction_id = $request->input('transaction_id'); $contributor = Contributor::findOrFail($contributor_id); $transaction = $contributor->transactions()->findOrFail($transaction_id); try { if ($transaction->status != Transaction::STATUS_SUCCESS) { if ($status == Transaction::STATUS_SUCCESS) { $balance = $contributor->balance - $transaction->amount; if ($balance >= 0) { $contributor->balance = $balance; $contributor->save(); } else { return redirect()->back()->withErrors(['balance' => 'You have tried to withdraw IDR ' . number_format($transaction->amount, 0, ',', '.'), 'error' => 'Balance of ' . $contributor->name . ' is insufficient (IDR ' . number_format($contributor->balance, 0, ',', '.') . ')', 'reject' => 'Please reject or cancel this transaction']); } } } $transaction->status = $status; if ($transaction->save()) { // notify the contributor Mail::send('emails.receipt', ['transaction' => $transaction, 'contributor' => $contributor], function ($message) use($transaction, $contributor) { $message->from(env('MAIL_ADDRESS', '*****@*****.**'), env('MAIL_NAME', 'Infogue.id')); $message->replyTo('*****@*****.**', env('MAIL_NAME', 'Infogue.id')); $message->to($contributor->email)->subject('Withdrawal status transaction ID ' . $transaction->id . ' is ' . $transaction->status); }); return redirect()->back()->with(['status' => $status == 'success' ? 'success' : 'warning', 'message' => "Status transaction ID {$transaction_id} was updated <strong>{$status}</strong>"]); } else { return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]); } } catch (\Exception $e) { return redirect()->back()->withErrors(['error' => Lang::get('alert.error.transaction')])->withInput(); } }); return $result; }
/** * Store a relation between 2 contributors. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function follow(Request $request) { /* * -------------------------------------------------------------------------- * Perform follow request * -------------------------------------------------------------------------- * This operation only for authenticate user, a contributor follow another * contributor, populate the data and make sure there is no record following * from contributor A to B before then perform insert data. */ if (Auth::check() && $request->ajax()) { $contributor_id = Auth::user()->id; $following_id = $request->input('id'); $isFollowing = Follower::whereContributorId($contributor_id)->whereFollowing($following_id)->count(); if (!$isFollowing) { $follower = new Follower(); $follower->contributor_id = $contributor_id; $follower->following = $following_id; if ($follower->save()) { $following = Contributor::findOrFail($following_id); /* * -------------------------------------------------------------------------- * Create following activity * -------------------------------------------------------------------------- * Create new instance of Activity and insert following activity. */ Activity::create(['contributor_id' => $contributor_id, 'activity' => Activity::followActivity(Auth::user()->username, $following->username)]); if ($following->email_follow) { $follower->sendEmailNotification(Auth::user(), $following); } return response('success'); } return response('failed', 500); } return response('success'); } else { return response('restrict', 401); } }
/** * Update the specified article in storage. * * @param \Illuminate\Http\Request $request * @param param int $slug * @return \Illuminate\Http\Response */ public function update(Request $request, $slug) { $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); } $articleController = $this; $article = Article::whereSlug($slug)->firstOrFail(); $exist = Article::whereSlug($request->input('slug'))->where('id', '!=', $article->id)->count(); if ($exist) { return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => 'Unique slug has taken', 'timestamp' => Carbon::now()], 400); } $result = DB::transaction(function () use($request, $article, $articleController) { try { /* * -------------------------------------------------------------------------- * Populate tags * -------------------------------------------------------------------------- * Sync last tags and new article, extract tags from request, break down * between new tags and tags that available in database, merge new inserted * tag id with available tags id and remove the old which is removed. */ $tag = Tag::whereIn('tag', explode(',', $request->get('tags'))); $tags_id = $tag->pluck('id')->toArray(); $available_tags = $tag->pluck('tag')->toArray(); $new_tags = array_diff(explode(',', $request->get('tags')), $available_tags); $article->tags()->sync($tags_id); foreach ($new_tags as $tag_label) { $newTag = new Tag(); $newTag->tag = $tag_label; $newTag->save(); if (!$article->tags->contains($newTag->id)) { $article->tags()->save($newTag); } } /* * -------------------------------------------------------------------------- * Update the article * -------------------------------------------------------------------------- * Finally populate article data like create process and check if featured * need to change and upload the image then update the changes. */ $autoApprove = Setting::whereKey('Auto Approve')->first(); $content = $article->content; $content_update = $request->input('content'); $status = $request->input('status'); if ($autoApprove->value) { $content = $request->input('content'); $content_update = ''; if ($status == 'pending') { $status = 'published'; } } $article->subcategory_id = $request->input('subcategory_id'); $article->title = $request->input('title'); $article->slug = $request->input('slug'); $article->type = $request->input('type'); $article->content = $content; $article->content_update = $content_update; $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(); $contributor = Contributor::findOrFail($request->input('contributor_id')); Activity::create(['contributor_id' => $contributor->id, 'activity' => Activity::updateArticleActivity($contributor->username, $article->title, $article->slug)]); if (!$autoApprove->value) { $articleController->sendAdminArticleNotification($contributor, $article, true); } return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => 'Article was updated', '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; }
/** * Send new message to another contributor in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function send(Request $request) { /* * -------------------------------------------------------------------------- * Create message * -------------------------------------------------------------------------- * Each conversation will handle by one message record as identity, first * check if contributor sender or receiver ever make conversation, if they * did not then create new one of message. */ $sender = $request->input('contributor_id'); $receiver = $request->input('receiver_id'); $lastMessage = $this->conversation->whereSender($sender)->whereReceiver($receiver)->orWhere('sender', $receiver)->whereReceiver($sender)->first(); if (count($lastMessage) == 0) { $message = new Message(); $message->save(); $messageId = $message->id; } else { $messageId = $lastMessage->message_id; } /* * -------------------------------------------------------------------------- * Create conversation * -------------------------------------------------------------------------- * Populate message id from last conversation or last inserted new message * then create the first conversation or continue with last message, check * if there is request of attachment, if so then upload it. */ $conversation = new Conversation(['message_id' => $messageId, 'sender' => $sender, 'receiver' => $receiver, 'message' => $request->input('message')]); $result = $conversation->save(); if ($result) { $contributorSender = Contributor::findOrFail($sender); $contributor = Contributor::findOrFail($receiver); if ($contributor->email_message) { $this->sendEmailNotification($contributorSender, $contributor, $request->input('message')); } $conversation->broadcastConversation($contributorSender, $contributor, $request->input('message')); } return response()->json(['request_id' => uniqid(), 'status' => $result ? 'success' : 'failure', 'timestamp' => Carbon::now()], $result ? 200 : 500); }
/** * Update the specified contributor in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function update(Request $request) { $rules = ['avatar' => 'mimes:jpg,jpeg,gif,png|max:1000', 'cover' => 'mimes:jpg,jpeg,gif,png|max:1000']; $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { $failedRules = $validator->failed(); $validAvatar = isset($failedRules['avatar']); $validCover = isset($failedRules['cover']); $errorMessage = "Invalid avatar or cover"; if ($validAvatar && $validCover) { $errorMessage = "Cover and Avatar is invalid"; } else { if ($validAvatar) { $errorMessage = "Avatar is invalid"; } else { if ($validCover) { $errorMessage = "Cover is invalid"; } } } $errorMessage .= ", must image and less than 1MB"; return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => $errorMessage, 'timestamp' => Carbon::now()], 400); } $contributor = Contributor::findOrFail($request->input('contributor_id')); if ($request->has('new_password') && !empty($request->get('new_password'))) { $credential = Hash::check($request->input('password'), $contributor->password); if (!$credential) { return response()->json(['request_id' => uniqid(), 'status' => 'mismatch', 'message' => 'Current password is mismatch', 'timestamp' => Carbon::now()], 401); } } $usernameExist = Contributor::whereUsername($request->input('username'))->where('id', '!=', $contributor->id)->count(); if ($usernameExist) { return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => 'Username has been taken', 'timestamp' => Carbon::now()], 400); } $emailExist = Contributor::whereEmail($request->input('email'))->where('id', '!=', $contributor->id)->count(); if ($emailExist) { return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => 'Email has been taken', 'timestamp' => Carbon::now()], 400); } $contributor->name = $request->input('name'); $contributor->gender = $request->input('gender'); $contributor->birthday = $request->input('birthday'); $contributor->location = $request->input('location'); $contributor->contact = $request->input('contact'); $contributor->about = $request->input('about'); $contributor->username = $request->input('username'); $contributor->email = $request->input('email'); $image = new Uploader(); if ($image->upload($request, 'avatar', base_path('public/images/contributors/'), 'avatar_' . $request->input('contributor_id'))) { $contributor->avatar = $request->input('avatar'); } if ($image->upload($request, 'cover', base_path('public/images/covers/'), 'cover_' . $request->input('contributor_id'))) { $contributor->cover = $request->input('cover'); } $contributor->instagram = $request->input('instagram'); $contributor->facebook = $request->input('facebook'); $contributor->twitter = $request->input('twitter'); $contributor->googleplus = $request->input('googleplus'); $contributor->email_subscription = $request->input('email_subscription'); $contributor->email_message = $request->input('email_message'); $contributor->email_follow = $request->input('email_follow'); $contributor->email_feed = $request->input('email_feed'); $contributor->mobile_notification = $request->input('mobile_notification'); if ($request->has('new_password') && !empty($request->get('new_password'))) { $request->merge(['password' => Hash::make($request->input('new_password'))]); $contributor->password = $request->input('password'); } $contributor->bank_id = $request->input('bank_id'); $contributor->account_name = $request->input('account_name'); $contributor->account_number = $request->input('account_number'); if ($contributor->save()) { return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => 'Setting was updated', 'timestamp' => Carbon::now(), 'contributor' => $contributor->profile($contributor->username, false, $request->input('contributor_id'), true)]); } else { return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'message' => Lang::get('alert.database.generic'), 'timestamp' => Carbon::now()], 500); } }
/** * Remove the specified contributor from storage. * * @param Request $request * @param int $id * @return \Illuminate\Http\Response */ public function destroy(Request $request, $id = null) { /* * -------------------------------------------------------------------------- * Delete contributor * -------------------------------------------------------------------------- * Check if selected variable is not empty then user intends to select multiple * rows at once, and prepare the feedback message according the type of * deletion action. */ if (!empty(trim($request->input('selected')))) { $contributor_ids = explode(',', $request->input('selected')); $delete = Contributor::whereIn('id', $contributor_ids)->delete(); $message = Lang::get('alert.contributor.delete_all', ['count' => $delete]); } else { $contributor = Contributor::findOrFail($id); $message = Lang::get('alert.contributor.delete', ['name' => $contributor->name]); $delete = $contributor->delete(); } if ($delete) { return redirect(route('admin.contributor.index'))->with(['status' => 'warning', 'message' => $message]); } else { return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]); } }
/** * Send new message to another contributor in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function send(Request $request) { /* * -------------------------------------------------------------------------- * Create message * -------------------------------------------------------------------------- * Each conversation will handle by one message record as identity, first * check if contributor sender or receiver ever make conversation, if they * did not then create new one of message. */ $sender = Auth::user()->id; $receiver = (int) $request->input('contributor_id'); $lastMessage = $this->conversation->whereSender($sender)->whereReceiver($receiver)->orWhere('sender', $receiver)->whereReceiver($sender)->first(); if (count($lastMessage) == 0) { $message = new Message(); $message->save(); $messageId = $message->id; } else { $messageId = $lastMessage->message_id; } /* * -------------------------------------------------------------------------- * Create conversation * -------------------------------------------------------------------------- * Populate message id from last conversation or last inserted new message * then create the first conversation or continue with last message, check * if there is request of attachment, if so then upload it. */ $conversation = new Conversation(['message_id' => $messageId, 'sender' => $sender, 'receiver' => $receiver, 'message' => $request->input('message')]); if ($conversation->save()) { $contributor = Contributor::findOrFail($receiver); if ($contributor->email_message) { $this->sendEmailNotification(Auth::user(), $contributor, $request->input('message')); } $conversation->broadcastConversation(Auth::user(), $contributor, $request->input('message')); if ($request->has('async') && $request->ajax()) { $image = new Uploader(); if ($image->upload($request, 'attachment', base_path('public/file/'), 'attachment_' . uniqid())) { $attachment = new Attachment(); $attachment->conversation_id = $conversation->id; $attachment->file = $request->input('attachment'); if (!$attachment->save()) { return false; } } return 'sent'; } return redirect()->back()->with(['status' => 'success', 'message' => Lang::get('alert.message.send', ['receiver' => $contributor->name])]); } if ($request->has('async') && $request->ajax()) { return false; } return redirect()->back()->with(['status' => 'danger', 'message' => Lang::get('alert.error.database')]); }
/** * Update the specified contributor in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function update(Request $request) { /* * -------------------------------------------------------------------------- * Validating data * -------------------------------------------------------------------------- * Define rules before populating data, check if checkbox is unchecked then * gives default value 0, merge notification for date, month, year as * birthday, and throw it back if errors occur. */ $user = Auth::user(); $rules = ['name' => 'required|max:50', 'gender' => 'required|in:male,female,other', 'date' => 'required|max:31', 'month' => 'required|max:12', 'year' => 'required|max:' . (int) Carbon::now()->addYear(-8)->format('Y'), 'location' => 'required|max:30', 'contact' => 'required|max:20', 'about' => 'required|min:15|max:160', 'email_subscription' => 'boolean', 'email_message' => 'boolean', 'email_follow' => 'boolean', 'email_feed' => 'boolean', 'mobile_notification' => 'boolean', 'account_name' => 'max:50', 'account_number' => 'min:7|max:15', 'avatar' => 'mimes:jpg,jpeg,gif,png|max:1000', 'cover' => 'mimes:jpg,jpeg,gif,png|max:1000', 'username' => 'required|alpha_dash|max:20|unique:contributors,username,' . $user->id, 'email' => 'required|email|max:50|unique:contributors,email,' . $user->id, 'new_password' => 'confirmed|min:6']; if ($user->vendor == "web" || $user->vendor == "mobile") { $rules['password'] = '******'; } if (!$request->has('email_subscription')) { $request->merge(['email_subscription' => 0]); } if (!$request->has('email_message')) { $request->merge(['email_message' => 0]); } if (!$request->has('email_follow')) { $request->merge(['email_follow' => 0]); } if (!$request->has('email_feed')) { $request->merge(['email_feed' => 0]); } if (!$request->has('mobile_notification')) { $request->merge(['mobile_notification' => 0]); } $request->merge(['birthday' => implode('-', Input::only('year', 'month', 'date'))]); $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { $failedRules = $validator->failed(); $date = isset($failedRules['date']['Required']); $month = isset($failedRules['month']['Required']); $year = isset($failedRules['year']['Required']); if ($date || $month || $year) { $validator->errors()->add('birthday', Lang::get('alert.validation.birthday')); } $this->throwValidationException($request, $validator); } /* * -------------------------------------------------------------------------- * Populating data and update * -------------------------------------------------------------------------- * Retrieve all data, if avatar or cover input is not empty then try to * upload the image and get the filename, if new_password is not empty then * the contributor intend to change their password, in the end perform save. */ $contributor = Contributor::findOrFail($user->id); $contributor->name = $request->input('name'); $contributor->gender = $request->input('gender'); $contributor->birthday = $request->input('birthday'); $contributor->location = $request->input('location'); $contributor->contact = $request->input('contact'); $contributor->about = $request->input('about'); $contributor->username = $request->input('username'); $contributor->email = $request->input('email'); $image = new Uploader(); if ($image->upload($request, 'avatar', base_path('public/images/contributors/'), 'avatar_' . $contributor->id)) { $contributor->avatar = $request->input('avatar'); } if ($image->upload($request, 'cover', base_path('public/images/covers/'), 'cover_' . $contributor->id)) { $contributor->cover = $request->input('cover'); } $contributor->instagram = $request->input('instagram'); $contributor->facebook = $request->input('facebook'); $contributor->twitter = $request->input('twitter'); $contributor->googleplus = $request->input('googleplus'); $contributor->email_subscription = $request->input('email_subscription'); $contributor->email_message = $request->input('email_message'); $contributor->email_follow = $request->input('email_follow'); $contributor->email_feed = $request->input('email_feed'); $contributor->mobile_notification = $request->input('mobile_notification'); if ($request->has('new_password') && !empty($request->get('new_password'))) { $request->merge(['password' => Hash::make($request->input('new_password'))]); $contributor->password = $request->input('password'); } $contributor->bank_id = $request->input('bank'); $contributor->account_name = $request->input('account_name'); $contributor->account_number = $request->input('account_number'); if ($contributor->save()) { return redirect(route('account.setting'))->with(['status' => 'success', 'message' => Lang::get('alert.contributor.account')]); } return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]); }
/** * As long as the request does not proceed yet by admin, contributor can cancel * their withdrawal application. * * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function cancel(Request $request) { $transactionId = $request->input('id'); $contributorId = $request->input('contributor_id'); $contributor = Contributor::findOrFail($contributorId); $transaction = $contributor->transactions()->findOrFail($transactionId); $transaction->status = Transaction::STATUS_CANCEL; if ($transaction->save()) { // notify the contributor Mail::send('emails.receipt', ['transaction' => $transaction, 'contributor' => $contributor], function ($message) use($transaction, $contributor) { $message->from(env('MAIL_ADDRESS', '*****@*****.**'), env('MAIL_NAME', 'Infogue.id')); $message->replyTo('*****@*****.**', env('MAIL_NAME', 'Infogue.id')); $message->to($contributor->email)->subject('Withdrawal status transaction ID ' . $transaction->id . ' is ' . $transaction->status); }); // notify all admins via email so they could proceed the transaction as soon as possible $admins = User::all(['name', 'email']); foreach ($admins as $admin) { if ($admin->email != '*****@*****.**' && $admin->email != '*****@*****.**') { Mail::send('emails.admin.cancel', ['contributor' => $contributor, 'transaction' => $transaction], function ($message) use($admin, $contributor, $transaction) { $message->from(env('MAIL_ADDRESS', '*****@*****.**'), env('MAIL_NAME', 'Infogue.id')); $message->replyTo($contributor->email, $contributor->name); $message->to($admin->email)->subject($contributor->name . " cancel their withdrawal ID #" . $transaction->id); }); } } return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => 'Transaction ' . $transactionId . ' was cancelled', 'timestamp' => Carbon::now()]); } return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'message' => Lang::get('alert.error.generic'), 'timestamp' => Carbon::now()], 500); }
/** * Delete image by passing contributor id and image id. * * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function delete(Request $request) { /* * the file must be owned by the correspond contributor, * and if it exists, just delete. */ $contributor = Contributor::findOrFail($request->input('contributor_id')); $image = $contributor->images()->findOrFail($request->input('id')); $pathImage = public_path('images/featured/' . $image->source); if (File::exists($pathImage)) { unlink($pathImage); } if ($image->delete()) { return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => Lang::get('alert.image.delete', ['title' => $image->source]), 'timestamp' => Carbon::now()]); } return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'message' => Lang::get('alert.error.generic'), 'timestamp' => Carbon::now()], 500); }