From 2b7abedfa3060708843e429900851b8341d9e276 Mon Sep 17 00:00:00 2001
From: Louis Leslie <spy.tech23@gmail.com>
Date: Sun, 10 Feb 2019 18:35:31 +0700
Subject: [PATCH] Telah diimplementasi registrasi dalam batch

---
 .../Auth/BatchRegisterController.php          | 147 ++++++++++++++++++
 .../Controllers/Auth/RegisterController.php   |   7 +
 resources/views/auth/batch-register.blade.php |  76 +++++++++
 .../views/auth/register-choice.blade.php      |  19 +++
 resources/views/manajer/index.blade.php       |   2 +-
 routes/web.php                                |   3 +
 6 files changed, 253 insertions(+), 1 deletion(-)
 create mode 100644 app/Http/Controllers/Auth/BatchRegisterController.php
 create mode 100644 resources/views/auth/batch-register.blade.php
 create mode 100644 resources/views/auth/register-choice.blade.php

diff --git a/app/Http/Controllers/Auth/BatchRegisterController.php b/app/Http/Controllers/Auth/BatchRegisterController.php
new file mode 100644
index 0000000..a6d722a
--- /dev/null
+++ b/app/Http/Controllers/Auth/BatchRegisterController.php
@@ -0,0 +1,147 @@
+<?php
+
+namespace App\Http\Controllers\Auth;
+
+use App\KelasTesis;
+use App\User;
+use App\Manajer;
+use App\Dosen;
+use App\Mahasiswa;
+use App\Http\Controllers\Controller;
+use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Foundation\Auth\RegistersUsers;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Storage;
+
+class BatchRegisterController extends Controller
+{
+    /*
+    |--------------------------------------------------------------------------
+    | Batch Register Controller
+    |--------------------------------------------------------------------------
+    |
+    | This controller handles the registration of new users in batch as well as their
+    | validation and creation. By default this controller uses a trait to
+    | provide this functionality without requiring any additional code.
+    |
+    */
+
+    use RegistersUsers;
+
+    /**
+     * Create a new controller instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+
+    }
+
+    /**
+     * Get a validator for an incoming registration request.
+     *
+     * @param  array  $data
+     * @return \Illuminate\Contracts\Validation\Validator
+     */
+    protected function validator(array $data)
+    {
+        return Validator::make($data, [
+            'name' => 'required|string|max:255',
+            'username' => 'required|string|max:4',
+            'email' => 'required|string|email|max:255|unique:users',
+            'password' => 'required|string|min:6|confirmed',
+        ]);
+    }
+
+    /**
+     * Create a new user instance after a valid registration.
+     *
+     * @param  array  $data
+     * @return \App\User
+     */
+    protected function create(array $data)
+    {
+        return User::create([
+            'name' => $data['name'],
+            'username' => $data['username'],
+            'email' => $data['email'],
+            'phone' => $data['phone'],
+            'password' => Hash::make($data['password']),
+        ]);
+    }
+
+    
+
+    public function showForm() {
+        if(Auth::user() && Auth::user()->isManajer()) {
+            return view('auth.batch-register');
+        } else {
+            return abort(403);
+        }
+    }
+
+    public function registerUsers(Request $request) {
+        $registrants = $request->file('registrants');
+        $filename = $registrants->getClientOriginalName();
+        $path = $registrants->storeAs('public', $filename);
+        $path = storage_path('app\\public\\registrants.csv');
+        $file = fopen($path, "r");
+        $registrants_data = array();
+        while ($data = fgetcsv($file, 200, ",")){
+            $registrant = array(
+                'name' => $data[0],
+                'username' => $data[1],
+                'email' => $data[2],
+                'phone' => $data[3],
+                'password' => $data[4],
+                'role' => $data[5]
+            );
+            array_push($registrants_data, $registrant);
+        }
+        fclose($file);
+
+        foreach($registrants_data as $data) {
+            $username = $data['username'];
+            if(User::where('username',$username)->count()>0) {
+                echo '<div class="alert alert-warning alert-dismissible fade show text-center">
+                        <button type="button" class="close" data-dismiss="alert">&times;</button>
+                        This user <strong>already exist.</strong>
+                    </div>';
+                return view('auth.batch-register');
+            } else if(strlen($data['username']) > 18){
+                echo '<div class="alert alert-warning alert-dismissible fade show text-center">
+                        <button type="button" class="close" data-dismiss="alert">&times;</button>
+                        <strong>Username</strong> too long (maximum size: 18 characters).
+                    </div>';
+                return view('auth.batch-register');
+            } else if(strlen($data['phone']) > 18){
+                echo '<div class="alert alert-warning alert-dismissible fade show text-center">
+                        <button type="button" class="close" data-dismiss="alert">&times;</button>
+                        <strong>Invalid</strong> phone number.
+                    </div>';
+                return view('auth.batch-register');
+            } else {
+            $user = $this->create($data);
+            $role = $data['role'];
+            if($role == User::ROLE_DOSEN) {
+                Dosen::create(['id'=>$user->id]);
+            }else if($role == User::ROLE_MAHASISWA) {
+                $id_kelas_tesis = KelasTesis::orderByRaw('updated_at - created_at DESC')->first();
+                Mahasiswa::create(['id'=>$user->id, 'id_kelas_tesis'=>$id_kelas_tesis->id]);
+            } else if($role == User::ROLE_MANAJER) {
+                Manajer::create(['id'=>$user->id]);
+            }
+                echo '<div class="alert alert-success alert-dismissible fade show text-center">
+                        <button type="button" class="close" data-dismiss="alert">&times;</button>
+                        <strong>Success !</strong> New user has successfully registered!
+                    </div>';
+            }
+        }
+
+        return view('auth.batch-register');
+    }
+
+}
diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php
index 9897320..d7c00d5 100644
--- a/app/Http/Controllers/Auth/RegisterController.php
+++ b/app/Http/Controllers/Auth/RegisterController.php
@@ -72,6 +72,13 @@ class RegisterController extends Controller
         ]);
     }
 
+    public function showChoice() {
+        if(Auth::user() && Auth::user()->isManajer()) {
+            return view('auth.register-choice');
+        } else {
+            return abort(403);
+        }
+    }
 
     public function showForm() {
         if(Auth::user() && Auth::user()->isManajer()) {
diff --git a/resources/views/auth/batch-register.blade.php b/resources/views/auth/batch-register.blade.php
new file mode 100644
index 0000000..4ceba5f
--- /dev/null
+++ b/resources/views/auth/batch-register.blade.php
@@ -0,0 +1,76 @@
+@extends('layouts.app')
+
+@section('content')
+<div class="container">
+    <div class="row justify-content-center">
+        <div class="col-md-8">
+            <div class="card">
+                <div class="card-header">Batch Register</div>
+
+                <div class="card-body" id="proposal-uploader">
+                        <div class="form-group row">
+
+                            <div class="col-md-6">
+
+                                <!-- Upload CSV -->
+                                <form method="POST" action="/register-batch" enctype="multipart/form-data" id="form-uploader" class="mt-4">
+                                    @csrf
+                                    {{csrf_field()}}
+                                    <div class="row justify-content-center form-group">
+                                        <input type="file" id="uploader" name="registrants" class="sr-only"  v-on:change="onFileChange">
+                                        <label for="uploader" class="uploader-label">
+                                            <i class="material-icons md-24">insert_drive_file</i>
+                                            Upload .csv File
+                                    </label>
+                                    </div>
+
+                                </form>
+                            </div>
+                        </div>
+
+                        <!-- <div class="form-group row mb-0"> -->
+                        <div v-if="file != null" class="mb-6">
+                            <div class="row">
+                                <div class="col-6 text-right">File Name :</div>
+                                <div class="col-6 text-left">@{{ file.name }}</div>
+                            </div>
+                            <div class="row">
+                                <div class="col-6 text-right">File Size :</div>
+                                <div class="col-6 text-left">@{{ humanFileSize(file.size) }}</div>
+                            </div>
+
+                            <div class="row justify-content-center align-items-center mt-4">
+                                <button type="exit" class="btn btn-white mr-2" onclick="backpage()">Cancel</button>
+                                <button type="submit" form="form-uploader" class="btn btn-primary ml-2">Register</button>
+                            </div>
+                        <!-- </div> -->
+                        </div>
+                </div>
+            </div>
+        </div>
+    </div>
+</div>
+@endsection
+
+@section('bottomjs')
+    <script>
+        var app = new Vue({
+            el: "#proposal-uploader",
+            data: {
+                file: null
+            },
+            methods: {
+                onFileChange: function() {
+                        this.file = $("#uploader")[0].files[0];
+                        console.log(this.file);
+                },
+                humanFileSize: function (size) {
+                    var i = Math.floor( Math.log(size) / Math.log(1024) );
+                    return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
+                }
+            }
+        });
+
+
+    </script>
+@endsection
diff --git a/resources/views/auth/register-choice.blade.php b/resources/views/auth/register-choice.blade.php
new file mode 100644
index 0000000..34fcaea
--- /dev/null
+++ b/resources/views/auth/register-choice.blade.php
@@ -0,0 +1,19 @@
+@extends('layouts.app')
+
+@section('content')
+<div class="container">
+    <div class="row justify-content-center">
+        <div class="col-md-8">
+            <div class="card">
+                <div class="card-header">Registration</div>
+                <div class="card-body">
+                        <div class="row justify-content-center align-items-center mt-4">
+                            <a href="/register"><button class="btn btn-primary mr-2">Individual</button></a>
+                            <a href="/register-batch"><button class="btn btn-primary ml-2">Batch</button></a>
+                        </div>
+                </div>
+            </div>
+        </div>
+    </div>
+</div>
+@endsection
diff --git a/resources/views/manajer/index.blade.php b/resources/views/manajer/index.blade.php
index 43107d5..d46240b 100644
--- a/resources/views/manajer/index.blade.php
+++ b/resources/views/manajer/index.blade.php
@@ -79,7 +79,7 @@
             </div>
 
             <div class="col-xs-6 col-lg-3 text-center">
-                <a href="/register" class="thumbnail">
+                <a href="/register-choice" class="thumbnail">
                     <div>
                         <i class="material-icons icon-style">person_add</i>
                     </div>
diff --git a/routes/web.php b/routes/web.php
index 0c8ba7b..67dc2b6 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -36,6 +36,9 @@ Route::post('mahasiswa/edit/{id}', 'MahasiswaController@edit');
 
 Route::get('register','Auth\RegisterController@showForm')->name('register');
 Route::post('register','Auth\RegisterController@registerUser')->name('registerPost');
+Route::get('register-choice', 'Auth\RegisterController@showChoice')->name('register-choice');
+Route::get('register-batch','Auth\BatchRegisterController@showForm')->name('register-batch');
+Route::post('register-batch','Auth\BatchRegisterController@registerUsers')->name('register-batchPost');
 
 Route::get('/home', 'HomeController@index')->name('home');
 Route::get('/generate/admin', 'Auth\RegisterController@generateAdmin');
-- 
GitLab