FOSSology  4.7.1
Open Source License Compliance by Open Source Software
ObligationController.php
Go to the documentation of this file.
1 <?php
2 /*
3  Author: Soham Banerjee <sohambanerjee4abc@hotmail.com>
4  SPDX-FileCopyrightText: © 2023 Soham Banerjee <sohambanerjee4abc@hotmail.com>
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 namespace Fossology\UI\Api\Controllers;
14 
25 use Psr\Http\Message\ServerRequestInterface;
26 use Slim\Psr7\Factory\StreamFactory;
27 
29 {
34  private $obligationMap;
35 
36  public function __construct($container)
37  {
38  parent::__construct($container);
39  $this->obligationMap = $this->container->get('businessrules.obligationmap');
40  }
41 
50  function obligationsList($request, $response, $args)
51  {
52  $apiVersion = ApiVersion::getVersion($request);
53  $finVal = [];
54  $listVal = $this->obligationMap->getObligations();
55  foreach ($listVal as $val) {
56  $row['id'] = intval($val['ob_pk']);
57  $row[$apiVersion == ApiVersion::V2 ? 'obligationTopic' : 'obligation_topic'] = $val['ob_topic'];
58  $finVal[] = $row;
59  }
60  return $response->withJson($finVal, 200);
61  }
62 
72  function obligationsDetails($request, $response, $args)
73  {
74  $obligationId = intval($args['id']);
75  if (!$this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obligationId)) {
76  throw new HttpNotFoundException("Obligation does not exist");
77  }
78 
79  $obligation = $this->createExtendedObligationFromId($obligationId);
80  return $response->withJson($obligation->getArray(), 200);
81  }
82 
91  function obligationsAllDetails($request, $response, $args)
92  {
93  $obligationArray = [];
94  $listVal = $this->obligationMap->getObligations();
95  foreach ($listVal as $val) {
96  $obligationId = intval($val['ob_pk']);
97  $obligationArray[] = $this->createExtendedObligationFromId($obligationId)
98  ->getArray();
99  }
100  return $response->withJson($obligationArray, 200);
101  }
102 
109  private function createExtendedObligationFromId($obligationId)
110  {
111  $obligationInfo = $this->obligationMap->getObligationById($obligationId);
112  $licenses = $this->obligationMap->getLicenseList($obligationId);
113  $candidateLicenses = $this->obligationMap->getLicenseList($obligationId,
114  true);
115  $associatedLicenses = explode(";", $licenses);
116  $associatedCandidateLicenses = explode(";", $candidateLicenses);
117 
118  return Obligation::fromArray($obligationInfo, true,
119  $associatedLicenses, $associatedCandidateLicenses);
120  }
121 
131  function deleteObligation($request, $response, $args)
132  {
133  $this->throwNotAdminException();
134  $obligationId = intval($args['id']);
135  if (!$this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obligationId)) {
136  throw new HttpNotFoundException("Obligation does not exist");
137  }
138  $this->obligationMap->deleteObligation($obligationId);
139  $returnVal = new Info(200, "Successfully removed Obligation.", InfoType::INFO);
140  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
141  }
142 
152  public function importObligationsFromCSV($request, $response, $args)
153  {
154  $this->throwNotAdminException();
155  $apiVersion = ApiVersion::getVersion($request);
156 
157  $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
159  $adminLicenseObligationFromCsv = $this->restHelper->getPlugin('admin_obligation_from_csv');
160 
161  $uploadedFile = $symReq->files->get($adminLicenseObligationFromCsv->getFileInputName($apiVersion),
162  null);
163 
164  $requestBody = $this->getParsedBody($request);
165  $delimiter = ',';
166  $enclosure = '"';
167  if (array_key_exists("delimiter", $requestBody) && !empty($requestBody["delimiter"])) {
168  $delimiter = $requestBody["delimiter"];
169  }
170  if (array_key_exists("enclosure", $requestBody) && !empty($requestBody["enclosure"])) {
171  $enclosure = $requestBody["enclosure"];
172  }
173 
174  $res = $adminLicenseObligationFromCsv->handleFileUpload($uploadedFile, $delimiter, $enclosure, true);
175  if (!$res[0]) {
176  throw new HttpBadRequestException($res[1]);
177  }
178 
179  $newInfo = new Info($res[2], $res[1], InfoType::INFO);
180  return $response->withJson($newInfo->getArray(), $newInfo->getCode());
181  }
182 
192  public function exportObligationsToCSV($request, $response, $args)
193  {
194  $this->throwNotAdminException();
195  $query = $request->getQueryParams();
196  $obPk = 0;
197  if (array_key_exists('id', $query)) {
198  $obPk = intval($query['id']);
199  }
200  if ($obPk != 0 &&
201  ! $this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obPk)) {
202  throw new HttpNotFoundException("Obligation does not exist");
203  }
204 
205  $dbManager = $this->dbHelper->getDbManager();
206  $obligationCsvExport = new ObligationCsvExport($dbManager);
207  $content = $obligationCsvExport->createCsv($obPk);
208  $fileName = "fossology-obligations-export-".date("YMj-Gis");
209  $newResponse = $response->withHeader('Content-type', 'text/csv, charset=UTF-8')
210  ->withHeader('Content-Disposition', 'attachment; filename=' . $fileName . '.csv')
211  ->withHeader('Pragma', 'no-cache')
212  ->withHeader('Cache-Control', 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0')
213  ->withHeader('Expires', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
214  $sf = new StreamFactory();
215  return $newResponse->withBody(
216  $content ? $sf->createStream($content) : $sf->createStream('')
217  );
218  }
219 
229  public function importObligationsFromJSON($request, $response, $args)
230  {
231  $this->throwNotAdminException();
232  $apiVersion = ApiVersion::getVersion($request);
233 
234  $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
236  $adminLicenseObligationFromCsv = $this->restHelper->getPlugin('admin_obligation_from_csv');
237 
238  $uploadedFile = $symReq->files->get($adminLicenseObligationFromCsv->getFileInputName($apiVersion),
239  null);
240 
241  $res = $adminLicenseObligationFromCsv->handleFileUpload($uploadedFile, ',', '"', true);
242  if (!$res[0]) {
243  throw new HttpBadRequestException($res[1]);
244  }
245 
246  $newInfo = new Info($res[2], $res[1], InfoType::INFO);
247  return $response->withJson($newInfo->getArray(), $newInfo->getCode());
248  }
258  public function exportObligationsToJSON($request, $response, $args)
259  {
260  $this->throwNotAdminException();
261  $query = $request->getQueryParams();
262  $obPk = 0;
263  if (array_key_exists('id', $query)) {
264  $obPk = intval($query['id']);
265  }
266  if ($obPk != 0 &&
267  ! $this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obPk)) {
268  throw new HttpNotFoundException("Obligation does not exist");
269  }
270 
271  $dbManager = $this->dbHelper->getDbManager();
272  $obligationCsvExport = new ObligationCsvExport($dbManager);
273  $content = $obligationCsvExport->createCsv($obPk, true);
274  $fileName = "fossology-obligations-export-".date("YMj-Gis");
275  $newResponse = $response->withHeader('Content-type', 'text/json; charset=UTF-8')
276  ->withHeader('Content-Disposition', 'attachment; filename=' . $fileName . '.json')
277  ->withHeader('Pragma', 'no-cache')
278  ->withHeader('Cache-Control', 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0')
279  ->withHeader('Expires', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
280  $sf = new StreamFactory();
281  return $newResponse->withBody(
282  $content ? $sf->createStream($content) : $sf->createStream('')
283  );
284  }
285 }
Helper class to export obligations as a CSV.
Wrapper class for obligation map.
Base controller for REST calls.
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
Definition: ApiVersion.php:29
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
static fromArray($db, $extended, $licenses, $candidateLicenses)
Definition: Obligation.php:392