9 namespace Fossology\UI\Page;
14 use Symfony\Component\HttpFoundation\File\UploadedFile;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
23 const NAME =
"admin_custom_text_import";
24 const KEY_UPLOAD_MAX_FILESIZE =
'upload_max_filesize';
25 const FILE_INPUT_NAME =
'file_input';
26 const MAX_IMPORT_BYTES = 5242880;
29 protected $customTextImport;
31 function __construct()
33 parent::__construct(self::NAME, array(
34 self::TITLE =>
"Import Custom Text (CSV/JSON)",
35 self::MENU_LIST =>
"Admin::Text Management::Import::CSV/JSON Import",
36 self::REQUIRES_LOGIN =>
true,
40 $this->customTextImport = $GLOBALS[
'container']->get(
'app.custom_text_import');
47 protected function handle(Request $request)
51 if ($request->isMethod(
'POST')) {
52 $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
53 $delimiter = $request->get(
'delimiter') ?:
',';
54 $enclosure = $request->get(
'enclosure') ?:
'"';
55 $vars[
'message'] = $this->
handleFileUpload($uploadFile, $delimiter, $enclosure)[1];
58 $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
59 $vars[
'baseUrl'] = $request->getBaseUrl();
60 $vars[
'custom_text_import'] =
true;
62 return $this->
render(
"admin_custom_text_import.html.twig", $this->mergeWithDefault($vars));
72 if (!($uploadedFile instanceof UploadedFile)) {
73 $errMsg = _(
"No file selected");
74 } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
75 $errMsg = $uploadedFile->getErrorMessage();
76 } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
77 $errMsg = _(
"Larger than upload_max_filesize ") .
78 ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
79 } elseif ($uploadedFile->getSize() > self::MAX_IMPORT_BYTES) {
80 $errMsg = _(
"File exceeds maximum allowed size of 5 MB");
81 } elseif ($uploadedFile->getClientOriginalExtension() !=
'csv'
82 && $uploadedFile->getClientOriginalExtension() !=
'json') {
83 $errMsg = _(
'Invalid file extension ') .
84 $uploadedFile->getClientOriginalExtension() .
' of file ' .
85 $uploadedFile->getClientOriginalName();
87 if (!empty($errMsg)) {
88 return array(
false, $errMsg, 400);
90 $this->customTextImport->setDelimiter($delimiter);
91 $this->customTextImport->setEnclosure($enclosure);
93 return array(
true, $this->customTextImport->handleFile($uploadedFile->getRealPath(), $uploadedFile->getClientOriginalExtension()), 200);
97 register_plugin(
new AdminCustomTextImport());
Import custom text phrases from CSV/JSON.
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.
handleFileUpload($uploadedFile, $delimiter=',', $enclosure='"')