FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseViewProxy.php
1 <?php
2 /*
3 Copyright (C) 2014, 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 
19 namespace Fossology\Lib\Proxy;
20 
22 {
23  const CANDIDATE_PREFIX = 'candidatePrefix';
24  const OPT_COLUMNS = 'columns';
26  private $groupId;
28  private $allColumns = array('rf_pk', 'rf_shortname', 'rf_text', 'rf_url', 'rf_add_date', 'rf_copyleft', 'rf_fullname',
29  'rf_notes', 'marydone', 'rf_active', 'rf_text_updatable', 'rf_md5', 'rf_detector_type', 'rf_source',
30  'group_fk');
31 
37  public function __construct($groupId, $options=array(), $dbViewName='license_all')
38  {
39  $this->groupId = $groupId;
40  if ($groupId == 0) {
41  $dbViewQuery = $this->queryOnlyLicenseRef($options);
42  parent::__construct($dbViewQuery, $dbViewName);
43  return;
44  }
45  $dbViewQuery = $this->queryLicenseCandidate($options);
46  if (! array_key_exists('diff', $options)) {
47  $dbViewQuery .= " UNION ".$this->queryOnlyLicenseRef($options);
48  }
49  parent::__construct($dbViewQuery, $dbViewName);
50  }
51 
52 
53  private function queryLicenseCandidate($options)
54  {
55  $columns = array_key_exists(self::OPT_COLUMNS, $options) ? $options[self::OPT_COLUMNS] : $this->allColumns;
56  if (array_key_exists(self::CANDIDATE_PREFIX, $options)) {
57  $shortnameId = array_search('rf_shortname',$columns);
58  if ($shortnameId !== false) {
59  $columns[$shortnameId] = "'". pg_escape_string($options[self::CANDIDATE_PREFIX]). '\'||rf_shortname AS rf_shortname';
60  }
61  }
62  $gluedColumns = implode(',', $columns);
63  $dbViewQuery = "SELECT $gluedColumns FROM license_candidate WHERE group_fk=$this->groupId";
64  if (array_key_exists('extraCondition', $options)) {
65  $dbViewQuery .= " AND $options[extraCondition]";
66  }
67  return $dbViewQuery;
68  }
69 
70  private function queryOnlyLicenseRef($options)
71  {
72  $columns = array_key_exists(self::OPT_COLUMNS, $options) ? $options[self::OPT_COLUMNS] : $this->allColumns;
73  $groupFkPos = array_search('group_fk',$columns);
74  if ($groupFkPos) {
75  $columns[$groupFkPos] = '0 AS group_fk';
76  }
77  $gluedColumns = implode(',', $columns);
78  $dbViewQuery = "SELECT $gluedColumns FROM ONLY license_ref";
79  if (array_key_exists('extraCondition', $options)) {
80  $dbViewQuery .= " WHERE $options[extraCondition]";
81  }
82  return $dbViewQuery;
83  }
84 }
__construct($groupId, $options=array(), $dbViewName='license_all')