FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
Xpview.php
1 <?php
2 /*
3  Copyright (C) 2014-2015, Siemens AG
4  Author: Johannes Najjar
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 
25 
38 use ui_view;
39 
44 class Xpview extends DefaultPlugin
45 {
47  protected $optionName;
49  protected $ajaxAction;
51  protected $modBack;
53  protected $uploadDao;
55  protected $copyrightDao;
57  protected $agentDao;
59  protected $highlightRenderer;
61  protected $decisionTypes;
63  protected $decisionTableName;
65  protected $tableName;
67  protected $agentName;
69  protected $highlightTypeToStringMap;
71  protected $typeToHighlightTypeMap;
73  protected $skipOption;
75  protected $xptext;
76 
77  function __construct($name, $params)
78  {
79  $mergedParams = array_merge($params, array(self::DEPENDENCIES=>array("browse", "view"),
80  self::PERMISSION=> Auth::PERM_READ));
81 
82  parent::__construct($name, $mergedParams);
83  $this->agentName = $this->tableName;
84 
85  $this->uploadDao = $this->getObject('dao.upload');
86  $this->copyrightDao = $this->getObject('dao.copyright');
87  $this->agentDao = $this->getObject('dao.agent');
88  $this->highlightRenderer = $this->getObject('view.highlight_renderer');
89  $this->decisionTypes = $this->getObject('decision.types');
90  }
91 
96  protected function handle(Request $request)
97  {
98  $vars = array();
99  $uploadId = intval($request->get('upload'));
100  $uploadTreeId = intval($request->get('item'));
101  if (empty($uploadTreeId) || empty($uploadId))
102  {
103  $text = _("Empty Input");
104  $vars['message']= "<h2>$text</h2>";
105  return $this->responseBad($vars);
106  }
107 
108  if( !$this->uploadDao->isAccessible($uploadId, Auth::getGroupId()) ) {
109  $text = _("Permission Denied");
110  $vars['message']= "<h2>$text</h2>";
111  return $this->responseBad();
112  }
113 
114  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
115  $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId, $uploadTreeTableName);
116  if (Isdir($uploadEntry['ufile_mode']) || Iscontainer($uploadEntry['ufile_mode']))
117  {
118  $parent = $this->uploadDao->getUploadParent($uploadEntry['upload_fk']);
119  if (!isset($parent))
120  {
121  return $this->responseBad();
122  }
123 
124  $uploadTree = $this->uploadDao->getNextItem($uploadEntry['upload_fk'], $parent);
125  if ($uploadTree === UploadDao::NOT_FOUND)
126  {
127  return $this->responseBad();
128  }
129  $uploadTreeId = $uploadTree->getId();
130 
131  return new RedirectResponse(Traceback_uri() . '?mod=' . $this->getName() . Traceback_parm_keep(array('show','upload')) . "&item=$uploadTreeId");
132  }
133  if (empty($uploadTreeId))
134  {
135  return $this->responseBad('No item selected.');
136  }
137 
138  $copyrightDecisionMap = $this->decisionTypes->getMap();
139  $vars['micromenu'] = Dir2Browse($this->modBack, $uploadTreeId, NULL, $showBox = 0, "View", -1, '', '', $uploadTreeTableName);
140 
141  $lastItem = GetParm("lastItem", PARM_INTEGER);
142  $changed= GetParm("changedSomething", PARM_STRING);
143  $userId = Auth::getUserId();
144  if (!empty($lastItem) && $changed =="true")
145  {
146  $lastUploadEntry = $this->uploadDao->getUploadEntry($lastItem, $uploadTreeTableName);
147  $clearingType = GetParm('clearingTypes', PARM_INTEGER);
148  $description = trim(GetParm('description', PARM_STRING));
149  $textFinding = trim(GetParm('textFinding', PARM_STRING));
150  $comment = trim(GetParm('comment', PARM_STRING));
151  $decision_pk = GetParm('decision_pk', PARM_INTEGER);
152  if (empty($clearingType) || empty($textFinding)) {
153  if (empty($clearingType)) {
154  $text = _("The clearing type cannot be empty. " .
155  "Please choose a value and submit again.");
156  } else {
157  $text = _("The text finding cannot be empty. " .
158  "Please enter a text and submit again.");
159  }
160  $vars['message']= "<strong>$text</strong>";
161  } else {
162  $this->copyrightDao->saveDecision($this->decisionTableName,
163  $lastUploadEntry['pfile_fk'], $userId , $clearingType, $description,
164  $textFinding, $comment, $decision_pk);
165  }
166  }
167 
168  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
169  $scanJobProxy->createAgentStatus(array($this->agentName));
170  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
171  $highlights = array();
172  if(array_key_exists($this->agentName, $selectedScanners))
173  {
174  $latestXpAgentId = $selectedScanners[$this->agentName];
175  $highlights = $this->copyrightDao->getHighlights($uploadTreeId, $this->tableName, $latestXpAgentId, $this->typeToHighlightTypeMap);
176  }
177 
178  if (count($highlights) < 1)
179  {
180  $vars['message'] = _("No ").$this->tableName ._(" data is available for this file.");
181  }
182 
183  /* @var $view ui_view */
184  $view = plugin_find("view");
185  $theView = $view->getView(null, null, $showHeader=0, "", $highlights, false, true);
186  list($pageMenu, $textView) = $theView;
187 
188  $decisions = $this->copyrightDao->getDecisions($this->decisionTableName, $uploadEntry['pfile_fk']);
189 
190  $vars['agentName'] = $this->agentName;
191  $vars['decisions'] = $decisions;
192  $vars['decisionsTable'] = $this->decisionTableName;
193  $vars['itemId'] = $uploadTreeId;
194  $vars['uploadId'] = $uploadId;
195  $vars['pfile'] = $uploadEntry['pfile_fk'];
196  $vars['pageMenu'] = $pageMenu;
197  $vars['textView'] = $textView;
198  $vars['legendBox'] = $this->legendBox();
199  $vars['uri'] = Traceback_uri() . "?mod=" . $this->Name;
200  $vars['optionName'] = $this->optionName;
201  $vars['formName'] = "CopyRightForm";
202  $vars['ajaxAction'] = $this->ajaxAction;
203  $vars['skipOption'] =$this->skipOption;
204  $vars['clearingTypes'] = $copyrightDecisionMap;
205  $vars['xptext'] = $this->xptext;
206 
207  $agentId = intval($request->get("agent"));
208  $vars = array_merge($vars,$this->additionalVars($uploadId, $uploadTreeId, $agentId));
209  return $this->render('ui-cp-view.html.twig',$this->mergeWithDefault($vars));
210  }
211 
220  protected function additionalVars($uploadId, $uploadTreeId, $agentId)
221  {
222  return array();
223  }
224 
229  protected function mergeWithDefault($vars)
230  {
231  $allVars = array_merge($this->getDefaultVars(), $vars);
232  $allVars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
233  return $allVars;
234  }
235 
241  private function responseBad($vars=array())
242  {
243  $vars['content'] = 'This upload contains no files!<br><a href="' . Traceback_uri() . '?mod=browse">Go back to browse view</a>';
244  return $this->render("include/base.html.twig",$vars);
245  }
246 
251  function legendBox()
252  {
253  $output = _("file text");
254  foreach ($this->highlightTypeToStringMap as $colorKey => $txt)
255  {
256  $output .= '<br/>' . $this->highlightRenderer->createStartSpan($colorKey, $txt) . $txt . '</span>';
257  }
258  return $output;
259  }
260 
265  function RegisterMenus()
266  {
267  $tooltipText = _("Copyright/Email/Url/Author");
268  menu_insert("Browse-Pfile::Copyright/Email/Url", 0, 'copyright-view', $tooltipText);
269 
270  $itemId = GetParm("item", PARM_INTEGER);
271  $textFormat = $this->microMenu->getFormatParameter($itemId);
272  $pageNumber = GetParm("page", PARM_INTEGER);
273  $this->microMenu->addFormatMenuEntries($textFormat, $pageNumber);
274 
275  // For all other menus, permit coming back here.
276  $uploadId = GetParm("upload", PARM_INTEGER);
277  if (!empty($itemId) && !empty($uploadId))
278  {
279  $menuText = "Copyright/Email/Url/Author";
280  $menuPosition = 57;
281  $tooltipText = _("View Copyright/Email/Url/Author info");
282  $URI = $this->getName() . Traceback_parm_keep(array("show", "format", "page", "upload", "item"));
283  $this->microMenu->insert(MicroMenu::TARGET_DEFAULT, $menuText, $menuPosition, $this->getName(), $URI, $tooltipText);
284  }
285  $licId = GetParm("lic", PARM_INTEGER);
286  if (!empty($licId))
287  {
288  $this->NoMenu = 1;
289  }
290  }
291 }
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:274
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
responseBad($vars=array())
Call on bad uploads.
Definition: Xpview.php:241
render($templateName, $vars=null, $headers=null)
Iscontainer($mode)
Definition: common-dir.php:49
Namespace for Copyright agent&#39;s UI components.
RegisterMenus()
Customize submenus.
Definition: Xpview.php:265
legendBox()
Create legend box.
Definition: Xpview.php:251
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
const PARM_STRING
Definition: common-parm.php:29
const PARM_INTEGER
Definition: common-parm.php:25
Isdir($mode)
Definition: common-dir.php:31
additionalVars($uploadId, $uploadTreeId, $agentId)
Get additional variables for a give item.
Definition: Xpview.php:220
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item...
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695