ldc-com-app-bo/app/Http/Controllers/API/SyncController.php

210 lines
7.6 KiB
PHP
Raw Normal View History

2021-01-15 14:47:59 +01:00
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Mail\SendFlyerFailed;
use App\Models\AppUsers;
use App\Models\Contacts;
use App\Models\Data;
use Google\Type\Date;
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\AppUsersCollection(\App\Models\AppUsers::where('activated', 1)->get());
}
public function getContacts(Request $request) {
$userId = $request->userId;
$this->syncContacts(json_decode($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\Documents::where('published', 1)->get());
}
public function getVersion(Request $request) {
$app_uuid = $request->app_uuid;
$app_version = $request->app_version;
$version = Data::select('value AS version')->where('key', 'version')->first();
$ShowIsActive = Data::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 = AppUsers::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' => '[Espri Restauration] - '. $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);
}
}
return 'OK';
}
private function syncContacts($contacts, $userId) {
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;
2021-01-15 14:47:59 +01:00
$tmp->name = $contact->name;
$tmp->firstname = $contact->firstname;
$tmp->company = $contact->company;
$tmp->address = $contact->address;
$tmp->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;
2021-01-15 14:47:59 +01:00
$tmp->name = $contact->name;
$tmp->firstname = $contact->firstname;
$tmp->company = $contact->company;
$tmp->address = $contact->address;
$tmp->phone = $contact->phone;
$tmp->products = $contact->products;
$tmp->notes = $contact->notes;
$tmp->visits = $contact->visits;
$tmp->user_id = $contact->user_id;
$tmp->save();
}
}
}
}
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>";
}
}
}