49 throw new \InvalidArgumentException(
"CSV delimiter must be a non-empty single-byte character.");
52 throw new \InvalidArgumentException(
"CSV delimiter and enclosure must be different characters.");
64 throw new \InvalidArgumentException(
"CSV enclosure must be a non-empty single-byte character.");
67 throw new \InvalidArgumentException(
"CSV delimiter and enclosure must be different characters.");
79 public function exportBulkText($user_pk=0, $group_pk=0, $generateJson=
false, $includeLicenseText=
false)
85 $whereClause =
"WHERE lrb.user_fk = $1";
87 } elseif ($group_pk > 0) {
88 $whereClause =
"WHERE lrb.group_fk = $1";
89 $params[] = $group_pk;
92 $licenseTextColumn = $includeLicenseText ?
",\n lr.rf_text AS license_text" :
"";
102 lr.rf_active$licenseTextColumn
104 SELECT rf_text, MAX(lrb_pk) AS lrb_pk
105 FROM license_ref_bulk
109 JOIN license_ref_bulk lrb ON lrb.lrb_pk = latest.lrb_pk
110 LEFT JOIN license_set_bulk lsb ON lsb.lrb_fk = lrb.lrb_pk
111 LEFT JOIN license_ref lr ON lr.rf_pk = lsb.rf_fk
112 ORDER BY lrb.rf_text, lr.rf_shortname";
114 $result = $this->
dbManager->getRows($sql, $params);
117 return $this->
createJson($result, $includeLicenseText);
132 foreach ($result as $row) {
133 $text = $row[
'rf_text'] ?:
'';
134 if (!isset($grouped[$text])) {
135 $grouped[$text] = array(
136 'licenses' => array(),
137 'is_active_values' => array()
141 if (!empty($row[
'rf_shortname'])) {
142 $removing = ($row[
'removing'] ===
't' || $row[
'removing'] ===
true);
144 'shortname' => $row[
'rf_shortname'],
145 'removing' => $removing,
146 'comment' => $row[
'comment'] ?:
'',
147 'reportinfo' => $row[
'reportinfo'] ?:
'',
148 'acknowledgement' => $row[
'acknowledgement'] ?:
''
150 if ($includeLicenseText && !empty($row[
'license_text'])) {
151 $license[
'license_text'] = $row[
'license_text'];
153 $grouped[$text][
'licenses'][] = $license;
156 if ($row[
'rf_active'] !==
null) {
157 $isActive = ($row[
'rf_active'] ===
't' || $row[
'rf_active'] ===
true);
158 $grouped[$text][
'is_active_values'][] = $isActive;
162 foreach ($grouped as $text => $values) {
163 $grouped[$text][
'is_active'] = empty($values[
'is_active_values'])
165 : !in_array(
false, $values[
'is_active_values'],
true);
166 unset($grouped[$text][
'is_active_values']);
181 'Text',
'Is Active',
'License Shortname',
'Removing',
182 'Comment',
'License Text',
'Acknowledgement'
184 if ($includeLicenseText) {
185 $headers[] =
'license_ref_text';
188 $out = fopen(
'php://output',
'w');
190 fputs($out, chr(0xEF) . chr(0xBB) . chr(0xBF));
191 fputcsv($out, $headers, $this->delimiter, $this->enclosure);
193 foreach ($this->
groupResultsByText($result, $includeLicenseText) as $text => $entry) {
194 $isActive = $entry[
'is_active'] ===
null ?
'' : ($entry[
'is_active'] ?
'true' :
'false');
196 if (empty($entry[
'licenses'])) {
199 $isActive,
'',
'',
'',
'',
''
201 if ($includeLicenseText) {
204 fputcsv($out, $row, $this->delimiter, $this->enclosure);
206 foreach ($entry[
'licenses'] as $lic) {
211 $lic[
'removing'] ?
'true' :
'false',
216 if ($includeLicenseText) {
219 fputcsv($out, $row, $this->delimiter, $this->enclosure);
224 $content = ob_get_contents();
245 private function createJson($result, $includeLicenseText=
false)
249 foreach ($this->
groupResultsByText($result, $includeLicenseText) as $text => $entry) {
250 $licenses = array_map(
function($lic) use ($includeLicenseText) {
252 'shortname' => $lic[
'shortname'],
253 'removing' => $lic[
'removing'],
254 'comment' => $lic[
'comment'],
255 'reportinfo' => $lic[
'reportinfo'],
256 'acknowledgement' => $lic[
'acknowledgement']
258 if ($includeLicenseText) {
259 $out[
'license_ref_text'] = $lic[
'license_text'] ??
'';
262 }, $entry[
'licenses']);
266 'is_active' => $entry[
'is_active'],
267 'licenses' => $licenses
271 return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
Helper class to export license reference bulk data as CSV or JSON from the DB.
setDelimiter($delimiter=',')
Update the delimiter.
exportBulkText($user_pk=0, $group_pk=0, $generateJson=false, $includeLicenseText=false)
Export license reference bulk data from the DB as CSV or JSON.
createJson($result, $includeLicenseText=false)
Create JSON content, nested licenses format, compatible with CustomTextImport.
normalizeNewlinesForCsv($value)
Convert CR/LF variants to literal for line-safe CSV rows.
groupResultsByText($result, $includeLicenseText=false)
Group flat DB rows by rf_text into the nested licenses format.
createCsvContent($result, $includeLicenseText=false)
Create CSV content, one row per text+license, compatible with CustomTextImport.
__construct(DbManager $dbManager)
setEnclosure($enclosure='"')
Update the enclosure.
static escapeNewlines($value)
Flatten real newlines to a literal so a field stays on one physical CSV line. Backslashes are escap...
fo_dbManager * dbManager
fo_dbManager object
Utility functions for specific applications.