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)
67  {
68  $csvarray = array();
69  $sql = "SELECT ob_pk,ob_type,ob_topic,ob_text,ob_classification,ob_modifications,ob_comment
70  FROM obligation_ref";
71  if ($ob>0) {
72  $stmt = __METHOD__.'.ob';
73  $sql .= ' WHERE ob_pk=$1;';
74  $row = $this->dbManager->getSingleRow($sql, [$ob], $stmt);
75  $liclist = $this->obligationMap->getLicenseList($ob);
76  $candidatelist = $this->obligationMap->getLicenseList($ob, True);
77  array_shift($row);
78  $row[] = $liclist;
79  $row[] = $candidatelist;
80  $csvarray[] = $row;
81  } else {
82  $stmt = __METHOD__;
83  $this->dbManager->prepare($stmt,$sql);
84  $res = $this->dbManager->execute($stmt);
85  $vars = $this->dbManager->fetchAll($res);
86  $this->dbManager->freeResult($res);
87 
88  foreach ($vars as $row) {
89  $liclist = $this->obligationMap->getLicenseList($row['ob_pk']);
90  $candidatelist = $this->obligationMap->getLicenseList($row['ob_pk'], True);
91  array_shift($row);
92  $row[] = $liclist;
93  $row[] = $candidatelist;
94  $csvarray[] = $row;
95  }
96  }
97 
98  $out = fopen('php://output', 'w');
99  ob_start();
100  $head = array('Type','Obligation or Risk topic','Full Text','Classification','Apply on modified source code','Comment','Associated Licenses','Associated candidate Licenses');
101  fputcsv($out, $head, $this->delimiter, $this->enclosure);
102  foreach ($csvarray as $row) {
103  fputcsv($out, $row, $this->delimiter, $this->enclosure);
104  }
105  $content = ob_get_contents();
106  ob_end_clean();
107  return $content;
108  }
109 }
Helper class to export obligations as a CSV.
setDelimiter($delimiter=',')
Update the delimiter.
setEnclosure($enclosure='"')
Update the enclosure.
createCsv($ob=0)
Create CSV from the obligations.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Utility functions for specific applications.