9 namespace Fossology\UI\Page;
15 use Symfony\Component\HttpFoundation\File\UploadedFile;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
24 const NAME =
"admin_obligation_from_csv";
25 const KEY_UPLOAD_MAX_FILESIZE =
'upload_max_filesize';
26 const FILE_INPUT_NAME =
'file_input';
27 const FILE_INPUT_NAME_V2 =
'fileInput';
29 function __construct()
31 parent::__construct(self::NAME, array(
32 self::TITLE =>
"Admin Obligation Import",
33 self::MENU_LIST =>
"Admin::Obligation Admin::Obligation Import",
34 self::REQUIRES_LOGIN =>
true,
43 protected function handle (Request $request)
47 if ($request->isMethod(
'POST')) {
48 $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
49 $delimiter = $request->get(
'delimiter')?:
',';
50 $enclosure = $request->get(
'enclosure')?:
'"';
51 $vars[
'message'] = $this->handleFileUpload($uploadFile,$delimiter,$enclosure);
54 $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
55 $vars[
'baseUrl'] = $request->getBaseUrl();
56 $vars[
'license_csv_import'] =
false;
58 return $this->
render(
"admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
65 public function handleFileUpload($uploadedFile,$delimiter=
',',$enclosure=
'"', $fromRest=
false)
68 if (! ($uploadedFile instanceof UploadedFile)) {
69 $errMsg = _(
"No file selected");
70 } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
71 $errMsg = _(
"Larger than upload_max_filesize ") . ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
72 } elseif ($uploadedFile->getClientOriginalExtension() !=
'csv'
73 && $uploadedFile->getClientOriginalExtension() !=
'json') {
74 $errMsg = _(
'Invalid extension ') .
75 $uploadedFile->getClientOriginalExtension() .
' of file ' .
76 $uploadedFile->getClientOriginalName();
78 if (! empty($errMsg)) {
80 return array(
false, $errMsg, 400);
85 $obligationCsvImport = $this->
getObject(
'app.obligation_csv_import');
86 $obligationCsvImport->setDelimiter($delimiter);
87 $obligationCsvImport->setEnclosure($enclosure);
89 $res = $obligationCsvImport->handleFile($uploadedFile->getRealPath(), $uploadedFile->getClientOriginalExtension());
91 return array(
true, $res, 200);
101 if ($apiVersion == ApiVersion::V2) {
102 return $this::FILE_INPUT_NAME_V2;
104 return $this::FILE_INPUT_NAME;
109 register_plugin(
new AdminObligationFromCSV());
Import licenses from CSV.
Contains the constants and helpers for authentication of user.
render($templateName, $vars=null, $headers=null)
Upload a file from the users computer using the UI.
getFileInputName($apiVersion=ApiVersion::V1)