FOSSology  4.6.0
Open Source License Compliance by Open Source Software
AjaxAdminScheduler.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\UI\Ajax;
10 
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 
18 {
19  const NAME = "ajax_admin_scheduler";
21  private $dbManager;
22 
23  function __construct()
24  {
25  parent::__construct(self::NAME, array(
26  self::TITLE => _("URL"),
27  self::PERMISSION => Auth::PERM_WRITE
28  ));
29 
30  $this->dbManager = $this->getObject('db.manager');
31  }
32 
37  public function handle(Request $request)
38  {
39  $V = '';
40  $operation = $request->get('operation');
41  $vars['jobOptions'] = $this->jobListOption($operation);
42  $vars['operation'] = $operation;
43  $vars['priorityList'] = $this->priorityListOption($request->get('fromRest'));
44  $content = $this->renderer->load('ajax-admin-scheduler.html.twig')->render($vars);
45  $restRes = [
46  'jobList' => $vars['jobOptions'] ?? [],
47  'priorityList' => $vars['priorityList'] ?? [],
48  'verboseList' => [],
49  'agentList' => [],
50  ];
51 
52  if ('pause' == $operation || 'restart' == $operation ||
53  'status' == $operation || 'priority' == $operation) {
54  $V = $content;
55  } else if ('verbose' == $operation) {
56  $verbose_list_option = $this->verboseListOption($request->get('fromRest'));
57  $text2 = _("Select a verbosity level");
58  $V = $content .
59  "<br>$text2: <select name='level_list' id='level_list'>$verbose_list_option</select>";
60  $restRes['verboseList'] = $verbose_list_option;
61  } else if ('agents' == $operation) {
63  $dbManager = $this->getObject('db.manager');
64  $dbManager->prepare($stmt = __METHOD__ . '.getAgents',
65  'SELECT MAX(agent_pk) agent_id, agent_name FROM agent WHERE agent_enabled GROUP BY agent_name');
66  $res = $dbManager->execute($stmt);
67  $V = '<ul>';
68  while ($row = $dbManager->fetchArray($res)) {
69  $restRes['agentList'][] = $row['agent_name'];
70  $V .= "<li>$row[agent_name]</li>";
71  }
72  $V .= '</ul>';
73  $dbManager->freeResult($res);
74  }
75 
76  if ($request->get('fromRest')) {
77  return $restRes;
78  }
79  return new Response($V, Response::HTTP_OK, array('content-type'=>'text/htm')); // not 'text/html' while console-logging
80  }
81 
87  private function convertToAssociative($simpleArray)
88  {
89  $result = [];
90  foreach ($simpleArray as $item) {
91  $result[$item] = $item;
92  }
93  return $result;
94  }
95 
101  function jobListOption($type)
102  {
103  if (empty($type)) {
104  return array();
105  }
106 
107  $job_array = array();
108  if ('status' == $type || 'verbose' == $type || 'priority' == $type) {
109  $job_array = $this->convertToAssociative(GetRunnableJobList());
110  if ('priority' != $type) {
111  $job_array[0] = "scheduler";
112  }
113  }
114  if ('pause' == $type) {
115  $job_array = $this->convertToAssociative(GetRunnableJobList());
116  }
117  if ('restart' == $type) {
118  $job_array = $this->convertToAssociative(GetRunnableJobList());
119  }
120  return $job_array;
121  }
122 
127  function verboseListOption($fromRest = false)
128  {
129  $verbose_list_option = "";
130  $min = 1;
131  $max = 3;
132  $restRes = [];
133  for ($i = $min; $i <= $max; $i++) {
134  $bitmask= (1<<$i) - 1;
135  $verbose_list_option .= "<option value='$bitmask'>$i</option>";
136  $restRes[] = $bitmask;
137  }
138  if ($fromRest) {
139  return $restRes;
140  }
141  return $verbose_list_option;
142  }
143 
148  function priorityListOption($fromRest = false)
149  {
150  $min = -20;
151  $max = 20;
152  $resRes = [];
153  $priority_list = array();
154  for ($i = $min; $i <= $max; $i++) {
155  $priority_list[$i]=$i;
156  $resRes[] = $i;
157  }
158  if ($fromRest) {
159  return $resRes;
160  }
161  return $priority_list;
162  }
163 }
164 
165 register_plugin(new AjaxAdminScheduler());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
convertToAssociative($simpleArray)
Convert simple array to associative array for Twig macro.
jobListOption($type)
get the job list for the specified operation
verboseListOption($fromRest=false)
get the verbose list: if the value of verbose is 1, set verbose as 1
priorityListOption($fromRest=false)
get the priority list for setting, -20..20
GetRunnableJobList()
Get runnable job list, the process is below:
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16