@@ -138,6 +138,11 @@ class Generator extends \yii\gii\generators\crud\Generator
138138 */
139139 public $ fixOptions = '' ;
140140
141+ /**
142+ * @var bool whether to enable or disable the pluralization of the models name
143+ */
144+ public $ disablePluralization = false ;
145+
141146 /**
142147 * @var string form field for selecting and loading saved gii forms
143148 */
@@ -158,6 +163,10 @@ class Generator extends \yii\gii\generators\crud\Generator
158163
159164 private $ _p = [];
160165
166+ public $ translateRelations = ['translation ' , 'translation_meta ' ];
167+
168+ public $ enableCopy = true ;
169+
161170 /**
162171 * {@inheritdoc}
163172 */
@@ -230,6 +239,7 @@ public function rules()
230239 'generateAccessFilterMigrations ' ,
231240 'singularEntities ' ,
232241 'modelMessageCategory ' ,
242+ 'enableCopy '
233243 ],
234244 'safe ' ,
235245 ],
@@ -270,6 +280,7 @@ public function formAttributes()
270280 'accessFilter ' ,
271281 'singularEntities ' ,
272282 'modelMessageCategory ' ,
283+ 'enableCopy '
273284 ];
274285 }
275286
@@ -305,20 +316,20 @@ public function getControllerID()
305316 public function getModuleId ()
306317 {
307318 if (!$ this ->moduleNs ) {
308- $ controllerNs = \ yii \ helpers \ StringHelper::dirname (ltrim ($ this ->controllerClass , '\\' ));
309- $ this ->moduleNs = \ yii \ helpers \ StringHelper::dirname (ltrim ($ controllerNs , '\\' ));
319+ $ controllerNs = StringHelper::dirname (ltrim ($ this ->controllerClass , '\\' ));
320+ $ this ->moduleNs = StringHelper::dirname (ltrim ($ controllerNs , '\\' ));
310321 }
311322
312- return \ yii \ helpers \ StringHelper::basename ($ this ->moduleNs );
323+ return StringHelper::basename ($ this ->moduleNs );
313324 }
314325
315326 public function generate ()
316327 {
317328 $ accessDefinitions = require $ this ->getTemplatePath ().'/access_definition.php ' ;
318329
319- $ this ->controllerNs = \ yii \ helpers \ StringHelper::dirname (ltrim ($ this ->controllerClass , '\\' ));
320- $ this ->moduleNs = \ yii \ helpers \ StringHelper::dirname (ltrim ($ this ->controllerNs , '\\' ));
321- $ controllerName = substr (\ yii \ helpers \ StringHelper::basename ($ this ->controllerClass ), 0 , -10 );
330+ $ this ->controllerNs = StringHelper::dirname (ltrim ($ this ->controllerClass , '\\' ));
331+ $ this ->moduleNs = StringHelper::dirname (ltrim ($ this ->controllerNs , '\\' ));
332+ $ controllerName = substr (StringHelper::basename ($ this ->controllerClass ), 0 , -10 );
322333
323334 if ($ this ->singularEntities ) {
324335 $ this ->modelClass = Inflector::singularize ($ this ->modelClass );
@@ -345,7 +356,7 @@ public function generate()
345356 }
346357
347358 $ files [] = new CodeFile ($ baseControllerFile , $ this ->render ('controller.php ' , ['accessDefinitions ' => $ accessDefinitions ]));
348- $ params ['controllerClassName ' ] = \ yii \ helpers \ StringHelper::basename ($ this ->controllerClass );
359+ $ params ['controllerClassName ' ] = StringHelper::basename ($ this ->controllerClass );
349360
350361 if ($ this ->overwriteControllerClass || !is_file ($ controllerFile )) {
351362 $ files [] = new CodeFile ($ controllerFile , $ this ->render ('controller-extended.php ' , $ params ));
@@ -365,12 +376,32 @@ public function generate()
365376 $ viewPath = $ this ->getViewPath ();
366377 $ templatePath = $ this ->getTemplatePath ().'/views ' ;
367378
379+ $ model = Yii::createObject ($ this ->modelClass );
380+ if (array_key_exists ('crud-form ' , $ model ->scenarios ())) {
381+ $ model ->setScenario ('crud-form ' );
382+ } else {
383+ $ model ->setScenario ('crud ' );
384+ }
385+
386+ $ safeAttributes = $ model ->safeAttributes ();
387+ if (empty ($ safeAttributes )) {
388+ $ model ->setScenario ('default ' );
389+ $ safeAttributes = $ model ->safeAttributes ();
390+ }
391+ if (empty ($ safeAttributes )) {
392+ $ safeAttributes = $ model ::getTableSchema ()->columnNames ;
393+ }
394+
368395 foreach (scandir ($ templatePath ) as $ file ) {
369- if (empty ( $ this -> searchModelClass ) && $ file === '_search.php ' ) {
396+ if ($ file === '_search.php ' && ! $ this -> getRenderWithSearch () ) {
370397 continue ;
371398 }
372399 if (is_file ($ templatePath .'/ ' .$ file ) && pathinfo ($ file , PATHINFO_EXTENSION ) === 'php ' ) {
373- $ files [] = new CodeFile ("$ viewPath/ $ file " , $ this ->render ("views/ $ file " , ['permisions ' => $ permisions ]));
400+ $ files [] = new CodeFile ("$ viewPath/ $ file " , $ this ->render ("views/ $ file " , [
401+ 'model ' => $ model ,
402+ 'safeAttributes ' => $ safeAttributes ,
403+ 'accessDefinitions ' => $ accessDefinitions
404+ ]));
374405 }
375406 }
376407
@@ -462,4 +493,73 @@ public function var_export54($var, $indent = '')
462493 return var_export ($ var , true );
463494 }
464495 }
496+
497+ /**
498+ * @return array
499+ * @throws \yii\base\InvalidConfigException
500+ */
501+ public function generateSearchRules ()
502+ {
503+
504+ $ rules = parent ::generateSearchRules ();
505+ $ model = \Yii::createObject ($ this ->modelClass );
506+ foreach ($ model ->behaviors () as $ key => $ behavior ) {
507+ if (!empty ($ behavior ['translationAttributes ' ])) {
508+ $ rules [] = "[[' " . implode ("', ' " , $ behavior ['translationAttributes ' ]) . "'], 'safe'] " ;
509+ }
510+ }
511+ return $ rules ;
512+ }
513+
514+ /**
515+ * @return array
516+ * @throws \yii\base\InvalidConfigException
517+ */
518+ public function generateSearchConditions ()
519+ {
520+
521+ $ searchConditions = parent ::generateSearchConditions ();
522+ $ model = \Yii::createObject ($ this ->modelClass );
523+ foreach ($ model ->behaviors () as $ key => $ behavior ) {
524+ if (!empty ($ behavior ['translationAttributes ' ])) {
525+ foreach ($ behavior ['translationAttributes ' ] as $ translationAttribute ) {
526+ $ searchConditions [] = "\$query->andFilterWhere(['like',' {$ translationAttribute }', \$this-> $ translationAttribute]); " ;
527+ }
528+ }
529+ }
530+ return $ searchConditions ;
531+ }
532+
533+
534+ /**
535+ * @return array
536+ */
537+ public function getTranslationRelationModels ()
538+ {
539+ $ translationRelationModels = [];
540+ foreach ($ this ->translateRelations as $ translateRelation ) {
541+ $ translationRelationModels [] = $ this ->modelClass . Inflector::camelize ($ translateRelation );
542+ }
543+ return $ translationRelationModels ;
544+ }
545+
546+ /**
547+ * @return string
548+ */
549+ public function getTranslationModelClass () {
550+ return '\\' . $ this ->modelClass . Inflector::camelize ('translation ' );
551+ }
552+
553+ /**
554+ * @return bool
555+ * @throws \yii\base\InvalidConfigException
556+ */
557+ public function getHasTranslationRelation () {
558+ return isset (\Yii::createObject ($ this ->modelClass )->behaviors ()['translation ' ]);
559+ }
560+
561+ public function getRenderWithSearch ()
562+ {
563+ return $ this ->indexWidgetType !== 'grid ' && $ this ->searchModelClass !== '' ;
564+ }
465565}
0 commit comments