Skip to content

Commit 8ea9e03

Browse files
committed
updated with tested code
1 parent 7f90f69 commit 8ea9e03

File tree

10 files changed

+215
-42
lines changed

10 files changed

+215
-42
lines changed

app/code/Base/etc/adminhtml/menu.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" ?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
33
<menu>
4-
<add id="A2bizz::top_level" title="A2bizz" module="A2bizz_Base" sortOrder="9999" resource="Magento_Backend::content"/>
4+
<add id="A2bizz::top_level" title="A2bizz" module="A2bizz_Base" sortOrder="10" resource="Magento_Backend::content"/>
55
</menu>
66
</config>

app/code/ModuleCreator/Controller/Adminhtml/ModuleCreator/Save.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public function execute()
4848
$data = $this->getRequest()->getPostValue();
4949
if ($data) {
5050

51+
//check if the module's namespace already created any module
52+
$parentModule=$this->helper->checkParentStatus($data);
53+
if($parentModule==NULL):
54+
$data['is_parent']=1;
55+
endif;
56+
5157
$id = $this->getRequest()->getParam('modulecreator_id');
5258

5359
$model = $this->_objectManager->create(\A2bizz\ModuleCreator\Model\ModuleCreator::class)->load($id);
@@ -56,23 +62,15 @@ public function execute()
5662
return $resultRedirect->setPath('*/*/');
5763
}
5864

59-
//echo "<pre>";
60-
//print_r($model->getData());
61-
//exit;
62-
63-
//this methods checks weather the module is already been created in Magento setup
65+
//this method checks weather the custom module is already been created/ present in Magento setup
6466
if($this->isModuleAlreadyCreated($data)){
65-
//echo "IF";exit;
6667
$this->messageManager->addErrorMessage(__('This Module Already exists.'));
6768
if(!$id){
6869
return $resultRedirect->setPath('*/*/new');
6970
} else {
7071
return $resultRedirect->setPath('*/*/edit', ['modulecreator_id' => $this->getRequest()->getParam('modulecreator_id')]);
7172
}
72-
} else {
73-
//echo "Else";
74-
//var_dump($id);
75-
//exit;
73+
} else {
7674
if (!$model->getId()) {
7775
// this will create a new module inside app/code/ with namespace_module name
7876
$this->createNewModule($data);
@@ -115,8 +113,7 @@ public function createNewModule($data){
115113
* isModuleAlreadyCreated method
116114
*
117115
* @return true if module is already avaiable in magento setup
118-
*/
119-
116+
*/
120117
public function isModuleAlreadyCreated($data){
121118

122119
//print_r($data);exit;

app/code/ModuleCreator/Helper/Data.php

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ class Data extends AbstractHelper
1818
{
1919
private $directoryList;
2020
private $driverFile;
21+
private $model;
2122

2223
public function __construct(
2324
\Magento\Framework\App\Helper\Context $context,
2425
\Magento\Framework\Filesystem\DirectoryList $directoryList,
25-
\Magento\Framework\Filesystem\Driver\File $driverFile
26+
\Magento\Framework\Filesystem\Driver\File $driverFile,
27+
\A2bizz\ModuleCreator\Model\ModuleCreator $model
2628
){
2729
$this->directoryList = $directoryList; // ?? ObjectManager::getInstance->get(DirectoryList::class);
2830
$this->driverFile = $driverFile;
31+
$this->model = $model;
2932

3033
parent::__construct($context);
3134
}
@@ -67,29 +70,56 @@ public function getAllFilesPath($path = '/var') {
6770
}
6871

6972
/*
70-
* return replaced path for new module to generate inside app/code/
73+
* return replaced path for new module to generate inside Root-app/code/
7174
*/
72-
public function newModuleFiles($data, $filePathsArray){
75+
public function newModuleFiles($parentModule, $data, $filePathsArray){
7376
$newFilesPathArray = [];
7477
$namespace = $data['namespace'];
7578
$module = $data['module'];
7679

77-
$capNamespace = ucfirst($namespace);
78-
$lowNamespace = strtolower($namespace);
79-
$capModule = ucfirst($module);
80-
$lowModule = strtolower($module);
81-
82-
foreach ($filePathsArray as $filePath):
83-
$filePath=str_replace("/app/code/A2bizz/ModuleCreator/MTemplate","",$filePath);
80+
$capNamespace = ucfirst($namespace); //return {{Namespace}}
81+
$lowNamespace = strtolower($namespace); //return {{namespace}}
82+
$capModule = ucfirst($module); //return {{Module}}
83+
$lowModule = strtolower($module); //return {{module}}
8484

85-
if ($filePath=="/var/www/html/app/code/Namespace/Module/etc/module.xml"):
85+
//echo "<pre>";
86+
//print_r($filePathsArray);
87+
//exit;
88+
89+
foreach ($filePathsArray as $key=>$filePath):
90+
$filePath=str_replace("/app/code/A2bizz/ModuleCreator/MTemplate","",$filePath);
91+
$pathRoot = $this->directoryList->getRoot();
92+
$moduleXMLPath = $pathRoot.'/app/code/Namespace/Module/etc/module.xml';
93+
if ($filePath==$moduleXMLPath):
8694
$filePath=str_replace("Namespace",$capNamespace,$filePath);
8795
$filePath=str_replace("Module",$capModule,$filePath);
8896

8997
$newFilesPathArray[]=$filePath;
9098
continue;
9199
endif;
92-
100+
101+
//Code for taking parent menu.xml and child menu.xml as per
102+
$menuXMLPath = $pathRoot.'/app/code/Namespace/Module/etc/adminhtml/menu.xml';
103+
$menuA2bizzXMLPath = $pathRoot.'/app/code/Namespace/Module/etc/adminhtml/menu.xml.a2bizz';
104+
105+
//Here we testing if Namespace already created, if not, then alternate menu.xml.a2bizz do not need modification
106+
//and will remain same except the path for new module
107+
if ($parentModule==NULL && $filePath==$menuA2bizzXMLPath):
108+
//var_dump($parentModule);
109+
$filePath=str_replace("Namespace",$capNamespace,$filePath);
110+
$filePath=str_replace("Module",$capModule,$filePath);
111+
112+
$newFilesPathArray[]=$filePath;
113+
continue;
114+
endif;
115+
//Here we testing if Namespace already created, if yes, then alternate menu.xml.a2bizz is needed modification
116+
//so here we are changing name of menu.xml to menu.xml.parent and menu.xaml.a2bizz will be menu.xml for new module created
117+
if ($parentModule!=NULL && $filePath==$menuXMLPath):
118+
//echo $parentModule;
119+
$filePath=str_replace("menu.xml","menu.xml.parent",$filePath);
120+
endif;
121+
122+
93123
$filePath=str_replace(".a2bizz","",$filePath);
94124
$filePath=str_replace("Namespace",$capNamespace,$filePath);
95125
$filePath=str_replace("namespace",$lowNamespace,$filePath);
@@ -101,7 +131,7 @@ public function newModuleFiles($data, $filePathsArray){
101131
endforeach;
102132

103133
//echo "<pre>";
104-
//print_r($newFilesPathArray);exit;
134+
//print_r($newFilesPathArray);
105135
//exit;
106136
return $newFilesPathArray;
107137
}
@@ -126,27 +156,34 @@ public function createFoldersAndCopyBlankoFiles($fromFilesPathArray, $toFilesPat
126156
return true;
127157
}
128158

129-
public function replaceXml($data,$content){
159+
public function replaceXml($parentModule,$data,$content){
130160
$namespace = $data['namespace'];
131161
$module = $data['module'];
132162

133163
$capNamespace = ucfirst($namespace);
134164
$lowNamespace = strtolower($namespace);
135165
$capModule = ucfirst($module);
136166
$lowModule = strtolower($module);
167+
if ($parentModule!=NULL):
168+
$parentCapModule = ucfirst($parentModule);
169+
else:
170+
$parentCapModule="";
171+
endif;
137172

138173
$search = array(
139174
'/\[Namespace\]/',
140175
'/\[namespace\]/',
141176
'/\[Module\]/',
142177
'/\[module\]/',
178+
'/\[PModule\]/',
143179
);
144180

145181
$replace = array(
146182
$capNamespace,
147183
$lowNamespace,
148184
$capModule,
149185
$lowModule,
186+
$parentCapModule,
150187
);
151188

152189
return preg_replace($search, $replace, $content);
@@ -178,7 +215,7 @@ public function replacePhp($data,$content){
178215
return preg_replace($search, $replace, $content);
179216
}
180217

181-
public function insertVarsInNewCustomModule($data,$toFilesPathArray){
218+
public function insertVarsInNewCustomModule($parentModule,$data,$toFilesPathArray){
182219
try{
183220
foreach($toFilesPathArray as $toFilePath):
184221
$handle = fopen ($toFilePath, 'r+');
@@ -191,7 +228,9 @@ public function insertVarsInNewCustomModule($data,$toFilesPathArray){
191228
$type = strrchr($toFilePath, '.');
192229
switch ($type) {
193230
case '.xml':
194-
$content = $this->replaceXml($data,$content);
231+
case '.a2bizz':
232+
case '.parent':
233+
$content = $this->replaceXml($parentModule,$data,$content);
195234
break;
196235
case '.php':
197236
case '.phtml':
@@ -214,19 +253,44 @@ public function insertVarsInNewCustomModule($data,$toFilesPathArray){
214253
return true;
215254
}
216255

256+
/*
257+
* return true if Parent Available, False if not
258+
*/
259+
public function checkParentStatus($data){
260+
//gets count of a collection of namespace if already has module created
261+
$parentModule=NULL;
262+
$collection=$this->model->getCollection()
263+
->addFieldToFilter('namespace',$data['namespace'])
264+
->addFieldToFilter('is_parent',1);
265+
266+
if ($collection->count() > 0):
267+
foreach($collection as $row):
268+
$parentModule=$row->getModule();
269+
break;
270+
endforeach;
271+
endif;
272+
return $parentModule;
273+
}
274+
217275
public function createNewModule($data){
276+
//Check if the Namespace for new custom module already preset
277+
//We can say that we are looking if the parent Namespace already present for new creating module
278+
//if the Namespace with a module already present then it will be considered as Parent Module for Other cusotom Modules
279+
$parentModule=$this->checkParentStatus($data);
280+
//echo $parentModule;exit;
281+
218282
// 1- get File paths from where we are copying files for creating custom Module
219283
$fromFiles = $this->getAllFilesPath();
220284

221285
// 2- convert path - Where you want to put new custom module
222286
// 3- add namespace and module - by which name, new custom module will be added
223-
$toFiles = $this->newModuleFiles($data,$fromFiles);
287+
$toFiles = $this->newModuleFiles($parentModule,$data,$fromFiles);
224288

225289
// 4- this function will create folders, on given path for new custom module
226290
// 5- also it will paste blanko files - files which we will use to creat custom module
227291
$this->createFoldersAndCopyBlankoFiles($fromFiles,$toFiles);
228292

229293
// 6- now this function will read each files and update namespace and modules in the given content
230-
$this->insertVarsInNewCustomModule($data,$toFiles);
294+
$this->insertVarsInNewCustomModule($parentModule,$data,$toFiles);
231295
}
232296
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" ?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
3+
<menu>
4+
<add id="[Namespace]_[Module]::[namespace]_[module]" title="[Module]" module="[Namespace]_[Module]" sortOrder="9999" resource="Magento_Backend::content" parent="[Namespace]::[PModule]_top_level" action="[namespace]_[module]/[module]/index"/>
5+
</menu>
6+
</config>

app/code/ModuleCreator/Model/ModuleCreator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
class ModuleCreator extends AbstractModel implements ModuleCreatorInterface
1414
{
1515

16+
const STATUS_ENABLED = 1;
17+
const STATUS_DISABLED = 0;
18+
1619
/**
1720
* @inheritDoc
1821
*/
@@ -148,5 +151,15 @@ public function setIsActive($isActive)
148151
{
149152
return $this->setData(self::IS_ACTIVE, $isActive);
150153
}
154+
155+
/**
156+
* Prepare statuses, available event module_creator_get_available_statuses to customize statuses.
157+
*
158+
* @return array
159+
*/
160+
public function getAvailableStatuses()
161+
{
162+
return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
163+
}
151164
}
152165

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace A2bizz\ModuleCreator\Model\Source;
7+
8+
use Magento\Framework\Data\OptionSourceInterface;
9+
10+
/**
11+
* Class IsActive
12+
*/
13+
class IsActive implements OptionSourceInterface
14+
{
15+
/**
16+
* @var \A2bizz\ModuleCreator\Model\ModuleCreator
17+
*/
18+
protected $moduleCreator;
19+
20+
/**
21+
* Constructor
22+
*
23+
* @param \Magento\Cms\Model\Page $cmsPage
24+
*/
25+
public function __construct(\A2bizz\ModuleCreator\Model\ModuleCreator $moduleCreator)
26+
{
27+
$this->moduleCreator = $moduleCreator;
28+
}
29+
30+
/**
31+
* Get options
32+
*
33+
* @return array
34+
*/
35+
public function toOptionArray()
36+
{
37+
$availableOptions = $this->moduleCreator->getAvailableStatuses();
38+
$options = [];
39+
foreach ($availableOptions as $key => $value) {
40+
$options[] = [
41+
'label' => $value,
42+
'value' => $key,
43+
];
44+
}
45+
return $options;
46+
}
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace A2bizz\ModuleCreator\Plugin;
3+
4+
class OptionArrayDecorator
5+
{
6+
protected $escaper;
7+
8+
public function __construct(
9+
\Magento\Framework\Escaper $escaper
10+
) {
11+
$this->escaper = $escaper;
12+
}
13+
14+
public function afterToOptionArray(\A2bizz\ModuleCreator\Model\Source\IsActive $subject, $result)
15+
{
16+
// Modify the $result array as per your requirements
17+
foreach ($result as &$option) {
18+
// Modify each option as needed
19+
// For example, add a prefix to the option label
20+
if ($option['value']==1):
21+
//$option['label'] = $this->escaper->escapeHtml(__('<span class="grid-severity-notice"><span>%1</span></span>',$option['label']));
22+
$option['label'] = __('<span class="grid-severity-notice"><span>%1</span></span>',$option['label']);
23+
endif;
24+
if ($option['value']==0):
25+
//$option['label'] = $this->escaper->escapeHtml(__('<span class="grid-severity-critical"><span>%1</span></span>',$option['label']));
26+
$option['label'] = __('<span class="grid-severity-critical"><span>%1</span></span>',$option['label']);
27+
endif;
28+
}
29+
30+
return $result;
31+
}
32+
}

app/code/ModuleCreator/etc/db_schema.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<column name="content" nullable="true" xsi:type="text" comment="content"/>
2424
<column name="creation_time" nullable="false" xsi:type="timestamp" comment="creation_time" default="CURRENT_TIMESTAMP"/>
2525
<column name="update_time" nullable="false" xsi:type="timestamp" comment="update_time" default="CURRENT_TIMESTAMP" on_update="true"/>
26+
<column name="is_parent" nullable="false" xsi:type="smallint" comment="it checks if the created module has entry of namespace is very first (is_parent), if yes then it insert 1(true) else it insert 0(false)" default="0" identity="false"/>
2627
<column name="is_active" nullable="false" xsi:type="smallint" comment="is_active" default="0" identity="false"/>
2728
</table>
2829
</schema>

app/code/ModuleCreator/etc/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@
1616
</argument>
1717
</arguments>
1818
</type>
19+
20+
<!--
21+
Decorates presence of Is Active Enable and Disable in Colorful manner
22+
-->
23+
<!--<type name="A2bizz\ModuleCreator\Model\Source\IsActive">
24+
<plugin name="module_creator_option_array_decorator" type="A2bizz\ModuleCreator\Plugin\OptionArrayDecorator" sortOrder="10" />
25+
</type>
26+
-->
27+
1928
</config>

0 commit comments

Comments
 (0)