mootensai\enhancedgii\crud\Generator::generateActiveField PHP Method

generateActiveField() public method

Generates code for active field
public generateActiveField ( string $attribute, $fk, $tableSchema = null, $relations = null, $isTree = false ) : string
$attribute string
return string
    public function generateActiveField($attribute, $fk, $tableSchema = null, $relations = null, $isTree = false)
    {
        if ($isTree) {
            $model = "\$node";
        } else {
            if (is_null($relations)) {
                $model = "\$model";
            } else {
                $model = '$' . $relations[self::REL_CLASS];
            }
        }
        if (is_null($tableSchema)) {
            $tableSchema = $this->getTableSchema();
        }
        if (in_array($attribute, $this->hiddenColumns)) {
            return "\$form->field({$model}, '{$attribute}', ['template' => '{input}'])->textInput(['style' => 'display:none']);";
        }
        $placeholder = Inflector::humanize($attribute, true);
        if ($tableSchema === false || !isset($tableSchema->columns[$attribute])) {
            if (preg_match('/^(password|pass|passwd|passcode)$/i', $attribute)) {
                return "\$form->field({$model}, '{$attribute}')->passwordInput()";
            } else {
                if (in_array($attribute, $this->hiddenColumns)) {
                    return "\$form->field({$model}, '{$attribute}')->hiddenInput()";
                } else {
                    return "\$form->field({$model}, '{$attribute}')";
                }
            }
        }
        $column = $tableSchema->columns[$attribute];
        if ($column->phpType === 'boolean' || $column->dbType === 'tinyint(1)') {
            return "\$form->field({$model}, '{$attribute}')->checkbox()";
        } elseif ($column->type === 'text' || $column->dbType === 'tinytext') {
            return "\$form->field({$model}, '{$attribute}')->textarea(['rows' => 6])";
        } elseif ($column->dbType === 'date') {
            return "\$form->field({$model}, '{$attribute}')->widget(\\kartik\\datecontrol\\DateControl::classname(), [\r\n        'type' => \\kartik\\datecontrol\\DateControl::FORMAT_DATE,\r\n        'saveFormat' => 'php:Y-m-d',\r\n        'ajaxConversion' => true,\r\n        'options' => [\r\n            'pluginOptions' => [\r\n                'placeholder' => " . $this->generateString('Choose ' . $placeholder) . ",\r\n                'autoclose' => true\r\n            ]\r\n        ],\r\n    ]);";
        } elseif ($column->dbType === 'time') {
            return "\$form->field({$model}, '{$attribute}')->widget(\\kartik\\datecontrol\\DateControl::className(), [\r\n        'type' => \\kartik\\datecontrol\\DateControl::FORMAT_TIME,\r\n        'saveFormat' => 'php:H:i:s',\r\n        'ajaxConversion' => true,\r\n        'options' => [\r\n            'pluginOptions' => [\r\n                'placeholder' => " . $this->generateString('Choose ' . $placeholder) . ",\r\n                'autoclose' => true\r\n            ]\r\n        ]\r\n    ]);";
        } elseif ($column->dbType === 'datetime') {
            return "\$form->field({$model}, '{$attribute}')->widget(\\kartik\\datecontrol\\DateControl::classname(), [\r\n        'type' => \\kartik\\datecontrol\\DateControl::FORMAT_DATETIME,\r\n        'saveFormat' => 'php:Y-m-d H:i:s',\r\n        'ajaxConversion' => true,\r\n        'options' => [\r\n            'pluginOptions' => [\r\n                'placeholder' => " . $this->generateString('Choose ' . $placeholder) . ",\r\n                'autoclose' => true,\r\n            ]\r\n        ],\r\n    ]);";
        } elseif (array_key_exists($column->name, $fk)) {
            $rel = $fk[$column->name];
            $labelCol = $this->getNameAttributeFK($rel[3]);
            $humanize = Inflector::humanize($rel[3]);
            //            $pk = empty($this->tableSchema->primaryKey) ? $this->tableSchema->getColumnNames()[0] : $this->tableSchema->primaryKey[0];
            $fkClassFQ = "\\" . $this->nsModel . "\\" . $rel[1];
            $output = "\$form->field({$model}, '{$attribute}')->widget(\\kartik\\widgets\\Select2::classname(), [\r\n        'data' => \\yii\\helpers\\ArrayHelper::map({$fkClassFQ}::find()->orderBy('{$rel['4']}')->asArray()->all(), '{$rel['4']}', '{$labelCol}'),\r\n        'options' => ['placeholder' => " . $this->generateString('Choose ' . $humanize) . "],\r\n        'pluginOptions' => [\r\n            'allowClear' => true\r\n        ],\r\n    ]);";
            return $output;
        } else {
            if (preg_match('/^(password|pass|passwd|passcode)$/i', $column->name)) {
                $input = 'passwordInput';
            } else {
                $input = 'textInput';
            }
            if (is_array($column->enumValues) && count($column->enumValues) > 0) {
                $dropDownOptions = [];
                foreach ($column->enumValues as $enumValue) {
                    $dropDownOptions[$enumValue] = Inflector::humanize($enumValue);
                }
                return "\$form->field({$model}, '{$attribute}')->dropDownList(" . preg_replace("/\n\\s*/", ' ', VarDumper::export($dropDownOptions)) . ", ['prompt' => ''])";
            } elseif ($column->phpType !== 'string' || $column->size === null) {
                return "\$form->field({$model}, '{$attribute}')->{$input}(['placeholder' => '{$placeholder}'])";
            } else {
                return "\$form->field({$model}, '{$attribute}')->{$input}(['maxlength' => true, 'placeholder' => '{$placeholder}'])";
            }
        }
    }