1<?php 2/* 3Copyright (C) 2017, Siemens AG 4 5This program is free software; you can redistribute it and/or 6modify it under the terms of the GNU General Public License 7version 2 as published by the Free Software Foundation. 8 9This program is distributed in the hope that it will be useful, 10but WITHOUT ANY WARRANTY; without even the implied warranty of 11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12GNU General Public License for more details. 13 14You should have received a copy of the GNU General Public License along 15with this program; if not, write to the Free Software Foundation, Inc., 1651 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17*/ 18 19namespace Fossology\Lib\Application; 20 21use Fossology\Lib\BusinessRules\LicenseMap; 22use Fossology\Lib\Db\DbManager; 23 24/** 25 * @file 26 * @brief Helper class to export obligations as a CSV 27 */ 28 29/** 30 * @class ObligationCsvExport 31 * @brief Helper class to export obligations as a CSV 32 */ 33class ObligationCsvExport 34{ 35 /** @var DbManager $dbManager 36 * DB manager to be used */ 37 protected $dbManager; 38 /** @var string $delimiter 39 * Delimiter used in the CSV */ 40 protected $delimiter = ','; 41 /** @var string $enclosure 42 * Ecnlosure used in the CSV */ 43 protected $enclosure = '"'; 44 45 /** 46 * Constructor 47 * @param DbManager $dbManager DbManager to be used. 48 */ 49 public function __construct(DbManager $dbManager) 50 { 51 $this->dbManager = $dbManager; 52 $this->obligationMap = $GLOBALS['container']->get('businessrules.obligationmap'); 53 } 54 55 /** 56 * @brief Update the delimiter 57 * @param string $delimiter New delimiter to use. 58 */ 59 public function setDelimiter($delimiter=',') 60 { 61 $this->delimiter = substr($delimiter,0,1); 62 } 63 64 /** 65 * @brief Update the enclosure 66 * @param string $enclosure New enclosure to use. 67 */ 68 public function setEnclosure($enclosure='"') 69 { 70 $this->enclosure = substr($enclosure,0,1); 71 } 72 73 /** 74 * @brief Create CSV from the obligations 75 * @param int $rf Obligation id to be returned, else set 0 to get all. 76 * @return string CSV 77 */ 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} 123