FOSSology  4.4.0
Open Source License Compliance by Open Source Software
SysConfigDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © Soham Banerjee <sohambanerjee4abc@hotmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Dao;
9 
12 use Monolog\Logger;
13 
14 
16 {
17  const TYPE_MAP = array(
18  '1' => "int",
19  '2' => "text",
20  '3' => "textarea",
21  '4' => "password",
22  '5' => "dropdown",
23  '6' => "boolean"
24  );
26  private $dbManager;
28  private $logger;
29 
30  function __construct(DbManager $dbManager, Logger $logger)
31  {
32  $this->dbManager = $dbManager;
33  $this->logger = $logger;
34  }
35 
39  function getConfigData()
40  {
41  $sql = "select * from sysconfig order by group_name, group_order";
42  return $this->dbManager->getRows($sql);
43  }
44 
48  function getBannerData()
49  {
50  return $this->dbManager->getSingleRow("SELECT conf_value FROM sysconfig
51  WHERE variablename = 'BannerMsg'")["conf_value"];
52  }
53 
59  public function getCustomiseData($data, $apiVersion = ApiVersion::V1)
60  {
61  $finalVal = [];
62  foreach ($data as $row) {
63  $type = self::TYPE_MAP[$row['vartype']];
64  if ($apiVersion == ApiVersion::V2) {
65  $finalVal[] = array(
66  "key" => $row['variablename'],
67  "value" => $row['conf_value'],
68  "type" => $type,
69  "label" => $row['ui_label'],
70  "groupName" => $row['group_name'],
71  "groupOrder" => intval($row['group_order']),
72  "optionValue" => $row["option_value"]
73  );
74  } else {
75  $finalVal[] = array(
76  "key" => $row['variablename'],
77  "value" => $row['conf_value'],
78  "type" => $type,
79  "label" => $row['ui_label'],
80  "group_name" => $row['group_name'],
81  "group_order" => intval($row['group_order']),
82  "option_value" => $row["option_value"]
83  );
84  }
85  }
86  return $finalVal;
87  }
88 
96  public function UpdateConfigData($data)
97  {
98  $key = strval($data['key']);
99  $value = strval($data['value']);
100 
101  $sysconfigData = $this->getConfigData();
102  $oldarray = [];
103  foreach ($sysconfigData as $item) {
104  $oldarray[$item['variablename']] = $item['conf_value'];
105  }
106 
107  if ($value != $oldarray[$key]) {
108  /* get validation_function row from sysconfig table */
109  $sys_array = $this->dbManager->getSingleRow("SELECT validation_function,
110  ui_label FROM sysconfig WHERE variablename = $1", [$key],__METHOD__.'.getVarNameData');
111  $validation_function = $sys_array['validation_function'];
112  $ui_label = $sys_array['ui_label'];
113  $is_empty = empty($validation_function);
114  /*
115  * 1. the validation_function is empty
116  * 2. the validation_function is not empty, and after checking, the value
117  * is valid update sysconfig table
118  */
119  if ($is_empty || (! $is_empty && (1 == $validation_function($value)))) {
120  $this->dbManager->getSingleRow(
121  "UPDATE sysconfig SET conf_value=$1 WHERE variablename=$2",
122  [$value, $key], __METHOD__ . '.setVarNameData');
123  return [true, $key];
124  } else if (! $is_empty && (0 == $validation_function($value))) {
125  /*
126  * the validation_function is not empty, but after checking, the value
127  * is invalid
128  */
129  $warning_msg = "Error: Unable to update $key.";
130  if (! strcmp($validation_function, 'check_boolean')) {
131  $warning_msg = _(
132  "Error: You set $ui_label to $value. Valid values are 'true' and 'false'.");
133  } else if (strpos($validation_function, "url")) {
134  $warning_msg = _(
135  "Error: $ui_label $value, is not a reachable URL.");
136  }
137  return [false, $warning_msg];
138  }
139  }
140  return [true, $key];
141  }
142 }
getCustomiseData($data, $apiVersion=ApiVersion::V1)
getBannerData()
Fetch banner message.
getConfigData()
Fetch configuration rows.
UpdateConfigData($data)
Update Configuration Data.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16