Activity panel: Refactor to use the new JsonPage and a dedicated controller for more readability 👌

This commit is contained in:
Molkobain
2021-03-16 16:39:41 +01:00
parent 0d7302d530
commit d2f6ac5015
6 changed files with 139 additions and 118 deletions

View File

@@ -454,7 +454,7 @@ $(function()
$.post(
this.options.save_state_endpoint,
{
'operation': 'save_activity_panel_state',
'operation': 'activity_panel_save_state',
'object_class': this._GetHostObjectClass(),
'object_mode': this._GetHostObjectMode(),
'is_expanded': this.element.hasClass(this.css_classes.is_expanded),
@@ -791,7 +791,7 @@ $(function()
// Prepare parameters
let oParams = {
operation: 'add_caselog_entries',
operation: 'activity_panel_add_caselog_entries',
object_class: this._GetHostObjectClass(),
object_id: this._GetHostObjectID(),
transaction_id: this.options.transaction_id,
@@ -812,15 +812,15 @@ $(function()
alert(sErrorThrown);
})
.done(function (oData) {
if (false === oData.success) {
if (false === oData.data.success) {
// TODO 3.0.0: Same comment as the fail() callback
alert(oData.error_message);
alert(oData.data.error_message);
return false;
}
// Update the feed
for (let sCaseLogAttCode in oData.entries) {
me._AddEntry(sCaseLogAttCode, oData.entries[sCaseLogAttCode]);
for (let sCaseLogAttCode in oData.data.entries) {
me._AddEntry(sCaseLogAttCode, oData.data.entries[sCaseLogAttCode]);
}
me._ApplyEntriesFilters();

View File

@@ -282,6 +282,7 @@ return array(
'Combodo\\iTop\\Application\\UI\\Printable\\BlockPrintHeader\\BlockPrintHeader' => $baseDir . '/sources/application/UI/Printable/BlockPrintHeader/BlockPrintHeader.php',
'Combodo\\iTop\\Composer\\iTopComposer' => $baseDir . '/sources/Composer/iTopComposer.php',
'Combodo\\iTop\\Controller\\AjaxRenderController' => $baseDir . '/sources/Controller/AjaxRenderController.php',
'Combodo\\iTop\\Controller\\Base\\Layout\\ActivityPanelController' => $baseDir . '/sources/Controller/Base/Layout/ActivityPanelController.php',
'Combodo\\iTop\\DesignDocument' => $baseDir . '/core/designdocument.class.inc.php',
'Combodo\\iTop\\DesignElement' => $baseDir . '/core/designdocument.class.inc.php',
'Combodo\\iTop\\Form\\Field\\BlobField' => $baseDir . '/sources/Form/Field/BlobField.php',

View File

@@ -512,6 +512,7 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
'Combodo\\iTop\\Application\\UI\\Printable\\BlockPrintHeader\\BlockPrintHeader' => __DIR__ . '/../..' . '/sources/application/UI/Printable/BlockPrintHeader/BlockPrintHeader.php',
'Combodo\\iTop\\Composer\\iTopComposer' => __DIR__ . '/../..' . '/sources/Composer/iTopComposer.php',
'Combodo\\iTop\\Controller\\AjaxRenderController' => __DIR__ . '/../..' . '/sources/Controller/AjaxRenderController.php',
'Combodo\\iTop\\Controller\\Base\\Layout\\ActivityPanelController' => __DIR__ . '/../..' . '/sources/Controller/Base/Layout/ActivityPanelController.php',
'Combodo\\iTop\\DesignDocument' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php',
'Combodo\\iTop\\DesignElement' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php',
'Combodo\\iTop\\Form\\Field\\BlobField' => __DIR__ . '/../..' . '/sources/Form/Field/BlobField.php',

View File

@@ -5,6 +5,7 @@
*/
use Combodo\iTop\Controller\AjaxRenderController;
use Combodo\iTop\Controller\Base\Layout\ActivityPanelController;
use Combodo\iTop\Renderer\Console\ConsoleBlockRenderer;
use Combodo\iTop\Renderer\Console\ConsoleFormRenderer;
@@ -2730,12 +2731,14 @@ EOF
$oPage->add(json_encode($aResult));
break;
//--------------------------------
// Activity panel
//--------------------------------
/** @internal */
case 'save_activity_panel_state':
$oPage->SetContentType('application/json');
case 'activity_panel_save_state':
$oPage = new JsonPage();
try {
$oAjaxRenderController::SaveActivityPanelState();
ActivityPanelController::SaveActivityPanelState();
$aResult = [
'success' => true,
];
@@ -2746,13 +2749,14 @@ EOF
'error_message' => $oException->getMessage(),
];
}
$oPage->add(json_encode($aResult));
$oPage->SetData($aResult);
break;
case 'add_caselog_entries':
$oPage->SetContentType('application/json');
/** @internal */
case 'activity_panel_add_caselog_entries':
$oPage = new JsonPage();
try {
$aResult = AjaxRenderController::AddCaseLogsEntries();
$aResult = ActivityPanelController::AddCaseLogsEntries();
}
catch (Exception $oException) {
$aResult = [
@@ -2760,12 +2764,12 @@ EOF
'error_message' => $oException->getMessage(),
];
}
$oPage->add(json_encode($aResult));
$oPage->SetData($aResult);
break;
case 'get_menus_count':
$oAjaxRenderController->GetMenusCount($oPage);
$oAjaxRenderController->GetMenusCount($oPage);
break;
default:

View File

@@ -18,9 +18,6 @@ use CMDBObjectSet;
use CMDBSource;
use Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableSettings;
use Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableUIBlockFactory;
use Combodo\iTop\Application\UI\Base\Layout\ActivityPanel\ActivityEntry\ActivityEntryFactory;
use Combodo\iTop\Application\UI\Base\Layout\ActivityPanel\ActivityPanelHelper;
use Combodo\iTop\Renderer\BlockRenderer;
use DBObjectSearch;
use DBObjectSet;
use DBSearch;
@@ -30,7 +27,6 @@ use ExecutionKPI;
use Expression;
use FieldExpression;
use FunctionExpression;
use InlineImage;
use JsonPage;
use MetaModel;
use ScalarExpression;
@@ -391,105 +387,6 @@ class AjaxRenderController
return $bRet;
}
/**
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MySQLException
*/
public static function SaveActivityPanelState(): void
{
$sObjectClass = utils::ReadPostedParam('object_class', '', utils::ENUM_SANITIZATION_FILTER_CLASS);
$sObjectMode = utils::ReadPostedParam('object_mode');
$bIsExpanded = utils::ReadPostedParam('is_expanded');
$bIsClosed = utils::ReadPostedParam('is_closed');
if (false === empty($bIsExpanded)) {
ActivityPanelHelper::SaveExpandedStateForClass($sObjectClass, $sObjectMode, ('true' === $bIsExpanded));
}
if (false === empty($bIsClosed)) {
ActivityPanelHelper::SaveClosedStateForClass($sObjectClass, $sObjectMode, ('true' === $bIsClosed));
}
}
/**
* Add new entries to some of the object's (identified by posted parameters) case logs
*
* @return array The status of the update, a renewed transaction ID and the entries as HTML so they can be append to the front.
* [
* 'success' => true,
* 'entries' => [
* '<ATT_CODE_1>' => [
* html_rendering => '<HTML_RENDERING_TO_BE_APPEND_IN_FRONT_END>',
* ],
* '<ATT_CODE_2>' => [
* html_rendering => '<HTML_RENDERING_TO_BE_APPEND_IN_FRONT_END>',
* ],
* ...
* ],
* 'renewed_transaction_id' => '<RENEWED_TRANSACTION_ID>',
* ]
*
* @throws \ArchivedObjectException
* @throws \CoreCannotSaveObjectException
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \OQLException
* @throws \ReflectionException
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public static function AddCaseLogsEntries(): array
{
$sObjectClass = utils::ReadPostedParam('object_class', null, utils::ENUM_SANITIZATION_FILTER_CLASS);
$sObjectId = utils::ReadPostedParam('object_id', 0);
$sTransactionId = utils::ReadPostedParam('transaction_id', null, utils::ENUM_SANITIZATION_FILTER_TRANSACTION_ID);
$aEntries = utils::ReadPostedParam('entries', [], utils::ENUM_SANITIZATION_FILTER_RAW_DATA);
// Consistency checks
// - Mandatory parameters
if (empty($sObjectClass) || empty($sObjectId) || empty($sTransactionId) || empty($aEntries)) {
throw new Exception('Missing mandatory parameters object_class / object_id / transaction_id / entries');
}
// - Transaction ID
// Note: We keep the transaction ID for several reasons:
// - We might send several messages, so renewing it would not make such a difference except making the follwoing line harder
// - We need the transaction ID to passed in the JS snippet that allows images to be uploaded (see InlineImage::EnableCKEditorImageUpload()), renewing it would only make things more complicated
// => For all those reasons, we let the GC clean the transactions IDs, just like when a transaction ID is not deleted when cancelling a regular object edition.
if (!utils::IsTransactionValid($sTransactionId, false)) {
throw new Exception(Dict::S('iTopUpdate:Error:InvalidToken'));
}
$aResults = [
'success' => true,
'entries' => [],
];
// Note: Will trigger an exception if object does not exists or not accessible to the user
$oObject = MetaModel::GetObject($sObjectClass, $sObjectId);
foreach ($aEntries as $sAttCode => $aData) {
// Add entry to object
$oObject->Set($sAttCode, $aData['value']);
// Make entry rendering to send back to the front
$aEntryAsArray = $oObject->Get($sAttCode)->GetAsArray()[0];
$oEntryBlock = ActivityEntryFactory::MakeFromCaseLogEntryArray($sAttCode, $aEntryAsArray);
$oEntryBlock->SetCaseLogRank((int)$aData['rank']);
$sEntryAsHtml = BlockRenderer::RenderBlockTemplates($oEntryBlock);
$aResults['entries'][$sAttCode] = [
'html_rendering' => $sEntryAsHtml,
];
}
$oObject->DBWrite();
// Finalize inline images
InlineImage::FinalizeInlineImages($oObject);
return $aResults;
}
/**
* @param string $sStyle
* @param string $sFilter

View File

@@ -0,0 +1,118 @@
<?php
/*
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Controller\Base\Layout;
use Combodo\iTop\Application\UI\Base\Layout\ActivityPanel\ActivityEntry\ActivityEntryFactory;
use Combodo\iTop\Application\UI\Base\Layout\ActivityPanel\ActivityPanelHelper;
use Combodo\iTop\Renderer\BlockRenderer;
use Dict;
use Exception;
use InlineImage;
use MetaModel;
use utils;
class ActivityPanelController
{
/**
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MySQLException
*/
public static function SaveActivityPanelState(): void
{
$sObjectClass = utils::ReadPostedParam('object_class', '', utils::ENUM_SANITIZATION_FILTER_CLASS);
$sObjectMode = utils::ReadPostedParam('object_mode');
$bIsExpanded = utils::ReadPostedParam('is_expanded');
$bIsClosed = utils::ReadPostedParam('is_closed');
if (false === empty($bIsExpanded)) {
ActivityPanelHelper::SaveExpandedStateForClass($sObjectClass, $sObjectMode, ('true' === $bIsExpanded));
}
if (false === empty($bIsClosed)) {
ActivityPanelHelper::SaveClosedStateForClass($sObjectClass, $sObjectMode, ('true' === $bIsClosed));
}
}
/**
* Add new entries to some of the object's (identified by posted parameters) case logs
*
* @return array The status of the update, a renewed transaction ID and the entries as HTML so they can be append to the front.
* [
* 'success' => true,
* 'entries' => [
* '<ATT_CODE_1>' => [
* html_rendering => '<HTML_RENDERING_TO_BE_APPEND_IN_FRONT_END>',
* ],
* '<ATT_CODE_2>' => [
* html_rendering => '<HTML_RENDERING_TO_BE_APPEND_IN_FRONT_END>',
* ],
* ...
* ],
* 'renewed_transaction_id' => '<RENEWED_TRANSACTION_ID>',
* ]
*
* @throws \ArchivedObjectException
* @throws \CoreCannotSaveObjectException
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \OQLException
* @throws \ReflectionException
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public static function AddCaseLogsEntries(): array
{
$sObjectClass = utils::ReadPostedParam('object_class', null, utils::ENUM_SANITIZATION_FILTER_CLASS);
$sObjectId = utils::ReadPostedParam('object_id', 0);
$sTransactionId = utils::ReadPostedParam('transaction_id', null, utils::ENUM_SANITIZATION_FILTER_TRANSACTION_ID);
$aEntries = utils::ReadPostedParam('entries', [], utils::ENUM_SANITIZATION_FILTER_RAW_DATA);
// Consistency checks
// - Mandatory parameters
if (empty($sObjectClass) || empty($sObjectId) || empty($sTransactionId) || empty($aEntries)) {
throw new Exception('Missing mandatory parameters object_class / object_id / transaction_id / entries');
}
// - Transaction ID
// Note: We keep the transaction ID for several reasons:
// - We might send several messages, so renewing it would not make such a difference except making the follwoing line harder
// - We need the transaction ID to passed in the JS snippet that allows images to be uploaded (see InlineImage::EnableCKEditorImageUpload()), renewing it would only make things more complicated
// => For all those reasons, we let the GC clean the transactions IDs, just like when a transaction ID is not deleted when cancelling a regular object edition.
if (!utils::IsTransactionValid($sTransactionId, false)) {
throw new Exception(Dict::S('iTopUpdate:Error:InvalidToken'));
}
$aResults = [
'success' => true,
'entries' => [],
];
// Note: Will trigger an exception if object does not exists or not accessible to the user
$oObject = MetaModel::GetObject($sObjectClass, $sObjectId);
foreach ($aEntries as $sAttCode => $aData) {
// Add entry to object
$oObject->Set($sAttCode, $aData['value']);
// Make entry rendering to send back to the front
$aEntryAsArray = $oObject->Get($sAttCode)->GetAsArray()[0];
$oEntryBlock = ActivityEntryFactory::MakeFromCaseLogEntryArray($sAttCode, $aEntryAsArray);
$oEntryBlock->SetCaseLogRank((int)$aData['rank']);
$sEntryAsHtml = BlockRenderer::RenderBlockTemplates($oEntryBlock);
$aResults['entries'][$sAttCode] = [
'html_rendering' => $sEntryAsHtml,
];
}
$oObject->DBWrite();
// Finalize inline images
InlineImage::FinalizeInlineImages($oObject);
return $aResults;
}
}