FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AdviceLicense.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2014-2015 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\UI\Page;
20 
28 
30 {
31  const NAME = "advice_license";
32 
33  function __construct()
34  {
35  parent::__construct(self::NAME, array(
36  self::TITLE => "Advice Licenses",
37  self::MENU_LIST => "Organize::Licenses",
38  self::REQUIRES_LOGIN => true
39  ));
40  }
41 
47  protected function handle(Request $request)
48  {
49  $rf = intval($request->get('rf'));
50  $userId = Auth::getUserId();
51  $groupId = Auth::getGroupId();
53  $userDao = $this->getObject('dao.user');
54  $canEdit = $userDao->isAdvisorOrAdmin($userId, $groupId);
55  if (empty($rf) || ! $canEdit) {
56  $vars = array(
57  'aaData' => json_encode($this->getArrayArrayData($groupId, $canEdit)),
58  'canEdit' => $canEdit
59  );
60  return $this->render('advice_license.html.twig', $this->mergeWithDefault($vars));
61  }
62 
63  $vars = $this->getDataRow($groupId, $rf);
64  if ($vars === false) {
65  return $this->flushContent( _('invalid license candidate'));
66  }
67 
68  if ($request->get('save')) {
69  try {
70  $vars = $this->saveInput($request, $vars);
71  $vars['message'] = 'Successfully updated.';
72  } catch (\Exception $e) {
73  $vars = array('rf_shortname' => $request->get('shortname'),
74  'rf_fullname' => $request->get('fullname'),
75  'rf_text' => $request->get('rf_text'),
76  'rf_url' => $request->get('url'),
77  'rf_notes' => $request->get('note'),
78  'rf_risk' => intval($request->get('risk'))
79  );
80  $vars['message'] = $e->getMessage();
81  }
82  }
83 
84  return $this->render('advice_license-edit.html.twig', $this->mergeWithDefault($vars));
85  }
86 
87 
88  private function getArrayArrayData($groupId,$canEdit)
89  {
90  $sql = "SELECT rf_pk,rf_shortname,rf_fullname,rf_text,rf_url,rf_notes,marydone FROM license_candidate WHERE group_fk=$1";
92  $dbManager = $this->getObject('db.manager');
93  $dbManager->prepare($stmt = __METHOD__, $sql);
94  $res = $dbManager->execute($stmt, array($groupId));
95  $aaData = array();
96  while ($row = $dbManager->fetchArray($res)) {
97  $aData = array(htmlentities($row['rf_shortname']),
98  htmlentities($row['rf_fullname']),
99  '<div style="overflow-y:scroll;max-height:150px;margin:0;">' . nl2br(htmlentities($row['rf_text'])) . '</div>',
100  htmlentities($row['rf_url']),
101  $this->bool2checkbox($dbManager->booleanFromDb($row['marydone'])));
102  if ($canEdit) {
103  $link = Traceback_uri() . '?mod=' . Traceback_parm() . '&rf=' . $row['rf_pk'];
104  $edit = '<a href="' . $link . '"><img border="0" src="images/button_edit.png"></a>';
105  array_unshift($aData,$edit);
106  }
107  $aaData[] = $aData;
108  }
109  $dbManager->freeResult($res);
110  return $aaData;
111  }
112 
113 
114  private function getDataRow($groupId, $licId)
115  {
116  if ($licId == -1) {
117  return array('rf_pk' => -1, 'rf_shortname' => '');
118  }
119  $sql = "SELECT rf_pk,rf_shortname,rf_fullname,rf_text,rf_url,rf_notes,marydone,rf_risk FROM license_candidate WHERE group_fk=$1 AND rf_pk=$2";
120  /* @var $dbManager DbManager */
121  $dbManager = $this->getObject('db.manager');
122  $row = $dbManager->getSingleRow($sql, array($groupId, $licId), __METHOD__);
123  if (false !== $row) {
124  $row['marydone'] = $dbManager->booleanFromDb($row['marydone']);
125  }
126  return $row;
127  }
128 
129 
130  private function bool2checkbox($bool)
131  {
132  $check = $bool ? ' checked="checked"' : '';
133  return '<input type="checkbox"' . $check . ' disabled="disabled"/>';
134  }
135 
142  private function saveInput(Request $request, $oldRow)
143  {
144  $shortname = $request->get('shortname');
145  $fullname = $request->get('fullname');
146  $rfText = $request->get('rf_text');
147  $url = $request->get('url');
148  $marydone = $request->get('marydone');
149  $note = $request->get('note');
150  $riskLvl = intval($request->get('risk'));
151 
152  if (empty($shortname) || empty($fullname) || empty($rfText)) {
153  throw new \Exception('missing shortname (or) fullname (or) reference text');
154  }
155 
156  /* @var $licenseDao LicenseDao */
157  $licenseDao = $this->getObject('dao.license');
158  $ok = ($oldRow['rf_shortname'] == $shortname);
159  if (!$ok) {
160  $ok = $licenseDao->isNewLicense($shortname, Auth::getGroupId());
161  }
162  if (!$ok) {
163  throw new \Exception('shortname already in use');
164  }
165  if ($oldRow['rf_pk'] == -1) {
166  $oldRow['rf_pk'] = $licenseDao->insertUploadLicense($shortname, $rfText, Auth::getGroupId());
167  }
168 
169  $licenseDao->updateCandidate($oldRow['rf_pk'], $shortname, $fullname,
170  $rfText, $url, $note, !empty($marydone), $riskLvl);
171  return array('rf_pk' => $oldRow['rf_pk'],
172  'rf_shortname' => $shortname,
173  'rf_fullname' => $fullname,
174  'rf_text' => $rfText,
175  'rf_url' => $url,
176  'rf_notes' => $note,
177  'rf_risk' => $riskLvl,
178  'marydone' => $marydone);
179  }
180 }
181 
182 register_plugin(new AdviceLicense());
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
Traceback_parm($ShowMod=1)
Get the URI query to this location.
render($templateName, $vars=null, $headers=null)
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
saveInput(Request $request, $oldRow)