8 namespace Fossology\UI\Page;
10 use Composer\Spdx\SpdxLicenses;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpFoundation\Response;
22 const NAME =
"advice_license";
24 function __construct()
26 parent::__construct(self::NAME, array(
27 self::TITLE =>
"Candidate Licenses",
28 self::MENU_LIST =>
"Organize::Licenses",
29 self::REQUIRES_LOGIN =>
true
38 protected function handle(Request $request)
40 $rf = intval($request->get(
'rf'));
45 $username = $userDao->getUserByPk($userId);
46 $canEdit = $userDao->isAdvisorOrAdmin($userId, $groupId);
47 if (empty($rf) || ! $canEdit) {
49 'aaData' => json_encode($this->getArrayArrayData($groupId, $canEdit)),
52 return $this->
render(
'advice_license.html.twig', $this->mergeWithDefault($vars));
55 $vars = $this->getDataRow($groupId, $rf);
56 if ($vars ===
false) {
57 return $this->flushContent( _(
'invalid license candidate'));
60 if ($request->get(
'save')) {
62 $vars = $this->
saveInput($request, $vars, $userId);
63 $vars[
'message'] =
'Successfully updated.';
64 }
catch (\Exception $e) {
65 $vars = array(
'rf_spdx_id' => $request->get(
'spdx_id'),
66 'rf_shortname' => $request->get(
'shortname'),
67 'rf_fullname' => $request->get(
'fullname'),
68 'rf_text' => $request->get(
'rf_text'),
69 'rf_url' => $request->get(
'url'),
70 'rf_notes' => $request->get(
'note'),
71 'rf_risk' => intval($request->get(
'risk'))
73 $vars[
'message'] = $e->getMessage();
77 return $this->
render(
'advice_license-edit.html.twig', $this->mergeWithDefault($vars));
81 private function getArrayArrayData($groupId,$canEdit)
83 $sql =
"SELECT rf_pk,rf_spdx_id,rf_shortname,rf_fullname,rf_text,rf_url,rf_notes,marydone,user_name FROM license_candidate lc INNER JOIN users ON lc.rf_user_fk_created=users.user_pk WHERE lc.group_fk=$1";
85 $dbManager = $this->
getObject(
'db.manager');
86 $dbManager->prepare($stmt = __METHOD__, $sql);
87 $res = $dbManager->execute($stmt, array($groupId));
89 while ($row = $dbManager->fetchArray($res)) {
90 $aData = array(htmlentities($row[
'rf_spdx_id']),
91 htmlentities($row[
'rf_shortname']), htmlentities($row[
'rf_fullname']),
92 '<div style="overflow-y:scroll;max-height:150px;margin:0;">' . nl2br(htmlentities($row[
'rf_text'])) .
'</div>',
93 htmlentities($row[
'rf_url']),
94 htmlentities($row[
'user_name']),
95 $this->bool2checkbox($dbManager->booleanFromDb($row[
'marydone']))
99 $edit =
'<a href="' . $link .
'"><img border="0" src="images/button_edit.png"></a>';
100 array_unshift($aData,$edit);
104 $dbManager->freeResult($res);
109 private function getDataRow($groupId, $licId)
112 return array(
'rf_pk' => -1,
'rf_shortname' =>
'');
114 $sql =
"SELECT rf_pk,rf_spdx_id,rf_shortname,rf_fullname,rf_text,rf_url," .
115 "rf_notes,rf_lastmodified,rf_user_fk_modified,rf_user_fk_created," .
116 "rf_creationdate,marydone,rf_risk FROM license_candidate " .
117 "WHERE group_fk=$1 AND rf_pk=$2";
119 $dbManager = $this->
getObject(
'db.manager');
120 $row = $dbManager->getSingleRow($sql, array($groupId, $licId), __METHOD__);
121 if (
false !== $row) {
122 $row[
'marydone'] = $dbManager->booleanFromDb($row[
'marydone']);
126 $username = $userDao->getUserByPk($row[
'rf_user_fk_created']);
127 $row[
'rf_user_fk_created'] = $username[
'user_name'];
128 $username = $userDao->getUserByPk($row[
'rf_user_fk_modified']);
129 $row[
'rf_user_fk_modified'] = $username[
'user_name'];
135 private function bool2checkbox($bool)
137 $check = $bool ?
' checked="checked"' :
'';
138 return '<input type="checkbox"' . $check .
' disabled="disabled"/>';
148 private function saveInput(Request $request, $oldRow, $userId)
150 $spdxLicenses =
new SpdxLicenses();
152 $spdxId = $request->get(
'spdx_id');
153 $shortname = $request->get(
'shortname');
154 $fullname = $request->get(
'fullname');
155 $rfText = $request->get(
'rf_text');
156 $url = $request->get(
'url');
157 $marydone = $request->get(
'marydone');
158 $note = $request->get(
'note');
159 $riskLvl = intval($request->get(
'risk'));
160 $lastmodified = date(DATE_ATOM);
161 $userIdcreated = $userId;
162 $userIdmodified = $userId;
164 if (empty($shortname) || empty($fullname) || empty($rfText)) {
165 throw new \Exception(
'missing shortname (or) fullname (or) reference text');
169 $licenseDao = $this->
getObject(
'dao.license');
170 $ok = ($oldRow[
'rf_shortname'] == $shortname);
175 throw new \Exception(
'shortname already in use');
177 if ($oldRow[
'rf_pk'] == -1) {
178 $oldRow[
'rf_pk'] = $licenseDao->insertUploadLicense($shortname, $rfText,
Auth::getGroupId(), $userId);
181 if (! empty($spdxId) &&
182 strstr(strtolower($spdxId), strtolower(LicenseRef::SPDXREF_PREFIX)) ===
false) {
183 if (! $spdxLicenses->validate($spdxId)) {
186 } elseif (empty($spdxId)) {
189 if (! empty($spdxId)) {
193 $licenseDao->updateCandidate($oldRow[
'rf_pk'], $shortname, $fullname,
194 $rfText, $url, $note, $lastmodified, $userIdmodified, !empty($marydone),
200 register_plugin(
new AdviceLicense());
Contains the constants and helpers for authentication of user.
static getUserId()
Get the current user's id.
static getGroupId()
Get the current user's group id.
static convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
static replaceSpaces($licenseName)
render($templateName, $vars=null, $headers=null)
saveInput(Request $request, $oldRow, $userId)
Traceback_uri()
Get the URI without query to this location.
Traceback_parm($ShowMod=1)
Get the URI query to this location.
Convert2BrowserTime($server_time)
Convert the server time to browser time.