Initial xentral_oss_20.3.c9ffacf

This commit is contained in:
Alex
2021-05-21 08:49:41 +02:00
parent 5406e4a551
commit 34e5ac43d9
18884 changed files with 2109867 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\RoleSurvey;
use Xentral\Core\DependencyInjection\ContainerInterface;
final class Bootstrap
{
/**
* @return array
*/
public static function registerServices(): array
{
return [
'SurveyGateway' => 'onInitSurveyGateway',
'SurveyService' => 'onInitSurveyService',
];
}
/**
* @return array
*/
public static function registerJavascript(): array
{
return [
'RoleSurvey' => [
'./classes/Modules/RoleSurvey/www/js/RoleSurvey.js',
],
];
}
/**
* @return array
*/
public static function registerStylesheets(): array
{
return [
'RoleSurvey' => [
'./classes/Modules/RoleSurvey/www/css/RoleSurvey.css',
],
];
}
/**
* @param ContainerInterface $container
*
* @return SurveyService
*/
public function onInitSurveyService(ContainerInterface $container): SurveyService
{
return new SurveyService($container->get('Database'), $container->get('SurveyGateway'));
}
/**
* @param ContainerInterface $container
*
* @return SurveyGateway
*/
public function onInitSurveyGateway(ContainerInterface $container): SurveyGateway
{
return new SurveyGateway($container->get('Database'));
}
}
@@ -0,0 +1,7 @@
<?php
namespace Xentral\Modules\RoleSurvey\Exception;
final class InvalidArgumentException extends \InvalidArgumentException implements SurveyExceptionInterface
{
}
@@ -0,0 +1,7 @@
<?php
namespace Xentral\Modules\RoleSurvey\Exception;
interface SurveyExceptionInterface
{
}
@@ -0,0 +1,174 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\RoleSurvey;
use Xentral\Components\Database\Database;
final class SurveyGateway
{
/** @var Database $db */
private $db;
/**
* SurveyGateway constructor.
*
* @param Database $db
*/
public function __construct(Database $db)
{
$this->db = $db;
}
/**
* @param int $surveyId
*
* @return array
*/
public function getById($surveyId): array
{
return $this->db->fetchRow(
'SELECT `id`, `name`, `once_per_user`, `send_to_xentral`, `module`, `action`
FROM `survey`
WHERE `id` = :id',
['id' => (int)$surveyId]
);
}
/**
* @param string $name
*
* @return array
*/
public function getByName($name): array
{
return $this->db->fetchRow(
'SELECT `id`, `name`, `once_per_user`, `send_to_xentral`, `module`, `action`
FROM `survey`
WHERE `name` = :name',
['name' => (string)$name]
);
}
/**
* @param string $module
*
* @return array
*/
public function getByModule($module): array
{
return $this->db->fetchAll(
'SELECT `id`, `name`, `once_per_user`, `send_to_xentral`, `module`, `action`
FROM `survey`
WHERE `module` = :module',
['module' => (string)$module]
);
}
/**
* @param string $module
* @param string $action
* @param int $userId
*
* @return array
*/
public function getOpenSurveysByModuleAndUser($module, $action, $userId): array
{
return $this->db->fetchAll(
'SELECT `s`.`id`, `s`.`name`, `s`.`once_per_user`, `s`.`send_to_xentral`, `s`.`module`, `s`.`action`
FROM `survey` AS `s`
LEFT JOIN `survey_user` AS `su` ON `s`.`id` = `su`.`survey_id` AND `su`.`user_id` = :userId
WHERE `s`.`module` = :module AND `s`.`action` = :action AND `s`.`once_per_user` = 1
AND `su`.`id` IS NULL ',
[
'module' => (string)$module,
'action' => (string)$action,
'userId' => (int)$userId,
]
);
}
/**
* @param int $surveyId
* @param int $userId
*
* @return bool
*/
public function isSurveyOpenForUser($surveyId, $userId): bool
{
return !empty(
$this->db->fetchRow(
'SELECT `s`.`id`
FROM `survey` AS `s`
LEFT JOIN `survey_user` AS `su` ON `s`.`id` = `su`.`survey_id` AND `su`.`user_id` = :userId
WHERE `s`.`id` = :surveyId AND `su`.`id` IS NULL AND `s`.`once_per_user` = 1
LIMIT 1',
[
'surveyId' => (int)$surveyId,
'userId' => (int)$userId,
]
)
);
}
/**
* @param int $surveyId
* @param int $userId
*
* @return array
*/
public function getFilledSurveyByUser($surveyId, $userId): array
{
return $this->db->fetchRow(
'SELECT `su`.`id`, `su`.`survey_id`, `su`.`data`
FROM `survey` AS `s`
INNER JOIN `survey_user` AS `su` ON `s`.`id` = `su`.`survey_id` AND `su`.`user_id` = :userId
WHERE `s`.`id` = :surveyId
LIMIT 1',
[
'surveyId' => (int)$surveyId,
'userId' => (int)$userId,
]
);
}
/**
* @param int $id
*
* @return array
*/
public function getFilledById($id): array
{
return $this->db->fetchRow(
'SELECT `su`.`id`, `su`.`survey_id`, `su`.`data`
FROM `survey` AS `s`
INNER JOIN `survey_user` AS `su` ON `s`.`id` = `su`.`survey_id`
WHERE `su`.`id` = :id
LIMIT 1',
[
'id' => (int)$id,
]
);
}
/**
* @param int $surveyId
*
* @return array
*/
public function getFilledBySurvey($surveyId): array
{
return $this->db->fetchAll(
'SELECT `su`.`id`, `s`.name, `su`.`survey_id`, `su`.`data`
FROM `survey` AS `s`
INNER JOIN `survey_user` AS `su` ON `s`.`id` = `su`.`survey_id`
WHERE `s`.`id` = :surveyId',
[
'surveyId' => (int)$surveyId,
]
);
}
}
@@ -0,0 +1,178 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\RoleSurvey;
use Xentral\Components\Database\Database;
use Xentral\Modules\RoleSurvey\Exception\InvalidArgumentException;
class SurveyService
{
/** @var Database $db */
private $db;
/** @var SurveyGateway $gateway */
private $gateway;
/**
* SurveyService constructor.
*
* @param Database $db
* @param SurveyGateway $gateway
*/
public function __construct(Database $db, SurveyGateway $gateway)
{
$this->db = $db;
$this->gateway = $gateway;
}
/**
* @param string $name
* @param string $module
* @param string $action
* @param bool $oncePerUser
* @param bool $sendToXentral
*
* @return int
*/
public function create($name, $module, $action, $oncePerUser, $sendToXentral): int
{
if (empty($name)) {
throw new InvalidArgumentException('Name is empty');
}
$survey = $this->gateway->getByName($name);
if (!empty($survey)) {
throw new InvalidArgumentException('survey allready exists');
}
$this->db->perform(
'INSERT INTO `survey` (`name`, `module`, `action`, `once_per_user`, `send_to_xentral`)
VALUES (:name, :module, :action, :oncePerUser, :sendToXentral)',
[
'name' => $name,
'module' => (string)$module,
'action' => (string)$action,
'oncePerUser' => (int)(bool)$oncePerUser,
'sendToXentral' => (int)(bool)$sendToXentral,
]
);
return $this->db->lastInsertId();
}
/**
* @param int $surveyId
* @param int $userId
*/
public function clearUserData($surveyId, $userId): void
{
$survey = $this->gateway->getById($surveyId);
if (empty($survey)) {
throw new InvalidArgumentException('survey not found');
}
$this->db->perform(
'DELETE FROM `survey_user` WHERE `survey_id` = :surveyId AND `user_id` = :userId',
[
'surveyId' => $surveyId,
'userId' => $userId,
]
);
}
/**
* @param int $surveyId
* @param int $userId
* @param array $data
*
* @return int
*/
public function saveUserAnswer($surveyId, $userId, $data): int
{
$survey = $this->gateway->getById($surveyId);
if (empty($survey)) {
throw new InvalidArgumentException('survey not found');
}
$filled = $this->gateway->getFilledSurveyByUser($surveyId, $userId);
if (empty($filled)) {
$this->db->perform(
'INSERT INTO `survey_user` (`survey_id`, `user_id`, `data`, `created_at`)
VALUES (:surveyId, :userId, :data, NOW())',
[
'surveyId' => (int)$surveyId,
'userId' => (int)$userId,
'data' => (string)json_encode($data),
]
);
return $this->db->lastInsertId();
}
$json = json_decode($filled['data'], true);
if (empty($json)) {
$json = [];
}
$data = array_merge($json, $data);
$this->db->perform(
'UPDATE `survey_user` SET `data` = :data WHERE `id` = :id',
[
'data' => (string)json_encode($data),
'id' => (int)$filled['id'],
]
);
return (int)$filled['id'];
}
/**
* @param int $surveyId
* @param string $url
* @param string $serial
* @param string $key
*/
public function sendToXentral($surveyId, $url, $serial, $key): void
{
$survey = $this->gateway->getById($surveyId);
if (empty($survey)) {
throw new InvalidArgumentException('survey not found');
}
if (empty($survey['send_to_xentral'])) {
throw new InvalidArgumentException('survey not marked as sendable');
}
$filled = $this->gateway->getFilledBySurvey($surveyId);
if (empty($filled)) {
throw new InvalidArgumentException('no data to send');
}
if (empty($serial) || empty($key)) {
throw new InvalidArgumentException('no serial number');
}
foreach($filled as $rowKey => $row) {
$filled[$rowKey]['name'] = $survey['name'];
}
$paras = [
'serial' => $serial,
'schluessel' => $key,
'paras' => json_encode($filled),
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($paras));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (substr((string)$info['http_code'], '2') !== 0) {
throw new InvalidArgumentException('xentral: ' . $ret);
}
}
}
@@ -0,0 +1,65 @@
new Vue({
el: '#role-survey',
data: {
showAssistant: true,
pagination: false,
pages: [{
type: 'survey',
submitType: 'survey',
submitUrl: 'index.php?module=welcome&action=survey&cmd=saveSurveyData',
icon: 'survey-icon',
headline: 'Wähle Deine Rolle(n) in xentral',
subHeadline: 'Damit hilfst Du uns, xentral noch besser zu machen',
dataRequiredForSubmit: {
surveyName: 'xentral_role'
},
form: [{
id:0,
name: 'role-survey',
surveyButtons: [
{
title: 'Vertrieb',
value: 'sales'
}, {
title: 'Einkauf',
value: 'purchase'
}, {
title: 'Verwaltung / Office',
value: 'administration'
}, {
title: 'Customer Service',
value: 'customerservice'
}, {
title: 'Lager / Logistik',
value: 'warehouse'
}, {
title: 'Controlling / Finance',
value: 'controlling'
}, {
title: 'Management',
value: 'management'
}, {
title: 'Produktion',
value: 'production'
},{
title: 'Sonstiges',
value: 'misc'
}
]
}],
ctaButtons: [{
title: 'Senden',
action: 'submit'
}],
errorMsg: "Bitte wähle mindestens eine Rolle"
}, {
type: 'defaultPage',
icon: 'thanks-icon',
headline: 'Vielen Dank für Deine Hilfe',
ctaButtons: [{
title: 'OK',
action: 'close'
}]
}]
}
});