|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +use Bausch\Repositories\UserRepositoryInterface as Users; | 
|  | 4 | +use Bausch\Models\User as User; | 
|  | 5 | + | 
|  | 6 | +class UserController extends \BaseController { | 
|  | 7 | + | 
|  | 8 | +    /** | 
|  | 9 | +     * user repository | 
|  | 10 | +     *  | 
|  | 11 | +     * @var Users | 
|  | 12 | +     */ | 
|  | 13 | +    private $users; | 
|  | 14 | + | 
|  | 15 | +    /** | 
|  | 16 | +     * constructor | 
|  | 17 | +     *  | 
|  | 18 | +     * @param \Bausch\Repositories\UserRepositoryInterface $users | 
|  | 19 | +     */ | 
|  | 20 | +    public function __construct(Users $users) { | 
|  | 21 | +        parent::__construct(); | 
|  | 22 | + | 
|  | 23 | +        $this->users = $users; | 
|  | 24 | +    } | 
|  | 25 | + | 
|  | 26 | +    /** | 
|  | 27 | +     * draw the users table | 
|  | 28 | +     *  | 
|  | 29 | +     * @return Respone | 
|  | 30 | +     */ | 
|  | 31 | +    public function getTable() { | 
|  | 32 | +        $users = $this->users->findAll(); | 
|  | 33 | + | 
|  | 34 | +        return View::make('user.table') | 
|  | 35 | +                        ->with('users', $users); | 
|  | 36 | +    } | 
|  | 37 | + | 
|  | 38 | +    /** | 
|  | 39 | +     * Display a listing of the resource. | 
|  | 40 | +     * | 
|  | 41 | +     * @return Response | 
|  | 42 | +     */ | 
|  | 43 | +    public function index() { | 
|  | 44 | +        Assets::add('dataTable'); | 
|  | 45 | + | 
|  | 46 | +        return View::make('user.index') | 
|  | 47 | +                        ->with('users_table', $this->getTable()); | 
|  | 48 | +    } | 
|  | 49 | + | 
|  | 50 | +    /** | 
|  | 51 | +     * Show the form for creating a new resource. | 
|  | 52 | +     * | 
|  | 53 | +     * @return Response | 
|  | 54 | +     */ | 
|  | 55 | +    public function create() { | 
|  | 56 | +        return View::make('user.form') | 
|  | 57 | +                        ->with('form', [ | 
|  | 58 | +                            'action' => 'UserController@store', | 
|  | 59 | +                            'method' => 'POST', | 
|  | 60 | +        ]); | 
|  | 61 | +    } | 
|  | 62 | + | 
|  | 63 | +    /** | 
|  | 64 | +     * Store a newly created resource in storage. | 
|  | 65 | +     * | 
|  | 66 | +     * @return Response | 
|  | 67 | +     */ | 
|  | 68 | +    public function store() { | 
|  | 69 | +        $input = Input::all(); | 
|  | 70 | + | 
|  | 71 | +        $new_user = $this->users->instance($input); | 
|  | 72 | + | 
|  | 73 | +        if (@$input['enabled'] == 'yes') { | 
|  | 74 | +            $input['enabled'] = true; | 
|  | 75 | +            $new_user->setEnabled(true); | 
|  | 76 | +        } else { | 
|  | 77 | +            $input['enabled'] = false; | 
|  | 78 | +            $new_user->setEnabled(false); | 
|  | 79 | +        } | 
|  | 80 | + | 
|  | 81 | +        $message = 'User ' . $new_user->getUsername() . ' (' . $new_user->getEmail() . ') was created.'; | 
|  | 82 | + | 
|  | 83 | +        if (!@$input['password'] && !@$input['password_confirmation']) { | 
|  | 84 | +            // generate a random password | 
|  | 85 | +            $random_password = str_random(8); | 
|  | 86 | + | 
|  | 87 | +            $input['password'] = $random_password; | 
|  | 88 | +            $input['password_confirmation'] = $random_password; | 
|  | 89 | + | 
|  | 90 | +            $message .= '<br />Password: ' . $random_password; | 
|  | 91 | +        } | 
|  | 92 | + | 
|  | 93 | +        $new_user->setPassword($input['password']); | 
|  | 94 | + | 
|  | 95 | +        $validator = Validator::make($input, User::$rules['store']); | 
|  | 96 | + | 
|  | 97 | +        if (!$validator->passes()) { | 
|  | 98 | +            Input::flashExcept('password', 'password_confirmation'); | 
|  | 99 | + | 
|  | 100 | +            return $this->create() | 
|  | 101 | +                            ->withErrors($validator); | 
|  | 102 | +        } | 
|  | 103 | + | 
|  | 104 | + | 
|  | 105 | +        $this->users->store($new_user); | 
|  | 106 | + | 
|  | 107 | +        return HTML::alert('success', $message); | 
|  | 108 | +    } | 
|  | 109 | + | 
|  | 110 | +    /** | 
|  | 111 | +     * Display the specified resource. | 
|  | 112 | +     * | 
|  | 113 | +     * @param  int  $id | 
|  | 114 | +     * @return Response | 
|  | 115 | +     */ | 
|  | 116 | +    public function show($id) { | 
|  | 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 | +        $data = $this->users->findById($id); | 
|  | 128 | + | 
|  | 129 | +        if ($this->isAjax()) { | 
|  | 130 | +            return View::make('user.form') | 
|  | 131 | +                            ->with('form', [ | 
|  | 132 | +                                'action' => ['UserController@update', $id], | 
|  | 133 | +                                'method' => 'PUT', | 
|  | 134 | +                            ]) | 
|  | 135 | +                            ->with('userdata', $data); | 
|  | 136 | +        } | 
|  | 137 | +    } | 
|  | 138 | + | 
|  | 139 | +    /** | 
|  | 140 | +     * Update the specified resource in storage. | 
|  | 141 | +     * | 
|  | 142 | +     * @param  int  $id | 
|  | 143 | +     * @return Response | 
|  | 144 | +     */ | 
|  | 145 | +    public function update($id) { | 
|  | 146 | +        $user = $this->users->findById($id); | 
|  | 147 | + | 
|  | 148 | +        $input = Input::all(); | 
|  | 149 | + | 
|  | 150 | +        if (@$input['enabled'] == 'yes') { | 
|  | 151 | +            $input['enabled'] = true; | 
|  | 152 | +            $user->setEnabled(true); | 
|  | 153 | +        } else { | 
|  | 154 | +            $input['enabled'] = false; | 
|  | 155 | +            $user->setEnabled(false); | 
|  | 156 | +        } | 
|  | 157 | + | 
|  | 158 | +        $user->setUsername($input['username']); | 
|  | 159 | +        $user->setEmail($input['email']); | 
|  | 160 | + | 
|  | 161 | + | 
|  | 162 | +        $message = 'User ' . $user->getUsername() . ' (' . $user->getEmail() . ') has been updated.'; | 
|  | 163 | + | 
|  | 164 | +        if (!@$input['password'] && !@$input['password_confirmation']) { | 
|  | 165 | +            unset($input['password']); | 
|  | 166 | +            unset($input['password_confirmation']); | 
|  | 167 | +        } | 
|  | 168 | + | 
|  | 169 | +        $rules = User::$rules['update']; | 
|  | 170 | +        $rules['username'] .= $id; | 
|  | 171 | + | 
|  | 172 | +        $validator = Validator::make($input, $rules); | 
|  | 173 | + | 
|  | 174 | +        if (!$validator->passes()) { | 
|  | 175 | +            Input::flashExcept('password', 'password_confirmation'); | 
|  | 176 | + | 
|  | 177 | +            return $this->edit($id) | 
|  | 178 | +                            ->withErrors($validator); | 
|  | 179 | +        } | 
|  | 180 | + | 
|  | 181 | +        if (@$input['password'] && @$input['password_confirmation']) { | 
|  | 182 | +            $user->setPassword($input['password']); | 
|  | 183 | + | 
|  | 184 | +            $message .= '<br />Password has been changed.'; | 
|  | 185 | +        } | 
|  | 186 | + | 
|  | 187 | +        $this->users->update($user); | 
|  | 188 | + | 
|  | 189 | +        return HTML::alert('success', $message); | 
|  | 190 | +    } | 
|  | 191 | + | 
|  | 192 | +    /** | 
|  | 193 | +     * Remove the specified resource from storage. | 
|  | 194 | +     * | 
|  | 195 | +     * @param  int  $id | 
|  | 196 | +     * @return Response | 
|  | 197 | +     */ | 
|  | 198 | +    public function destroy($id) { | 
|  | 199 | +        // | 
|  | 200 | +    } | 
|  | 201 | + | 
|  | 202 | +    /** | 
|  | 203 | +     * delete multiple users | 
|  | 204 | +     *  | 
|  | 205 | +     * @return Response | 
|  | 206 | +     */ | 
|  | 207 | +    public function deleteUsers() { | 
|  | 208 | +        $delete_ids = Input::get('userids', array()); | 
|  | 209 | + | 
|  | 210 | +        // prevent the logged in user from deleting his own record | 
|  | 211 | +        // (thank you Stack Overflow! http://stackoverflow.com/a/7225113) | 
|  | 212 | +        if (($key = array_search($this->userid, $delete_ids)) !== false) { | 
|  | 213 | +            unset($delete_ids[$key]); | 
|  | 214 | +        } | 
|  | 215 | + | 
|  | 216 | +        $num_deletes = 0; | 
|  | 217 | + | 
|  | 218 | +        foreach ($delete_ids as $key => $val) { | 
|  | 219 | +            $delete_user = $this->users->findById($val); | 
|  | 220 | + | 
|  | 221 | +            if ($this->users->destroy($delete_user)) { | 
|  | 222 | +                $num_deletes++; | 
|  | 223 | +            } | 
|  | 224 | +        } | 
|  | 225 | + | 
|  | 226 | +        return Redirect::action($this->getAction('index')) | 
|  | 227 | +                        ->withSuccess('Deleted Users: ' . $num_deletes); | 
|  | 228 | +    } | 
|  | 229 | + | 
|  | 230 | +} | 
0 commit comments