FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AdminObligationFromCSV.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  * Copyright (C) 2014-2017 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
20 namespace Fossology\UI\Page;
21 
28 
33 {
34  const NAME = "admin_obligation_from_csv";
35  const KEY_UPLOAD_MAX_FILESIZE = 'upload_max_filesize';
36 
37  function __construct()
38  {
39  parent::__construct(self::NAME, array(
40  self::TITLE => "Admin Obligation CSV Import",
41  self::MENU_LIST => "Admin::Obligation Admin::CSV Import",
42  self::REQUIRES_LOGIN => true,
43  self::PERMISSION => Auth::PERM_ADMIN
44  ));
45  }
46 
51  protected function handle (Request $request)
52  {
53  $vars = array();
54 
55  if ($request->isMethod('POST')) {
56  $uploadFile = $request->files->get('file_input');
57  $delimiter = $request->get('delimiter')?:',';
58  $enclosure = $request->get('enclosure')?:'"';
59  $vars['message'] = $this->handleFileUpload($uploadFile,$delimiter,$enclosure);
60  }
61 
62  $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
63  $vars['baseUrl'] = $request->getBaseUrl();
64 
65  return $this->render("admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
66  }
67 
72  protected function handleFileUpload($uploadedFile,$delimiter=',',$enclosure='"')
73  {
74  $errMsg = '';
75  if (! ($uploadedFile instanceof UploadedFile)) {
76  $errMsg = _("No file selected");
77  } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
78  $errMsg = _("Larger than upload_max_filesize ") . ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
79  } elseif ($uploadedFile->getClientOriginalExtension()!='csv') {
80  $errMsg = _('Invalid extension ') .
81  $uploadedFile->getClientOriginalExtension() . ' of file ' .
82  $uploadedFile->getClientOriginalName();
83  }
84  if (! empty($errMsg)) {
85  return $errMsg;
86  }
88  $obligationCsvImport = $this->getObject('app.obligation_csv_import');
89  $obligationCsvImport->setDelimiter($delimiter);
90  $obligationCsvImport->setEnclosure($enclosure);
91  return $obligationCsvImport->handleFile($uploadedFile->getRealPath());
92  }
93 }
94 
95 register_plugin(new AdminObligationFromCSV());
render($templateName, $vars=null, $headers=null)
Upload a file from the users computer using the UI.