schmunk42\giiant\generators\crud\providers\extensions\EditableProvider::relationGridEditable PHP Метод

relationGridEditable() публичный Метод

Renders a grid view for a given relation.
public relationGridEditable ( string $name, $relation, boolean $showAllRecords = false ) : mixed | string
$name string grid name
$relation grid relation
$showAllRecords boolean
Результат mixed | string
    public function relationGridEditable($name, $relation, $showAllRecords = false)
    {
        $model = new $relation->modelClass();
        // column counter
        $counter = 0;
        $columns = '';
        if (!$this->generator->isPivotRelation($relation)) {
            // hasMany relations
            $template = '{view} {update}';
            $deleteButtonPivot = '';
        } else {
            // manyMany relations
            $template = '{view} {delete}';
            $deleteButtonPivot = <<<EOS
'delete' => function (\$url, \$model) {
                return Html::a('<span class="glyphicon glyphicon-remove"></span>', \$url, [
                    'class' => 'text-danger',
                    'title'         => {$this->generator->generateString('Remove')},
                    'data-confirm'  => {$this->generator->generateString('Are you sure you want to delete the related item?')},
                    'data-method' => 'post',
                    'data-pjax' => '0',
                ]);
            },
'view' => function (\$url, \$model) {
                return Html::a(
                    '<span class="glyphicon glyphicon-cog"></span>',
                    \$url,
                    [
                        'data-title'  => {$this->generator->generateString('View Pivot Record')},
                        'data-toggle' => 'tooltip',
                        'data-pjax'   => '0',
                        'class'       => 'text-muted',
                    ]
                );
            },
EOS;
        }
        $reflection = new \ReflectionClass($relation->modelClass);
        $controller = $this->generator->pathPrefix . Inflector::camel2id($reflection->getShortName(), '-', true);
        //        $actionColumn = <<<EOS
        //[
        //    'class'      => '{$this->generator->actionButtonClass}',
        //    'template'   => '$template',
        //    'contentOptions' => ['nowrap'=>'nowrap'],
        //    'urlCreator' => function (\$action, \$model, \$key, \$index) {
        //        // using the column name as key, not mapping to 'id' like the standard generator
        //        \$params = is_array(\$key) ? \$key : [\$model->primaryKey()[0] => (string) \$key];
        //        \$params[0] = '$controller' . '/' . \$action;
        //        return \$params;
        //    },
        //    'buttons'    => [
        //        $deleteButtonPivot
        //    ],
        //    'controller' => '$controller'
        //]
        //EOS;
        //        // add action column
        //        $columns .= $actionColumn.",\n";
        // prepare grid column formatters
        $model->setScenario('crud');
        $safeAttributes = $model->safeAttributes();
        if (empty($safeAttributes)) {
            $safeAttributes = $model->getTableSchema()->columnNames;
        }
        $hasDate = false;
        foreach ($safeAttributes as $attribute) {
            // max seven columns
            if ($counter > $this->generator->gridRelationMaxColumns) {
                continue;
            }
            //skip primeary key
            if ($model->isPrimaryKey([$attribute])) {
                continue;
            }
            // skip virtual attributes
            if ($this->skipVirtualAttributes && !isset($model->tableSchema->columns[$attribute])) {
                continue;
            }
            // don't show current model
            if (key($relation->link) == $attribute) {
                continue;
            }
            /*
             * search opts... method
             */
            $optsFunc = 'opts' . str_replace('_', '', $attribute);
            $optsCamelFunc = 'opts' . str_replace(' ', '', ucwords(implode(' ', explode('_', $attribute))));
            $useOptsFunc = false;
            if (method_exists($model::className(), $optsFunc)) {
                $useOptsFunc = $optsFunc;
            } elseif (method_exists($model::className(), $optsCamelFunc)) {
                $useOptsFunc = $optsCamelFunc;
            }
            $tableColumn = $this->generator->getColumnByAttribute($attribute, $model);
            $inputType = $this->getInputType($tableColumn);
            $relRelation = $this->generator->getRelationByColumn($model->ClassName(), $tableColumn);
            if ($relRelation) {
                $relModelStatic = $relRelation->modelClass . 'Static';
            }
            if ($tableColumn->type == 'date') {
                $hasDate = true;
                $code = "\n        [\n            'class' => '\\kartik\\grid\\EditableColumn',\n            'attribute' => '{$attribute}',\n            'format' => 'date',                \n            'editableOptions' => [\n                'inputType' => \\kartik\\editable\\Editable::INPUT_WIDGET,\n                'widgetClass' => 'kartik\\datecontrol\\DateControl',\n                'formOptions' => [\n                    'action' => [\n                        '{$controller}/editable-column-update'\n                    ]\n                ],                            \n                'options' => [\n                    'type' => \\kartik\\datecontrol\\DateControl::FORMAT_DATE,\n                    'displayFormat' => \$datePattern,\n                    'saveFormat' => 'php:Y-m-d',\n                    'options' => [\n                        'pluginOptions' => [\n                            'autoclose' => true\n                        ]\n                    ]\n                ]\n            ]\n        ],\n\n        ]";
            } elseif ($relRelation && !$relRelation->multiple && method_exists($relModelStatic, 'getListData')) {
                $hasParameterForValue = false;
                $r = new \ReflectionMethod($relModelStatic, 'getListData');
                $params = $r->getParameters();
                foreach ($params as $param) {
                    if ($hasParameterForValue = $param->getName() == 'forValue') {
                        break;
                    }
                }
                if ($hasParameterForValue) {
                    $code = "\n        [\n            'class' => '\\kartik\\grid\\EditableColumn',\n            'attribute' => '{$attribute}',\n            'editableOptions' => [\n                'formOptions' => [\n                    'action' => [\n                        '{$controller}/editable-column-update'\n                    ]\n                ],\n                'inputType' => Editable::INPUT_DROPDOWN_LIST,\n                'data' => {$relRelation->modelClass}Static::getListData(),\n                'displayValueConfig' => {$relRelation->modelClass}Static::getListData(true),\n            ]\n        ]";
                } else {
                    $code = "\n        [\n            'class' => '\\kartik\\grid\\EditableColumn',\n            'attribute' => '{$attribute}',\n            'editableOptions' => [\n                'formOptions' => [\n                    'action' => [\n                        '{$controller}/editable-column-update'\n                    ]\n                ],\n                'inputType' => Editable::INPUT_DROPDOWN_LIST,\n                'data' => {$relRelation->modelClass}Static::getListData(),\n                'displayValueConfig' => {$relRelation->modelClass}Static::getListData(),\n            ]\n        ]";
                }
            } elseif ($relRelation && !$relRelation->multiple) {
                $relPk = key($relRelation->link);
                $relName = $this->generator->getModelNameAttribute($relRelation->modelClass);
                $code = "\n        [\n            'class' => '\\kartik\\grid\\EditableColumn',\n            'attribute' => '{$attribute}',\n            'editableOptions' => [\n                'formOptions' => [\n                    'action' => [\n                        '{$controller}/editable-column-update'\n                    ]\n                ],\n                'inputType' => Editable::INPUT_DROPDOWN_LIST,\n                'data' => \\yii\\helpers\\ArrayHelper::map({$relRelation->modelClass}::find()->all(), '{$relPk}', '{$relName}'),\n                'displayValueConfig' => \\yii\\helpers\\ArrayHelper::map({$relRelation->modelClass}::find()->all(), '{$relPk}', '{$relName}'),\n            ]\n        ]";
            } elseif ($useOptsFunc) {
                $code = "\n        [\n            'class' => '\\kartik\\grid\\EditableColumn',\n            'attribute' => '{$attribute}',\n            'editableOptions' => [\n                'formOptions' => [\n                    'action' => [\n                        '{$controller}/editable-column-update'\n                    ]\n                ],\n                'inputType' => Editable::INPUT_DROPDOWN_LIST,\n                'data' => {$model->ClassName()}::{$useOptsFunc}(),\n                'displayValueConfig' => {$model->ClassName()}::{$useOptsFunc}(),                            \n            ]\n        ]";
            } else {
                $code = "\n        [\n            'class' => '\\kartik\\grid\\EditableColumn',\n            'attribute' => '{$attribute}',\n            'editableOptions' => [\n                'formOptions' => [\n                    'action' => [\n                        '{$controller}/editable-column-update'\n                    ]\n                ],\n                'inputType' => " . $inputType . '
            ]
        ]';
            }
            //$code = $this->generator->columnFormat($attr, $model);
            if ($code == false) {
                continue;
            }
            $columns .= $code . ",\n";
            ++$counter;
        }
        // action column
        $columns .= "  \n        [\n            'class' => '\\kartik\\grid\\ActionColumn',\n            'template' => '{view} {update} {delete}',\n            'urlCreator' =>  \n                function(\$action, \$model, \$key, \$index) {\n                    \$params = is_array(\$key) ? \$key : ['id' => (string) \$key];\n                    \$params[0] = '{$controller}/' . \$action;\n                    \$params['{$model->formName()}'] = ['" . key($relation->link) . "' => \$model->primaryKey()[0]];\n                    return Url::toRoute(\$params);            \n                },\n        ]            \n                 ";
        $query = $showAllRecords ? "'query' => \\{$relation->modelClass}::find()" : "'query' => \$model->get{$name}()";
        $pageParam = Inflector::slug("page-{$name}");
        $firstPageLabel = $this->generator->generateString('First');
        $lastPageLabel = $this->generator->generateString('Last');
        $code = '';
        if ($hasDate) {
            $code .= <<<EOS
            \$formatter = new IntlDateFormatter(\\Yii::\$app->language,IntlDateFormatter::SHORT, IntlDateFormatter::NONE);            
            \$datePattern = \$formatter->getPattern();


EOS;
        }
        $code .= <<<EOS
            echo GridView::widget([
                'layout' => '{items}{pager}',
                'export' => false,                
                'dataProvider' => new \\yii\\data\\ActiveDataProvider([{$query}, 'pagination' => ['pageSize' => 20, 'pageParam'=>'{$pageParam}']]),
                'export' => false,
                'tableOptions' => [
                    'class' => 'table table-striped table-success'
                ],               

            //    'pager'        => [
            //        'class'          => yii\\widgets\\LinkPager::className(),
            //        'firstPageLabel' => {$firstPageLabel},
            //        'lastPageLabel'  => {$lastPageLabel}
            //    ],
                'columns' => [{$columns}]
            ]);
EOS;
        return $code;
    }