Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.84 KB | None | 0 0
  1. <?php
  2. namespace App\Http\Controllers\Backend;
  3. use App\Traits\Authorizable;
  4. use App\Http\Requests\UserRequest;
  5. use App\Role;
  6. use App\User;
  7. use App\Rank;
  8. use App\Duty;
  9. use App\Category;
  10. use Illuminate\Http\Request;
  11. use Auth;
  12. use Illuminate\Support\Facades\DB;
  13. use Lang;
  14. use Mail;
  15. use App\Http\Controllers\Controller;
  16. class UserController extends Controller
  17. {
  18.     use Authorizable;
  19.     /**
  20.      * Display a listing of the resource.
  21.      *
  22.      * @return \Illuminate\Http\Response
  23.      */
  24.     public function index()
  25.     {
  26.         $users = User::latest()->paginate(10);
  27.         return view('backend.users.index', compact('users'));
  28.     }
  29.     /**
  30.      * Show the form for creating a new resource.
  31.      *
  32.      * @return \Illuminate\Http\Response
  33.      */
  34.     public function create()
  35.     {
  36.         $roles = Role::pluck('name', 'id');
  37.         $ranks = Rank::pluck('title', 'id');
  38.         //get all duties without alias
  39.         $duties = Duty::whereNull('alias')->pluck('title', 'id');
  40.         //get all duties with alias
  41.         $duties_grp = Duty::whereNotNull('alias')->pluck('title', 'id');
  42.         $commands = Category::where('parent_id', '0')->pluck('title', 'id');
  43.         $user_categories_array = [];
  44.         return view('backend.users.create', compact('roles', 'ranks', 'duties', 'duties_grp', 'commands', 'user_categories_array'));
  45.     }
  46.     /**
  47.      * Store a newly created resource in storage.
  48.      *
  49.      * @param  \Illuminate\Http\Request $request
  50.      * @return \Illuminate\Http\Response
  51.      */
  52.     public function store(UserRequest $request)
  53.     {
  54.         //duties_array for database in json format (duty_id)
  55.         $duties_array = [];
  56.         //check if duty has been selected in form
  57.         if ($request->duties[0] != null) {
  58.             foreach ($request->duties as $duty)
  59.                 array_push($duties_array, $duty);
  60.         }
  61.         //check if grp duty has been selected in form
  62.         if ($request->grp_duties[0] != null) {
  63.             foreach ($request->grp_duties as $grp_duty)
  64.                 array_push($duties_array, $grp_duty);
  65.         }
  66.         $request->merge([
  67.             'username' => $this->setUsername($request->get('first_name'), $request->get('last_name')),
  68.             'token' => str_random(25),
  69.             'duty_id' => json_encode($duties_array)
  70.         ]);
  71.         $user = User::create($request->except('roles', 'permissions'));
  72.         if (!$user) {
  73.             flash()->error(Lang::get('actions.store.messages.error'));
  74.             return redirect()->route('backend.users.index');
  75.         } else {
  76.             //store information about categories in database
  77.             DB::table('user_has_categories')->insert([
  78.                 'user_id' => $user->id,
  79.                 'command_id' => $request->command,
  80.                 'formation_id' => $request->formation,
  81.                 'unit_id' => $request->unit
  82.             ]);
  83.             $this->syncPermissions($request, $user);
  84.             Mail::send('mails.passwords.create', $request->all(), function ($message) use ($request) {
  85.                 $message->to($request['email']);
  86.                 $message->subject('Registration confirmation');
  87.             });
  88.             flash()->success(Lang::get('actions.store.messages.success'));
  89.             return redirect()->route('backend.users.index');
  90.         }
  91.     }
  92.     /**
  93.      * Display the specified resource.
  94.      *
  95.      * @param  int $id
  96.      * @return \Illuminate\Http\Response
  97.      */
  98.     public function show($id)
  99.     {
  100.         //
  101.     }
  102.     /**
  103.      * Show the form for editing the specified resource.
  104.      *
  105.      * @param  int $id
  106.      * @return \Illuminate\Http\Response
  107.      */
  108.     public function edit($id)
  109.     {
  110.         $user = User::findOrFail($id);
  111.         $ranks = Rank::pluck('title', 'id');
  112.         $duties = Duty::whereNull('alias')->pluck('title', 'id');
  113.         $duties_grp = Duty::whereNotNull('alias')->pluck('title', 'id');
  114.         $commands = Category::where('parent_id', '0')->pluck('title', 'id');
  115.         $user_duty_array = [];
  116.         $user_duty_grp_array = [];
  117.         $user_categories_array = [];
  118.         //check if user has duty_id column filled in
  119.         if ($user->duty_id) {
  120.             //foreach to check all duties from user table duty_id(json format) column
  121.             foreach (json_decode($user->duty_id) as $duty) {
  122.                 //if duty id from json exists in duties array
  123.                 if (array_key_exists($duty, $duties->all())) {
  124.                     array_push($user_duty_array, $duty);
  125.                 }
  126.                 //if duty id from json exists in duties_grp array
  127.                 if (array_key_exists($duty, $duties_grp->all())) {
  128.                     array_push($user_duty_grp_array, $duty);
  129.                 }
  130.             }
  131.         }
  132.         $user_categories = DB::table('user_has_categories')->select('command_id', 'formation_id', 'unit_id')->where('user_id', $user->id)->first();
  133.         if ($user_categories) {
  134.             //put user category ids to the array
  135.             array_push($user_categories_array, $user_categories->command_id);
  136.             array_push($user_categories_array, $user_categories->formation_id);
  137.             array_push($user_categories_array, $user_categories->unit_id);
  138.             //get all formations from chosen command
  139.             $_formations = Category::where('parent_id', $user_categories->command_id)->pluck('title', 'id');
  140.             //get all units from chosen formation
  141.             $_units = Category::where('parent_id', $user_categories->formation_id)->pluck('title', 'id');;
  142.         }
  143.         $roles = Role::pluck('name', 'id');
  144.         return view('backend.users.edit', compact('user', 'roles', 'ranks', 'duties', 'duties_grp', 'commands', '_formations', '_units', 'user_duty_array', 'user_duty_grp_array', 'user_categories_array'));
  145.     }
  146.     /**
  147.      * Update the specified resource in storage.
  148.      *
  149.      * @param  \Illuminate\Http\Request $request
  150.      * @param  int $id
  151.      * @return \Illuminate\Http\Response
  152.      */
  153.     public function update(UserRequest $request, $id)
  154.     {
  155.         $user = User::findOrFail($id);
  156.         //duties_array for database in json format (duty_id)
  157.         $duties_array = [];
  158.         if ($request->duties[0] != null) {
  159.             foreach ($request->duties as $duty)
  160.                 array_push($duties_array, $duty);
  161.         }
  162.         if ($request->grp_duties[0] != null) {
  163.             foreach ($request->grp_duties as $grp_duty)
  164.                 array_push($duties_array, $grp_duty);
  165.         }
  166.         $request->merge([
  167.             'username' => $this->setUsername($request->get('first_name'), $request->get('last_name')),
  168.             'duty_id' => json_encode($duties_array)
  169.         ]);
  170.         $user->fill($request->except('roles', 'permissions', 'password'));
  171.         // Check for password change
  172.         if ($request->get('password')) {
  173.             $user->password = bcrypt($request->get('password'));
  174.         }
  175.         // Handle the user roles
  176.         $this->syncPermissions($request, $user);
  177.         $user->save();
  178.         if (!$user) {
  179.             flash()->error(Lang::get('actions.update.messages.error'));
  180.             return redirect()->route('backend.users.index');
  181.         } else {
  182.             //update user's categories
  183.             $category_update = DB::table('user_has_categories')->where('user_id', $user->id)
  184.                 ->update([
  185.                     'user_id' => $user->id,
  186.                     'command_id' => $request->command,
  187.                     'formation_id' => $request->formation,
  188.                     'unit_id' => $request->unit
  189.                 ]);
  190.             if($category_update == 0){
  191.                 DB::table('user_has_categories')->insert([
  192.                     'user_id' => $user->id,
  193.                     'command_id' => $request->command,
  194.                     'formation_id' => $request->formation,
  195.                     'unit_id' => $request->unit
  196.                 ]);
  197.             }
  198.             flash()->success(Lang::get('actions.update.messages.success'));
  199.             return redirect()->route('backend.users.index');
  200.         }
  201.     }
  202.     /**
  203.      * Remove the specified resource from storage.
  204.      *
  205.      * @param  int $id
  206.      * @return \Illuminate\Http\Response
  207.      */
  208.     public function destroy($id)
  209.     {
  210.         if (Auth::user()->id == $id) {
  211.             flash()->warning(Lang::get('actions.destroy.messages.myself'))->important();
  212.             return redirect()->back();
  213.         }
  214.         $user = User::findOrFail($id)->delete();
  215.         if (!$user) {
  216.             flash()->error(Lang::get('actions.destroy.messages.error'));
  217.         }
  218.         flash()->success(Lang::get('actions.destroy.messages.success'));
  219.         return redirect()->back();
  220.     }
  221.     private function syncPermissions(Request $request, $user)
  222.     {
  223.         // Get the submitted roles
  224.         $roles = $request->get('roles', []);
  225.         $permissions = $request->get('permissions', []);
  226.         // Get the roles
  227.         $roles = Role::find($roles);
  228.         // Check for current role changes
  229.         if (!$user->hasAllRoles($roles)) {
  230.             // reset all direct permissions for user
  231.             $user->permissions()->sync([]);
  232.         } else {
  233.             // Handle permissions
  234.             $user->syncPermissions($permissions);
  235.         }
  236.         $user->syncRoles($roles);
  237.         return $user;
  238.     }
  239.     private function setUsername($firstName, $lastName)
  240.     {
  241.         $firstName = $this->convertText($firstName);
  242.         $lastName = $this->convertText($lastName);
  243.         $username = $firstName . '.' . $lastName;
  244.         return $username;
  245.     }
  246.     private function convertText($text)
  247.     {
  248.         // Remove Spaces
  249.         $text = str_replace(' ', '', $text);
  250.         // Replace LT characters
  251.         $text = @iconv('UTF-8', 'ASCII//TRANSLIT', $text);
  252.         // Remove Symbols
  253.         $text = preg_replace('/[^A-Za-z0-9\-]/', '', $text);
  254.         // Lower case
  255.         $text = mb_strtolower($text);
  256.         return $text;
  257.     }
  258.     public function resendLink(Request $request, $id)
  259.     {
  260.         $user = User::findOrFail($id);
  261.         if (!$user) {
  262.             return redirect()->back();
  263.         } else {
  264.             if ($user->password == null) {
  265.                 $request->merge([
  266.                     'first_name' => $user->first_name,
  267.                     'email' => $user->email,
  268.                     'token' => str_random(25),
  269.                 ]);
  270.                 $user->token = $request->token;
  271.                 $user->save();
  272.                 Mail::send('mails.passwords.create', $request->all(), function ($message) use ($request) {
  273.                     $message->to($request['email']);
  274.                     $message->subject('Registration confirmation');
  275.                 });
  276.                 return redirect()->route('backend.users.index');
  277.             } else {
  278.                 return redirect()->back();
  279.             }
  280.         }
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement