FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ObligationCsvExport.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2017 Siemens AG
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  $this->obligationMap = $GLOBALS['container']->get('businessrules.obligationmap');
41  }
42 
47  public function setDelimiter($delimiter=',')
48  {
49  $this->delimiter = substr($delimiter,0,1);
50  }
51 
56  public function setEnclosure($enclosure='"')
57  {
58  $this->enclosure = substr($enclosure,0,1);
59  }
60 
66  public function createCsv($ob=0, $generateJson=false)
67  {
68  $csvarray = array();
69  $sql = "SELECT ob_pk,ob_type AS Type,ob_topic AS \"Obligation or Risk topic\",ob_text AS \"Full Text\",
70  ob_classification AS Classification,ob_modifications AS \"Apply on modified source code\",ob_comment AS Comment
71  FROM obligation_ref";
72  if ($ob>0) {
73  $stmt = __METHOD__.'.ob';
74  $sql .= ' WHERE ob_pk=$1;';
75  $row = $this->dbManager->getSingleRow($sql, [$ob], $stmt);
76  $liclist = $this->obligationMap->getLicenseList($ob);
77  $candidatelist = $this->obligationMap->getLicenseList($ob, True);
78  array_shift($row);
79  $row["Associated Licenses"] = $liclist;
80  $row["Associated candidate Licenses"] = $candidatelist;
81  $csvarray[] = $row;
82  } else {
83  $stmt = __METHOD__;
84  $this->dbManager->prepare($stmt,$sql);
85  $res = $this->dbManager->execute($stmt);
86  $vars = $this->dbManager->fetchAll($res);
87  $this->dbManager->freeResult($res);
88 
89  foreach ($vars as $row) {
90  $liclist = $this->obligationMap->getLicenseList($row['ob_pk']);
91  $candidatelist = $this->obligationMap->getLicenseList($row['ob_pk'], True);
92  array_shift($row);
93  $row["Associated Licenses"] = $liclist;
94  $row["Associated candidate Licenses/"] = $candidatelist;
95  $csvarray[] = $row;
96  }
97  }
98  if ($generateJson) {
99  return json_encode($csvarray, JSON_PRETTY_PRINT);
100  } else {
101  $out = fopen('php://output', 'w');
102  ob_start();
103  $head = array('Type','Obligation or Risk topic','Full Text','Classification','Apply on modified source code','Comment','Associated Licenses','Associated candidate Licenses');
104  fputcsv($out, $head, $this->delimiter, $this->enclosure);
105  foreach ($csvarray as $row) {
106  fputcsv($out, $row, $this->delimiter, $this->enclosure);
107  }
108  $content = ob_get_contents();
109  ob_end_clean();
110  return $content;
111  }
112  }
113 }
Helper class to export obligations as a CSV.
createCsv($ob=0, $generateJson=false)
Create CSV from the obligations.
setDelimiter($delimiter=',')
Update the delimiter.
setEnclosure($enclosure='"')
Update the enclosure.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Utility functions for specific applications.