FOSSology  4.7.1
Open Source License Compliance by Open Source Software
CustomTextExport.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2025 Harshit Gandhi <gandhiharshit716@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
11 
22 {
25  protected $dbManager;
28  protected $delimiter = ',';
31  protected $enclosure = '"';
32 
37  public function __construct(DbManager $dbManager)
38  {
39  $this->dbManager = $dbManager;
40  }
41 
46  public function setDelimiter($delimiter=',')
47  {
48  $this->delimiter = substr($delimiter,0,1);
49  }
50 
55  public function setEnclosure($enclosure='"')
56  {
57  $this->enclosure = substr($enclosure,0,1);
58  }
59 
66  public function export($cp_pk=0, $generateJson=false)
67  {
68  $whereClause = "";
69  $params = array();
70 
71  if ($cp_pk > 0) {
72  $whereClause = "WHERE cp.cp_pk = $1";
73  $params[] = $cp_pk;
74  }
75 
76  $sql = "SELECT
77  cp.text,
78  cp.acknowledgement,
79  cp.comments,
80  cp.created_date,
81  cp.is_active,
82  u.user_name,
83  g.group_name,
84  STRING_AGG(CASE WHEN cplm.removing = false THEN lr.rf_shortname END, ', ' ORDER BY lr.rf_shortname) as licenses_to_add,
85  STRING_AGG(CASE WHEN cplm.removing = true THEN lr.rf_shortname END, ', ' ORDER BY lr.rf_shortname) as licenses_to_remove
86  FROM custom_phrase cp
87  LEFT JOIN users u ON cp.user_fk = u.user_pk
88  LEFT JOIN groups g ON cp.group_fk = g.group_pk
89  LEFT JOIN custom_phrase_license_map cplm ON cp.cp_pk = cplm.cp_fk
90  LEFT JOIN license_ref lr ON cplm.rf_fk = lr.rf_pk
91  $whereClause
92  GROUP BY cp.cp_pk, cp.text, cp.acknowledgement, cp.comments,
93  cp.created_date, cp.is_active, u.user_name, g.group_name
94  ORDER BY cp.created_date DESC";
95 
96  $result = $this->dbManager->getRows($sql, $params);
97 
98  if ($generateJson) {
99  return $this->createJson($result);
100  } else {
101  return $this->createCsvContent($result);
102  }
103  }
104 
110  private function createCsvContent($result)
111  {
112  $csv = '';
113 
114  // Add header row
115  $headers = array(
116  'Text',
117  'Acknowledgement',
118  'Comments',
119  'Created Date',
120  'Is Active',
121  'Created By',
122  'Group',
123  'Licenses To Add',
124  'Licenses To Remove'
125  );
126  $csv .= $this->arrayToCsvLine($headers);
127 
128  // Add data rows
129  foreach ($result as $row) {
130  $csvRow = array(
131  $row['text'],
132  $row['acknowledgement'] ?: '',
133  $row['comments'] ?: '',
134  $row['created_date'],
135  $row['is_active'] ? 'true' : 'false',
136  $row['user_name'] ?: '',
137  $row['group_name'] ?: '',
138  $row['licenses_to_add'] ?: '',
139  $row['licenses_to_remove'] ?: ''
140  );
141  $csv .= $this->arrayToCsvLine($csvRow);
142  }
143 
144  return $csv;
145  }
146 
152  private function createJson($result)
153  {
154  $data = array();
155 
156  foreach ($result as $row) {
157  $data[] = array(
158  'text' => $row['text'],
159  'acknowledgement' => $row['acknowledgement'] ?: '',
160  'comments' => $row['comments'] ?: '',
161  'created_date' => $row['created_date'],
162  'is_active' => $row['is_active'] ? true : false,
163  'created_by' => $row['user_name'] ?: '',
164  'group' => $row['group_name'] ?: '',
165  'licenses_to_add' => $row['licenses_to_add'] ? explode(', ', $row['licenses_to_add']) : array(),
166  'licenses_to_remove' => $row['licenses_to_remove'] ? explode(', ', $row['licenses_to_remove']) : array()
167  );
168  }
169 
170  return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
171  }
172 
178  private function arrayToCsvLine($array)
179  {
180  $csvLine = '';
181  foreach ($array as $key => $value) {
182  if ($key > 0) {
183  $csvLine .= $this->delimiter;
184  }
185 
186  // Escape the value if it contains delimiter, enclosure, or newline
187  if (strpos($value, $this->delimiter) !== false ||
188  strpos($value, $this->enclosure) !== false ||
189  strpos($value, "\n") !== false ||
190  strpos($value, "\r") !== false) {
191  $value = $this->enclosure . str_replace($this->enclosure, $this->enclosure . $this->enclosure, $value) . $this->enclosure;
192  }
193 
194  $csvLine .= $value;
195  }
196  $csvLine .= "\n";
197 
198  return $csvLine;
199  }
200 }
Helper class to export custom text phrases as CSV/JSON from the DB.
createCsvContent($result)
Create CSV content from result array.
setEnclosure($enclosure='"')
Update the enclosure.
createJson($result)
Create JSON content from result array.
setDelimiter($delimiter=',')
Update the delimiter.
arrayToCsvLine($array)
Convert array to CSV line.
export($cp_pk=0, $generateJson=false)
Create the CSV/JSON export from the DB.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Utility functions for specific applications.