Skip to content

Commit

Permalink
Layout: add API to clear layout canvas (#2817)
Browse files Browse the repository at this point in the history
Layout: API method for clearing the canvas.
 xibosignageltd/xibo-private#875

---------

Co-authored-by: Mauro Ferrão <[email protected]>
  • Loading branch information
dasgarner and maurofmferrao authored Dec 19, 2024
1 parent 378154f commit 9d2fa41
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 37 deletions.
63 changes: 46 additions & 17 deletions lib/Controller/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,45 +1086,74 @@ function delete(Request $request, Response $response, $id)
* @param Request $request
* @param Response $response
* @param $id
* @return \Psr\Http\Message\ResponseInterface|Response
* @throws AccessDeniedException
* @throws GeneralException
* @throws InvalidArgumentException
* @throws NotFoundException
* @throws \Xibo\Support\Exception\ControllerNotImplemented
* @return \Slim\Http\Response
* @throws \Xibo\Support\Exception\GeneralException
*
* @SWG\Clear(
* path="/layout/{layoutId}",
* operationId="layoutClear",
* tags={"layout"},
* summary="Clear Layout",
* description="Clears a Layout",
* description="Clear a draft layouts canvas of all widgets and elements, leaving it blank.",
* @SWG\Parameter(
* name="layoutId",
* in="path",
* description="The Layout ID to Clear",
* description="The Layout ID to Clear, must be a draft.",
* type="integer",
* required=true
* ),
* @SWG\Response(
* response=204,
* description="successful operation"
* )
* response=201,
* description="successful operation",
* @SWG\Schema(ref="#/definitions/Layout"),
* @SWG\Header(
* header="Location",
* description="Location of the new record",
* type="string"
* )
* )
*/
function clear(Request $request, Response $response, $id)
public function clear(Request $request, Response $response, $id): Response
{
$layout = $this->layoutFactory->loadById($id);
// Get the existing layout
$layout = $this->layoutFactory->getById($id);

// Make sure we have permission
if (!$this->getUser()->checkEditable($layout)) {
throw new AccessDeniedException(__('You do not have permissions to clear this layout'));
throw new AccessDeniedException();
}

$layout->clear();
// Check that this Layout is a Draft
if (!$layout->isChild()) {
throw new InvalidArgumentException(__('This Layout is not a Draft, please checkout.'), 'layoutId');
}

// Discard the current draft and replace it
$layout->discardDraft(false);

// Blank
$resolution = $this->resolutionFactory->getClosestMatchingResolution($layout->width, $layout->height);
$blank = $this->layoutFactory->createFromResolution(
$resolution->resolutionId,
$layout->ownerId,
$layout->layout,
null,
null,
null,
false
);

// Persist the parentId
$blank->parentId = $layout->parentId;
$blank->campaignId = $layout->campaignId;
$blank->publishedStatusId = 2;
$blank->save(['validate' => false, 'auditMessage' => 'Canvas Cleared']);

// Return
$this->getState()->hydrate([
'httpStatus' => 204,
'message' => sprintf(__('Cleared %s'), $layout->layout)
'message' => sprintf(__('Cleared %s'), $layout->layout),
'id' => $blank->layoutId,
'data' => $blank,
]);

return $this->render($request, $response);
Expand Down
41 changes: 23 additions & 18 deletions lib/Entity/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ public function save($options = [])
'import' => false,
'appendCountOnDuplicate' => false,
'setModifiedDt' => true,
'auditMessage' => null,
], $options);

if ($options['validate']) {
Expand All @@ -909,12 +910,27 @@ public function save($options = [])

if ($options['audit']) {
if ($this->parentId === null) {
$this->audit($this->layoutId, 'Added', ['layoutId' => $this->layoutId, 'layout' => $this->layout, 'campaignId' => $this->campaignId]);
$this->audit(
$this->layoutId,
$options['auditMessage'] ?? 'Added',
[
'layoutId' => $this->layoutId,
'layout' => $this->layout,
'campaignId' => $this->campaignId,
]
);
} else {
$this->audit($this->layoutId, 'Checked out', ['layoutId' => $this->parentId, 'layout' => $this->layout, 'campaignId' => $this->campaignId]);
$this->audit(
$this->layoutId,
$options['auditMessage'] ?? 'Checked out',
[
'layoutId' => $this->parentId,
'layout' => $this->layout,
'campaignId' => $this->campaignId,
]
);
}
}

} else if (($this->hash() != $this->hash && $options['saveLayout']) || $options['setBuildRequired']) {
$this->update($options);

Expand All @@ -923,14 +939,14 @@ public function save($options = [])
$change['campaignId'][] = $this->campaignId;

if ($this->parentId === null) {
$this->audit($this->layoutId, 'Updated', $change);
$this->audit($this->layoutId, $options['auditMessage'] ?? 'Updated', $change);
} else {
$this->audit($this->layoutId, 'Updated Draft', $change);
$this->audit($this->layoutId, $options['auditMessage'] ?? 'Updated Draft', $change);
}
}

} else {
$this->getLog()->info('Save layout properties unchanged for layoutId ' . $this->layoutId . ', status = ' . $this->status);
$this->getLog()->info('Save layout properties unchanged for layoutId ' . $this->layoutId
. ', status = ' . $this->status);
}

if ($options['saveRegions']) {
Expand Down Expand Up @@ -1071,17 +1087,6 @@ public function delete($options = [])
$this->audit($this->layoutId, 'Deleted' . (($this->parentId !== null) ? ' draft for ' . $this->parentId : ''));
}

/**
* Clear Layout
* @param array $options
* @throws GeneralException
*/
public function clear($options = [])
{
// TODO: Clear layout here
$this->getLog()->debug('WIP: Clear Layout');
}

/**
* Validate this layout
* @throws GeneralException
Expand Down
2 changes: 1 addition & 1 deletion lib/Helper/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
class Environment
{
public static $WEBSITE_VERSION_NAME = '4.1.2';
public static $WEBSITE_VERSION_NAME = '4.2.0-alpha';
public static $XMDS_VERSION = '7';
public static $XLF_VERSION = 4;
public static $VERSION_REQUIRED = '8.1.0';
Expand Down
2 changes: 1 addition & 1 deletion ui/src/layout-editor/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ Layout.prototype.clear = function() {

toastr.success(res.message);

lD.reloadData(lD.layout);
lD.reloadData(res.id, {refreshEditor: true});
} else {
// Login Form needed?
if (res.login) {
Expand Down

0 comments on commit 9d2fa41

Please sign in to comment.