Skip to content

Commit

Permalink
adapt changes from other experiences
Browse files Browse the repository at this point in the history
  • Loading branch information
eluhr committed Mar 24, 2023
1 parent 51972d6 commit 059de44
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 90 deletions.
6 changes: 5 additions & 1 deletion src/commands/BatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,15 @@ class BatchController extends Controller
*/
public $modelGenerateRelationsFromCurrentSchema = true;

public $modelTranslationTableAdditions = ['name' => 'meta', 'fallbackLanguage' => false];

/**
* @var array application configuration for creating temporary applications
*/
protected $appConfig;

/**
* @var instance of class schmunk42\giiant\generators\model\Generator
* @var \schmunk42\giiant\generators\model\Generator instance of class schmunk42\giiant\generators\model\Generator
*/
protected $modelGenerator;

Expand Down Expand Up @@ -357,6 +359,7 @@ public function options($id)
'modelCacheRelationsData',
'modelGenerateRelations',
'modelGenerateRelationsFromCurrentSchema',
'modelTranslationTableAdditions',
'modelGenerateJunctionRelationMode',
'modelGenerateQuery',
'modelQueryNamespace',
Expand Down Expand Up @@ -477,6 +480,7 @@ public function actionModels()
'cacheRelationsData' => $this->modelCacheRelationsData,
'generateRelations' => $this->modelGenerateRelations,
'generateRelationsFromCurrentSchema' => $this->modelGenerateRelationsFromCurrentSchema,
'translationTableAdditions' => $this->modelTranslationTableAdditions,
'generateJunctionRelationMode' => $this->modelGenerateJunctionRelationMode,
'tableNameMap' => $this->tableNameMap,
'generateQuery' => $this->modelGenerateQuery,
Expand Down
101 changes: 100 additions & 1 deletion src/generators/crud/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class Generator extends \yii\gii\generators\crud\Generator

private $_p = [];

public $translateRelations = ['translation', 'translation_meta'];

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -370,12 +372,31 @@ public function generate()
$viewPath = $this->getViewPath();
$templatePath = $this->getTemplatePath().'/views';

$model = Yii::createObject($this->modelClass);
if (array_key_exists('crud-form', $model->scenarios())) {
$model->setScenario('crud-form');
} else {
$model->setScenario('crud');
}

$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
$model->setScenario('default');
$safeAttributes = $model->safeAttributes();
}
if (empty($safeAttributes)) {
$safeAttributes = $model::getTableSchema()->columnNames;
}

foreach (scandir($templatePath) as $file) {
if (empty($this->searchModelClass) && $file === '_search.php') {
continue;
}
if (is_file($templatePath.'/'.$file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$files[] = new CodeFile("$viewPath/$file", $this->render("views/$file", ['permisions' => $permisions]));
$files[] = new CodeFile("$viewPath/$file", $this->render("views/$file", [
'model' => $model,
'safeAttributes' => $safeAttributes
]));
}
}

Expand Down Expand Up @@ -467,4 +488,82 @@ public function var_export54($var, $indent = '')
return var_export($var, true);
}
}

/**
* @return array
* @throws \yii\base\InvalidConfigException
*/
public function generateSearchRules()
{

$rules = parent::generateSearchRules();
$model = \Yii::createObject($this->modelClass);
foreach ($model->behaviors() as $key => $behavior) {
if (!empty($behavior['translationAttributes'])) {
$rules[] = "[['" . implode("', '", $behavior['translationAttributes']) . "'], 'safe']";
}
}
return $rules;
}

/**
* @return array
* @throws \yii\base\InvalidConfigException
*/
public function generateSearchConditions()
{

$searchConditions = parent::generateSearchConditions();
$model = \Yii::createObject($this->modelClass);
foreach ($model->behaviors() as $key => $behavior) {
if (!empty($behavior['translationAttributes'])) {
foreach ($behavior['translationAttributes'] as $translationAttribute) {
$searchConditions[] = "\$query->andFilterWhere(['like','{$translationAttribute}', \$this->$translationAttribute]);";
}
}
}
return $searchConditions;
}


/**
* @return array
*/
public function getTranslationRelationModels()
{
$translationRelationModels = [];
foreach ($this->translateRelations as $translateRelation) {
$translationRelationModels[] = $this->modelClass . Inflector::camelize($translateRelation);
}
return $translationRelationModels;
}

/**
* @return string
*/
public function getTranslationModelClass() {
return '\\' . $this->modelClass . Inflector::camelize('translation');
}
/**
* @return string
*/
public function getTranslationMetaModelClass() {
return '\\' . $this->modelClass . Inflector::camelize('translation_meta');
}

/**
* @return bool
* @throws \yii\base\InvalidConfigException
*/
public function getHasTranslationRelation() {
return isset(\Yii::createObject($this->modelClass)->behaviors()['translation']);
}

/**
* @return bool
* @throws \yii\base\InvalidConfigException
*/
public function getHasTranslationMetaRelation() {
return isset(\Yii::createObject($this->modelClass)->behaviors()['translation_meta']);
}
}
16 changes: 14 additions & 2 deletions src/generators/crud/default/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ public function scenarios()
*/
public function search($params)
{
$query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();
$query = <?= $modelAlias ?? $modelClass ?>::find();

<?php if ($generator->hasTranslationRelation): ?>
$query->leftJoin(<?= $generator->translationModelClass?>::tableName(),<?= $modelAlias ?? $modelClass ?>::tableName() . '.id = ' . <?= $generator->translationModelClass?>::tableName() . '.<?= mb_strtolower(basename($modelClass))?>_id');
<?php endif; ?>

<?php if ($generator->hasTranslationMetaRelation): ?>
$query->leftJoin(<?= $generator->translationMetaModelClass?>::tableName(),<?= $modelAlias ?? $modelClass ?>::tableName() . '.id = ' . <?= $generator->translationMetaModelClass?>::tableName() . '.<?= mb_strtolower(basename($modelClass))?>_id');
<?php endif; ?>


$dataProvider = new ActiveDataProvider([
'query' => $query,
Expand All @@ -74,9 +83,12 @@ public function search($params)
// $query->where('0=1');
return $dataProvider;
}
<?php if ($generator->hasTranslationRelation): ?>
$query->groupBy(<?= $modelAlias ?? $modelClass ?>::tableName() . '.id');
<?php endif; ?>

<?= implode("\n ", $searchConditions) ?>

return $dataProvider;
}
}
}
36 changes: 36 additions & 0 deletions src/generators/crud/default/views/_form-fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @var yii\gii\generators\crud\Generator $generator
* @var ActiveRecord $model
* @var array $safeAttributes
*/

use yii\db\ActiveRecord;

echo "<?php\n";
?>
/**
* @var yii\web\View $this
* @var <?= ltrim($generator->modelClass, '\\') ?> $model
* @var yii\widgets\ActiveForm $form
*/

?>
<?php
foreach ($safeAttributes as $attribute) {
echo "\n\n\t<!-- attribute $attribute -->\n";
$prepend = $generator->prependActiveField($attribute, $model);
$field = $generator->activeField($attribute, $model);
$append = $generator->appendActiveField($attribute, $model);

if ($prepend) {
echo $prepend;
}
if ($field) {
echo "<?php echo " . $field . ' ?>';
}
if ($append) {
echo $append;
}
}
47 changes: 6 additions & 41 deletions src/generators/crud/default/views/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,16 @@
/**
* @var yii\web\View $this
* @var yii\gii\generators\crud\Generator $generator
* @var \yii\db\ActiveRecord $model
* @var array $safeAttributes
*/

/** @var \yii\db\ActiveRecord $model */
## TODO: move to generator (?); cleanup
$model = new $generator->modelClass();
if (array_key_exists('crud-form', $model->scenarios())) {
$model->setScenario('crud-form');
} else {
$model->setScenario('crud');
}
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
$model->setScenario('default');
$safeAttributes = $model->safeAttributes();
}
if (empty($safeAttributes)) {
$safeAttributes = $model::getTableSchema()->columnNames;
}

echo "<?php\n";
?>

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use \dmstr\bootstrap\Tabs;
use yii\bootstrap\Tabs;
use yii\helpers\StringHelper;

/**
Expand All @@ -55,7 +40,6 @@
'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
'horizontalCssClasses' => [
'label' => 'col-sm-2',
#'offset' => 'col-sm-offset-4',
'wrapper' => 'col-sm-8',
'error' => '',
'hint' => '',
Expand All @@ -67,28 +51,9 @@

<div class="">
<?php echo "<?php \$this->beginBlock('main'); ?>\n"; ?>

<p>
<?php
foreach ($safeAttributes as $attribute) {
echo "\n\n<!-- attribute $attribute -->";
$prepend = $generator->prependActiveField($attribute, $model);
$field = $generator->activeField($attribute, $model);
$append = $generator->appendActiveField($attribute, $model);

if ($prepend) {
echo "\n\t\t\t".$prepend;
}
if ($field) {
echo "\n\t\t\t<?= ".$field.' ?>';
}
if ($append) {
echo "\n\t\t\t".$append;
}
}
?>

</p>
<div class="form-fields">
<?php echo "<?php echo \$this->render('_form-fields', ['form' => \$form, 'model' => \$model])?>\n"?>
</div>
<?php echo '<?php $this->endBlock(); ?>'; ?>

<?php
Expand Down
4 changes: 2 additions & 2 deletions src/generators/crud/default/views/_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/*
/**
* @var yii\web\View $this
* @var yii\gii\generators\crud\Generator $generator
*/
Expand Down Expand Up @@ -32,7 +32,7 @@
$count = 0;
foreach ($generator->getTableSchema()->getColumnNames() as $attribute) {
if (++$count < 6) {
echo "\t\t<?= ".$generator->generateActiveSearchField($attribute)." ?>\n\n";
echo "\t\t<? ".$generator->generateActiveSearchField($attribute)." ?>\n\n";
} else {
echo "\t\t<?php // echo ".$generator->generateActiveSearchField($attribute)." ?>\n\n";
}
Expand Down
2 changes: 1 addition & 1 deletion src/generators/crud/default/views/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use yii\grid\GridView;
use yii\widgets\DetailView;
use yii\widgets\Pjax;
use dmstr\bootstrap\Tabs;
use yii\bootstrap\Tabs;

/**
* @var yii\web\View $this
Expand Down
Loading

0 comments on commit 059de44

Please sign in to comment.