Skip to content

Commit

Permalink
Display Alerts : Command input
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMis committed Apr 11, 2024
1 parent 8842775 commit 6b2e834
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 28 deletions.
20 changes: 19 additions & 1 deletion lib/Controller/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,14 @@ public function deleteForm(Request $request, Response $response, $id)
* type="string",
* required=false
* ),
* @SWG\Parameter(
* name="createAlertOn",
* in="formData",
* description="On command execution, when should a Display alert be created?
* success, failure, always or never",
* type="string",
* required=false
* ),
* @SWG\Response(
* response=201,
* description="successful operation",
Expand Down Expand Up @@ -398,6 +406,7 @@ public function add(Request $request, Response $response)
$command->userId = $this->getUser()->userId;
$command->commandString = $sanitizedParams->getString('commandString');
$command->validationString = $sanitizedParams->getString('validationString');
$command->createAlertOn = $sanitizedParams->getString('createAlertOn', ['default' => 'never']);
$availableOn = $sanitizedParams->getArray('availableOn');
if (empty($availableOn)) {
$command->availableOn = null;
Expand Down Expand Up @@ -476,6 +485,14 @@ public function add(Request $request, Response $response)
* type="string",
* required=false
* ),
* @SWG\Parameter(
* name="createAlertOn",
* in="formData",
* description="On command execution, when should a Display alert be created?
* success, failure, always or never",
* type="string",
* required=false
* ),
* @SWG\Response(
* response=200,
* description="successful operation",
Expand All @@ -496,6 +513,7 @@ public function edit(Request $request, Response $response, $id)
$command->description = $sanitizedParams->getString('description');
$command->commandString = $sanitizedParams->getString('commandString');
$command->validationString = $sanitizedParams->getString('validationString');
$command->createAlertOn = $sanitizedParams->getString('createAlertOn', ['default' => 'never']);
$availableOn = $sanitizedParams->getArray('availableOn');
if (empty($availableOn)) {
$command->availableOn = null;
Expand Down Expand Up @@ -552,7 +570,7 @@ public function delete(Request $request, Response $response, $id)
throw new AccessDeniedException();
}

$this->getDispatcher()->dispatch(CommandDeleteEvent::$NAME, new CommandDeleteEvent($command));
$this->getDispatcher()->dispatch(new CommandDeleteEvent($command), CommandDeleteEvent::$NAME);

$command->delete();

Expand Down
1 change: 1 addition & 0 deletions lib/Controller/DisplayProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ public function edit(Request $request, Response $response, $id)
// Set and assign the command
$command->commandString = $parsedParams->getString('commandString_' . $command->commandId);
$command->validationString = $parsedParams->getString('validationString_' . $command->commandId);
$command->createAlertOn = $parsedParams->getString('createAlertOn_' . $command->commandId);

$displayProfile->assignCommand($command);
} else {
Expand Down
87 changes: 72 additions & 15 deletions lib/Entity/Command.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/*
* Copyright (c) 2022 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
Expand Down Expand Up @@ -126,6 +126,21 @@ class Command implements \JsonSerializable
*/
public $availableOn;

/**
* @SWG\Property(
* description="Define if execution of this command should create an alert on success, failure, always or never."
* )
* @var string
*/
public $createAlertOn;

/**
* @SWG\Property(
* description="Create Alert On specific to the provided DisplayProfile."
* )
*/
public $createAlertOnDisplayProfile;

/**
* @SWG\Property(description="A comma separated list of groups/users with permissions to this Command")
* @var string
Expand Down Expand Up @@ -174,7 +189,19 @@ public function getCommandString()
*/
public function getValidationString()
{
return empty($this->validationStringDisplayProfile) ? $this->validationString : $this->validationStringDisplayProfile;
return empty($this->validationStringDisplayProfile)
? $this->validationString
: $this->validationStringDisplayProfile;
}

/**
* @return string
*/
public function getCreateAlertOn(): string
{
return empty($this->createAlertOnDisplayProfile)
? $this->createAlertOn
: $this->createAlertOnDisplayProfile;
}

/**
Expand Down Expand Up @@ -210,18 +237,24 @@ public function isReady()
public function validate()
{
if (!v::stringType()->notEmpty()->length(1, 254)->validate($this->command)) {
throw new InvalidArgumentException(__('Please enter a command name between 1 and 254 characters'),
'command');
throw new InvalidArgumentException(
__('Please enter a command name between 1 and 254 characters'),
'command'
);
}

if (!v::alpha('_')->NoWhitespace()->notEmpty()->length(1, 50)->validate($this->code)) {
throw new InvalidArgumentException(__('Please enter a code between 1 and 50 characters containing only alpha characters and no spaces'),
'code');
throw new InvalidArgumentException(
__('Please enter a code between 1 and 50 characters containing only alpha characters and no spaces'),
'code'
);
}

if (!v::stringType()->length(0, 1000)->validate($this->description)) {
throw new InvalidArgumentException(__('Please enter a description between 1 and 1000 characters'),
'description');
throw new InvalidArgumentException(
__('Please enter a description between 1 and 1000 characters'),
'description'
);
}
}

Expand Down Expand Up @@ -251,22 +284,44 @@ public function save($options = [])
*/
public function delete()
{
$this->getStore()->update('DELETE FROM `command` WHERE `commandId` = :commandId', ['commandId' => $this->commandId]);
$this->getStore()->update(
'DELETE FROM `command` WHERE `commandId` = :commandId',
['commandId' => $this->commandId]
);
}

private function add()
{
$this->commandId = $this->getStore()->insert('
INSERT INTO `command` (`command`, `code`, `description`, `userId`, `commandString`, `validationString`, `availableOn`)
VALUES (:command, :code, :description, :userId, :commandString, :validationString, :availableOn)
INSERT INTO `command` (
`command`,
`code`,
`description`,
`userId`,
`commandString`,
`validationString`,
`availableOn`,
`createAlertOn`
)
VALUES (
:command,
:code,
:description,
:userId,
:commandString,
:validationString,
:availableOn,
:createAlertOn
)
', [
'command' => $this->command,
'code' => $this->code,
'description' => $this->description,
'userId' => $this->userId,
'commandString' => $this->commandString,
'validationString' => $this->validationString,
'availableOn' => $this->availableOn
'availableOn' => $this->availableOn,
'createAlertOn' => $this->createAlertOn
]);
}

Expand All @@ -280,7 +335,8 @@ private function edit()
`userId` = :userId,
`commandString` = :commandString,
`validationString` = :validationString,
`availableOn` = :availableOn
`availableOn` = :availableOn,
`createAlertOn` = :createAlertOn
WHERE `commandId` = :commandId
', [
'command' => $this->command,
Expand All @@ -290,7 +346,8 @@ private function edit()
'commandId' => $this->commandId,
'commandString' => $this->commandString,
'validationString' => $this->validationString,
'availableOn' => $this->availableOn
'availableOn' => $this->availableOn,
'createAlertOn' => $this->createAlertOn
]);
}
}
38 changes: 31 additions & 7 deletions lib/Entity/DisplayProfile.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
Expand Down Expand Up @@ -302,13 +302,15 @@ public function assignCommand($command)
if ($alreadyAssigned->getId() == $command->getId()) {
$alreadyAssigned->commandString = $command->commandString;
$alreadyAssigned->validationString = $command->validationString;
$alreadyAssigned->createAlertOn = $command->createAlertOn;
$assigned = true;
break;
}
}

if (!$assigned)
if (!$assigned) {
$this->commands[] = $command;
}
}

/**
Expand Down Expand Up @@ -481,22 +483,41 @@ private function manageAssignments()
foreach ($this->commands as $command) {
/* @var Command $command */
$this->getStore()->update('
INSERT INTO `lkcommanddisplayprofile` (`commandId`, `displayProfileId`, `commandString`, `validationString`) VALUES
(:commandId, :displayProfileId, :commandString, :validationString) ON DUPLICATE KEY UPDATE commandString = :commandString2, validationString = :validationString2
INSERT INTO `lkcommanddisplayprofile` (
`commandId`,
`displayProfileId`,
`commandString`,
`validationString`,
`createAlertOn`
)
VALUES (
:commandId,
:displayProfileId,
:commandString,
:validationString,
:createAlertOn
)
ON DUPLICATE KEY UPDATE
commandString = :commandString2,
validationString = :validationString2,
createAlertOn = :createAlertOn2
', [
'commandId' => $command->commandId,
'displayProfileId' => $this->displayProfileId,
'commandString' => $command->commandString,
'validationString' => $command->validationString,
'createAlertOn' => $command->createAlertOn,
'commandString2' => $command->commandString,
'validationString2' => $command->validationString
'validationString2' => $command->validationString,
'createAlertOn2' => $command->createAlertOn
]);
}

// Unlink
$params = ['displayProfileId' => $this->displayProfileId];

$sql = 'DELETE FROM `lkcommanddisplayprofile` WHERE `displayProfileId` = :displayProfileId AND `commandId` NOT IN (0';
$sql = 'DELETE FROM `lkcommanddisplayprofile`
WHERE `displayProfileId` = :displayProfileId AND `commandId` NOT IN (0';

$i = 0;
foreach ($this->commands as $command) {
Expand Down Expand Up @@ -554,7 +575,10 @@ public function getCustomEditTemplate()
if ($this->isCustom()) {
return $this->displayProfileFactory->getCustomEditTemplate($this->getClientType());
} else {
$this->getLog()->error('Attempting to get Custom Edit template for Display Profile ' . $this->getClientType() . ' that is not custom');
$this->getLog()->error(
'Attempting to get Custom Edit template for Display Profile ' .
$this->getClientType() . ' that is not custom'
);
return null;
}
}
Expand Down
20 changes: 15 additions & 5 deletions lib/Factory/CommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,19 @@ public function query($sortOrder = null, $filterBy = [])
`command`.userId,
`command`.availableOn,
`command`.commandString,
`command`.validationString ';
`command`.validationString,
`command`.createAlertOn
';

if ($sanitizedFilter->getInt('displayProfileId') !== null) {
$select .= ',
:displayProfileId AS displayProfileId,
`lkcommanddisplayprofile`.commandString AS commandStringDisplayProfile,
`lkcommanddisplayprofile`.validationString AS validationStringDisplayProfile ';
`lkcommanddisplayprofile`.validationString AS validationStringDisplayProfile,
`lkcommanddisplayprofile`.createAlertOn AS createAlertOnDisplayProfile ';
}

$select .= " , (SELECT GROUP_CONCAT(DISTINCT `group`.group)
$select .= ' , (SELECT GROUP_CONCAT(DISTINCT `group`.group)
FROM `permission`
INNER JOIN `permissionentity`
ON `permissionentity`.entityId = permission.entityId
Expand All @@ -135,7 +138,7 @@ public function query($sortOrder = null, $filterBy = [])
WHERE entity = :permissionEntityForGroup
AND objectId = command.commandId
AND view = 1
) AS groupsWithPermissions ";
) AS groupsWithPermissions ';
$params['permissionEntityForGroup'] = 'Xibo\\Entity\\Command';

$body = ' FROM `command` ';
Expand Down Expand Up @@ -195,7 +198,14 @@ public function query($sortOrder = null, $filterBy = [])
$params['userId'] = $sanitizedFilter->getInt('userId');
}

$this->viewPermissionSql('Xibo\Entity\Command', $body, $params, 'command.commandId', 'command.userId', $filterBy);
$this->viewPermissionSql(
'Xibo\Entity\Command',
$body,
$params,
'command.commandId',
'command.userId',
$filterBy
);

// Sorting?
$order = '';
Expand Down
1 change: 1 addition & 0 deletions lib/Xmds/Soap5.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ public function RegisterDisplay(
}

$node = $return->createElement($command->code);
$node->setAttribute('createAlertOn', $command->getCreateAlertOn());
$commandString = $return->createElement('commandString');
$commandStringCData = $return->createCDATASection($command->getCommandString());
$commandString->appendChild($commandStringCData);
Expand Down
11 changes: 11 additions & 0 deletions views/command-form-add.twig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@
{% set helpText %}{% trans "Leave empty if this command should be available on all types of Display." %}{% endset %}

{{ forms.dropdown("availableOn[]", "dropdownmulti", title, "", options, "optionid", "option", helpText, "selectPicker") }}

{% set options = [
{ optionid: "never", option: "Never" },
{ optionid: "success", option: "Success" },
{ optionid: "failure", option: "Failure" },
{ optionid: "always", option: "Always" },
] %}
{% set title %}{% trans "Create Alert On" %}{% endset %}
{% set helpText %}{% trans "On command execution, when should a Display alert be created?" %}{% endset %}

{{ forms.dropdown("createAlertOn", "single", title, "never", options, "optionid", "option", helpText) }}
</div>

<div class="tab-pane" id="description">
Expand Down
11 changes: 11 additions & 0 deletions views/command-form-edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@
{% set helpText %}{% trans "Leave empty if this command should be available on all types of Display." %}{% endset %}

{{ forms.dropdown("availableOn[]", "dropdownmulti", title, command.getAvailableOn(), options, "optionid", "option", helpText, "selectPicker") }}

{% set options = [
{ optionid: "never", option: "Never" },
{ optionid: "success", option: "Success" },
{ optionid: "failure", option: "Failure" },
{ optionid: "always", option: "Always" },
] %}
{% set title %}{% trans "Create Alert On" %}{% endset %}
{% set helpText %}{% trans "On command execution, when should a Display alert be created?" %}{% endset %}

{{ forms.dropdown("createAlertOn", "single", title, command.createAlertOn, options, "optionid", "option", helpText) }}
</div>

<div class="tab-pane" id="description">
Expand Down
Loading

0 comments on commit 6b2e834

Please sign in to comment.