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 
24 use Psr\Http\Message\ServerRequestInterface;
25 use Slim\Psr7\Factory\StreamFactory;
26 
28 {
33  private $obligationMap;
34 
35  public function __construct($container)
36  {
37  parent::__construct($container);
38  $this->obligationMap = $this->container->get('businessrules.obligationmap');
39  }
40 
49  function obligationsList($request, $response, $args)
50  {
51  $finVal = [];
52  $listVal = $this->obligationMap->getObligations();
53  foreach ($listVal as $val) {
54  $row['id'] = intval($val['ob_pk']);
55  $row['obligation_topic'] = $val['ob_topic'];
56  $finVal[] = $row;
57  }
58  return $response->withJson($finVal, 200);
59  }
60 
70  function obligationsDetails($request, $response, $args)
71  {
72  $obligationId = intval($args['id']);
73  if (!$this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obligationId)) {
74  throw new HttpNotFoundException("Obligation does not exist");
75  }
76 
77  $obligation = $this->createExtendedObligationFromId($obligationId);
78  return $response->withJson($obligation->getArray(), 200);
79  }
80 
89  function obligationsAllDetails($request, $response, $args)
90  {
91  $obligationArray = [];
92  $listVal = $this->obligationMap->getObligations();
93  foreach ($listVal as $val) {
94  $obligationId = intval($val['ob_pk']);
95  $obligationArray[] = $this->createExtendedObligationFromId($obligationId)
96  ->getArray();
97  }
98  return $response->withJson($obligationArray, 200);
99  }
100 
107  private function createExtendedObligationFromId($obligationId)
108  {
109  $obligationInfo = $this->obligationMap->getObligationById($obligationId);
110  $licenses = $this->obligationMap->getLicenseList($obligationId);
111  $candidateLicenses = $this->obligationMap->getLicenseList($obligationId,
112  true);
113  $associatedLicenses = explode(";", $licenses);
114  $associatedCandidateLicenses = explode(";", $candidateLicenses);
115 
116  return Obligation::fromArray($obligationInfo, true,
117  $associatedLicenses, $associatedCandidateLicenses);
118  }
119 
129  function deleteObligation($request, $response, $args)
130  {
131  $obligationId = intval($args['id']);
132  if (!$this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obligationId)) {
133  throw new HttpNotFoundException("Obligation does not exist");
134  }
135  $this->obligationMap->deleteObligation($obligationId);
136  $returnVal = new Info(200, "Successfully removed Obligation.", InfoType::INFO);
137  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
138  }
139 
149  public function importObligationsFromCSV($request, $response, $args)
150  {
151  $this->throwNotAdminException();
152 
153  $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
155  $adminLicenseObligationFromCsv = $this->restHelper->getPlugin('admin_obligation_from_csv');
156 
157  $uploadedFile = $symReq->files->get($adminLicenseObligationFromCsv->getFileInputName(),
158  null);
159 
160  $requestBody = $this->getParsedBody($request);
161  $delimiter = ',';
162  $enclosure = '"';
163  if (array_key_exists("delimiter", $requestBody) && !empty($requestBody["delimiter"])) {
164  $delimiter = $requestBody["delimiter"];
165  }
166  if (array_key_exists("enclosure", $requestBody) && !empty($requestBody["enclosure"])) {
167  $enclosure = $requestBody["enclosure"];
168  }
169 
170  $res = $adminLicenseObligationFromCsv->handleFileUpload($uploadedFile, $delimiter, $enclosure, true);
171  if (!$res[0]) {
172  throw new HttpBadRequestException($res[1]);
173  }
174 
175  $newInfo = new Info($res[2], $res[1], InfoType::INFO);
176  return $response->withJson($newInfo->getArray(), $newInfo->getCode());
177  }
178 
188  public function exportObligationsToCSV($request, $response, $args)
189  {
190  $this->throwNotAdminException();
191  $query = $request->getQueryParams();
192  $obPk = 0;
193  if (array_key_exists('id', $query)) {
194  $obPk = intval($query['id']);
195  }
196  if ($obPk != 0 &&
197  ! $this->dbHelper->doesIdExist("obligation_ref", "ob_pk", $obPk)) {
198  throw new HttpNotFoundException("Obligation does not exist");
199  }
200 
201  $dbManager = $this->dbHelper->getDbManager();
202  $obligationCsvExport = new ObligationCsvExport($dbManager);
203  $content = $obligationCsvExport->createCsv($obPk);
204  $fileName = "fossology-obligations-export-".date("YMj-Gis");
205  $newResponse = $response->withHeader('Content-type', 'text/csv, charset=UTF-8')
206  ->withHeader('Content-Disposition', 'attachment; filename=' . $fileName . '.csv')
207  ->withHeader('Pragma', 'no-cache')
208  ->withHeader('Cache-Control', 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0')
209  ->withHeader('Expires', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
210  $sf = new StreamFactory();
211  return $newResponse->withBody(
212  $content ? $sf->createStream($content) : $sf->createStream('')
213  );
214  }
215 }
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.
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