FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AdminLicenseAcknowledgements.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
13 namespace Fossology\UI\Page;
14 
18 use Symfony\Component\HttpFoundation\JsonResponse;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
21 
27 {
28 
33  const NAME = "admin_license_acknowledgements";
34 
39  const ACKNOWLEDGEMENT_UPDATE_PARAM_NAME = "formUpdated";
40 
45  const ACKNOWLEDGEMENT_PARAM_NAME = "licenseAcknowledgement";
46 
51  const ACKNOWLEDGEMENT_ID_PARAM_NAME = "licenseAcknowledgementLaPK";
52 
57  const ACKNOWLEDGEMENT_NAME_PARAM_NAME = "licenseAcknowledgementName";
58 
63  const INSERT_ACKNOWLEDGEMENT_NAME_PARAM = "insertLicNames";
64 
69  const ACKNOWLEDGEMENT_INSERT_PARAM_NAME = "insertLicAcknowledgements";
70 
75  const ACKNOWLEDGEMENT_ENABLE_PARAM_NAME = "licAcknowledgementEnabled";
76 
82 
83  function __construct()
84  {
85  parent::__construct(self::NAME,
86  array(
87  self::TITLE => "Admin License Acknowledgements",
88  self::MENU_LIST => "Admin::License Admin::Acknowledgements",
89  self::REQUIRES_LOGIN => true,
90  self::PERMISSION => Auth::PERM_ADMIN
91  ));
92  $this->licenseAcknowledgementDao = $this->getObject('dao.license.acknowledgement');
93  }
94 
99  protected function handle(Request $request)
100  {
101  if ($request->get(self::ACKNOWLEDGEMENT_UPDATE_PARAM_NAME, 0) == 1) {
102  return new JsonResponse($this->updateAcknowledgements($request),
103  JsonResponse::HTTP_OK);
104  }
105 
106  $vars = [];
107  $vars["updateParam"] = self::ACKNOWLEDGEMENT_UPDATE_PARAM_NAME;
108  $vars["acknowledgementParam"] = self::ACKNOWLEDGEMENT_PARAM_NAME;
109  $vars["acknowledgementIdParam"] = self::ACKNOWLEDGEMENT_ID_PARAM_NAME;
110  $vars["acknowledgementNameParam"] = self::ACKNOWLEDGEMENT_NAME_PARAM_NAME;
111  $vars["enableParam"] = self::ACKNOWLEDGEMENT_ENABLE_PARAM_NAME;
112 
113  $vars['acknowledgementArray'] = $this->licenseAcknowledgementDao->getAllAcknowledgements();
114  return $this->render('admin_license_acknowledgements.html.twig',
115  $this->mergeWithDefault($vars));
116  }
117 
125  private function updateAcknowledgements(Request $request)
126  {
127  $acknowledgements = [];
128  $update = [
129  "updated" => -1,
130  "inserted" => []
131  ];
132  $acknowledgementStrings = $request->get(self::ACKNOWLEDGEMENT_PARAM_NAME);
133  $acknowledgementNames = $request->get(self::ACKNOWLEDGEMENT_NAME_PARAM_NAME);
134  $insertNames = $request->get(self::INSERT_ACKNOWLEDGEMENT_NAME_PARAM);
135  $insertAcknowledgements = $request->get(self::ACKNOWLEDGEMENT_INSERT_PARAM_NAME);
136  if ($acknowledgementStrings !== null && !empty($acknowledgementStrings)) {
137  foreach ($acknowledgementStrings as $acknowledgementPk => $acknowledgement) {
138  $acknowledgements[$acknowledgementPk]['acknowledgement'] = $acknowledgement;
139  }
140  }
141  if ($acknowledgementNames !== null && !empty($acknowledgementNames)) {
142  foreach ($acknowledgementNames as $acknowledgementPk => $name) {
143  $acknowledgements[$acknowledgementPk]['name'] = $name;
144  }
145  }
146  if (! empty($acknowledgements)) {
147  try {
148  $update['updated'] = $this->licenseAcknowledgementDao->updateAcknowledgementFromArray(
149  $acknowledgements);
150  } catch (\UnexpectedValueException $e) {
151  $update['updated'] = $e->getMessage();
152  }
153  }
154  $update["inserted"] = $this->insertAcknowledgements($insertNames, $insertAcknowledgements);
155  return $update;
156  }
157 
165  private function insertAcknowledgements($namesArray, $acknowledgementsArray)
166  {
167  $returnVal = [];
168  if (($namesArray !== null && $acknowledgementsArray !== null) &&
169  (! empty($namesArray) && !empty($acknowledgementsArray))) {
170  for ($i = 0; $i < count($namesArray); $i++) {
171  $returnVal[] = $this->licenseAcknowledgementDao->insertAcknowledgement($namesArray[$i],
172  $acknowledgementsArray[$i]);
173  }
174  $returnVal['status'] = 0;
175  // Check if at least one value was inserted
176  if (count(array_filter($returnVal, function($val) {
177  return $val > 0; // No error
178  })) > 0) {
179  $returnVal['status'] |= 1;
180  }
181  // Check if an error occurred while insertion
182  if (in_array(-1, $returnVal)) {
183  $returnVal['status'] |= 1 << 1;
184  }
185  // Check if an exception occurred while insertion
186  if (in_array(-2, $returnVal)) {
187  $returnVal['status'] |= 1 << 2;
188  }
189  }
190  return $returnVal;
191  }
192 }
193 
194 register_plugin(new AdminLicenseAcknowledgements());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
render($templateName, $vars=null, $headers=null)
insertAcknowledgements($namesArray, $acknowledgementsArray)