182 lines
5.3 KiB
PHP
182 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\CAPI;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
use Doctrine\ORM\Query;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
|
|
|
use Catalyst\ApiBundle\Controller\ApiController;
|
|
use Catalyst\ApiBundle\Response\APIResponse;
|
|
|
|
use App\Entity\WarrantySerialQueue;
|
|
|
|
use App\Service\WarrantySerialUploadLogger;
|
|
|
|
use DateTime;
|
|
|
|
use Catalyst\AuthBundle\Service\ACLGenerator as ACLGenerator;
|
|
|
|
// third party API
|
|
class WarrantySerialController extends APIController
|
|
{
|
|
protected $acl_gen;
|
|
protected $upload_logger;
|
|
|
|
public function __construct(ACLGenerator $acl_gen, WarrantySerialUploadLogger $upload_logger)
|
|
{
|
|
$this->acl_gen = $acl_gen;
|
|
$this->upload_logger = $upload_logger;
|
|
}
|
|
|
|
public function uploadWarrantySerialFile(Request $req, EntityManagerInterface $em, KernelInterface $kernel)
|
|
{
|
|
$this->denyAccessUnlessGranted('warrantyserial.upload', null, 'No access.');
|
|
|
|
$required_params = [
|
|
'serial_file',
|
|
];
|
|
|
|
$user_id = $_SERVER['HTTP_X_CATA_API_KEY'];
|
|
|
|
$res = $this->checkRequiredParamsForFiles($req, $required_params, $user_id);
|
|
|
|
if ($res !== true)
|
|
return $res;
|
|
|
|
// get the csv file
|
|
$csv_file = $req->files->get('serial_file');
|
|
|
|
// process file upload
|
|
$upload_dir = $kernel->getProjectDir() . '/public/warranty_serial_uploads';
|
|
$serial_filename = $this->handleSerialFileUpload($csv_file, $upload_dir);
|
|
|
|
// insert to warranty serial queue
|
|
$res = $this->registerWarrantySerialFile($em, $csv_file, $serial_filename, $user_id);
|
|
|
|
// flush to db
|
|
$em->flush();
|
|
|
|
return $res;
|
|
}
|
|
|
|
protected function registerWarrantySerialFile($em, $file, $serial_filename, $user_id)
|
|
{
|
|
// parse the serial filename to get the file id
|
|
$parts = explode('/', $serial_filename);
|
|
$file_id = $parts[0];
|
|
$orig_filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) . '.' . $file->getClientOriginalExtension();
|
|
|
|
$ws_file = new WarrantySerialQueue();
|
|
|
|
$ws_file->setFileSerial($serial_filename)
|
|
->setStatus('pending')
|
|
->setOrigFileSerial($orig_filename)
|
|
->setFileID($file_id)
|
|
->setApiUser($user_id);
|
|
|
|
$em->persist($ws_file);
|
|
|
|
// log upload
|
|
$log_data = [
|
|
'user_id' => $user_id,
|
|
'is_uploaded' => true,
|
|
'orig_file_serial' => $orig_filename,
|
|
'uploaded_file_serial' => $serial_filename,
|
|
];
|
|
|
|
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
|
|
|
|
$data = [
|
|
'id' => $file_id,
|
|
];
|
|
|
|
return new APIResponse(true, 'Warranty serial file uploaded.', $data);
|
|
}
|
|
|
|
protected function handleSerialFileUpload($file, $target_dir)
|
|
{
|
|
// create target dir if it doesn't exist
|
|
if (!file_exists($target_dir))
|
|
{
|
|
if (!mkdir($target_dir, 0744, true))
|
|
{
|
|
$log_data = [
|
|
'user_id' => $user_id,
|
|
'is_uploaded' => false,
|
|
'error' => 'Failed to create folder for warranty serial files.'
|
|
];
|
|
|
|
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// get current date
|
|
$curr_date = new DateTime();
|
|
$str_curr_date = $curr_date->format('Ymd');
|
|
$file_id = $str_curr_date . uniqid();
|
|
|
|
// move file
|
|
$filename = 'warranty_serial' . '.' . $file->getClientOriginalExtension();
|
|
$file->move($target_dir . '/' . $file_id, $filename);
|
|
|
|
return $file_id . '/' . $filename;
|
|
}
|
|
|
|
protected function checkRequiredParamsForFiles(Request $req, $params, $user_id)
|
|
{
|
|
// check required parameters
|
|
$missing = $this->checkMissingParametersForFiles($req, $params);
|
|
if (count($missing) > 0)
|
|
{
|
|
// log the error
|
|
$miss_string = implode(', ', $missing);
|
|
$log_data = [
|
|
'user_id' => $user_id,
|
|
'is_uploaded' => false,
|
|
'error' => 'Missing parameter(s): ' . $miss_string
|
|
];
|
|
|
|
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
|
|
|
|
return new APIResponse(false, 'Missing parameter(s): ' . $miss_string);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function checkMissingParametersForFiles(Request $req, $params = [])
|
|
{
|
|
$missing = [];
|
|
|
|
// check if parameters are there
|
|
foreach ($params as $param)
|
|
{
|
|
if ($req->getMethod() == 'GET')
|
|
{
|
|
$check = $req->query->get($param);
|
|
if (empty($check))
|
|
$missing[] = $param;
|
|
}
|
|
else if ($req->getMethod() == 'POST')
|
|
{
|
|
// get files from request.
|
|
$check = $req->files->get($param);
|
|
if (empty($check))
|
|
{
|
|
$missing[] = $param;
|
|
}
|
|
}
|
|
else
|
|
return $params;
|
|
}
|
|
|
|
return $missing;
|
|
}
|
|
}
|