diff --git a/adminator3/app/Controllers/Core/workController.php b/adminator3/app/Controllers/Core/workController.php
index 3c2ff5d81..e11a407ab 100644
--- a/adminator3/app/Controllers/Core/workController.php
+++ b/adminator3/app/Controllers/Core/workController.php
@@ -49,21 +49,29 @@ public function work(ServerRequestInterface $request, ResponseInterface $respons
return $this->response;
};
- // $this->smarty->assign("action", $_SERVER['SCRIPT_URL']);
+ $csrf = $this->generateCsrfToken($request, $response, true);
+ // $this->logger->info("workController\work: csrf generated: ".var_export($csrf_name, true));
$assignData = [
"page_title" => "Adminator3 :: Work",
+ "csrf_html" => $csrf[0],
];
$work = new work($this->container);
+ list($allItemsRs, $allItemsData) = $work->getAllItems();
+ $assignData["items_list_select"] = $allItemsData;
+
$rs = $work->taskGroupList();
- $assignData["p_bs_alerts"] = $work->p_bs_alerts;
if ($rs[0] === true) {
$assignData["work_list_groups_items"] = $rs[1];
}
+ $work->handleSingleActionForm();
+
+ $assignData["p_bs_alerts"] = $work->p_bs_alerts;
+
return $this->renderer->template($request, $response, 'work/work.tpl', $assignData);
}
}
diff --git a/adminator3/app/Core/work.php b/adminator3/app/Core/work.php
index 2afa45722..7ceb652c0 100644
--- a/adminator3/app/Core/work.php
+++ b/adminator3/app/Core/work.php
@@ -6,15 +6,18 @@
use SebastianBergmann\Type\VoidType;
use Illuminate\Support\Facades\Redis;
use HyssaDev\HibikenAsynqClient\Client;
+use Exception;
-class work
+class work extends adminator
{
// DI
- protected \Monolog\Logger $logger;
+ public \Monolog\Logger $logger;
- protected \mysqli|\PDO $conn_mysql;
+ public \mysqli|\PDO $conn_mysql;
- protected \PgSql\Connection|\PDO|null $conn_pgsql;
+ public \PgSql\Connection|\PDO|null $conn_pgsql;
+
+ public \PDO|null $pdoMysql;
protected $sentinel;
@@ -29,12 +32,17 @@ class work
*/
public array $p_bs_alerts = [];
+ public $action_form;
+
+ public int $form_single_action;
+
public function __construct(ContainerInterface $container)
{
// $this->container = $container;
$this->logger = $container->get('logger');
$this->conn_mysql = $container->get('connMysql');
$this->conn_pgsql = $container->get('connPgsql');
+ $this->pdoMysql = $container->get('pdoMysql');
$this->sentinel = $container->get('sentinel');
@@ -46,6 +54,49 @@ public function __construct(ContainerInterface $container)
$this->logger->info(message: __CLASS__ . "\\" . __FUNCTION__ . " called");
}
+ public function callPdoQueryAndFetch($query): array
+ {
+ $rs_error = null;
+ try {
+ $rs = $this->pdoMysql->query($query);
+ } catch (Exception $e) {
+ $rs_error = $e->getMessage();
+ }
+
+ if (is_object($rs)) {
+ $rs_data = $rs->fetchAll();
+
+ } else {
+ $this->logger->error(__CLASS__ . "\\" . __FUNCTION__ . ": PDO result is not object");
+ $rs_data = [];
+ }
+
+ return [$rs_data, $rs_error];
+ }
+
+ public function getAllItems(): array
+ {
+ $q = "SELECT id, name FROM workitems_names WHERE id > 0 ORDER BY id";
+ list($data_rs, $dotaz_error) = $this->callPdoQueryAndFetch($q);
+
+ if ($dotaz_error != null) {
+ $this->logger->error(__CLASS__ . "\\" . __FUNCTION__ . ": Caught Exception: " . var_export($dotaz_error, true));
+ $this->p_bs_alerts["Nelze načíst data pro výpis akcí pro manuální restart.
(SQL error: $dotaz_error)"] = "danger";
+
+ return [false, []];
+
+ } elseif (count($data_rs) < 1) {
+ $this->p_bs_alerts["Žádné data pro výpis akcí pro manuální restart."] = "warning";
+
+ return [true, []];
+ } else {
+ foreach ($data_rs as $key => $val) {
+ $itemsList[] = ["id" => $val["id"], "name" => $val["name"]];
+ }
+ return [true, $itemsList];
+ }
+ }
+
public function getItemName(int $id): string|null
{
$rs_item_name = $this->conn_mysql->query("SELECT name FROM workitems_names WHERE id = '$id' ");
@@ -56,7 +107,43 @@ public function getItemName(int $id): string|null
return $item_name;
}
- public function taskEnqueue(int $item_id): bool|int
+ public function handleSingleActionForm(): void
+ {
+ $this->logger->info(__CLASS__ . "\\" . __FUNCTION__ . " called");
+
+ // get form data
+ $this->action_form = $this->formInit();
+ $form_data = $this->action_form->validate("single_action");
+ $this->form_single_action = intval($form_data["single_action"]);
+
+ $this->logger->debug(__CLASS__ . "\\" . __FUNCTION__ . ": " . var_export($this->form_single_action, true));
+
+ // check if form was sended
+ if ($this->form_single_action > 0) {
+ // test if we have valid ID
+ $item_name = $this->getItemName($this->form_single_action);
+
+ if (is_null($item_name)) {
+ $this->logger->warning(message: __CLASS__ . "\\" . __FUNCTION__ . ": parsing item_name failed (item_id $this->form_single_action)");
+ } else {
+ [$queue_rs, $queue_err] = $this->taskEnqueue($this->form_single_action);
+ if ($queue_rs) {
+ $this->p_bs_alerts["Manuální přidání akce pro restart bylo provedeno úspěšně"] = "success";
+ } else {
+ $this->p_bs_alerts["Manuální přidání akce pro restart selhalo. ($queue_err)"] = "danger";
+ $this->logger->error(message: __CLASS__ . "\\" . __FUNCTION__ . ": single_action failed ($queue_err)");
+ }
+ }
+ }
+ }
+
+ /**
+ * @return array [
+ * bool, // false if something failed
+ * bool|int|string, // results from asynq_client or error message
+ * ]
+ */
+ public function taskEnqueue(int $item_id): array
{
$this->logger->info(__CLASS__ . "\\" . __FUNCTION__ . " called");
@@ -79,10 +166,10 @@ public function taskEnqueue(int $item_id): bool|int
} catch (\RedisException $ex) {
$m = $ex->getMessage();
$this->logger->error(__CLASS__ . "\\" . __FUNCTION__ . ": Redis error: $m");
- return false;
+ return [false, "Redis error: $m"];
}
- return $res;
+ return [true, $res];
}
public function taskGroupList(): array
@@ -144,17 +231,18 @@ public function work_handler($item_id): array
$item_name = $this->getItemName($item_id);
if (is_null($item_name)) {
- //TODO: add warning over bootstrap.JS
+ // TODO: check/fix rendering in objekty/topology page(s)
+ $this->p_bs_alerts["Nepodařilo se načíst název WorkItem položky.
(item_id: $item_id)"] = "warning";
$this->logger->warning(message: __CLASS__ . "\\" . __FUNCTION__ . ": parsing item_name failed (item_id $item_id)");
} else {
$this->logger->info(message: __CLASS__ . "\\" . __FUNCTION__ . ": parsed item_name: " . var_export($item_name, true));
}
// asynqClient part
- $rs_queue = $this->taskEnqueue($item_id);
- $this->logger->debug(__CLASS__ . "\\" . __FUNCTION__ . ": rs_queue: " . var_export($rs_queue, true));
+ [$queue_rs, $queue_err] = $this->taskEnqueue($item_id);
+ $this->logger->debug(__CLASS__ . "\\" . __FUNCTION__ . ": rs_queue: " . var_export($queue_rs, true));
- if ($rs_queue) {
+ if ($queue_rs) {
$rs_write = 1;
} else {
$rs_write = 0;
@@ -172,7 +260,7 @@ public function work_handler($item_id): array
// generate output view
$output .= "