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

generateTabularFormField() public method

Generates code for Kartik Tabular Form field
public generateTabularFormField ( string $attribute, $fk, $tableSchema = null ) : string
$attribute string
return string
    public function generateTabularFormField($attribute, $fk, $tableSchema = null)
    {
        if (is_null($tableSchema)) {
            $tableSchema = $this->getTableSchema();
        }
        if (in_array($attribute, $this->hiddenColumns)) {
            return "\"{$attribute}\" => ['type' => TabularForm::INPUT_HIDDEN, 'visible' => false]";
        }
        $humanize = Inflector::humanize($attribute, true);
        if ($tableSchema === false || !isset($tableSchema->columns[$attribute])) {
            if (preg_match('/^(password|pass|passwd|passcode)$/i', $attribute)) {
                return "\"{$attribute}\" => ['type' => TabularForm::INPUT_PASSWORD]";
            } else {
                return "\"{$attribute}\" => ['type' => TabularForm::INPUT_TEXT]";
            }
        }
        $column = $tableSchema->columns[$attribute];
        if ($column->autoIncrement) {
            return "'{$attribute}' => ['type' => TabularForm::INPUT_HIDDEN, 'visible' => false]";
        } elseif ($column->phpType === 'boolean' || $column->dbType === 'tinyint(1)') {
            return "'{$attribute}' => ['type' => TabularForm::INPUT_CHECKBOX,\r\n            'options' => [\r\n                'style' => 'position : relative; margin-top : -9px'\r\n            ]\r\n        ]";
        } elseif ($column->type === 'text' || $column->dbType === 'tinytext') {
            return "'{$attribute}' => ['type' => TabularForm::INPUT_TEXTAREA]";
        } elseif ($column->dbType === 'date') {
            return "'{$attribute}' => ['type' => TabularForm::INPUT_WIDGET,\r\n            'widgetClass' => \\kartik\\datecontrol\\DateControl::classname(),\r\n            'options' => [\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 ' . $humanize) . ",\r\n                        'autoclose' => true\r\n                    ]\r\n                ],\r\n            ]\r\n        ]";
        } elseif ($column->dbType === 'time') {
            return "'{$attribute}' => ['type' => TabularForm::INPUT_WIDGET,\r\n            'widgetClass' => \\kartik\\datecontrol\\DateControl::classname(),\r\n            'options' => [\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 ' . $humanize) . ",\r\n                        'autoclose' => true\r\n                    ]\r\n                ]\r\n            ]\r\n        ]";
        } elseif ($column->dbType === 'datetime') {
            return "'{$attribute}' => ['type' => TabularForm::INPUT_WIDGET,\r\n            'widgetClass' => \\kartik\\datecontrol\\DateControl::classname(),\r\n            'options' => [\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 ' . $humanize) . ",\r\n                        'autoclose' => true,\r\n                    ]\r\n                ],\r\n            ]\r\n        ]";
        } elseif (array_key_exists($column->name, $fk)) {
            $rel = $fk[$column->name];
            $labelCol = $this->getNameAttributeFK($rel[self::REL_TABLE]);
            $humanize = Inflector::humanize($rel[self::REL_TABLE]);
            //            $pk = empty($this->tableSchema->primaryKey) ? $this->tableSchema->getColumnNames()[0] : $this->tableSchema->primaryKey[0];
            $fkClassFQ = "\\" . $this->nsModel . "\\" . $rel[self::REL_CLASS];
            $output = "'{$attribute}' => [\r\n            'label' => '{$humanize}',\r\n            'type' => TabularForm::INPUT_WIDGET,\r\n            'widgetClass' => \\kartik\\widgets\\Select2::className(),\r\n            'options' => [\r\n                'data' => \\yii\\helpers\\ArrayHelper::map({$fkClassFQ}::find()->orderBy('{$labelCol}')->asArray()->all(), '{$rel[self::REL_PRIMARY_KEY]}', '{$labelCol}'),\r\n                'options' => ['placeholder' => " . $this->generateString('Choose ' . $humanize) . "],\r\n            ],\r\n            'columnOptions' => ['width' => '200px']\r\n        ]";
            return $output;
        } else {
            if (preg_match('/^(password|pass|passwd|passcode)$/i', $column->name)) {
                $input = 'INPUT_PASSWORD';
            } else {
                $input = 'INPUT_TEXT';
            }
            if (is_array($column->enumValues) && count($column->enumValues) > 0) {
                $dropDownOptions = [];
                foreach ($column->enumValues as $enumValue) {
                    $dropDownOptions[$enumValue] = Inflector::humanize($enumValue);
                }
                return "'{$attribute}' => ['type' => TabularForm::INPUT_DROPDOWN_LIST,\r\n                    'items' => " . preg_replace("/\n\\s*/", ' ', VarDumper::export($dropDownOptions)) . ",\r\n                    'options' => [\r\n                        'columnOptions' => ['width' => '185px'],\r\n                        'options' => ['placeholder' => " . $this->generateString('Choose ' . $humanize) . "],\r\n                    ]\r\n        ]";
            } elseif ($column->phpType !== 'string' || $column->size === null) {
                return "'{$attribute}' => ['type' => TabularForm::{$input}]";
            } else {
                return "'{$attribute}' => ['type' => TabularForm::{$input}]";
                //max length??
            }
        }
    }