Advertisement
freedfelipe

UserController.php

Jul 9th, 2015
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.02 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6.  
  7. use App\Http\Requests;
  8. use App\Http\Controllers\Controller;
  9.  
  10. use App\User as User;
  11. use App\UserType as UserType;
  12.  
  13. class UserController extends Controller
  14. {
  15.     protected $title = 'Usuário';
  16.  
  17.     protected $searchable = array(
  18.         'name'  => 'l',
  19.         'email' => 'l'
  20.     );
  21.  
  22.     /**
  23.      * Display a listing of the resource.
  24.      *
  25.      * @return Response
  26.      */
  27.     public function index()
  28.     {
  29.         // var_dump(\Input::except('page'));
  30.  
  31.         $title      = $this->title;
  32.         $users      = User::Search(\Input::except('page'), $this->searchable)->paginate(10)
  33.         ->OrderBy(\Input::except('page'))->paginate(10);
  34.         $links      = $users->appends(\Input::except('page'));
  35.         $searchable = \Input::except('page');
  36.  
  37.         return view('user.index')->with(compact('title', 'searchable', 'links', 'users'));
  38.     }
  39.  
  40.     /**
  41.      * Show the form for creating a new resource.
  42.      *
  43.      * @return Response
  44.      */
  45.     public function create()
  46.     {
  47.         $title      = $this->title;
  48.         $userTypes  = UserType::lists('name', 'id');
  49.  
  50.         return view('user.form')->with(compact('title', 'userTypes'));
  51.     }
  52.  
  53.     /**
  54.      * Store a newly created resource in storage.
  55.      *
  56.      * @return Response
  57.      */
  58.     public function store(Request $request)
  59.     {
  60.         $attributes = array(
  61.             'name'                  => 'nome',
  62.             'email'                 => 'Email',
  63.             'user_types_id'         => 'Tipo de usuário',
  64.             'password'              => 'Senha',
  65.             'password_confirmation' => 'Confirmar senha',
  66.         );
  67.  
  68.         $rules = array(
  69.             'name'          => 'required',
  70.             'email'         => 'required|unique:users',
  71.             'user_types_id' => 'required',
  72.             'password'      => 'required|confirmed|min:6',
  73.             'image'         => 'image|max:300'
  74.         );
  75.  
  76.         $validator = \Validator::make(\Input::all(), $rules);
  77.         $validator->setAttributeNames($attributes);
  78.  
  79.         if($validator->fails()) {
  80.             return redirect()->back()
  81.             ->withErrors($validator)
  82.             ->withInput( \Input::all() );
  83.         } else {
  84.             $user                   = new User;
  85.             $user->name             = \Input::get('name');
  86.             $user->email            = \Input::get('email');
  87.             $user->user_types_id    = \Input::get('user_types_id');
  88.             $user->password         = \Hash::make(\Input::get('password'));
  89.             $user->save();
  90.  
  91.             if ($request->hasFile('image')) {
  92.  
  93.                 $user->image = $user->id . '.' . $request->file('image')->getClientOriginalExtension();
  94.  
  95.                 $request->file('image')->move(
  96.                     base_path() . '/public/images/profile/', $user->image
  97.                 );
  98.  
  99.                 $user->save();
  100.             }
  101.  
  102.  
  103.             \Session::flash('message_success', $this->title . ' criado com sucesso!');
  104.  
  105.             return redirect()->route('user.index');
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * Display the specified resource.
  111.      *
  112.      * @param  int  $id
  113.      * @return Response
  114.      */
  115.     public function show($id)
  116.     {
  117.         //
  118.     }
  119.  
  120.     /**
  121.      * Show the form for editing the specified resource.
  122.      *
  123.      * @param  int  $id
  124.      * @return Response
  125.      */
  126.     public function edit($id)
  127.     {
  128.         $title = $this->title;
  129.  
  130.         $user       = User::find($id);
  131.         $userTypes  = UserType::lists('name', 'id');
  132.  
  133.         return view('user.form')->with(compact('title', 'user', 'userTypes'));
  134.     }
  135.  
  136.     /**
  137.      * Update the specified resource in storage.
  138.      *
  139.      * @param  int  $id
  140.      * @return Response
  141.      */
  142.     public function update(Request $request, $id)
  143.     {
  144.         $attributes = array(
  145.             'name'                  => 'nome',
  146.             'email'                 => 'Email',
  147.             'user_types_id'         => 'Tipo de usuário',
  148.             'password'              => 'Senha',
  149.             'password_confirmation' => 'Confirmar senha',
  150.         );
  151.  
  152.         $rules = array(
  153.             'name'          => 'required',
  154.             'email'         => 'required|unique:users,email,'.$id,
  155.             'user_types_id' => 'required',
  156.             'image'         => 'image|max:300'
  157.         );
  158.  
  159.         if(!empty(\Input::get('password'))){
  160.             $rules['password'] = 'required|confirmed|min:6';
  161.         }
  162.  
  163.         $validator = \Validator::make(\Input::all(), $rules);
  164.         $validator->setAttributeNames($attributes);
  165.  
  166.         if($validator->fails()) {
  167.             return redirect()->back()
  168.             ->withErrors($validator)
  169.             ->withInput( \Input::all() );
  170.         } else {
  171.             $user                   = User::find($id);
  172.             $user->name             = \Input::get('name');
  173.             $user->email            = \Input::get('email');
  174.             $user->user_types_id    = \Input::get('user_types_id');
  175.  
  176.             if(!empty(\Input::get('password'))){
  177.                 $user->password         = \Hash::make(\Input::get('password'));
  178.             }
  179.  
  180.             if ($request->hasFile('image')) {
  181.                 $user->image = $user->id . '.' . $request->file('image')->getClientOriginalExtension();
  182.  
  183.                 $request->file('image')->move(
  184.                     base_path() . '/public/images/profile/', $user->image
  185.                 );
  186.             }
  187.  
  188.             $user->save();
  189.  
  190.             \Session::flash('message_success', $this->title . ' editado com sucesso!');
  191.  
  192.             return redirect()->route('user.index');
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Remove the specified resource from storage.
  198.      *
  199.      * @param  int  $id
  200.      * @return Response
  201.      */
  202.     public function destroy($id)
  203.     {
  204.         $user = User::find($id);
  205.         $user->delete();
  206.  
  207.         \Session::flash('message_success', $this->title.' removido com sucesso!');
  208.  
  209.         return redirect()->route('user.index');
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement