77use Yii ;
88use yii \gii \CodeFile ;
99use yii \gii \Generator as BaseGenerator ;
10-
10+ use yii \ helpers \ ArrayHelper ;
1111
1212class Generator extends BaseGenerator
1313{
1414 use VariableExportTrait;
1515
1616 public $ path = '@app/migrations ' ;
1717
18- public $ block ;
18+ public $ blocks ;
1919
20- public static function getBlocks ()
20+ public function generate ()
2121 {
22- return Block::find ()->all ();
23- }
22+ $ blockIds = self ::getBlockIds ($ this ->blocks );
2423
24+ /** @var Block[] $selectedBlocks */
25+ $ selectedBlocks = Block::find ()->where (['id ' => $ blockIds ])->all ();
26+
27+ $ name = 'm ' . gmdate ('ymd_Hi ' ) . '00_blocks_ ' . count ($ selectedBlocks );
2528
26- public function generate ()
27- {
28- /** @var Block $selectedBlock */
29- $ selectedBlock = Block::find ()->where (['id ' => $ this ->block ])->one ();
3029 $ time = new \DateTime ();
3130 $ time = $ time ->getTimestamp ();
3231
33- $ resultConfig = 'serialize( ' . $ this ->varExport (unserialize ($ selectedBlock ->config )) . ') ' ;
34-
35- $ offset = "\n\t\t\t" ;
36- $ commands = "[ {$ offset }'id' => ' $ selectedBlock ->id ', " .
37- "{$ offset }'class_name' => ' $ selectedBlock ->class_name ', " .
38- "{$ offset }'name' => ' $ selectedBlock ->name ', " .
39- "{$ offset }'visibility' => ' $ selectedBlock ->visibility ', " .
40- "{$ offset }'config' => $ resultConfig, " .
41- "{$ offset }'created_at' => $ time, " .
42- "{$ offset }'updated_at' => $ time, {$ offset }] " ;
43-
32+ $ blocksToMigrate = [];
33+
34+ foreach ($ selectedBlocks as $ key => $ selectedBlock ) {
35+ $ resultConfig = 'serialize( ' . $ this ->varExport (unserialize ($ selectedBlock ->config )) . ') ' ;
36+ $ time += $ key ;
37+ $ offset = "\n\t\t\t" ;
38+ $ commands = "[ {$ offset }'id' => ' $ selectedBlock ->id ', " .
39+ "{$ offset }'class_name' => ' $ selectedBlock ->class_name ', " .
40+ "{$ offset }'name' => ' $ selectedBlock ->name ', " .
41+ "{$ offset }'visibility' => ' $ selectedBlock ->visibility ', " .
42+ "{$ offset }'config' => $ resultConfig, " .
43+ "{$ offset }'created_at' => $ time, " .
44+ "{$ offset }'updated_at' => $ time, {$ offset }] " ;
45+
46+ $ blocksToMigrate [$ key ] = [
47+ 'blockId ' => $ selectedBlock ->id ,
48+ 'commands ' => $ commands ,
49+ ];
50+ }
4451 $ files = [];
45- $ name = ' m ' . gmdate ( ' ymd_Hi ' ) . ' 00_block_ ' . str_replace ( ' - ' , ' _ ' , $ selectedBlock -> id );
52+
4653 $ code = $ this ->render ('migration.php ' , [
4754 'name ' => $ name ,
48- 'blockId ' => $ selectedBlock ->id ,
49- 'commands ' => $ commands ,
55+ 'blocksToMigrate ' => $ blocksToMigrate ,
5056 ]);
57+
5158 $ files [] = new CodeFile (
5259 Yii::getAlias ($ this ->path ) . '/ ' . $ name . '.php ' ,
5360 $ code
5461 );
62+
5563 return $ files ;
5664 }
5765
@@ -61,7 +69,7 @@ public function generate()
6169 public function rules ()
6270 {
6371 return [
64- [['block ' , 'path ' ], 'required ' ],
72+ [['blocks ' , 'path ' ], 'required ' ],
6573 ];
6674 }
6775
@@ -92,4 +100,71 @@ public function hints()
92100 ]);
93101 }
94102
103+ public static function getBlocks ()
104+ {
105+ return Block::find ()->all ();
106+ }
107+
108+ public static function getBlockIds ($ blocks )
109+ {
110+ $ blockIds = [];
111+ $ blockTypes = [];
112+
113+ foreach ($ blocks as $ block ) {
114+ $ typePosition = strpos ($ block , '-type ' );
115+ if ($ typePosition ) {
116+ array_push ($ blockTypes , substr ($ block , 0 , $ typePosition ));
117+ } else {
118+ array_push ($ blockIds , $ block );
119+ }
120+ }
121+ $ otherIds = Block::find ()->select (['id ' ])->where (['class_name ' => $ blockTypes ])->column ();
122+
123+ return ArrayHelper::merge ($ blockIds , $ otherIds );
124+ }
125+
126+ public static function getNestedList ($ blockIds )
127+ {
128+ if (!empty ($ blockIds )) {
129+ $ blockIds = self ::getBlockIds ($ blockIds );
130+ }
131+
132+ $ blocks = Block::find ()->orderBy (['id ' => SORT_ASC ])->orderBy (['class_name ' => SORT_ASC ])->asArray ()->all ();
133+
134+ $ key = -1 ;
135+ $ names = [];
136+ $ result = [];
137+ foreach ($ blocks as $ block ) {
138+ $ name = $ block ['class_name ' ];
139+ if (!in_array ($ name , $ names )) {
140+ $ groupName = $ name ;
141+ $ key ++;
142+ array_push ($ names , $ name );
143+
144+ $ result [$ key ] = [
145+ 'title ' => $ groupName ,
146+ 'key ' => $ groupName . '-type ' ,
147+ 'selected ' => false ,
148+ 'folder ' => true ,
149+ 'children ' => [],
150+ 'expanded ' => true ,
151+ ];
152+ }
153+
154+ $ selected = false ;
155+ if (!empty ($ blockIds )) {
156+ $ selected = (in_array ($ block ['id ' ], $ blockIds )) ? true : false ;
157+ }
158+
159+ $ block = [
160+ 'title ' => $ block ['id ' ],
161+ 'key ' => $ block ['id ' ],
162+ 'selected ' => $ selected
163+ ];
164+ $ result [$ key ]['children ' ][] = $ block ;
165+
166+ }
167+ return $ result ;
168+ }
169+
95170}
0 commit comments