Skip to content

Commit 2a0ec5f

Browse files
author
silinmykola
committed
967 fixes after CR
1 parent 42ca0e0 commit 2a0ec5f

File tree

4 files changed

+48
-57
lines changed

4 files changed

+48
-57
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0"?>
22
#parse("XML File Header.xml")
3-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
3+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #if (${IS_ADMIN})layout="admin-1column"#end
4+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
45
</page>

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewLayoutTemplateDialog.java

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,14 @@ public static void open(
123123
*/
124124
private void onOK() {
125125
if (validateFormFields() && isUnderscoreCorrect()) {
126+
final String[] layoutNameParts = getLayoutNameParts();
126127
new LayoutXmlTemplateGenerator(
127128
new LayoutXmlData(
128129
getArea(),
129-
getRouteName(),
130+
layoutNameParts[0],
130131
moduleName,
131-
getControllerName(),
132-
getActionName()
132+
layoutNameParts[1],
133+
layoutNameParts[2]
133134
),
134135
project
135136
).generate(NewLayoutXmlAction.ACTION_NAME, true);
@@ -166,6 +167,29 @@ private void autoSelectCurrentArea() {
166167
}
167168
}
168169

170+
/**
171+
* Get parts of inserted layout name
172+
*
173+
* @return String[]
174+
*/
175+
private String[] getLayoutNameParts() {
176+
177+
final String[] layoutNameParts = layoutName.getText().trim().split("_");
178+
String routeName = "";
179+
String controllerName = "";
180+
String actionName = "";
181+
182+
if (layoutNameParts.length >= 1) { // NOPMD
183+
routeName = layoutNameParts[0];
184+
}
185+
186+
if (layoutNameParts.length == 3) { // NOPMD
187+
controllerName = layoutNameParts[1];
188+
actionName = layoutNameParts[2];
189+
}
190+
return new String[]{routeName, controllerName, actionName};
191+
}
192+
169193
/**
170194
* Check is count of underscore is correct in layout name.
171195
*
@@ -210,20 +234,6 @@ private int countUnderscore(final String name) {
210234
return count;
211235
}
212236

213-
/**
214-
* Get action name.
215-
*
216-
* @return String
217-
*/
218-
private String getActionName() {
219-
if (layoutName.getText().trim().contains("_")) {
220-
final int position = layoutName.getText().trim().lastIndexOf("_") + 1;
221-
return layoutName.getText().trim().substring(position);
222-
}
223-
224-
return "";
225-
}
226-
227237
/**
228238
* Get area.
229239
*
@@ -232,40 +242,4 @@ private String getActionName() {
232242
private String getArea() {
233243
return area.getSelectedItem().toString();
234244
}
235-
236-
/**
237-
* Get controller name.
238-
*
239-
* @return String
240-
*/
241-
private String getControllerName() {
242-
final String newLayoutName = layoutName.getText().trim();
243-
if (newLayoutName.contains("_")) {
244-
final int start = newLayoutName.indexOf('_') + 1;
245-
int end = newLayoutName.lastIndexOf('_');
246-
247-
if (start == end) {
248-
end = newLayoutName.length() - 1;
249-
}
250-
return newLayoutName.substring(start, end);
251-
}
252-
253-
return "";
254-
}
255-
256-
/**
257-
* Get route name.
258-
*
259-
* @return String
260-
*/
261-
private String getRouteName() {
262-
final String newLayoutName = layoutName.getText().trim();
263-
int position = newLayoutName.length();
264-
265-
if (newLayoutName.contains("_")) {
266-
position = newLayoutName.indexOf('_');
267-
}
268-
269-
return newLayoutName.substring(0, position);
270-
}
271245
}

src/com/magento/idea/magento2plugin/actions/generation/generator/LayoutXmlTemplateGenerator.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.intellij.psi.xml.XmlTag;
1515
import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData;
1616
import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateLayoutXml;
17+
import com.magento.idea.magento2plugin.magento.packages.Areas;
18+
import java.util.Objects;
1719
import java.util.Properties;
1820
import org.jetbrains.annotations.NotNull;
1921

@@ -36,7 +38,8 @@ public LayoutXmlTemplateGenerator(
3638
super(project);
3739
this.layoutXmlData = layoutXmlData;
3840
this.project = project;
39-
this.findOrCreateLayoutXml = new FindOrCreateLayoutXml(project);
41+
final Properties attributes = getAttributes();
42+
this.findOrCreateLayoutXml = new FindOrCreateLayoutXml(project, attributes);
4043
}
4144

4245
/**
@@ -80,5 +83,11 @@ public PsiFile generate(final @NotNull String actionName) {
8083
}
8184

8285
@Override
83-
protected void fillAttributes(final Properties attributes) {} //NOPMD
86+
protected void fillAttributes(final Properties attributes) {
87+
if (Objects.equals(layoutXmlData.getArea(), Areas.adminhtml.toString())) {
88+
attributes.setProperty("IS_ADMIN", Boolean.TRUE.toString());
89+
} else {
90+
attributes.setProperty("IS_ADMIN", Boolean.FALSE.toString());
91+
}
92+
}
8493
}

src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateLayoutXml.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@
1919
public final class FindOrCreateLayoutXml {
2020

2121
private final Project project;
22+
private final Properties properties;
2223

2324
public FindOrCreateLayoutXml(final Project project) {
2425
this.project = project;
26+
this.properties = new Properties();
27+
}
28+
29+
public FindOrCreateLayoutXml(final Project project, Properties properties) {
30+
this.project = project;
31+
this.properties = properties;
2532
}
2633

2734
/**
@@ -77,7 +84,7 @@ public PsiFile execute(
7784
if (layoutXmlFile == null) {
7885
layoutXmlFile = fileFromTemplateGenerator.generate(
7986
layoutXml,
80-
new Properties(),
87+
this.properties,
8188
parentDirectory,
8289
actionName
8390
);

0 commit comments

Comments
 (0)