FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ObligationCsvExport.php
Go to the documentation of this file.
1 <?php
2 /*
3 Copyright (C) 2017, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
20 
23 
34 {
37  protected $dbManager;
40  protected $delimiter = ',';
43  protected $enclosure = '"';
44 
49  public function __construct(DbManager $dbManager)
50  {
51  $this->dbManager = $dbManager;
52  $this->obligationMap = $GLOBALS['container']->get('businessrules.obligationmap');
53  }
54 
59  public function setDelimiter($delimiter=',')
60  {
61  $this->delimiter = substr($delimiter,0,1);
62  }
63 
68  public function setEnclosure($enclosure='"')
69  {
70  $this->enclosure = substr($enclosure,0,1);
71  }
72 
78  public function createCsv($ob=0)
79  {
80  $csvarray = array();
81  $sql = "SELECT ob_pk,ob_type,ob_topic,ob_text,ob_classification,ob_modifications,ob_comment
82  FROM obligation_ref;";
83  if ($ob>0) {
84  $stmt = __METHOD__.'.ob';
85  $sql .= ' WHERE ob_pk=$'.$ob;
86  $row = $this->dbManager->getSingleRow($sql,$stmt);
87  $vars = $row ? array( $row ) : array();
88  $liclist = $this->obligationMap->getLicenseList($ob);
89  $candidatelist = $this->obligationMap->getLicenseList($ob, True);
90  array_shift($vars);
91  array_push($vars,$liclist);
92  array_push($vars,$candidatelist);
93  $csvarray = $vars;
94  } else {
95  $stmt = __METHOD__;
96  $this->dbManager->prepare($stmt,$sql);
97  $res = $this->dbManager->execute($stmt);
98  $vars = $this->dbManager->fetchAll($res);
99  $this->dbManager->freeResult($res);
100 
101  foreach ($vars as $row) {
102  $liclist = $this->obligationMap->getLicenseList($row['ob_pk']);
103  $candidatelist = $this->obligationMap->getLicenseList($row['ob_pk'], True);
104  array_shift($row);
105  array_push($row,$liclist);
106  array_push($row,$candidatelist);
107  array_push($csvarray,$row);
108  }
109  }
110 
111  $out = fopen('php://output', 'w');
112  ob_start();
113  $head = array('Type','Obligation or Risk topic','Full Text','Classification','Apply on modified source code','Comment','Associated Licenses','Associated candidate Licenses');
114  fputcsv($out, $head, $this->delimiter, $this->enclosure);
115  foreach ($csvarray as $row) {
116  fputcsv($out, $row, $this->delimiter, $this->enclosure);
117  }
118  $content = ob_get_contents();
119  ob_end_clean();
120  return $content;
121  }
122 }
Helper class to export obligations as a CSV.
setDelimiter($delimiter=',')
Update the delimiter.
Utility functions for specific applications.
createCsv($ob=0)
Create CSV from the obligations.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
setEnclosure($enclosure='"')
Update the enclosure.