ldc-com-app-bo/app/Http/Controllers/API/SyncController.php
Vincent BRIDIER aed37bfad3
phpcbf
2022-09-29 10:09:43 +02:00

217 lines
8 KiB
PHP

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Mail\SendFlyerFailed;
use App\Models\AppUser;
use App\Models\Contacts;
use App\Models\Setting;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use App\Mail\SendFlyer;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
class SyncController extends Controller
{
public function getUsers(Request $request)
{
return new \App\Http\Resources\AppUserCollection(\App\Models\AppUser::where('activated', 1)->get());
}
public function getContacts(Request $request)
{
$userId = $request->userId;
$this->syncContacts($request->contacts, $userId);
return new \App\Http\Resources\ContactsCollection(\App\Models\Contacts::where('user_id', $userId)->get());
}
public function getDirectories(Request $request)
{
return new \App\Http\Resources\DirectoriesCollection(\App\Models\Directories::all());
}
public function getDocuments(Request $request)
{
return new \App\Http\Resources\DocumentsCollection(\App\Models\Document::where('published', 1)->get());
}
public function getVersion(Request $request)
{
$app_uuid = $request->app_uuid;
$app_version = $request->app_version;
$version = Setting::select('value AS version')->where('key', 'version')->first();
$ShowIsActive = Setting::select('value AS ShowIsActive')->where('key', 'ShowIsActive')->first();
try {
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://store.ldc.fr/api/get-version?token=@P$oi6or!bL2-r%3C&app_uuid=' . $app_uuid);
// echo $response->getStatusCode();
// echo $response->getHeaderLine('content-type');
$body = $response->getBody();
$storeVersion = json_decode($body)->version;
} catch (ClientException $e) {
// var_dump($e->getRequest());
// var_dump($e->getResponse()->getStatusCode());
$storeVersion = $app_version;
}
if ($storeVersion > $app_version) {
$newApp = true;
} else {
$newApp = false;
}
return ['version' => $version->version, 'ShowIsActive' => $ShowIsActive->ShowIsActive, 'newApp' => $newApp];
}
public function SendFlyer(Request $request)
{
$emails = json_decode($request->emails);
foreach ($emails as $datas) {
$email = $datas->email;
$subject = $datas->subject;
$message = $datas->message;
$user_id = $datas->user_id;
$docs = json_decode($datas->documents);
$user = AppUser::find($user_id);
$attach = [];
$filesName = [];
$noExists = [];
foreach ($docs as $doc) {
$exists = Storage::disk('public')->exists('docs/' . $doc->uuid . '.' . $doc->type);
if ($exists) {
array_push($attach, URL::temporarySignedRoute(
'docs.download',
now()->addDays(30),
['uuid' => $doc->uuid]
));
array_push($filesName, $doc->title);
} else {
array_push($noExists, $doc->title);
}
}
if (count($noExists) === 0) {
$message = new SendFlyer([
'body' => $message,
'from' => $user->email,
'fromName' => $user->surname . ' ' . $user->firstname,
'subject' => '[' . config('app.BRAND') . '] - ' . $subject,
'attach' => $attach,
'filesName' => $filesName,
]);
Mail::to($email)
->cc($user->email)
->queue($message);
} else {
$message = new SendFlyerFailed([
'email' => $email,
'message' => $message,
'subject' => $subject,
'filesName' => $filesName,
'noExists' => $noExists
]);
Mail::to($user->email)
->queue($message);
}//end if
}//end foreach
return 'OK';
}
private function syncContacts($contacts, $userId)
{
$contacts = json_decode(json_encode($contacts), false);
foreach ($contacts as $contact) {
if ($contact->to_delete === true) {
$tmp = Contacts::where([
['id', '=', $contact->uniq_id],
['user_id', '=', $contact->user_id]
])->first();
if ($tmp) {
$tmp->delete();
}
} else {
if ($contact->user_id != $userId) {
$tmp = Contacts::where([
['id', '=', $contact->uniq_id],
['user_id', '=', $userId]
])->first();
if ($tmp) {
$tmp->delete();
}
}
$tmp = Contacts::where([
['id', '=', $contact->uniq_id],
['user_id', '=', $contact->user_id]
])->first();
if ($tmp) {
$tmp->email = $contact->email;
$tmp->type = $contact->type;
$tmp->other = $contact->other;
$tmp->select1 = $contact->select1;
$tmp->select2 = $contact->select2;
$tmp->name = $contact->name;
$tmp->firstname = $contact->firstname;
$tmp->company = $contact->company;
$tmp->address = $contact->address;
$tmp->phone = empty($contact->phone) ? "" : $contact->phone;
$tmp->products = $contact->products;
$tmp->notes = $contact->notes;
$tmp->visits = $contact->visits;
$tmp->user_id = $contact->user_id;
$tmp->save();
} else {
$tmp = new Contacts();
$tmp->id = $contact->uniq_id;
$tmp->email = $contact->email;
$tmp->type = $contact->type;
$tmp->other = $contact->other;
$tmp->select1 = $contact->select1;
$tmp->select2 = $contact->select2;
$tmp->name = $contact->name;
$tmp->firstname = $contact->firstname;
$tmp->company = $contact->company;
$tmp->address = $contact->address;
$tmp->phone = empty($contact->phone) ? "" : $contact->phone;
$tmp->products = $contact->products;
$tmp->notes = $contact->notes;
$tmp->visits = $contact->visits;
$tmp->user_id = $contact->user_id;
$tmp->save();
}//end if
}//end if
}//end foreach
}
public function processOCR(Request $request)
{
// instantiates a client
$imageAnnotator = new ImageAnnotatorClient([
'credentials' => public_path() . '/credentials.json'
]);
// the name of the image file to annotate
$fileName = 'test.jpg';
// prepare the image to be annotated
$image = file_get_contents(public_path() . "/" . $fileName);
// performs label detection on the image file
$response = $imageAnnotator->textDetection($image);
$labels = $response->getTextAnnotations();
foreach ($labels as $label) {
echo $label->getDescription() . "<br>";
}
}
}