FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AdminLicenseStdComments.php
Go to the documentation of this file.
1 <?php
27 namespace Fossology\UI\Page;
28 
35 
41 {
42 
47  const NAME = "admin_license_std_comments";
48 
53  const UPDATE_PARAM_NAME = "formUpdated";
54 
59  const COMMENT_PARAM_NAME = "licenseStdComment";
60 
65  const COMMENT_ID_PARAM_NAME = "licenseCommentLscPK";
66 
71  const COMMENT_NAME_PARAM_NAME = "licenseCommentName";
72 
77  const INSERT_NAME_PARAM_NAME = "insertStdLicNames";
78 
83  const INSERT_COMMENT_PARAM_NAME = "insertStdLicComments";
84 
89  const ENABLE_PARAM_NAME = "stdLicCommentEnabled";
90 
96 
97  function __construct()
98  {
99  parent::__construct(self::NAME,
100  array(
101  self::TITLE => "Admin Standard License Comments",
102  self::MENU_LIST => "Admin::License Admin::Standard Comments",
103  self::REQUIRES_LOGIN => true,
104  self::PERMISSION => Auth::PERM_ADMIN
105  ));
106  $this->licenseCommentDao = $this->getObject('dao.license.stdc');
107  }
108 
113  protected function handle(Request $request)
114  {
115  if ($request->get(self::UPDATE_PARAM_NAME, 0) == 1) {
116  return new JsonResponse($this->updateComments($request),
117  JsonResponse::HTTP_OK);
118  }
119 
120  $vars = [];
121  $vars["updateParam"] = self::UPDATE_PARAM_NAME;
122  $vars["commentParam"] = self::COMMENT_PARAM_NAME;
123  $vars["commentIdParam"] = self::COMMENT_ID_PARAM_NAME;
124  $vars["commentNameParam"] = self::COMMENT_NAME_PARAM_NAME;
125  $vars["enableParam"] = self::ENABLE_PARAM_NAME;
126 
127  $vars['commentArray'] = $this->licenseCommentDao->getAllComments();
128  return $this->render('admin_license_std_comments.html.twig',
129  $this->mergeWithDefault($vars));
130  }
131 
139  private function updateComments(Request $request)
140  {
141  $comments = [];
142  $update = [
143  "updated" => -1,
144  "inserted" => []
145  ];
146  $commentStrings = $request->get(self::COMMENT_PARAM_NAME);
147  $commentNames = $request->get(self::COMMENT_NAME_PARAM_NAME);
148  $insertNames = $request->get(self::INSERT_NAME_PARAM_NAME);
149  $insertComments = $request->get(self::INSERT_COMMENT_PARAM_NAME);
150  if ($commentStrings !== null && !empty($commentStrings)) {
151  foreach ($commentStrings as $commentPk => $comment) {
152  $comments[$commentPk]['comment'] = $comment;
153  }
154  }
155  if ($commentNames !== null && !empty($commentNames)) {
156  foreach ($commentNames as $commentPk => $name) {
157  $comments[$commentPk]['name'] = $name;
158  }
159  }
160  if (! empty($comments)) {
161  try {
162  $update['updated'] = $this->licenseCommentDao->updateCommentFromArray(
163  $comments);
164  } catch (\UnexpectedValueException $e) {
165  $update['updated'] = $e->getMessage();
166  }
167  }
168  $update["inserted"] = $this->insertComments($insertNames, $insertComments);
169  return $update;
170  }
171 
179  private function insertComments($namesArray, $commentsArray)
180  {
181  $returnVal = [];
182  if (($namesArray !== null && $commentsArray !== null) &&
183  (! empty($namesArray) && !empty($commentsArray))) {
184  for ($i = 0; $i < count($namesArray); $i++) {
185  $returnVal[] = $this->licenseCommentDao->insertComment($namesArray[$i],
186  $commentsArray[$i]);
187  }
188  $returnVal['status'] = 0;
189  // Check if at least one value was inserted
190  if (count(array_filter($returnVal, function($val) {
191  return $val > 0; // No error
192  })) > 0) {
193  $returnVal['status'] |= 1;
194  }
195  // Check if an error occurred while insertion
196  if (in_array(-1, $returnVal)) {
197  $returnVal['status'] |= 1 << 1;
198  }
199  // Check if an exception occurred while insertion
200  if (in_array(-2, $returnVal)) {
201  $returnVal['status'] |= 1 << 2;
202  }
203  }
204  return $returnVal;
205  }
206 }
207 
208 register_plugin(new AdminLicenseStdComments());
render($templateName, $vars=null, $headers=null)