FOSSology  4.4.0
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  $obligationId = intval($args['id']);
134  if (!$this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obligationId)) {
135  throw new HttpNotFoundException("Obligation does not exist");
136  }
137  $this->obligationMap->deleteObligation($obligationId);
138  $returnVal = new Info(200, "Successfully removed Obligation.", InfoType::INFO);
139  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
140  }
141 
151  public function importObligationsFromCSV($request, $response, $args)
152  {
153  $this->throwNotAdminException();
154  $apiVersion = ApiVersion::getVersion($request);
155 
156  $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
158  $adminLicenseObligationFromCsv = $this->restHelper->getPlugin('admin_obligation_from_csv');
159 
160  $uploadedFile = $symReq->files->get($adminLicenseObligationFromCsv->getFileInputName($apiVersion),
161  null);
162 
163  $requestBody = $this->getParsedBody($request);
164  $delimiter = ',';
165  $enclosure = '"';
166  if (array_key_exists("delimiter", $requestBody) && !empty($requestBody["delimiter"])) {
167  $delimiter = $requestBody["delimiter"];
168  }
169  if (array_key_exists("enclosure", $requestBody) && !empty($requestBody["enclosure"])) {
170  $enclosure = $requestBody["enclosure"];
171  }
172 
173  $res = $adminLicenseObligationFromCsv->handleFileUpload($uploadedFile, $delimiter, $enclosure, true);
174  if (!$res[0]) {
175  throw new HttpBadRequestException($res[1]);
176  }
177 
178  $newInfo = new Info($res[2], $res[1], InfoType::INFO);
179  return $response->withJson($newInfo->getArray(), $newInfo->getCode());
180  }
181 
191  public function exportObligationsToCSV($request, $response, $args)
192  {
193  $this->throwNotAdminException();
194  $query = $request->getQueryParams();
195  $obPk = 0;
196  if (array_key_exists('id', $query)) {
197  $obPk = intval($query['id']);
198  }
199  if ($obPk != 0 &&
200  ! $this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obPk)) {
201  throw new HttpNotFoundException("Obligation does not exist");
202  }
203 
204  $dbManager = $this->dbHelper->getDbManager();
205  $obligationCsvExport = new ObligationCsvExport($dbManager);
206  $content = $obligationCsvExport->createCsv($obPk);
207  $fileName = "fossology-obligations-export-".date("YMj-Gis");
208  $newResponse = $response->withHeader('Content-type', 'text/csv, charset=UTF-8')
209  ->withHeader('Content-Disposition', 'attachment; filename=' . $fileName . '.csv')
210  ->withHeader('Pragma', 'no-cache')
211  ->withHeader('Cache-Control', 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0')
212  ->withHeader('Expires', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
213  $sf = new StreamFactory();
214  return $newResponse->withBody(
215  $content ? $sf->createStream($content) : $sf->createStream('')
216  );
217  }
218 
228  public function importObligationsFromJSON($request, $response, $args)
229  {
230  $this->throwNotAdminException();
231  $apiVersion = ApiVersion::getVersion($request);
232 
233  $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
235  $adminLicenseObligationFromCsv = $this->restHelper->getPlugin('admin_obligation_from_csv');
236 
237  $uploadedFile = $symReq->files->get($adminLicenseObligationFromCsv->getFileInputName($apiVersion),
238  null);
239 
240  $res = $adminLicenseObligationFromCsv->handleFileUpload($uploadedFile, ',', '"', true);
241  if (!$res[0]) {
242  throw new HttpBadRequestException($res[1]);
243  }
244 
245  $newInfo = new Info($res[2], $res[1], InfoType::INFO);
246  return $response->withJson($newInfo->getArray(), $newInfo->getCode());
247  }
257  public function exportObligationsToJSON($request, $response, $args)
258  {
259  $this->throwNotAdminException();
260  $query = $request->getQueryParams();
261  $obPk = 0;
262  if (array_key_exists('id', $query)) {
263  $obPk = intval($query['id']);
264  }
265  if ($obPk != 0 &&
266  ! $this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obPk)) {
267  throw new HttpNotFoundException("Obligation does not exist");
268  }
269 
270  $dbManager = $this->dbHelper->getDbManager();
271  $obligationCsvExport = new ObligationCsvExport($dbManager);
272  $content = $obligationCsvExport->createCsv($obPk, true);
273  $fileName = "fossology-obligations-export-".date("YMj-Gis");
274  $newResponse = $response->withHeader('Content-type', 'text/json; charset=UTF-8')
275  ->withHeader('Content-Disposition', 'attachment; filename=' . $fileName . '.json')
276  ->withHeader('Pragma', 'no-cache')
277  ->withHeader('Cache-Control', 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0')
278  ->withHeader('Expires', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
279  $sf = new StreamFactory();
280  return $newResponse->withBody(
281  $content ? $sf->createStream($content) : $sf->createStream('')
282  );
283  }
284 }
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