Skip to content

Commit

Permalink
[Custom Layouts] Added definable identifier and changed storage to php (
Browse files Browse the repository at this point in the history
  • Loading branch information
dvesh3 authored and brusch committed Dec 13, 2018
1 parent 3f32d5b commit f91e5c1
Show file tree
Hide file tree
Showing 10 changed files with 540 additions and 99 deletions.
Expand Up @@ -218,7 +218,7 @@ public function getAction(Request $request)
*/
public function getCustomLayoutAction(Request $request)
{
$customLayout = DataObject\ClassDefinition\CustomLayout::getById(intval($request->get('id')));
$customLayout = DataObject\ClassDefinition\CustomLayout::getById($request->get('id'));

return $this->adminJson(['success' => true, 'data' => $customLayout]);
}
Expand Down Expand Up @@ -262,12 +262,21 @@ public function addAction(Request $request)
*/
public function addCustomLayoutAction(Request $request)
{
$layoutId = $request->get('layoutIdentifier');
$existingLayout = DataObject\ClassDefinition\CustomLayout::getById($layoutId);
if ($existingLayout) {
throw new \Exception('Custom Layout identifier already exists');
}

$customLayout = DataObject\ClassDefinition\CustomLayout::create(
['name' => $request->get('name'),
[
'name' => $request->get('layoutName'),
'userOwner' => $this->getAdminUser()->getId(),
'classId' => $request->get('classId')]
'classId' => $request->get('classId')
]
);

$customLayout->setId($layoutId);
$customLayout->save();

return $this->adminJson(['success' => true, 'id' => $customLayout->getId(), 'name' => $customLayout->getName(),
Expand Down Expand Up @@ -298,7 +307,7 @@ public function deleteAction(Request $request)
*/
public function deleteCustomLayoutAction(Request $request)
{
$customLayout = DataObject\ClassDefinition\CustomLayout::getById(intval($request->get('id')));
$customLayout = DataObject\ClassDefinition\CustomLayout::getById($request->get('id'));
if ($customLayout) {
$customLayout->delete();
}
Expand Down Expand Up @@ -609,7 +618,7 @@ public function exportClassAction(Request $request)
*/
public function exportCustomLayoutDefinitionAction(Request $request)
{
$id = intval($request->get('id'));
$id = $request->get('id');

if ($id) {
$customLayout = DataObject\ClassDefinition\CustomLayout::getById($id);
Expand Down Expand Up @@ -1663,4 +1672,41 @@ public function suggestClassIdentifierAction()

return $this->adminJson($result);
}


/**
* @Route("/suggest-custom-layout-identifier")
*
* @param Request $request
*
* @return Response
*/
public function suggestCustomLayoutIdentifierAction(Request $request)
{
$classId = $request->get('classId');

$identifier = DataObject\ClassDefinition\CustomLayout::getIdentifier($classId);

$list = new DataObject\ClassDefinition\CustomLayout\Listing();

$list = $list->load();
$existingIds = [];
$existingNames = [];

/** @var $item DataObject\ClassDefinition\CustomLayout */
foreach ($list as $item) {
$existingIds[] = $item->getId();
if ($item->getClassId() == $classId) {
$existingNames[] = $item->getName();
}
}

$result = [
'suggestedIdentifier' => $identifier,
'existingIds' => $existingIds,
'existingNames' => $existingNames
];

return $this->adminJson($result);
}
}
Expand Up @@ -441,24 +441,25 @@ public function getAction(Request $request, EventDispatcherInterface $eventDispa
$user = Tool\Admin::getCurrentUser();

if (!is_null($currentLayoutId)) {
if ($currentLayoutId == 0 && !$user->isAdmin()) {
if ($currentLayoutId == "0" && !$user->isAdmin()) {
$first = reset($validLayouts);
$currentLayoutId = $first->getId();
}
}

if ($currentLayoutId > 0) {
if ($currentLayoutId == -1 && $user->isAdmin()) {
$layout = DataObject\Service::getSuperLayoutDefinition($object);
$objectData['layout'] = $layout;
} elseif (!empty($currentLayoutId)) {
// check if user has sufficient rights
if ($validLayouts && $validLayouts[$currentLayoutId]) {
if (is_array($validLayouts) && $validLayouts[$currentLayoutId]) {
$customLayout = DataObject\ClassDefinition\CustomLayout::getById($currentLayoutId);

$customLayoutDefinition = $customLayout->getLayoutDefinitions();
$objectData['layout'] = $customLayoutDefinition;
} else {
$currentLayoutId = 0;
}
} elseif ($currentLayoutId == -1 && $user->isAdmin()) {
$layout = DataObject\Service::getSuperLayoutDefinition($object);
$objectData['layout'] = $layout;
}

$objectData['currentLayoutId'] = $currentLayoutId;
Expand Down
1 change: 1 addition & 0 deletions bundles/AdminBundle/Resources/public/js/pimcore/helpers.js
Expand Up @@ -3104,3 +3104,4 @@ pimcore.helpers.dragAndDropValidateSingleItem = function (data) {
};



Expand Up @@ -265,7 +265,7 @@ pimcore.object.helpers.customLayoutEditor = Class.create({
xtype: "button",
text: t("add_layout"),
iconCls: "pimcore_icon_add",
handler: this.addLayout.bind(this)
handler: this.suggestIdentifier.bind(this)
},
{
xtype: "button",
Expand Down Expand Up @@ -626,6 +626,20 @@ pimcore.object.helpers.customLayoutEditor = Class.create({
return newNode;
},

suggestIdentifier: function() {
Ext.Ajax.request({
url: "/admin/class/suggest-custom-layout-identifier",
params: {
classId: this.klass.id,
},
success: function (response) {
var layouts = Ext.decode(response.responseText);
this.addLayout(layouts);
}.bind(this)
});

},

addLayoutChild: function (record, type, initData, addListener) {

var nodeLabel = t(type);
Expand Down Expand Up @@ -749,7 +763,9 @@ pimcore.object.helpers.customLayoutEditor = Class.create({
save: function () {
var id = this.layoutChangeCombo.getValue();

if (id > 0) {
var regresult = this.data["name"].match(/[a-zA-Z][a-zA-Z0-9]+/);

if (this.data["name"].length > 2 && this.data["name"].length < 64 && regresult == this.data["name"]) {
this.saveCurrentNode();

delete this.data.layoutDefinitions;
Expand All @@ -770,6 +786,8 @@ pimcore.object.helpers.customLayoutEditor = Class.create({
failure: this.saveOnError.bind(this)
});
}
} else {
Ext.Msg.alert(' ', t('invalid_name'));
}
},

Expand All @@ -792,45 +810,118 @@ pimcore.object.helpers.customLayoutEditor = Class.create({
pimcore.helpers.showNotification(t("error"), t("saving_failed"), "error");
},

addLayout: function () {
Ext.MessageBox.prompt(t('add_layout'), t('enter_the_name_of_the_new_item'), this.addLayoutComplete.bind(this),
null, null, "");
},
addLayout: function (layouts) {
var suggestedIdentifier = layouts["suggestedIdentifier"];
var nameField = new Ext.form.field.Text(
{
fieldLabel: t('name'),
labelWidth: 200
}
);

addLayoutComplete: function (button, value, object) {
if (button == "ok" && value.length > 2 // && regresult == value
&& !in_array(value.toLowerCase(), this.forbiddennames)) {
Ext.Ajax.request({
url: "/admin/class/add-custom-layout",
method: 'POST',
params: {
name: value,
classId: this.klass.id
var identifierField = new Ext.form.field.Text({
fieldLabel: t('unique_identifier'),
labelWidth: 200,
maxLength: 20,
value: suggestedIdentifier
});

this.win = new Ext.Window({
title: t('add_layout'),
width: 400,
height: 250,
draggable: false,
border: false,
modal: true,
bodyStyle: "padding: 10px;",
resizable: true,
buttonAlign: 'center',
items: [
nameField,
identifierField, {
xtype: 'panel',
html: t('identifier_warning')
}
],
buttons: [
{
xtype: 'button',
text: t('cancel'),
iconCls: 'pimcore_icon_cancel',
handler: function () {
this.win.close();

}.bind(this)
},
success: function (response) {

var data = Ext.decode(response.responseText);
if(data && data.success) {
this.setCurrentNode("root");
this.editPanel.removeAll();
this.classDefinitionPanel.enable();
this.enableButtons();
this.layoutComboStore.reload();
this.currentLayoutId = data.id;
this.layoutChangeCombo.setValue(data.id);
this.initLayoutFields(true, response);
} else {
Ext.Msg.alert(t('error'), t('saving_failed'));
}
}.bind(this)
});
{
xtype: 'button',
text: t('OK'),
iconCls: 'pimcore_icon_save',
handler: function ( nameField, identifierField, layouts, button) {
if (this.addLayoutComplete(nameField.getValue(), identifierField.getValue(), layouts)) {
this.win.close();
}
}.bind(this, nameField, identifierField, layouts)
}
]
})
this.win.show();
nameField.focus();
},

addLayoutComplete: function (layoutName, layoutIdentifier, layouts) {

var layoutNameRegresult = layoutName.match(/[a-zA-Z][a-zA-Z0-9]+/);

if (layoutName.length <= 2 || layoutNameRegresult != layoutName) {
Ext.Msg.alert(' ', t('invalid_name'));
return false;
}

if (in_arrayi(layoutName, layouts["existingNames"])) {
Ext.Msg.alert(' ', t('name_already_in_use'));
return false;
}
else if (button == "cancel") {
return;

var layoutIdentifierRegresult = layoutIdentifier.match(/[a-zA-Z0-9]+/);

if (layoutIdentifier.length < 1 || layoutIdentifierRegresult != layoutIdentifier) {
Ext.Msg.alert(' ', t('invalid_identifier'));
return false;
}
else {
Ext.Msg.alert(' ', t('invalid_class_name'));

if (in_arrayi(layoutIdentifier, layouts["existingIds"])) {
Ext.Msg.alert(' ', t('identifier_already_exists'));
return false;
}

Ext.Ajax.request({
url: "/admin/class/add-custom-layout",
method: 'POST',
params: {
layoutName: layoutName,
layoutIdentifier: layoutIdentifier,
classId: this.klass.id
},
success: function (response) {

var data = Ext.decode(response.responseText);
if(data && data.success) {
this.setCurrentNode("root");
this.editPanel.removeAll();
this.classDefinitionPanel.enable();
this.enableButtons();
this.layoutComboStore.reload();
this.currentLayoutId = data.id;
this.layoutChangeCombo.setValue(data.id);
this.initLayoutFields(true, response);
} else {
Ext.Msg.alert(t('error'), t('saving_failed'));
}
}.bind(this)
});

return true;
},


Expand All @@ -843,31 +934,29 @@ pimcore.object.helpers.customLayoutEditor = Class.create({
deleteLayout: function () {
var id = this.layoutChangeCombo.getValue();

if (id > 0) {
Ext.Msg.confirm(t('delete'), t('delete_message'), function(btn){
if (btn == 'yes'){
Ext.Ajax.request({
url: "/admin/class/delete-custom-layout",
method: 'DELETE',
params: {
id: id
}
});

this.layoutComboStore.reload();
this.currentLayoutId = 0;
this.layoutChangeCombo.setValue(this.currentLayoutId);
Ext.Msg.confirm(t('delete'), t('delete_message'), function(btn){
if (btn == 'yes'){
Ext.Ajax.request({
url: "/admin/class/delete-custom-layout",
method: 'DELETE',
params: {
id: id
}
});

this.editPanel.removeAll();
this.clearSelectionPanel();
this.classDefinitionPanel.disable();
this.saveButton.disable();
this.importButton.disable();
this.exportButton.disable();
this.rootPanel = null;
}
}.bind(this));
}
this.layoutComboStore.reload();
this.currentLayoutId = 0;
this.layoutChangeCombo.setValue(this.currentLayoutId);

this.editPanel.removeAll();
this.clearSelectionPanel();
this.classDefinitionPanel.disable();
this.saveButton.disable();
this.importButton.disable();
this.exportButton.disable();
this.rootPanel = null;
}
}.bind(this));
},

upload: function() {
Expand Down
Expand Up @@ -451,7 +451,7 @@ pimcore.object.object = Class.create(pimcore.object.abstract, {
var menu = [];
for (var i = 0; i < this.data.validLayouts.length; i++) {
var menuLabel = ts(this.data.validLayouts[i].name);
if (Number(this.data.currentLayoutId) == this.data.validLayouts[i].id) {
if (this.data.currentLayoutId == this.data.validLayouts[i].id) {
menuLabel = "<b>" + menuLabel + "</b>";
}
menu.push({
Expand Down

0 comments on commit f91e5c1

Please sign in to comment.