FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AdminLicenseStdComments.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 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_std_comments";
34 
39  const UPDATE_PARAM_NAME = "formUpdated";
40 
45  const COMMENT_PARAM_NAME = "licenseStdComment";
46 
51  const COMMENT_ID_PARAM_NAME = "licenseCommentLscPK";
52 
57  const COMMENT_NAME_PARAM_NAME = "licenseCommentName";
58 
63  const INSERT_NAME_PARAM_NAME = "insertStdLicNames";
64 
69  const INSERT_COMMENT_PARAM_NAME = "insertStdLicComments";
70 
75  const ENABLE_PARAM_NAME = "stdLicCommentEnabled";
76 
82 
83  function __construct()
84  {
85  parent::__construct(self::NAME,
86  array(
87  self::TITLE => "Admin Standard License Comments",
88  self::MENU_LIST => "Admin::License Admin::Standard Comments",
89  self::REQUIRES_LOGIN => true,
90  self::PERMISSION => Auth::PERM_ADMIN
91  ));
92  $this->licenseCommentDao = $this->getObject('dao.license.stdc');
93  }
94 
99  protected function handle(Request $request)
100  {
101  if ($request->get(self::UPDATE_PARAM_NAME, 0) == 1) {
102  return new JsonResponse($this->updateComments($request),
103  JsonResponse::HTTP_OK);
104  }
105 
106  $vars = [];
107  $vars["updateParam"] = self::UPDATE_PARAM_NAME;
108  $vars["commentParam"] = self::COMMENT_PARAM_NAME;
109  $vars["commentIdParam"] = self::COMMENT_ID_PARAM_NAME;
110  $vars["commentNameParam"] = self::COMMENT_NAME_PARAM_NAME;
111  $vars["enableParam"] = self::ENABLE_PARAM_NAME;
112 
113  $vars['commentArray'] = $this->licenseCommentDao->getAllComments();
114  return $this->render('admin_license_std_comments.html.twig',
115  $this->mergeWithDefault($vars));
116  }
117 
125  private function updateComments(Request $request)
126  {
127  $comments = [];
128  $update = [
129  "updated" => -1,
130  "inserted" => []
131  ];
132  $commentStrings = $request->get(self::COMMENT_PARAM_NAME);
133  $commentNames = $request->get(self::COMMENT_NAME_PARAM_NAME);
134  $insertNames = $request->get(self::INSERT_NAME_PARAM_NAME);
135  $insertComments = $request->get(self::INSERT_COMMENT_PARAM_NAME);
136  if ($commentStrings !== null && !empty($commentStrings)) {
137  foreach ($commentStrings as $commentPk => $comment) {
138  $comments[$commentPk]['comment'] = $comment;
139  }
140  }
141  if ($commentNames !== null && !empty($commentNames)) {
142  foreach ($commentNames as $commentPk => $name) {
143  $comments[$commentPk]['name'] = $name;
144  }
145  }
146  if (! empty($comments)) {
147  try {
148  $update['updated'] = $this->licenseCommentDao->updateCommentFromArray(
149  $comments);
150  } catch (\UnexpectedValueException $e) {
151  $update['updated'] = $e->getMessage();
152  }
153  }
154  $update["inserted"] = $this->insertComments($insertNames, $insertComments);
155  return $update;
156  }
157 
165  private function insertComments($namesArray, $commentsArray)
166  {
167  $returnVal = [];
168  if (($namesArray !== null && $commentsArray !== null) &&
169  (! empty($namesArray) && !empty($commentsArray))) {
170  for ($i = 0; $i < count($namesArray); $i++) {
171  $returnVal[] = $this->licenseCommentDao->insertComment($namesArray[$i],
172  $commentsArray[$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 AdminLicenseStdComments());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
render($templateName, $vars=null, $headers=null)