FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AjaxBrowse.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2014-2015 Siemens AG
4  * Author: J.Najjar, S. Weber
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
20 namespace Fossology\UI\Ajax;
21 
34 
36 {
37  const NAME = "browse-processPost";
38 
40  private $uploadDao;
42  private $userDao;
44  private $dbManager;
46  private $dataTablesUtility;
48  private $filterParams;
50  private $userPerm;
52  private $statusTypes;
53 
54  function __construct()
55  {
56  parent::__construct(self::NAME, array(
57  self::REQUIRES_LOGIN => false,
58  self::PERMISSION => Auth::PERM_READ
59  ));
60  global $container;
61  $this->uploadDao = $container->get('dao.upload');
62  $this->userDao = $container->get('dao.user');
63  $this->dbManager = $container->get('db.manager');
64  $this->dataTablesUtility = $container->get('utils.data_tables_utility');
65  }
66 
70  protected function handle(Request $request)
71  {
72  $groupId = Auth::getGroupId();
73  $gup = $this->dbManager->getSingleRow('SELECT group_perm FROM group_user_member WHERE user_fk=$1 AND group_fk=$2',
74  array(Auth::getUserId(), $groupId), __METHOD__ . '.user_perm');
75  if (!$gup) {
76  throw new \Exception('You are assigned to wrong group.');
77  }
78  $this->userPerm = $gup['group_perm'];
79 
80  $uploadId = intval($request->get('uploadId'));
81  if ($uploadId && !$this->uploadDao->isAccessible($uploadId, $groupId)) {
82  throw new \Exception('You cannot access to this upload');
83  }
84 
85  $columnName = $request->get('columnName');
86  $statusId = intval($request->get('statusId'));
87  $value = intval($request->get('value'));
88  $moveUpload = intval($request->get("move"));
89  $beyondUpload = intval($request->get("beyond"));
90  $commentText = $request->get('commentText');
91  $direction = $request->get('direction');
92 
93  if (! empty($columnName) && ! empty($uploadId) && ! empty($value)) {
94  $uploadBrowseProxy = new UploadBrowseProxy($groupId, $this->userPerm, $this->dbManager);
95  $uploadBrowseProxy->updateTable($columnName, $uploadId, $value);
96  } else if (! empty($moveUpload) && ! empty($beyondUpload)) {
97  $uploadBrowseProxy = new UploadBrowseProxy($groupId, $this->userPerm, $this->dbManager);
98  $uploadBrowseProxy->moveUploadBeyond($moveUpload, $beyondUpload);
99  } else if (! empty($uploadId) && ! empty($direction)) {
100  $uploadBrowseProxy = new UploadBrowseProxy($groupId, $this->userPerm, $this->dbManager);
101  $uploadBrowseProxy->moveUploadToInfinity($uploadId, $direction == 'top');
102  } else if (!empty($uploadId) && !empty($commentText) && !empty($statusId)) {
103  $uploadBrowseProxy = new UploadBrowseProxy($groupId, $this->userPerm, $this->dbManager);
104  $uploadBrowseProxy->setStatusAndComment($uploadId, $statusId, $commentText);
105  } else {
106  return $this->respondFolderGetTableData($request);
107  }
108  return new Response('');
109  }
110 
111 
116  protected function respondFolderGetTableData(Request $request)
117  {
118  /* Get list of uploads in this folder */
119  list($result, $iTotalDisplayRecords, $iTotalRecords) = $this->getListOfUploadsOfFolder($request);
120 
121  $uri = Traceback_uri() . "?mod=license";
122  /* Browse-Pfile menu */
123  $menuPfile = menu_find("Browse-Pfile", $menuDepth);
124  /* Browse-Pfile menu without the compare menu item */
125  $menuPfileNoCompare = menu_remove($menuPfile, "Compare");
126 
127  $users = $this->userDao->getUserChoices();
128 
129  $statusTypesAvailable = $this->uploadDao->getStatusTypeMap();
130  if (!$this->userPerm) {
131  unset($statusTypesAvailable[4]);
132  }
133 
134  $output = array();
135  $rowCounter = 0;
136  while ($row = $this->dbManager->fetchArray($result)) {
137  if (empty($row['upload_pk']) || !$this->uploadDao->isAccessible($row['upload_pk'],Auth::getGroupId())) {
138  continue;
139  }
140  $rowCounter++;
141  $output[] = $this->showRow($row, $request, $uri, $menuPfile, $menuPfileNoCompare, $statusTypesAvailable, $users, $rowCounter);
142  }
143  $this->dbManager->freeResult($result);
144  return new JsonResponse(array(
145  'sEcho' => intval($request->get('sEcho')),
146  'aaData' => $output,
147  'iTotalRecords' => $iTotalRecords,
148  'iTotalDisplayRecords' => $iTotalDisplayRecords
149  ));
150  }
151 
152 
164  private function showRow($row,Request $request, $uri, $menuPfile, $menuPfileNoCompare, $statusTypesAvailable, $users, $rowCounter)
165  {
166  $show = $request->get('show');
167  $folder = $request->get('folder');
168 
169  $uploadId = intval($row['upload_pk']);
170  $description = htmlentities($row['upload_desc']);
171 
172  $fileName = $row['ufile_name'];
173  if (empty($fileName)) {
174  $fileName = $row['upload_filename'];
175  }
176 
177  $itemId = Isartifact($row['ufile_mode']) ? DirGetNonArtifact($row['uploadtree_pk']) : $row['uploadtree_pk'];
178 
179  $nameColumn = "<b>$fileName</b>";
180  if (IsContainer($row['ufile_mode'])) {
181  $nameColumn = "<a href='$uri&upload=$uploadId&folder=$folder&item=$itemId&show=$show'>$nameColumn</a>";
182  }
183  $nameColumn .= "<br>";
184  if (!empty($description)) {
185  $nameColumn .= "<i>$description</i><br>";
186  }
187  $Parm = "upload=$uploadId&show=$show&item=" . $row['uploadtree_pk'];
188  if (Iscontainer($row['ufile_mode'])) {
189  $nameColumn .= MenuRenderer::menuToActiveSelect($menuPfile, $Parm, $uploadId);
190  } else {
191  $nameColumn .= MenuRenderer::menuToActiveSelect($menuPfileNoCompare, $Parm, $uploadId);
192  }
193 
194  $modsUploadMulti = MenuHook::getAgentPluginNames('UploadMulti');
195  if (!empty($modsUploadMulti)) {
196  $nameColumn = '<input type="checkbox" name="uploads[]" class="browse-upload-checkbox" value="'.$uploadId.'"/>'.$nameColumn;
197  }
198 
199  $dateCol = Convert2BrowserTime(substr($row['upload_ts'], 0, 19));
200  $pairIdPrio = array($uploadId, floatval($row[UploadBrowseProxy::PRIO_COLUMN]));
201  if (!$this->userPerm && 4 == $row['status_fk']) {
202  $currentStatus = $this->statusTypes[4];
203  } else {
204  $statusAction = " onchange =\"changeTableEntry(this, $uploadId,'status_fk' )\" ";
205  $currentStatus = $this->createSelect("Status" . $this->userPerm . "Of_$rowCounter", $statusTypesAvailable, $row['status_fk'], $statusAction);
206  }
207  if ($this->userPerm) {
208  $action = " onchange =\"changeTableEntry(this, $uploadId, 'assignee')\"";
209  $currentAssignee = $this->createSelectUsers("AssignedTo_$rowCounter", $users, $row['assignee'], $action );
210  } else {
211  $currentAssignee = array_key_exists($row['assignee'], $users) ? $users[$row['assignee']] : _('Unassigned');
212  }
213  $rejectableUploadId = ($this->userPerm || $row['status_fk'] < 4) ? $uploadId : 0;
214  $tripleComment = array($rejectableUploadId, $row['status_fk'], htmlspecialchars($row['status_comment']));
215 
216  $sql = "SELECT rf_pk, rf_shortname FROM upload_clearing_license ucl, license_ref"
217  . " WHERE ucl.group_fk=$1 AND upload_fk=$2 AND ucl.rf_fk=rf_pk";
218  $stmt = __METHOD__.'.collectMainLicenses';
219  $this->dbManager->prepare($stmt, $sql);
220  $res = $this->dbManager->execute($stmt,array(Auth::getGroupId(),$uploadId));
221  $mainLicenses = array();
222  while ($lic=$this->dbManager->fetchArray($res)) {
223  $mainLicenses[] = '<a onclick="javascript:window.open(\''.Traceback_uri()
224  ."?mod=popup-license&rf=$lic[rf_pk]','License text','width=600,height=400,toolbar=no,scrollbars=yes,resizable=yes');"
225  .'" href="javascript:;">'.$lic['rf_shortname'].'</a>'
226  ."<img onclick=\"removeMainLicense($uploadId,$lic[rf_pk]);\" class=\"delete\" src=\"images/space_16.png\" alt=\"\"/></img>";
227  }
228  $this->dbManager->freeResult($res);
229 
230  $output = array($nameColumn, $currentStatus, $tripleComment, implode(', ', $mainLicenses), $currentAssignee, $dateCol, $pairIdPrio);
231  return $output;
232  }
233 
240  private function createSelectUsers($selectElementName, $databaseMap, $selectedValue, $action = "")
241  {
242  if (array_key_exists($_SESSION['UserId'], $databaseMap)) {
243  $databaseMap[$_SESSION['UserId']] = _('-- Me --');
244  }
245  $databaseMap[1] = _('Unassigned');
246  return $this->createSelect($selectElementName,$databaseMap, $selectedValue,$action);
247  }
248 
249 
250  private function createSelect($id,$options,$select='',$action='')
251  {
252  $html = "<select name=\"$id\" id=\"$id\" $action class=\"ui-render-select2\">";
253  foreach ($options as $key=>$disp) {
254  $html .= '<option value="'.$key.'"';
255  if ($key == $select) {
256  $html .= ' selected';
257  }
258  $html .= ">$disp</option>";
259  }
260  $html .= '</select>';
261  return $html;
262  }
263 
264 
269  private function getListOfUploadsOfFolder(Request $request)
270  {
271  $uploadBrowseProxy = new UploadBrowseProxy(Auth::getGroupId(), $this->userPerm, $this->dbManager);
272  $params = array($request->get('folder'));
273  $partQuery = $uploadBrowseProxy->getFolderPartialQuery($params);
274 
275  $iTotalRecordsRow = $this->dbManager->getSingleRow("SELECT count(*) FROM $partQuery", $params, __METHOD__ . "count.all");
276  $iTotalRecords = $iTotalRecordsRow['count'];
277 
278  $this->filterParams = $params;
279  $filter = $this->getSearchString($request->get('sSearch'));
280  $filter .= $this->getIntegerFilter(intval($request->get('assigneeSelected')), 'assignee');
281  $filter .= $this->getIntegerFilter(intval($request->get('statusSelected')), 'status_fk');
282 
283  $iTotalDisplayRecordsRow = $this->dbManager->getSingleRow("SELECT count(*) FROM $partQuery $filter",
284  $this->filterParams, __METHOD__ . ".count.". $filter);
285  $iTotalDisplayRecords = $iTotalDisplayRecordsRow['count'];
286 
287  $orderString = $this->getOrderString();
288  $stmt = __METHOD__ . "getFolderContents" . $orderString . $filter;
289 
290  $statementString = "SELECT upload.*,upload_clearing.*,uploadtree.ufile_name,uploadtree.ufile_mode,uploadtree.uploadtree_pk"
291  . " FROM $partQuery $filter $orderString";
292  $rangedFilterParams = $this->filterParams;
293  $rangedFilterParams[] = intval($request->get('iDisplayStart'));
294  $statementString .= ' OFFSET $' . count($rangedFilterParams);
295  $rangedFilterParams[] = intval($request->get('iDisplayLength'));
296  $statementString .= ' LIMIT $' . count($rangedFilterParams);
297 
298  $this->dbManager->prepare($stmt, $statementString);
299  $result = $this->dbManager->execute($stmt, $rangedFilterParams);
300 
301  return array($result, $iTotalDisplayRecords, $iTotalRecords);
302  }
303 
304  private function getOrderString()
305  {
306  $columnNamesInDatabase = array('upload_filename', 'upload_clearing.status_fk', 'UNUSED', 'UNUSED', 'upload_clearing.assignee', 'upload_ts', 'upload_clearing.priority');
307 
308  $orderString = $this->dataTablesUtility->getSortingString($_GET, $columnNamesInDatabase);
309 
310  return $orderString;
311  }
312 
313  private function getSearchString($searchPattern)
314  {
315  if (empty($searchPattern)) {
316  return '';
317  }
318  $this->filterParams[] = "%$searchPattern%";
319  return ' AND upload_filename ilike $' . count($this->filterParams) . ' ';
320  }
321 
326  private function getIntegerFilter($var, $columnName)
327  {
328  if (empty($var)) {
329  return '';
330  }
331  $this->filterParams[] = $var;
332  return " AND $columnName=$" . count($this->filterParams) . ' ';
333  }
334 }
335 
336 register_plugin(new AjaxBrowse());
getIntegerFilter($var, $columnName)
Definition: AjaxBrowse.php:326
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
getListOfUploadsOfFolder(Request $request)
Definition: AjaxBrowse.php:269
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu...
respondFolderGetTableData(Request $request)
Definition: AjaxBrowse.php:116
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:298
Iscontainer($mode)
Definition: common-dir.php:49
static getAgentPluginNames($hook='Agents')
Definition: MenuHook.php:27
static menuToActiveSelect($menu, &$parm, $uploadId="")
createSelectUsers($selectElementName, $databaseMap, $selectedValue, $action="")
Definition: AjaxBrowse.php:240
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk)...
Definition: common-dir.php:169
showRow($row, Request $request, $uri, $menuPfile, $menuPfileNoCompare, $statusTypesAvailable, $users, $rowCounter)
Definition: AjaxBrowse.php:164
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
menu_remove($Menu, $RmName)
Remove a menu object (based on an object name) from a menu list.
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
Isartifact($mode)
Definition: common-dir.php:40
handle(Request $request)
Display the loaded menu and plugins.
Definition: AjaxBrowse.php:70