コード例 #1
0
 /**
  * Override setting the user_id attribute to automatically adjust user roles.
  * @param $value
  */
 public function setUserIdAttribute($value)
 {
     // Only process the change in permissions
     // if the assigned user has changed
     $old_id = $this->getAttributeValue('user_id');
     if ($old_id != $value) {
         // Don't allow self-unassignment
         if ($old_id == Auth::user()->id) {
             Flash::warning('You can\'t remove yourself from the committee');
             return;
         }
         // Get the necessary roles
         $committee = Role::where('name', 'committee')->first();
         $member = Role::where('name', 'member')->first();
         // Look through the database for any other committee roles for
         // the old user. If they exist then we don't want to remove
         // their committee permissions.
         $old_user = User::find($old_id);
         if ($old_user && $old_user->hasRole($committee->name)) {
             if (CommitteeRole::where('user_id', '=', $old_user->id)->where('id', '<>', $this->id)->get()->count() == 0) {
                 $old_user->detachRole($committee);
                 $old_user->attachRole($member);
             }
         }
         // Always give the new user committee permissions
         $new_user = User::find($value);
         if ($new_user && !$new_user->hasRole($committee->name)) {
             $new_user->attachRole($committee);
             $new_user->detachRole($member);
         }
     }
     // Set the new id
     $this->attributes['user_id'] = $value;
 }
コード例 #2
0
 /**
  * Delete a quote.
  * @param \Illuminate\Http\Request $request
  * @return string
  */
 public function destroy(Request $request)
 {
     $quote = Quote::find($request->get('deleteQuote'));
     if ($quote) {
         if ($quote->delete()) {
             Flash::success("Quote deleted");
         } else {
             Flash::error("Oops", "Something went wrong when trying to delete that quote.");
         }
     } else {
         Flash::warning("Oops", "The selected quote couldn't be found; perhaps it's been deleted?");
     }
     return redirect()->back();
 }
コード例 #3
0
 /**
  * Process changes to the user's profile using AJAX.§
  * @param Request $request
  * @return Response
  */
 public function postMyProfile(Request $request)
 {
     // If the request was made by AJAX then we are updating a single
     // field. If this is the case then we need to check what's been
     // submitted, to protect against updating disallowed fields,
     // validate the value and then update the user's attribute.
     if ($request->ajax()) {
         // Check a field is specified
         $field = $request->get('field') ?: @key($request->except('_token'));
         $value = $request->get('value') ?: $request->get($field);
         if (!$field) {
             return $this->ajaxError('Invalid submission');
         }
         // Check that the field is allowed
         if (!in_array($field, ['name', 'nickname', 'email', 'dob', 'phone', 'address', 'tool_colours', 'show_email', 'show_phone', 'show_address', 'show_age'])) {
             return $this->ajaxError('Unknown field');
         }
         // Only validate the input if the field isn't one of the privacy settings
         $is_privacy = in_array($field, ['show_email', 'show_phone', 'show_address', 'show_age']);
         if (!$is_privacy) {
             $validator = Validator::make([$field => $value], User::getValidationRules($field), User::getValidationMessages($field));
             if ($validator->fails()) {
                 return $this->ajaxError($validator->messages()->first());
             }
         }
         // Update
         $this->user->update([$field => $is_privacy ? $value == 'true' : ($field == 'dob' && $value ? Carbon::createFromFormat('d/m/Y', $value) : $value)]);
         return \Illuminate\Support\Facades\Response::json(true);
     } else {
         if ($request->get('action') == 'change-pic') {
             $file = $request->file('avatar');
             if (!$file) {
                 Flash::warning('Please select an image to use');
             } else {
                 $this->user->setAvatar($file);
                 Flash::success('Profile picture changed');
             }
             return redirect(route('members.myprofile'));
         } else {
             if ($request->get('action') == 'remove-pic') {
                 if ($this->user->hasAvatar()) {
                     unlink(base_path('public') . $this->user->getAvatarUrl());
                     Flash::success("Profile picture removed");
                 }
                 return redirect(route('members.myprofile'));
             }
         }
     }
     // Set a default return should the request not be recognised
     App::abort(404);
 }
コード例 #4
0
ファイル: OrderController.php プロジェクト: jbatalla/lpexam
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $order_id = $this->getNewOrderId();
     $pick_id = 'P' . $this->getRandomRackNo();
     $warning = array();
     $error = array();
     $totalPrice = 0;
     $totaProducts = count($request->input('products'));
     foreach ($request->input('products') as $selected_id) {
         //GET Product Price
         $productPrice = $this->dbgetProductPrice($selected_id);
         //GET Bin Location
         $bin = $this->dbgetProductBinLoc($selected_id);
         //GET Distance
         //$psd = $this->dbcalculatePickDistance($bin[0]->bin_location);
         $remarks = $this->dbcalculatePickDistance($bin[0]->bin_location, $totaProducts);
         $finalPrice = $productPrice->price;
         $totalPrice += $finalPrice;
         //QUERY STOCK LEVEL
         $results = $this->dbStockLevel($selected_id);
         if ($results[0]->stock_level != 0) {
             //Prepare to insert data in orders table
             $new_order = array('order_id' => $order_id, 'customer_id' => $request->input('customers'), 'product_id' => $selected_id, 'product_price' => $productPrice->price, 'remarks' => '', 'picking_station' => $bin[0]->picking_id, 'picking_station_distance' => $remarks['value'] . 'm');
             $post = new Orders($new_order);
             $post->save();
             // Saves Warning Message for Low Level Stock
             if ($results[0]->stock == 0) {
                 array_push($warning, $results[0]->product_name);
             }
             //DECREMENT STOCK
             $this->dbUpdateStock($selected_id);
         } else {
             if ($results[0]->stock_level == 0) {
                 array_push($error, $results[0]->product_name);
             }
         }
     }
     //SHOW IF THERE IS ANY WARNING MESSAGES
     $res = count($warning);
     if ($res > 0) {
         foreach ($warning as $re) {
             Flash::warning('Low on stock on: ' . $re);
         }
     }
     //SHOW IF THERE IS ANY ERROR MESSAGES
     $res_e = count($error);
     if ($res_e > 0) {
         foreach ($error as $er) {
             Flash::error('No stock available on: ' . $er);
         }
     }
     //ADD Optimum route to database when empty values
     $order = $this->dbQueryOrders($order_id);
     if (!empty($order) || $order[0]->remarks == '') {
         $add_remarks = $this->getOptimumDistance($order_id);
         DB::table('orders')->where('order_id', $order_id)->update(array('remarks' => $add_remarks));
     }
     #FLASH CLASS
     //Flash::message('Your order has been created!'); //blue
     //Flash::warning('Your order has been created!'); //brown
     //Flash::error('Your order has been created!');  //red
     //Flash::success('Your order has been created!');  //green
     //Flash::info('Your order has been created!');  //light blue
     Flash::success('Your order has been created!');
     return redirect('orders');
 }
コード例 #5
0
 /**
  * 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));
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #6
0
 /**
  * @param $page
  */
 private function unpublishedFlash($page)
 {
     Flash::warning('Page not published', "The page '{$page->title}' is not visible to the public as it isn't published. <a href=\"" . route('page.edit', $page->slug) . "#published\">Edit the page</a>.");
 }
コード例 #7
0
 /**
  * @param                                   $id
  * @param \App\Http\Requests\GenericRequest $request
  * @return \Illuminate\Support\Facades\Response
  */
 public function toggleVolunteer($id, GenericRequest $request)
 {
     // Require ajax
     $this->requireAjax($request);
     // Get the event
     $event = Event::findOrFail($id);
     // Test if they are the EM
     if ($event->isEM($this->user)) {
         return $this->ajaxError('You can\'t unvolunteer as you are the EM!');
     }
     // Test if the user is already crew
     $crew = $event->crew->where('user_id', $this->user->id)->first();
     if ($crew) {
         // Only unvolunteer if it's not a social
         if ($event->isSocial()) {
             Flash::warning("You can't unvolunteer from a social");
         } else {
             $crew->delete();
             Flash::success('You have unvolunteered');
         }
     } else {
         $event->crew()->create(['name' => null, 'user_id' => $this->user->id]);
         Flash::success('You have volunteered');
         // Send the email to the crew
         $user = $this->user;
         Mail::queue('emails.events.volunteered_crew', ['event' => $event->name, 'user' => $user->forename, 'em' => $event->em_id ? $event->em->name : ''], function ($message) use($user, $event) {
             $message->to($user->email, $user->name)->subject("Volunteered to crew event '{$event->name}'");
         });
         // Send the email to the EM
         if ($event->em_id) {
             $em = $event->em;
             Mail::queue('emails.events.volunteered_em', ['em' => $em->forename, 'user' => $user->name, 'event' => $event->name], function ($message) use($em, $user, $event) {
                 $message->to($em->email, $em->name)->from($user->email, $user->name)->subject("Crew volunteered for '{$event->name}'");
             });
         }
     }
     return Response::json(true);
 }
コード例 #8
0
 /**
  * Make the user an associate
  * @return bool
  */
 public function makeAssociate()
 {
     if ($this->id == Auth::user()->id) {
         Flash::warning('You cannot make yourself an associate');
         return false;
     }
     $this->roles()->sync([Role::where('name', 'associate')->first()->id]);
     return true;
 }
コード例 #9
0
 /**
  * Cast the user's vote.
  * @param int                      $id
  * @param \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function castVote($id, Request $request)
 {
     // Get the poll
     $poll = Poll::find($id);
     if (!$poll) {
         return redirect(route('polls.index'));
     }
     // Check if already voted
     if ($poll->voted($this->user)) {
         Flash::warning('You have already voted for this poll.');
         return redirect(route('polls.view', $id));
     }
     // Cast vote
     $option = PollOption::find($request->get('vote'));
     if (!$option) {
         return redirect(route('polls.view', $id));
     }
     $option->votes()->create(['user_id' => $this->user->id]);
     Flash::success('Vote cast');
     return redirect(route('polls.view', $id));
 }