Skip to content

Commit 3ce4d07

Browse files
author
Vitaliy
authored
Merge pull request #481 from anzin/create-new-action-controller-generation
Create new action controller generation
2 parents f429534 + 19e9274 commit 3ce4d07

File tree

16 files changed

+435
-24
lines changed

16 files changed

+435
-24
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
<internalFileTemplate name="Magento Entity Index Adminhtml Controller Class"/>
237237
<internalFileTemplate name="Magento Grid Ui Component Action Column Class"/>
238238
<internalFileTemplate name="Magento PHP Form Generic Button Block Class"/>
239+
<internalFileTemplate name="Magento Entity New Action Controller Class"/>
239240

240241
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
241242

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${NAMESPACE};
5+
6+
#set($uses = ${USES})
7+
#foreach ($use in $uses.split(","))
8+
use $use;
9+
#end
10+
11+
/**
12+
* New action ${ENTITY_NAME} controller.
13+
*/
14+
class ${CLASS_NAME} extends ${EXTENDS} implements ${IMPLEMENTS}
15+
{
16+
/**
17+
* Authorization level of a basic admin session.
18+
*
19+
* @see _isAllowed()
20+
*/
21+
const ADMIN_RESOURCE = '${ADMIN_RESOURCE}';
22+
23+
/**
24+
* Create new ${ENTITY_NAME} action.
25+
*
26+
* @return ${RESULT_PAGE}|${RESULT_INTERFACE}
27+
*/
28+
public function execute()
29+
{
30+
/** @var ${RESULT_PAGE} $resultPage */
31+
$resultPage = $this->resultFactory->create(${RESULT_FACTORY}::TYPE_PAGE);
32+
$resultPage->setActiveMenu('${MENU_IDENTIFIER}');
33+
$resultPage->getConfig()->getTitle()->prepend(__('New ${ENTITY_NAME}'));
34+
35+
return $resultPage;
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html lang="en">
2+
<body>
3+
<p face="verdana" size="-1">
4+
5+
</p>
6+
7+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
8+
<tr>
9+
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
10+
</tr>
11+
<tr>
12+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
13+
<td width="10">&nbsp;</td>
14+
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
15+
</tr>
16+
</table>
17+
</body>
18+
</html>

resources/fileTemplates/internal/Magento UI Component Grid XML.xml.ft

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
</item>
88
</argument>
99
<settings>
10+
<buttons>
11+
<button name="add">
12+
<url path="*/*/new"/>
13+
<class>primary</class>
14+
<label translate="true">Add New</label>
15+
</button>
16+
</buttons>
1017
<spinner>${NAME}_columns</spinner>
1118
<deps>
1219
<dep>${NAME}.${NAME}_data_source</dep>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.data;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class NewActionEntityControllerFileData {
11+
12+
private final String entityName;
13+
private final String moduleName;
14+
private final String namespace;
15+
private final String acl;
16+
private final String menuIdentifier;
17+
18+
/**
19+
* Controller NewAction file constructor.
20+
*
21+
* @param entityName String
22+
* @param moduleName String
23+
* @param namespace String
24+
*/
25+
public NewActionEntityControllerFileData(
26+
final @NotNull String entityName,
27+
final @NotNull String moduleName,
28+
final @NotNull String namespace,
29+
final @NotNull String acl,
30+
final @NotNull String menuIdentifier
31+
) {
32+
this.entityName = entityName;
33+
this.moduleName = moduleName;
34+
this.namespace = namespace;
35+
this.acl = acl;
36+
this.menuIdentifier = menuIdentifier;
37+
}
38+
39+
/**
40+
* Get entity name.
41+
*
42+
* @return String
43+
*/
44+
public String getEntityName() {
45+
return entityName;
46+
}
47+
48+
/**
49+
* Get module name.
50+
*
51+
* @return String
52+
*/
53+
public String getModuleName() {
54+
return moduleName;
55+
}
56+
57+
/**
58+
* Get namespace.
59+
*
60+
* @return String
61+
*/
62+
public String getNamespace() {
63+
return namespace;
64+
}
65+
66+
/**
67+
* Get acl.
68+
*
69+
* @return String
70+
*/
71+
public String getAcl() {
72+
return acl;
73+
}
74+
75+
/**
76+
* Get menu.
77+
*
78+
* @return String
79+
*/
80+
public String getMenu() {
81+
return menuIdentifier;
82+
}
83+
}

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414
import com.magento.idea.magento2plugin.actions.generation.NewEntityAction;
1515
import com.magento.idea.magento2plugin.actions.generation.OverrideClassByAPreferenceAction;
1616
import com.magento.idea.magento2plugin.actions.generation.data.AclXmlData;
17+
import com.magento.idea.magento2plugin.actions.generation.data.AdminListViewEntityActionData;
1718
import com.magento.idea.magento2plugin.actions.generation.data.CollectionData;
1819
import com.magento.idea.magento2plugin.actions.generation.data.ControllerFileData;
1920
import com.magento.idea.magento2plugin.actions.generation.data.DataModelData;
2021
import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData;
2122
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
2223
import com.magento.idea.magento2plugin.actions.generation.data.EntityDataMapperData;
23-
import com.magento.idea.magento2plugin.actions.generation.data.AdminListViewEntityActionData;
2424
import com.magento.idea.magento2plugin.actions.generation.data.FormGenericButtonBlockData;
2525
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
2626
import com.magento.idea.magento2plugin.actions.generation.data.GridActionColumnData;
2727
import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData;
2828
import com.magento.idea.magento2plugin.actions.generation.data.MenuXmlData;
2929
import com.magento.idea.magento2plugin.actions.generation.data.ModelData;
30+
import com.magento.idea.magento2plugin.actions.generation.data.NewActionEntityControllerFileData;
3031
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
3132
import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData;
3233
import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData;
@@ -49,12 +50,12 @@
4950
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
5051
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NumericRule;
5152
import com.magento.idea.magento2plugin.actions.generation.generator.AclXmlGenerator;
53+
import com.magento.idea.magento2plugin.actions.generation.generator.AdminListViewEntityActionGenerator;
5254
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelGenerator;
5355
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator;
5456
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaWhitelistJsonGenerator;
5557
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
5658
import com.magento.idea.magento2plugin.actions.generation.generator.EntityDataMapperGenerator;
57-
import com.magento.idea.magento2plugin.actions.generation.generator.AdminListViewEntityActionGenerator;
5859
import com.magento.idea.magento2plugin.actions.generation.generator.FormGenericButtonBlockGenerator;
5960
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
6061
import com.magento.idea.magento2plugin.actions.generation.generator.GridActionColumnFileGenerator;
@@ -64,6 +65,7 @@
6465
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleControllerClassGenerator;
6566
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleModelGenerator;
6667
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator;
68+
import com.magento.idea.magento2plugin.actions.generation.generator.NewActionEntityControllerFileGenerator;
6769
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
6870
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
6971
import com.magento.idea.magento2plugin.actions.generation.generator.SaveEntityCommandGenerator;
@@ -83,6 +85,7 @@
8385
import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp;
8486
import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderPhp;
8587
import com.magento.idea.magento2plugin.magento.files.actions.AdminListViewActionFile;
88+
import com.magento.idea.magento2plugin.magento.files.actions.NewActionFile;
8689
import com.magento.idea.magento2plugin.magento.files.actions.SaveActionFile;
8790
import com.magento.idea.magento2plugin.magento.files.commands.SaveEntityCommandFile;
8891
import com.magento.idea.magento2plugin.magento.packages.Areas;
@@ -365,6 +368,7 @@ private void onOK() {
365368
generateSaveEntityCommandFile();
366369
generateFormSaveControllerFile();
367370
generateFormUiComponentGenericButtonFile();
371+
generateFormNewActionControllerFile();
368372
generateUiComponentFormFile();
369373
}
370374

@@ -1200,6 +1204,25 @@ private void generateFormSaveControllerFile() {
12001204
), project).generate(ACTION_NAME, false);
12011205
}
12021206

1207+
/**
1208+
* Generate NewAction Controller file.
1209+
*/
1210+
private void generateFormNewActionControllerFile() {
1211+
final NamespaceBuilder namespace = new NamespaceBuilder(
1212+
getModuleName(),
1213+
NewActionFile.CLASS_NAME,
1214+
NewActionFile.getDirectory(getEntityName())
1215+
);
1216+
1217+
new NewActionEntityControllerFileGenerator(new NewActionEntityControllerFileData(
1218+
getEntityName(),
1219+
getModuleName(),
1220+
namespace.getNamespace(),
1221+
getAcl(),
1222+
getMenuIdentifier()
1223+
), project).generate(ACTION_NAME, false);
1224+
}
1225+
12031226
/**
12041227
* Get save entity command class Fqn.
12051228
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.magento.idea.magento2plugin.actions.generation.generator;
2+
3+
import com.intellij.openapi.project.Project;
4+
import com.intellij.psi.PsiDirectory;
5+
import com.intellij.psi.PsiFile;
6+
import com.magento.idea.magento2plugin.actions.generation.data.NewActionEntityControllerFileData;
7+
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
8+
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
9+
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
10+
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
11+
import com.magento.idea.magento2plugin.magento.files.actions.NewActionFile;
12+
import com.magento.idea.magento2plugin.magento.packages.HttpMethod;
13+
import com.magento.idea.magento2plugin.magento.packages.code.BackendModuleType;
14+
import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType;
15+
import org.jetbrains.annotations.NotNull;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
import java.util.Properties;
20+
21+
public class NewActionEntityControllerFileGenerator extends FileGenerator {
22+
23+
private final NewActionEntityControllerFileData fileData;
24+
private final FileFromTemplateGenerator fileFromTemplateGenerator;
25+
private final DirectoryGenerator directoryGenerator;
26+
private final ModuleIndex moduleIndex;
27+
private final List<String> uses;
28+
29+
/**
30+
* NewAction Entity Controller File Generator.
31+
* @param fileData NewActionEntityControllerFileData
32+
* @param project Project
33+
*/
34+
public NewActionEntityControllerFileGenerator(
35+
final NewActionEntityControllerFileData fileData,
36+
final @NotNull Project project
37+
) {
38+
super(project);
39+
this.fileData = fileData;
40+
fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project);
41+
moduleIndex = ModuleIndex.getInstance(project);
42+
directoryGenerator = DirectoryGenerator.getInstance();
43+
uses = new ArrayList<>();
44+
}
45+
46+
/**
47+
* Generate NewAction controller for entity.
48+
*
49+
* @param actionName String
50+
*
51+
* @return PsiFile
52+
*/
53+
@Override
54+
public PsiFile generate(final @NotNull String actionName) {
55+
final PsiDirectory moduleBaseDir = moduleIndex.getModuleDirectoryByModuleName(
56+
fileData.getModuleName()
57+
);
58+
final PsiDirectory baseDirectory = directoryGenerator.findOrCreateSubdirectories(
59+
moduleBaseDir,
60+
NewActionFile.getDirectory(fileData.getEntityName())
61+
);
62+
63+
return fileFromTemplateGenerator.generate(
64+
NewActionFile.getInstance(),
65+
getAttributes(),
66+
baseDirectory,
67+
actionName
68+
);
69+
}
70+
71+
@Override
72+
protected void fillAttributes(final @NotNull Properties attributes) {
73+
attributes.setProperty("NAMESPACE", fileData.getNamespace());
74+
attributes.setProperty("ENTITY_NAME", fileData.getEntityName());
75+
attributes.setProperty("CLASS_NAME", NewActionFile.CLASS_NAME);
76+
addProperty(attributes, "EXTENDS", BackendModuleType.EXTENDS.getType());
77+
addProperty(attributes, "IMPLEMENTS", HttpMethod.GET.getInterfaceFqn());
78+
attributes.setProperty("ADMIN_RESOURCE", fileData.getAcl());
79+
attributes.setProperty("MENU_IDENTIFIER", fileData.getMenu());
80+
addProperty(attributes, "RESULT_INTERFACE",
81+
FrameworkLibraryType.RESULT_INTERFACE.getType()
82+
);
83+
addProperty(attributes, "RESULT_FACTORY", FrameworkLibraryType.RESULT_FACTORY.getType());
84+
addProperty(attributes, "RESULT_PAGE", BackendModuleType.RESULT_PAGE.getType());
85+
86+
attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses));
87+
}
88+
89+
/**
90+
* Add type to properties.
91+
*
92+
* @param properties Properties
93+
* @param propertyName String
94+
* @param type String
95+
*/
96+
protected void addProperty(
97+
final @NotNull Properties properties,
98+
final String propertyName,
99+
final String type
100+
) {
101+
uses.add(type);
102+
properties.setProperty(propertyName, PhpClassGeneratorUtil.getNameFromFqn(type));
103+
}
104+
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ protected void fillAttributes(final @NotNull Properties attributes) {
111111
attributes.setProperty("ENTITY_NAME", fileData.getEntityName());
112112
attributes.setProperty("CLASS_NAME", SaveActionFile.CLASS_NAME);
113113
attributes.setProperty("ENTITY_ID", fileData.getEntityId());
114-
attributes.setProperty("ADMIN_RESOURCE", SaveActionFile.getAdminResource(
115-
fileData.getModuleName(),
116-
fileData.getAcl()
117-
)
118-
);
114+
attributes.setProperty("ADMIN_RESOURCE", fileData.getAcl());
119115
addProperty(attributes, "IMPLEMENTS", HttpMethod.POST.getInterfaceFqn());
120116
addProperty(attributes, "DATA_PERSISTOR", FrameworkLibraryType.DATA_PERSISTOR.getType());
121117
addProperty(attributes, "ENTITY_DTO", fileData.getDtoType());

0 commit comments

Comments
 (0)