sitemap.php 2.92 KB
<?php
    use artbox\core\models\SitemapStatic;
    use yii\bootstrap\ActiveForm;
    use yii\bootstrap\Html;
    use yii\helpers\Url;
    use yii\web\View;
    use yiister\gentelella\widgets\Panel;
    
    /**
     * @var View            $this
     * @var SitemapStatic[] $models
     */
    $form = ActiveForm::begin();
    $xPanel = Panel::begin(
        [
            'header'      => \Yii::t('core', 'Static pages'),
            'collapsable' => true,
            'options'     => [
                'class' => 'x_panel dynamic_fields',
            ],
        ]
    );
    foreach ($models as $index => $model) {
        echo Html::tag(
            'div',
            $form->field(
                $model,
                "[$index]url",
                [
                    'options' => [
                        'class' => 'form-group col-xs-5',
                    ],
                ]
            )
                 ->textInput() . $form->field(
                $model,
                "[$index]priority",
                [
                    'options' => [
                        'class' => 'form-group col-xs-2',
                    ],
                ]
            )
                                      ->textInput() . Html::icon(
                'minus',
                [
                    'class'   => 'col-xs-1 field-group-remove',
                    'onclick' => 'sitemap_remove(this)',
                ]
            ),
            [
                'class' => 'row field-group',
            ]
        );
    }
    echo Html::button(
        \Yii::t('core', 'Add field'),
        [
            'class'   => 'btn btn-default',
            'onclick' => 'sitemap_add(this)',
            'data'    => [
                'url' => Url::to([ 'seo/create-static' ]),
            ],
        ]
    );
    $xPanel::end();
    $xPanel2 = Panel::begin(
        [
            'header'      => \Yii::t('core', 'Dynamic pages'),
            'collapsable' => true,
        ]
    );
    echo Html::tag('h3', 'To be done...');
    $xPanel2::end();
    echo Html::submitButton(
        \Yii::t('core', 'Generate'),
        [
            'class' => 'btn btn-primary',
        ]
    );
    $form::end();
    $js = <<<JS
    function sitemap_add(e) {
        var url = $(e)
            .data('url');
        var form_id = $(e)
            .parents('form')
            .attr('id');
        var form = $('#' + form_id);
        var count = $('.field-group').length;
        console.log(form_id);
        $.get(
            url, {
                'formId': form_id,
                'count': count
            }, function(data) {
                form.find('.field-group')
                    .last()
                    .after(data);
            }
        );
    }
    function sitemap_remove(e) {
        if($('.field-group').length > 1) {
            $(e)
                .parents('.field-group')
                .remove();
        }
    }
JS;
    $this->registerJs($js, $this::POS_END);