/** * @return mixed */ public function getUnreadNotifications() { $response = new AjaxResponse(); $response->setSuccessMessage(trans('common.success')); $notificationDetails = Notifications::getUnread(\Auth::user()->id); $response->addExtraFields($notificationDetails); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Check if given product code is used by some user or not. * * @param CheckIfProductCodeIsUsedRequest $request * @param AjaxResponse $response * @return mixed */ public function checkIfCodeIsUsed(CheckIfProductCodeIsUsedRequest $request, AjaxResponse $response) { $response->setSuccessMessage(trans('common.success')); // Assume product is not used and update status if is used $used = false; if (Product::where('code', $request->get('product_code'))->count() || ApplicationProduct::where('code', $request->get('product_code'))->count()) { $used = true; } $response->addExtraFields(['used' => $used]); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Get product data. * * @param int $productId * @param string $productCode * @param GetProductRequest $request * @param AjaxResponse $response * @return mixed */ public function get($productId, $productCode, GetProductRequest $request, AjaxResponse $response) { // Make sure product exists if (!ApplicationProduct::where('code', $productCode)->where('id', $productId)->count()) { $response->setFailMessage(trans('common.general_error')); return response($response->get())->header('Content-Type', 'application/json'); } $response->setSuccessMessage(trans('common.success')); $response->addExtraFields(['product' => ProductsManagerHelper::productDetails($productCode, $productId)]); return response($response->get())->header('Content-Type', 'application/json'); }
public static function changeAccountStatus($status = 1, $userId = false) { $response = new AjaxResponse(); $message = trans('users_manager.account_enabled'); if ($status == 0) { $message = trans('users_manager.account_disabled'); } if (!$userId) { $userId = Auth::user()->id; } \App\User::where('id', $userId)->update(['active' => $status]); $response->setSuccessMessage($message); $response->addExtraFields(['active' => $status]); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Create new account. * * @param CreateAccountRequest $request * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function register(CreateAccountRequest $request) { $response = new AjaxResponse(); $roles = new Roles(); // Build user data array $data = ['email' => $request->get('email'), 'password' => bcrypt($request->get('password')), 'role_id' => $roles->getUserRoleId()]; // Insert user $user = User::create($data); // User settings UserSetting::insert(['user_id' => $user->id, 'language_id' => Settings::defaultLanguageId()]); // Create trial period UserTrialPeriod::create(['user_id' => $user->id, 'trial_period_id' => TrialPeriod::where('validity_days', 90)->first()->id]); Auth::login($user); $response->setSuccessMessage(trans('register.account_created')); return response($response->get()); }
/** * Handle given targeted group. * * @param string $targetedGroup * @param int $notificationId * @return mixed */ public static function handle($targetedGroup, $notificationId) { $response = new AjaxResponse(); $response->setSuccessMessage(trans('notifications.targeted_users_set')); // Handle case when all users are targeted if ($targetedGroup === self::$targetGroups['all']) { self::_handleAll($notificationId); return response($response->get())->header('Content-Type', 'application/json'); } // Handle case when no user is targeted if ($targetedGroup === self::$targetGroups['none']) { self::_handleNone($notificationId); return response($response->get())->header('Content-Type', 'application/json'); } $response->setFailMessage(trans('common.general_error')); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Log user in * * @param LoginRequest $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function login(LoginRequest $request) { $response = new AjaxResponse(); // Get inputs $email = $request->get('email'); $password = $request->get('password'); $userId = User::where('email', $email)->value('id'); // todo check for login attempts // Check if credentials are ok if ($this->auth->attempt(['email' => $email, 'password' => $password, 'active' => 1])) { event(new UserLoggedIn($this->auth->user()->id)); $response->setSuccessMessage(trans('common.success')); return response($response->get())->header('Content-Type', 'application/json'); } // If email exists in database log the login attempt if ($userId) { event(new FailedLogIn($userId)); } $response->setFailMessage(trans('login.login_failed')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); }
/** * Return all campaign numbers for given year. * * @param Requests\Statistics\GetCampaignNumbersRequest $request * @return mixed */ public function getCampaignNumbers(GetCampaignNumbersRequest $request) { $response = new AjaxResponse(); $response->setSuccessMessage(trans('common.success')); $response->addExtraFields(['numbers' => Campaign::select('number')->distinct()->where('year', $request->get('year'))->get()]); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Delete client. * * @param DeleteClientRequest $request * @param int $clientId * @return array */ public function delete($clientId, DeleteClientRequest $request) { $response = new AjaxResponse(); $table = 'clients'; // Count rows, delete record and count rows after the operation $initialRows = DB::table($table)->where('user_id', Auth::user()->id)->count(); DB::table($table)->where('id', $clientId)->where('user_id', Auth::user()->id)->delete(); $finalRows = DB::table($table)->where('user_id', Auth::user()->id)->count(); // Check if record was deleted or not and return a success or error response if ($finalRows < $initialRows) { $response->setSuccessMessage(trans('clients.client_deleted')); return response($response->get()); } $response->setFailMessage(trans('common.delete_error')); return response($response->get(), $response->getDefaultErrorResponseCode()); }
/** * Set targeted users. * * @param SetTargetedUsersRequest $request * @param AjaxResponse $response * @return mixed */ public function setTargetedUsers(SetTargetedUsersRequest $request, AjaxResponse $response) { $response->setSuccessMessage(trans('notifications.targeted_users_set')); $notificationId = $request->get('notification_id'); $targetGroup = $request->get('target_group'); return Notifications::handle($targetGroup, $notificationId); }
/** * Edit article title and content. * * @param int $categoryId * @param EditArticleRequest $request * @return mixed */ public function editArticle($categoryId, EditArticleRequest $request) { $response = new AjaxResponse(); $category = HelpCenterCategory::where('id', $categoryId)->first(); if (!$category) { $response->setFailMessage(trans('help_center.category_not_found')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); } // Edit article $article = HelpCenterArticle::find($request->get('article_id')); $article->title = $request->get('article_title'); $article->content = $request->get('article_content'); $article->save(); // Get updated version of articles $extraFields = []; $articles = HelpCenterManagerHelper::getCategoryArticles($categoryId); if (count($articles)) { $extraFields['articles'] = $articles; } $response->setSuccessMessage(trans('help_center.article_updated')); $response->addExtraFields($extraFields); return response($response->get())->header('Content-Type', 'application/json'); }
/** * @param DenyUsersToChangeLanguageRequest $request * @return mixed */ public function denyUsersToChangeLanguage(DenyUsersToChangeLanguageRequest $request) { $securitySetting = SecuritySetting::first(); $securitySetting->allow_users_to_change_language = 0; $securitySetting->save(); // Success response $response = new AjaxResponse(); $response->setSuccessMessage(trans('application_settings.users_are_not_allowed_to_change_language')); $response->addExtraFields(['allow_users_to_change_language' => trans('common.no'), 'allow_users_to_change_language_bool' => false]); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Handle creation of new bill. * * @param CreateBillRequest $request * @return array */ public function create(CreateBillRequest $request) { // Save request data $clientName = $request->get('client'); $useCurrentCampaign = $request->get('use_current_campaign'); $campaignYear = $request->get('campaign_year'); $campaignNumber = $request->get('campaign_number'); $client = DB::table('clients')->where('name', $clientName)->where('user_id', Auth::user()->id)->first(); // Create new client if not exists if (!$client) { $client = new Client(); $client->user_id = Auth::user()->id; $client->name = $clientName; $client->save(); } // Create new bill $bill = new Bill(); $bill->client_id = $client->id; $bill->user_id = Auth::user()->id; $campaign = Campaigns::current(); // Check if current campaign should be used if (!$useCurrentCampaign) { $campaign = Campaign::where('year', $campaignYear)->where('number', $campaignNumber)->first(); } $bill->campaign_id = $campaign->id; $bill->campaign_order = Campaigns::autoDetermineOrderNumber($campaign, $client->id); $bill->save(); event(new UserCreatedNewBill(Auth::user()->id, $bill->id)); // Return response $response = new AjaxResponse(); $response->setSuccessMessage(trans('bills.bill_created')); return response($response->get()); }
/** * Reset user settings to default. * * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function resetToDefaultValues() { $response = new AjaxResponse(); $defaultSettings = UserDefaultSetting::first(); Auth::user()->settings()->update(['displayed_bills' => $defaultSettings->displayed_bills, 'displayed_clients' => $defaultSettings->displayed_clients, 'displayed_products' => $defaultSettings->displayed_products, 'displayed_custom_products' => $defaultSettings->displayed_custom_products]); $response->setSuccessMessage(trans('settings.restored_to_default_settings')); $response->addExtraFields(Settings::all()); return response($response->get()); }
/** * @param int $userId * @param string $code * @param SetNewPasswordRequest $request * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function setNewPassword($userId, $code, SetNewPasswordRequest $request) { $response = new AjaxResponse(); $recover = RecoverCode::where('user_id', $userId)->where('code', $code)->valid()->first(); if (!$recover) { $response->setFailMessage(trans('common.general_error')); return response($response->get(), $response->badRequest()); } $user = User::find($userId); if (!$user) { // User not found $response->setFailMessage(trans('common.general_error')); return response($response->get(), $response->badRequest()); } User::where('id', $userId)->update(['password' => bcrypt($request->get('new_password'))]); $response->setSuccessMessage(trans('recover.password_updated')); return response($response->get()); }
/** * Delete offer. * * @param DeleteOfferRequest $request * @return mixed */ public function deleteOffer(DeleteOfferRequest $request) { // Find offer $offer = Offer::find($request->get('offer_id')); // Delete all subscriptions that belongs to this offer Subscription::where('offer_id', $offer->id)->delete(); // Delete offer $offer->delete(); // Return success response $response = new AjaxResponse(); $response->setSuccessMessage(trans('offers.offer_deleted')); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Mark bill as unpaid. * * @param int $billId * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public static function markAsUnpaid($billId) { $response = new AjaxResponse(); // Make sure bill exists if (!Bill::where('id', $billId)->where('user_id', Auth::user()->id)->count()) { $response->setFailMessage(trans('bill.bill_not_found')); return response($response->get(), 404)->header('Content-Type', 'application/json'); } Auth::user()->bills()->where('id', $billId)->update(['paid' => 0]); $response->setSuccessMessage(trans('bill.marked_as_unpaid')); $response->addExtraFields(['paid' => 0]); return response($response->get()); }
/** * Allow user to ask questions. * * @param AskQuestionRequest $request * @return mixed */ public function askQuestion(AskQuestionRequest $request) { $question = new Question(); $question->title = $request->get('question_title'); $question->content = $request->get('question_content'); $question->question_category_id = $request->get('question_category_id'); $question->user_id = Auth::user()->id; $question->save(); $response = new AjaxResponse(); $response->setSuccessMessage(trans('help_center.question_sent')); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Get product details. * * @param string $productCode * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public static function details($productCode) { $response = new AjaxResponse(); $isApplicationProduct = false; // Check if is in products table $product = Product::where('user_id', Auth::user()->id)->where('code', $productCode)->first(); if (!$product) { $product = ApplicationProduct::where('code', $productCode)->first(); $isApplicationProduct = true; } // Check if is in application_products table if (!$product) { $response->setFailMessage('not found'); return response($response->get(), $response->getDefaultErrorResponseCode()); } $response->setSuccessMessage('ok'); if ($isApplicationProduct) { $data = ['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'sold_pieces' => self::productSoldPieces($product->id), 'total_price' => self::productTotalPrice($product->id), 'paid_bills' => self::paidBillsThatContainProduct($product->id), 'not_paid_bills' => self::notPaidBillsThatContainProduct($product->id), 'is_application_product' => $isApplicationProduct]; $response->addExtraFields($data); return response($response->get()); } $response->addExtraFields(['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'sold_pieces' => self::productSoldPieces($product->id, true), 'total_price' => self::productTotalPrice($product->id, true), 'paid_bills' => self::paidBillsThatContainProduct($product->id, true), 'not_paid_bills' => self::notPaidBillsThatContainProduct($product->id, true), 'is_application_product' => $isApplicationProduct]); return response($response->get()); }
/** * Allow admin to delete user account. * * @param int $userId * @param DeleteUserAccountRequest $request * @return mixed */ public function deleteUserAccount($userId, DeleteUserAccountRequest $request) { $response = new AjaxResponse(); // Make sure user id exists if (!User::where('id', $userId)->count()) { $response->setFailMessage(trans('users_manager.user_not_found')); return response($response->get())->header('Content-Type', 'application/json'); } Subscription::where('user_id', $userId)->delete(); User::where('id', $userId)->delete(); $response->setSuccessMessage(trans('users_manager.account_deleted')); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Check if a product code is already used by application products or user products * * @param string $code * @return mixed */ public function checkProductCode($code) { $response = new AjaxResponse(); // Validation rules $validator = Validator::make(['code' => $code], ['code' => ['required', 'digits:5']]); // Run validator if ($validator->fails()) { $response->setFailMessage($this->getValidatorFirstErrorMessage($validator->messages())); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); } // Check if product code is available if ($this->isProductCodeAlreadyUsed($code)) { $response->setFailMessage(trans('my_products.product_code_used')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); } $response->setSuccessMessage(trans('my_products.product_code_available')); return response($response->get())->header('Content-Type', 'application/json'); }