|
| 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; |
| 7 | + |
| 8 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 9 | +import com.intellij.openapi.project.Project; |
| 10 | +import com.intellij.openapi.vfs.VirtualFile; |
| 11 | +import com.intellij.psi.PsiFile; |
| 12 | +import com.magento.idea.magento2plugin.MagentoIcons; |
| 13 | +import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideTemplateInThemeDialog; |
| 14 | +import com.magento.idea.magento2plugin.magento.packages.Areas; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +public class OverrideEmailTemplateInThemeAction extends OverrideFileInThemeAction { |
| 18 | + |
| 19 | + public static final String ACTION_NAME = "Override email template in a project theme"; |
| 20 | + public static final String ACTION_TEMPLATE_DESCRIPTION = "Override email template file in project theme"; |
| 21 | + public static final String EMAIL_DIRECTORY = "email"; |
| 22 | + public static final String HTML = "html"; |
| 23 | + |
| 24 | + public OverrideEmailTemplateInThemeAction() { |
| 25 | + super(ACTION_NAME, ACTION_TEMPLATE_DESCRIPTION, MagentoIcons.MODULE); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void actionPerformed(final @NotNull AnActionEvent event) { |
| 30 | + final Project project = event.getProject(); |
| 31 | + |
| 32 | + if (project == null || psiFile == null) { |
| 33 | + return; |
| 34 | + } |
| 35 | + OverrideTemplateInThemeDialog.open(project, psiFile); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected boolean isOverrideAllowed( |
| 40 | + final @NotNull PsiFile file, |
| 41 | + final @NotNull Project project |
| 42 | + ) { |
| 43 | + final VirtualFile virtualFile = file.getVirtualFile(); |
| 44 | + |
| 45 | + if (virtualFile == null) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + final String fileExtension = virtualFile.getExtension(); |
| 49 | + |
| 50 | + if (fileExtension == null) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + if(!HTML.equals(fileExtension)) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + return isEmailTemplateFile(virtualFile); |
| 59 | + } |
| 60 | + |
| 61 | + private boolean isEmailTemplateFile(final @NotNull VirtualFile virtualFile) { |
| 62 | + final VirtualFile directory = virtualFile.getParent(); |
| 63 | + |
| 64 | + if (directory == null) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if (EMAIL_DIRECTORY.equals(directory.getName())) { |
| 69 | + return Areas.getAreaByString(directory.getParent().getName()) != null; |
| 70 | + } else { |
| 71 | + return isEmailTemplateFile(directory); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments