9 namespace Fossology\UI\Page;
17 use Symfony\Component\HttpFoundation\JsonResponse;
18 use Symfony\Component\HttpFoundation\Request;
19 use Symfony\Component\HttpFoundation\Response;
20 use Symfony\Component\HttpFoundation\RedirectResponse;
24 const NAME =
"admin_custom_text_management";
26 function __construct()
28 parent::__construct(self::NAME, array(
29 self::TITLE =>
"Add Custom Text",
30 self::MENU_LIST =>
"Admin::Text Management::Add",
31 self::REQUIRES_LOGIN =>
true,
41 protected function handle(Request $request)
50 return $this->flushContent(_(
'Access denied. Admin privileges required.'));
53 $action = $request->get(
'action');
56 if ($action ==
'check_duplicate' && $request->getMethod() ==
'POST') {
61 if ($request->get(
'updateit') || $request->get(
'addit')) {
62 $result = $this->savePhrase($request, $userId, $groupId);
63 if (!$result[
'success']) {
65 $vars[
'message'] = $result[
'message'];
66 return $this->
render(
'admin_custom_text_edit.html.twig', $this->mergeWithDefault($vars));
69 $redirectUrl =
Traceback_uri() .
'?mod=admin_custom_text_list';
70 return new RedirectResponse($redirectUrl);
74 if ($request->get(
'edit') !==
null) {
75 $cp_pk = intval($request->get(
'edit'));
76 $vars = $this->getEditFormVars($cp_pk);
77 return $this->
render(
'admin_custom_text_edit.html.twig', $this->mergeWithDefault($vars));
81 $vars = $this->getEditFormVars(0);
82 return $this->
render(
'admin_custom_text_edit.html.twig', $this->mergeWithDefault($vars));
88 private function getEditFormVars($cp_pk)
92 $phraseData = $cp_pk > 0 ? $this->getPhraseData($cp_pk) :
false;
96 $vars = array_merge($vars, $phraseData);
97 $vars[
'isEdit'] =
true;
99 $vars[
'selectedLicenses'] = $this->getAssociatedLicenses($cp_pk);
102 $vars[
'isEdit'] =
false;
105 $vars[
'selectedLicenses'] = array();
108 $vars[
'formAction'] =
Traceback_uri() .
'?mod=' . self::NAME;
109 $vars[
'updateParam'] = $vars[
'isEdit'] ?
'updateit' :
'addit';
110 $vars[
'textParam'] =
'text';
111 $vars[
'isActiveParam'] =
'is_active';
114 $vars[
'licenseOptions'] = $this->getLicenseOptions();
119 $vars[
'bulkDataUsers'] = $userDao->getUsersByGroup();
129 $vars = $this->getEditFormVars(intval($request->get(
'cp_pk', 0)));
130 $vars[
'text'] = $this->
stringValue($request->get(
'text'));
131 $vars[
'is_active'] = $request->get(
'is_active') ==
'on';
133 $selectedLicenses = array();
134 foreach ($this->
parseLicenseData($request->get(
'license_data')) as $mapping) {
135 $shortname = $mapping[
'rf_shortname'];
136 if ($shortname ===
'') {
137 $shortname = isset($vars[
'licenseOptions'][$mapping[
'rf_pk']]) ?
138 $vars[
'licenseOptions'][$mapping[
'rf_pk']] :
'';
140 $mapping[
'rf_shortname'] = $shortname;
141 $selectedLicenses[] = $mapping;
143 $vars[
'selectedLicenses'] = $selectedLicenses;
154 if (empty($licenseData) || !is_string($licenseData)) {
157 $decodedData = json_decode($licenseData,
true);
158 if (!is_array($decodedData)) {
162 foreach ($decodedData as $item) {
163 if (!is_array($item) || empty($item[
'licenseId'])) {
167 'rf_pk' => intval($item[
'licenseId']),
168 'rf_shortname' => $this->
stringValue($item[
'licenseName'] ??
null),
169 'removing' => ($this->
stringValue($item[
'action'] ??
null) ===
'Remove'),
170 'comment' => $this->
stringValue($item[
'comment'] ??
null),
171 'reportinfo' => $this->
stringValue($item[
'reportinfo'] ??
null),
172 'acknowledgement' => $this->
stringValue($item[
'acknowledgement'] ??
null)
183 return is_string($value) ?
trim($value) :
'';
192 $currentCpPk = intval($request->get(
'cp_pk'));
195 return new JsonResponse(array(
'duplicate' =>
false));
198 $isDuplicate = $this->checkDuplicateTextMd5(md5($text), $currentCpPk > 0 ? $currentCpPk :
null);
200 return new JsonResponse(array(
'duplicate' => $isDuplicate));
206 private function checkDuplicateTextMd5($textMd5, $excludeCpPk =
null)
209 $dbManager = $this->
getObject(
'db.manager');
211 $sql =
"SELECT cp_pk FROM custom_phrase WHERE text_md5 = $1";
212 $params = array($textMd5);
215 $sql .=
" AND cp_pk != $2";
216 $params[] = $excludeCpPk;
219 $result = $dbManager->getSingleRow($sql, $params, __METHOD__);
221 return $result !==
false;
227 private function getPhraseData($cp_pk)
230 $dbManager = $this->
getObject(
'db.manager');
232 $sql =
"SELECT * FROM custom_phrase WHERE cp_pk = $1";
233 $row = $dbManager->getSingleRow($sql, array($cp_pk), __METHOD__);
236 $row[
'is_active'] = $dbManager->booleanFromDb($row[
'is_active']);
246 private function getAssociatedLicenses($cp_pk)
249 $dbManager = $this->
getObject(
'db.manager');
251 $sql =
"SELECT lr.rf_pk, lr.rf_shortname, cplm.removing,
252 cplm.comment, cplm.reportinfo, cplm.acknowledgement
253 FROM custom_phrase_license_map cplm
254 JOIN license_ref lr ON cplm.rf_fk = lr.rf_pk
255 WHERE cplm.cp_fk = $1
256 ORDER BY lr.rf_shortname";
258 $result = $dbManager->getRows($sql, array($cp_pk));
261 foreach ($result as $row) {
263 'rf_pk' => $row[
'rf_pk'],
264 'rf_shortname' => $row[
'rf_shortname'],
265 'removing' => $dbManager->booleanFromDb($row[
'removing']),
266 'comment' => $row[
'comment'] ??
'',
267 'reportinfo' => $row[
'reportinfo'] ??
'',
268 'acknowledgement' => $row[
'acknowledgement'] ??
''
280 private function savePhrase(Request $request, $userId, $groupId)
282 $cp_pk = intval($request->get(
'cp_pk'));
283 $isUpdate = $cp_pk > 0;
285 $user_fk = intval($request->get(
'user_fk'));
286 $group_fk = intval($request->get(
'group_fk'));
287 $is_active = $request->get(
'is_active') ==
'on' ?
'true' :
'false';
290 return array(
'success' =>
false,
291 'message' => _(
"ERROR: The text field cannot be empty."));
297 if (empty($licenseMappings)) {
298 return array(
'success' =>
false,
299 'message' => _(
"ERROR: At least one license must be associated with the custom text."));
303 $textMd5 = md5($text);
306 if ($this->checkDuplicateTextMd5($textMd5, $isUpdate ? $cp_pk :
null)) {
307 return array(
'success' =>
false,
308 'message' => _(
"ERROR: A custom text with the same content already exists in the database. Please modify the text or use the existing entry."));
312 if (empty($user_fk)) {
315 if (empty($group_fk)) {
316 $group_fk = $groupId;
320 $dbManager = $this->
getObject(
'db.manager');
329 $sql =
"UPDATE custom_phrase SET
330 text = $2, text_md5 = $3, user_fk = $4, group_fk = $5, is_active = $6
332 $params = array($cp_pk, $text, $textMd5, $user_fk, $group_fk, $is_active);
333 $dbManager->prepare($stmt = __METHOD__ .
".update", $sql);
334 $dbManager->freeResult($dbManager->execute($stmt, $params));
337 $deleteSql =
"DELETE FROM custom_phrase_license_map WHERE cp_fk = $1";
338 $dbManager->prepare($deleteStmt = __METHOD__ .
".delete_licenses", $deleteSql);
339 $dbManager->freeResult($dbManager->execute($deleteStmt, array($cp_pk)));
343 $sql =
"INSERT INTO custom_phrase
344 (text, text_md5, user_fk, group_fk, is_active, created_date)
345 VALUES ($1, $2, $3, $4, $5, CURRENT_TIMESTAMP) RETURNING cp_pk";
346 $params = array($text, $textMd5, $user_fk, $group_fk, $is_active);
347 $dbManager->prepare($stmt = __METHOD__ .
".insert", $sql);
348 $result = $dbManager->execute($stmt, $params);
349 $row = $dbManager->fetchArray($result);
350 $cp_pk = $row[
'cp_pk'];
351 $dbManager->freeResult($result);
354 if (!empty($licenseMappings)) {
355 $insertLicenseSql =
"INSERT INTO custom_phrase_license_map
356 (cp_fk, rf_fk, removing, comment, reportinfo, acknowledgement)
357 VALUES ($1, $2, $3, $4, $5, $6)";
358 $dbManager->prepare($insertLicenseStmt = __METHOD__ .
".insert_license", $insertLicenseSql);
360 foreach ($licenseMappings as $mapping) {
361 if (!empty($mapping[
'rf_pk'])) {
362 $dbManager->freeResult($dbManager->execute($insertLicenseStmt, array(
365 $mapping[
'removing'] ?
'true' :
'false',
366 $mapping[
'comment'] ?:
null,
367 $mapping[
'reportinfo'] ?:
null,
368 $mapping[
'acknowledgement'] ?:
null
375 $dbManager->commit();
377 return array(
'success' =>
true,
378 'message' => $isUpdate ? _(
"Custom text updated successfully.") :
379 _(
"Custom text added successfully."));
381 }
catch (\Throwable $e) {
382 $dbManager->rollback();
383 return array(
'success' =>
false,
384 'message' => _(
"ERROR: Failed to save custom text: ") . $e->getMessage());
388 private function getLicenseOptions()
391 $licenseDao = $this->
getObject(
'dao.license');
397 register_plugin(
new AdminCustomTextManagement());
Contains the constants and helpers for authentication of user.
static getUserId()
Get the current user's id.
static getGroupId()
Get the current user's group id.
static isAdmin()
Check if user is admin.
render($templateName, $vars=null, $headers=null)
static replaceUnicodeControlChar($input, $replace="")
parseLicenseData($licenseData)
checkDuplicateAjax(Request $request)
getEditFormVarsFromRequest(Request $request)
Traceback_uri()
Get the URI without query to this location.
char * trim(char *ptext)
Trimming whitespace.