161 lines
5.5 KiB
PHP
161 lines
5.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers;
|
||
|
|
||
|
|
||
|
use App\Models\Identification;
|
||
|
use App\Models\User;
|
||
|
use App\Traits\ApiResponser;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Http\Response;
|
||
|
use Illuminate\Support\Facades\Date;
|
||
|
use Illuminate\Support\Facades\Mail;
|
||
|
|
||
|
class UserController extends Controller
|
||
|
{
|
||
|
use ApiResponser;
|
||
|
|
||
|
/**
|
||
|
* Create a new controller instance.
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function __construct()
|
||
|
{
|
||
|
//
|
||
|
}
|
||
|
|
||
|
public function identification(Request $request)
|
||
|
{
|
||
|
|
||
|
$identification = new Identification();
|
||
|
$this->validate($request, $identification->rules());
|
||
|
|
||
|
$dbIdentification = Identification::where('id_user', $request->id_user)->first();
|
||
|
if($dbIdentification)
|
||
|
return $this->errorResponse(trans('errors.identification_carried_out'));
|
||
|
|
||
|
$user = User::findOrFail($request->id_user);
|
||
|
$identification->fill($request->all());
|
||
|
$identification->user_code = $this->generateRandomString();
|
||
|
$identification->status = 0;
|
||
|
|
||
|
$identification->save();
|
||
|
$this->sendMail($user->email,trans('messages.successful_identification'),
|
||
|
trans('messages.successful_identification_message',['name'=>$identification->lastname.' '.$identification->firstname,'user_code'=>$identification->user_code]));
|
||
|
return $this->successResponse(trans('messages.successful_identification'));
|
||
|
}
|
||
|
|
||
|
public function validateIdentification(Request $request, $id_identification)
|
||
|
{
|
||
|
$identification = Identification::findOrFail($id_identification);
|
||
|
if ($identification->status == 1) {
|
||
|
return $this->errorResponse(trans('messages.identification_already_validated'));
|
||
|
}
|
||
|
// dd($request->allFiles());
|
||
|
$this->validate($request, [
|
||
|
'id_network' => 'required|integer|min:0|not_in:0',
|
||
|
'document_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6048',
|
||
|
'user_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6048',
|
||
|
]);
|
||
|
|
||
|
$identification->idNetwork = $request->id_network;
|
||
|
$identification->document_image = $this->uploadImage($request,'document_image','D',"documents");
|
||
|
$identification->user_image = $this->uploadImage($request,'user_image','U',"photos");
|
||
|
$identification->status = 1;
|
||
|
|
||
|
$identification->save();
|
||
|
$this->sendMail($identification->user->email,trans('messages.validated_identification'),trans('messages.validated_identification_message'));
|
||
|
return $this->successResponse(trans('messages.validated_identification'));
|
||
|
}
|
||
|
|
||
|
private function generateRandomString($length = 10)
|
||
|
{
|
||
|
$characters = '23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ';
|
||
|
$charactersLength = strlen($characters);
|
||
|
$randomString = '';
|
||
|
for ($i = 0; $i < $length; $i++) {
|
||
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||
|
}
|
||
|
return $randomString;
|
||
|
}
|
||
|
|
||
|
public function uploadImage(Request $request , $key , $imageCode, $folderName)
|
||
|
{
|
||
|
|
||
|
// if ($request->hasFile('image')) {
|
||
|
$original_filename = $request->file($key)->getClientOriginalName();
|
||
|
$original_filename_arr = explode('.', $original_filename);
|
||
|
$file_ext = end($original_filename_arr);
|
||
|
$image = $imageCode.'-' . time() . '.' . $file_ext;
|
||
|
|
||
|
//Check if the directory already exists.
|
||
|
$directoryName = './'.$folderName;
|
||
|
if(!is_dir($directoryName)){
|
||
|
//Directory does not exist, so lets create it.
|
||
|
mkdir($directoryName, 0755);
|
||
|
}
|
||
|
|
||
|
// Allow certain file formats
|
||
|
// $allowTypes = array('jpg', 'png', 'jpeg', 'gif');
|
||
|
// if (in_array(strtolower($file_ext), $allowTypes)) {
|
||
|
|
||
|
$compressedImage = $this->compressImage($request->file($key), './'.$folderName.'/' . $image, 70);
|
||
|
if ($compressedImage) {
|
||
|
return $image;
|
||
|
} else {
|
||
|
return $this->errorResponse(trans('errors.compression_failed'));
|
||
|
}
|
||
|
// } else {
|
||
|
// return $this->errorResponse('Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.');
|
||
|
// }
|
||
|
|
||
|
// } else {
|
||
|
// return $this->errorResponse('File not found');
|
||
|
// }
|
||
|
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Custom function to compress image size and
|
||
|
* upload to the server using PHP
|
||
|
*/
|
||
|
private function compressImage($source, $destination, $quality)
|
||
|
{
|
||
|
// Get image info
|
||
|
$imgInfo = getimagesize($source);
|
||
|
$mime = $imgInfo['mime'];
|
||
|
|
||
|
// Create a new image from file
|
||
|
switch ($mime) {
|
||
|
case 'image/jpeg':
|
||
|
$image = imagecreatefromjpeg($source);
|
||
|
break;
|
||
|
case 'image/png':
|
||
|
$image = imagecreatefrompng($source);
|
||
|
break;
|
||
|
case 'image/gif':
|
||
|
$image = imagecreatefromgif($source);
|
||
|
break;
|
||
|
default:
|
||
|
$image = imagecreatefromjpeg($source);
|
||
|
}
|
||
|
|
||
|
// Save image
|
||
|
imagejpeg($image, $destination, $quality);
|
||
|
|
||
|
// Return compressed image
|
||
|
return $destination;
|
||
|
}
|
||
|
|
||
|
private function sendMail($email , $title , $messageText){
|
||
|
|
||
|
$recipients = [$email];
|
||
|
Mail::mailer('smtp')->raw($messageText, function ($message) use ($recipients,$title) {
|
||
|
$message->subject($title);
|
||
|
$message->to($recipients);
|
||
|
});
|
||
|
// return $this->successResponse("mail envoye");
|
||
|
}
|
||
|
}
|