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.");
85 $whereClause =
"WHERE lrb.user_fk = $1";
87 } elseif ($group_pk > 0) {
88 $whereClause =
"WHERE lrb.group_fk = $1";
89 $params[] = $group_pk;
92 $sql =
"SELECT DISTINCT
99 FROM license_ref_bulk lrb
100 LEFT JOIN license_set_bulk lsb ON lsb.lrb_fk = lrb.lrb_pk
101 LEFT JOIN license_ref lr ON lr.rf_pk = lsb.rf_fk
103 ORDER BY lrb.rf_text, lr.rf_shortname";
105 $result = $this->
dbManager->getRows($sql, $params);
122 foreach ($result as $row) {
123 $text = $row[
'rf_text'] ?:
'';
124 if (!isset($grouped[$text])) {
125 $grouped[$text] = array(
126 'licenses_to_add' => array(),
127 'licenses_to_remove' => array(),
128 'comments' => array(),
129 'acknowledgements' => array(),
130 'is_active_values' => array()
133 if (!empty($row[
'rf_shortname'])) {
134 if ($row[
'removing'] ===
't' || $row[
'removing'] ===
true) {
135 $grouped[$text][
'licenses_to_remove'][] = $row[
'rf_shortname'];
137 $grouped[$text][
'licenses_to_add'][] = $row[
'rf_shortname'];
141 if (!empty($row[
'comment'])) {
142 $grouped[$text][
'comments'][] = $row[
'comment'];
145 if (!empty($row[
'acknowledgement'])) {
146 $grouped[$text][
'acknowledgements'][] = $row[
'acknowledgement'];
149 if ($row[
'rf_active'] !==
null) {
150 $isActive = ($row[
'rf_active'] ===
't' || $row[
'rf_active'] ===
true ||
151 $row[
'rf_active'] === 1 || $row[
'rf_active'] ===
'1');
152 $grouped[$text][
'is_active_values'][] = $isActive;
156 foreach ($grouped as $text => $values) {
157 $grouped[$text][
'licenses_to_add'] = array_values(array_unique($values[
'licenses_to_add']));
158 $grouped[$text][
'licenses_to_remove'] = array_values(array_unique($values[
'licenses_to_remove']));
159 $grouped[$text][
'comments'] = array_values(array_unique($values[
'comments']));
160 $grouped[$text][
'acknowledgements'] = array_values(array_unique($values[
'acknowledgements']));
162 if (empty($values[
'is_active_values'])) {
163 $grouped[$text][
'is_active'] =
null;
165 $grouped[$text][
'is_active'] = !in_array(
false, $values[
'is_active_values'],
true);
168 unset($grouped[$text][
'is_active_values']);
181 $headers = array(
'text',
'licenses_to_add',
'licenses_to_remove',
'comments',
'acknowledgements',
'is_active');
182 $out = fopen(
'php://output',
'w');
184 fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
185 fputcsv($out, $headers, $this->delimiter, $this->enclosure);
190 implode(
'|', array_map(array($this,
'normalizeNewlinesForCsv'), $licenses[
'licenses_to_add'])),
191 implode(
'|', array_map(array($this,
'normalizeNewlinesForCsv'), $licenses[
'licenses_to_remove'])),
192 implode(
'|', array_map(array($this,
'normalizeNewlinesForCsv'), $licenses[
'comments'])),
193 implode(
'|', array_map(array($this,
'normalizeNewlinesForCsv'), $licenses[
'acknowledgements'])),
194 $licenses[
'is_active'] ===
null ?
'' : ($licenses[
'is_active'] ?
'true' :
'false')
196 fputcsv($out, $csvRow, $this->delimiter, $this->enclosure);
199 $content = ob_get_contents();
211 if ($value ===
null) {
214 return str_replace(array(
"\r\n",
"\r",
"\n"),
'\\n', (
string)$value);
229 'licenses_to_add' => $licenses[
'licenses_to_add'],
230 'licenses_to_remove' => $licenses[
'licenses_to_remove'],
231 'comments' => $licenses[
'comments'],
232 'acknowledgements' => $licenses[
'acknowledgements'],
233 'is_active' => $licenses[
'is_active']
237 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.
createCsvContent($result)
Create CSV content from result array.
normalizeNewlinesForCsv($value)
Convert CR/LF variants to literal for line-safe CSV rows.
exportBulkText($user_pk=0, $group_pk=0, $generateJson=false)
Export license reference bulk data from the DB as CSV or JSON.
createJson($result)
Create JSON content from result array.
groupResultsByText($result)
Group flat DB rows by bulk text, splitting licenses into add/remove buckets.
__construct(DbManager $dbManager)
setEnclosure($enclosure='"')
Update the enclosure.
fo_dbManager * dbManager
fo_dbManager object
Utility functions for specific applications.