/** * Update the specified user in storage. * @param string $username * @param GenericRequest $request * @return \Illuminate\Http\Response */ public function update($username, GenericRequest $request) { $user = User::where('username', $username)->firstOrFail(); $ownAccount = $user->id == $this->user->id; // If performing a general save then validate the inputs and perform the // update, setting the account type as necessary. If updating the active // user, the restricted attributes will be set to their current values. if ($request->get('action') == 'save') { $data = $request->stripped('name', 'username', 'nickname', 'email', 'phone', 'dob', 'address', 'tool_colours', 'type') + ['show_email' => $request->has('show_email'), 'show_phone' => $request->has('show_phone'), 'show_address' => $request->has('show_address'), 'show_age' => $request->has('show_age')]; $data['dob'] = $data['dob'] ?: null; if ($ownAccount) { $data['username'] = $user->username; $data['type'] = $user->type; } $validator = Validator::make($data, $user->getProfileValidationRules(), $user->getProfileValidationMessages()); if ($validator->fails()) { return redirect()->back()->withInput($data)->withErrors($validator); } else { if ($user->update($data)) { Flash::success('User updated'); return redirect(route('user.index')); } else { Flash::error('Something went wrong while updating the user'); return redirect(route('user.edit', $username)); } } } else { if ($request->get('action') == 'archive') { if ($ownAccount) { Flash::warning('You cannot archive your own account'); } else { if ($user->archive()) { Flash::success('User archived'); } else { Flash::error('Something went wrong when archiving the user'); } } return redirect(route('user.edit', $username)); } else { if ($request->get('action') == 'unarchive') { if ($user->update(['status' => true])) { Flash::success('User unarchived'); } else { Flash::error('Something went wrong when unarchiving the user'); } return redirect(route('user.edit', $username)); } else { if ($request->get('action') == 'change-pic') { $file = $request->file('avatar'); if (!$file) { Flash::warning('Please select an image to use'); } else { $user->setAvatar($file); Flash::success('Profile picture changed'); } return redirect(route('user.edit', $username)); } else { if ($request->get('action') == 'remove-pic') { if ($user->hasAvatar()) { $path = base_path('public') . $user->getAvatarUrl(); if (is_writeable($path)) { unlink($path); Flash::success("Profile picture removed"); } else { Flash::error("The user's picture is not writeable"); } } return redirect(route('user.edit', $username)); } else { if ($request->get('action') == 'reset-password') { $password = str_random(15); $user->update(['password' => bcrypt($password)]); Flash::success('New password sent'); Mail::queue('emails.users.reset_password', ['name' => $user->forename, 'password' => $password], function ($message) use($user) { $message->subject('Your new password')->to($user->email, $user->name); }); return redirect(route('user.edit', $username)); } else { return redirect(route('user.edit', $username)); } } } } } } }
/** * Provide a method that parses a search * query for a category and any tags. * @param $request * @return array */ private function parseSearchRequest(GenericRequest $request) { // Get the query, category and tags from the request $query = $request->get('query') ?: null; $category = $request->get('category') ?: null; $tags = $request->get('tag') ?: []; // Initialise the parsed array $params = ['query' => $query]; if ($category) { $params['category'] = $category; } if ($tags) { foreach ($tags as $tag) { @($params['tag'][] = $tag); } } // Look for a category in the query preg_match('/category:([a-z0-9-]+)/i', $query, $matches); if (count($matches) > 0) { $params['category'] = $matches[1]; $query = trim(str_replace($matches[0], '', $query)); } // Look for any tags in the query preg_match_all('/tag:([a-z0-9-]+)/i', $query, $matches); if (count($matches[0]) > 0) { foreach ($matches[1] as $i => $tag) { @($params['tag'][] = $tag); $query = trim(str_replace($matches[0][$i], '', $query)); } } // Set the query $params['query'] = $query; return $params; }
/** * Process the form for updating a repair's details. * @param $id * @param \App\Http\Requests\GenericRequest $request * @return \Illuminate\Http\Response */ public function update($id, GenericRequest $request) { // Get the breakage entry $breakage = EquipmentBreakage::findOrFail($id); // Updating the event's details if ($request->get('action') == 'update') { // Validate $this->validate($request, ['status' => 'required|in:' . implode(',', array_keys(EquipmentBreakage::$status))], ['status.required' => 'Please choose a status for the breakage', 'status.in' => 'Please choose a valid status']); // Update, message and redirect $breakage->update($request->stripped('comment', 'status') + ['closed' => $request->get('status') == EquipmentBreakage::STATUS_RESOLVED]); Flash::success('Breakage updated'); } else { if ($request->get('action') == 'close') { $breakage->update(['closed' => true]); Flash::success('Breakage closed'); } } return redirect(route('equipment.repairs')); }
/** * Common function for setting the slug in the request. * This checks if a slug was manually set before * defaulting to "slugifying" the name. * @param \App\Http\Requests\GenericRequest $request * @param string $slugName * @param string $defaultName * @return string */ protected function createSlug(GenericRequest $request, $slugName = 'slug', $defaultName = 'name') { return $request->get($slugName) ? strtolower($request->get($slugName)) : str_slug($request->get($defaultName)); }
/** * Allow the finance DB to send an email to the EM. * @param $eventId * @param \App\Http\Requests\GenericRequest $request * @return \Illuminate\Support\Facades\Response */ public function sendFinanceEmail($eventId, GenericRequest $request) { // Check the key is valid if (env('FINANCE_DB_KEY') == $request->get('appKey')) { // Check the event exists $event = Event::find($eventId); if ($event && $event->em_id) { $subject = $request->get('subject') . " ({$event->name})"; Mail::queue('emails.events.finance_db.' . strtolower($request->get('message')), ['name' => $event->em->forename, 'event' => $event->name, 'event_id' => $request->get('fsid')], function ($message) use($event, $subject) { $message->to($event->em->email, $event->em->name)->subject($subject)->from('*****@*****.**'); }); return Response::json(['code' => 200, 'result' => 'success']); } else { return Response::json(['code' => '500', 'result' => 'Event not found or no EM assigned'], 500); } } else { return Response::json(['code' => 500, 'result' => 'Incorrect key'], 500); } }