N°633 - Portal: CreateBrick now accepts abstract classes. This is a refactoring from an extension (CreateBrickExtended) that has been merge into the core.

SVN:trunk[4553]
This commit is contained in:
Guillaume Lajarige
2017-02-21 08:10:47 +00:00
parent 9a6a562ed2
commit 820d7108e4
10 changed files with 107 additions and 24 deletions

View File

@@ -118,4 +118,5 @@ Dict::Add('CS CZ', 'Czech', 'Čeština', array(
// CreateBrick brick
Dict::Add('CS CZ', 'Czech', 'Čeština', array(
'Brick:Portal:Create:Name' => 'Rychlé vytvoření',
'Brick:Portal:Create:ChooseType' => 'Please, choose a type~~',
));

View File

@@ -113,5 +113,5 @@ Dict::Add('DE DE', 'German', 'Deutsch', array(
// CreateBrick brick
Dict::Add('DE DE', 'German', 'Deutsch', array(
'Brick:Portal:Create:Name' => 'Schnelles Erstellen',
'Brick:Portal:Create:ChooseType' => 'Please, choose a type~~',
));
?>

View File

@@ -114,5 +114,5 @@ Dict::Add('EN US', 'English', 'English', array(
// CreateBrick brick
Dict::Add('EN US', 'English', 'English', array(
'Brick:Portal:Create:Name' => 'Quick creation',
'Brick:Portal:Create:ChooseType' => 'Please, choose a type',
));
?>

View File

@@ -114,5 +114,5 @@ Dict::Add('ES CR', 'Spanish', 'Español, Castellano', array(
// CreateBrick brick
Dict::Add('ES CR', 'Spanish', 'Español, Castellano', array(
'Brick:Portal:Create:Name' => 'Creación rápida',
'Brick:Portal:Create:ChooseType' => 'Please, choose a type~~',
));
?>

View File

@@ -114,5 +114,6 @@ Dict::Add('FR FR', 'French', 'Français', array(
// CreateBrick brick
Dict::Add('FR FR', 'French', 'Français', array(
'Brick:Portal:Create:Name' => 'Création rapide',
'Brick:Portal:Create:ChooseType' => 'Veuillez choisir le type',
));
?>

View File

@@ -22,8 +22,10 @@ namespace Combodo\iTop\Portal\Controller;
use \Silex\Application;
use \Symfony\Component\HttpFoundation\Request;
use \Symfony\Component\HttpKernel\HttpKernelInterface;
use \MetaModel;
use \Combodo\iTop\Portal\Helper\ApplicationHelper;
use \Combodo\iTop\Portal\Helper\ContextManipulatorHelper;
use \Combodo\iTop\Portal\Helper\SecurityHelper;
class CreateBrickController extends BrickController
{
@@ -31,28 +33,62 @@ class CreateBrickController extends BrickController
public function DisplayAction(Request $oRequest, Application $oApp, $sBrickId)
{
$oBrick = ApplicationHelper::GetLoadedBrickFromId($oApp, $sBrickId);
$sObjectClass = $oBrick->GetClass();
$aRouteParams = array(
'sObjectClass' => $oBrick->GetClass()
);
// Preparing redirection route
// - Checking for action rules
$aRules = $oBrick->GetRules();
if (!empty($aRules))
{
$aRouteParams['ar_token'] = ContextManipulatorHelper::EncodeRulesToken($aRules);
}
// - Adding brick id to the params
$aRouteParams['sBrickId'] = $sBrickId;
// - Generating route
$sRedirectRoute = $oApp['url_generator']->generate('p_object_create', $aRouteParams);
// - Request
$oSubRequest = Request::create($sRedirectRoute, 'GET', $oRequest->query->all(), $oRequest->cookies->all(), array(), $oRequest->server->all());
return $oApp->handle($oSubRequest, HttpKernelInterface::SUB_REQUEST, true);
// Checking for actions rules
$aRules = $oBrick->GetRules();
if (!empty($aRules))
{
$aRouteParams['ar_token'] = ContextManipulatorHelper::EncodeRulesToken($aRules);
}
// Checking if the target object class is asbtract or not
// - If is not abstract, we redirect to object creation form
if (!MetaModel::IsAbstract($sObjectClass))
{
// Preparing redirection route
// - Adding brick id to the params
$aRouteParams['sBrickId'] = $sBrickId;
// - Generating route
$sRedirectRoute = $oApp['url_generator']->generate('p_object_create', $aRouteParams);
// - Request
$oSubRequest = Request::create($sRedirectRoute, 'GET', $oRequest->query->all(), $oRequest->cookies->all(), array(), $oRequest->server->all());
$oResponse = $oApp->handle($oSubRequest, HttpKernelInterface::SUB_REQUEST, true);
}
// - Else, we list the leaf classes as an intermediate step
else
{
$aData = array(
'oBrick' => $oBrick,
'sBrickId' => $sBrickId,
'aLeafClasses' => array(),
'ar_token' => $aRouteParams['ar_token']
);
$aLeafClasses = array();
$aChildClasses = MetaModel::EnumChildClasses($sObjectClass);
foreach ($aChildClasses as $sChildClass)
{
if (!MetaModel::IsAbstract($sChildClass) && SecurityHelper::IsActionAllowed($oApp, UR_ACTION_CREATE, $sChildClass))
{
$aLeafClasses[] = array(
'id' => $sChildClass,
'name' => MetaModel::GetName($sChildClass)
);
}
}
$aData['aLeafClasses'] = $aLeafClasses;
$oResponse = $oApp['twig']->render($oBrick->GetPageTemplatePath(), $aData);
}
return $oResponse;
}
}
?>

View File

@@ -32,6 +32,7 @@ class CreateBrick extends PortalBrick
{
const DEFAULT_HOME_ICON_CLASS = 'fa fa-plus';
const DEFAULT_NAVIGATION_MENU_ICON_CLASS = 'fa fa-plus fa-2x';
const DEFAULT_PAGE_TEMPLATE_PATH = 'itop-portal-base/portal/src/views/bricks/create/modal.html.twig';
const DEFAULT_CLASS = '';
static $sRouteName = 'p_create_brick';
@@ -131,5 +132,3 @@ class CreateBrick extends PortalBrick
}
}
?>

View File

@@ -30,5 +30,3 @@ class CreateBrickRouter extends AbstractRouter
);
}
?>

View File

@@ -0,0 +1,47 @@
{# itop-portal-base/portal/src/views/bricks/create/layout.html.twig #}
{# Create brick base layout #}
{% extends 'itop-portal-base/portal/src/views/modal/layout.html.twig' %}
{% block pModalTitle %}
{{ oBrick.GetTitle()|dict_s }}
{% endblock %}
{% block pModalBody %}
<p>{{ 'Brick:Portal:Create:ChooseType'|dict_s }}</p>
<ul id="{{ sBrickId }}_leaf_classes">
{% for aLeafClass in aLeafClasses %}
<li><a href="#" data-target-class="{{ aLeafClass.id }}">{{ aLeafClass.name }}</a></li>
{% endfor %}
</ul>
<script type="text/javascript">
$(document).ready(function(){
$('#{{ sBrickId }}_leaf_classes a').off('click').on('click', function(oEvent){
oEvent.preventDefault();
var sUrl = '{{ app['url_generator'].generate('p_object_create', {sObjectClass : '-sObjectClass-'})|raw }}';
var oModalElem = $(this).closest('.modal');
// Showing loader
oModalElem.find('.modal-content').html($('#page_overlay .overlay_content').html());
// Preparing target class url
sUrl = sUrl.replace(/-sObjectClass-/, $(this).attr('data-target-class') );
sUrl = AddParameterToUrl(sUrl, 'ar_token', '{{ ar_token }}');
// Loading form
oModalElem.find('.modal-content').load(sUrl, function(oData, sStatus, oXHR){
var oResponse = (oXHR.responseJSON !== undefined) ? oXHR.responseJSON : JSON.parse(oXHR.responseText);
// Note : This could be refactored for a global use
oModalElem.html( $('#modal-for-alert').html() );
oModalElem.find('.modal-title').html(oResponse.error_title);
oModalElem.find('.modal-body .alert').html(oResponse.error_message)
.removeClass('alert-success alert-info alert-warning alert-danger')
.addClass('alert-danger');
});
});
});
</script>
{% endblock %}
{% block pModalFooter %}
<button type="button" class="btn btn-default" data-dismiss="modal">{{ 'Portal:Button:Cancel'|dict_s }}</button>
{% endblock %}

View File

@@ -96,4 +96,5 @@ Dict::Add('RU RU', 'Russian', 'Русский', array(
// CreateBrick brick
Dict::Add('RU RU', 'Russian', 'Русский', array(
'Brick:Portal:Create:Name' => 'Быстрое создание',
'Brick:Portal:Create:ChooseType' => 'Please, choose a type~~',
));