Commit a42025b8959beeda6df0d86bf5915c482349e15a
1 parent
15b8e84d
Sitemap and slugbehavior fix
Showing
11 changed files
with
881 additions
and
5 deletions
Show diff stats
backend/config/main.php
| 1 | 1 | <?php |
| 2 | + use artbox\core\models\Page; | |
| 3 | + use common\components\Sitemap; | |
| 2 | 4 | use yii\web\UrlManager; |
| 3 | 5 | |
| 4 | 6 | $params = array_merge( |
| ... | ... | @@ -100,6 +102,18 @@ |
| 100 | 102 | 'showScriptName' => false, |
| 101 | 103 | 'rules' => [], |
| 102 | 104 | ], |
| 105 | + 'sitemap' => [ | |
| 106 | + 'class' => Sitemap::className(), | |
| 107 | + 'entities' => [ | |
| 108 | + [ | |
| 109 | + 'class' => Page::className(), | |
| 110 | + 'conditions' => [ | |
| 111 | + [ 'in_menu' => 1 ], | |
| 112 | + ], | |
| 113 | + 'url' => 'site/page', | |
| 114 | + ], | |
| 115 | + ], | |
| 116 | + ], | |
| 103 | 117 | ], |
| 104 | 118 | 'params' => $params, |
| 105 | 119 | ]; | ... | ... |
| 1 | +<?php | |
| 2 | + namespace backend\controllers; | |
| 3 | + | |
| 4 | + use common\components\Sitemap; | |
| 5 | + use common\models\SitemapDynamic; | |
| 6 | + use common\models\SitemapStatic; | |
| 7 | + use yii\bootstrap\ActiveForm; | |
| 8 | + use yii\bootstrap\Html; | |
| 9 | + use yii\filters\AccessControl; | |
| 10 | + use yii\helpers\Json; | |
| 11 | + use yii\web\Controller; | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * Class SitemapController | |
| 15 | + */ | |
| 16 | + class SitemapController extends Controller | |
| 17 | + { | |
| 18 | + /** | |
| 19 | + * @inheritdoc | |
| 20 | + */ | |
| 21 | + public function behaviors() | |
| 22 | + { | |
| 23 | + return [ | |
| 24 | + 'access' => [ | |
| 25 | + 'class' => AccessControl::className(), | |
| 26 | + 'rules' => [ | |
| 27 | + [ | |
| 28 | + 'actions' => [ | |
| 29 | + 'login', | |
| 30 | + 'error', | |
| 31 | + ], | |
| 32 | + 'allow' => true, | |
| 33 | + ], | |
| 34 | + [ | |
| 35 | + 'allow' => true, | |
| 36 | + 'roles' => [ '@' ], | |
| 37 | + ], | |
| 38 | + ], | |
| 39 | + ], | |
| 40 | + ]; | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Action to configure sitemap of the website | |
| 45 | + * | |
| 46 | + * @return string | |
| 47 | + */ | |
| 48 | + public function actionIndex() | |
| 49 | + { | |
| 50 | + return $this->render('index'); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Action to configure sitemap of the website | |
| 55 | + * | |
| 56 | + * @return string | |
| 57 | + */ | |
| 58 | + public function actionUpdate() | |
| 59 | + { | |
| 60 | + /** | |
| 61 | + * @var Sitemap $sitemap | |
| 62 | + */ | |
| 63 | + $request = \Yii::$app->request; | |
| 64 | + // ***** Generate SitemapDynamic models for every entity in Sitemap component | |
| 65 | + $sitemap = \Yii::$app->get('sitemap'); | |
| 66 | + $entities = $sitemap->entities; | |
| 67 | + /** | |
| 68 | + * @var SitemapDynamic[] $entity_models | |
| 69 | + */ | |
| 70 | + $entity_models = []; | |
| 71 | + foreach ($entities as $entity) { | |
| 72 | + $entity_model = new SitemapDynamic(); | |
| 73 | + $entity_model->entity = $entity[ 'class' ]; | |
| 74 | + $entity_model->status = SitemapDynamic::STATUS_DISABLED; | |
| 75 | + $entity_models[] = $entity_model; | |
| 76 | + } | |
| 77 | + // ***** <<< End | |
| 78 | + if ($request->isPost) { | |
| 79 | + $success = false; | |
| 80 | + // ***** Create SitemapStatic models from POST and delete existing | |
| 81 | + $models = []; | |
| 82 | + $index = 1; | |
| 83 | + foreach ($request->post('SitemapStatic') as $item) { | |
| 84 | + $model = new SitemapStatic(); | |
| 85 | + if ($model->load($item, '') && $model->validate()) { | |
| 86 | + $model->id = $index++; | |
| 87 | + $models[] = $model; | |
| 88 | + } | |
| 89 | + } | |
| 90 | + if (!empty( $models )) { | |
| 91 | + $old = SitemapStatic::find() | |
| 92 | + ->all(); | |
| 93 | + foreach ($old as $item) { | |
| 94 | + $item->delete(); | |
| 95 | + } | |
| 96 | + foreach ($models as $model) { | |
| 97 | + $model->save(false); | |
| 98 | + $success = true; | |
| 99 | + } | |
| 100 | + } | |
| 101 | + // ***** <<< End | |
| 102 | + // ***** Create SitemapDynamic models from POST and delete existing | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * @var SitemapDynamic[] $old_entity_models | |
| 106 | + */ | |
| 107 | + $old_entity_models = SitemapDynamic::find() | |
| 108 | + ->all(); | |
| 109 | + foreach ($old_entity_models as $old_entity_model) { | |
| 110 | + $old_entity_model->delete(); | |
| 111 | + } | |
| 112 | + $index = 1; | |
| 113 | + $entity_models = []; | |
| 114 | + foreach ($request->post('SitemapDynamic') as $item) { | |
| 115 | + $entity = new SitemapDynamic(); | |
| 116 | + if ($entity->load($item, '') && $entity->validate()) { | |
| 117 | + $entity->id = $index++; | |
| 118 | + $entity->save(false); | |
| 119 | + $entity_models[] = $entity; | |
| 120 | + $success = true; | |
| 121 | + } | |
| 122 | + } | |
| 123 | + if ($success) { | |
| 124 | + if ($request->post('action', '') == 'generate') { | |
| 125 | + if ($sitemap->generateXML()) { | |
| 126 | + \Yii::$app->session->setFlash( | |
| 127 | + 'success', | |
| 128 | + \Yii::t( | |
| 129 | + 'core', | |
| 130 | + 'Sitemap generated to ' . \Yii::getAlias( | |
| 131 | + $sitemap->path . '.' | |
| 132 | + ) | |
| 133 | + ) | |
| 134 | + ); | |
| 135 | + } | |
| 136 | + } | |
| 137 | + return $this->redirect([ 'index' ]); | |
| 138 | + } | |
| 139 | + // ***** <<< End | |
| 140 | + } else { | |
| 141 | + // ***** Find existing SitemapStatic models | |
| 142 | + $models = SitemapStatic::find() | |
| 143 | + ->all(); | |
| 144 | + if (empty( $models )) { | |
| 145 | + $models = [ new SitemapStatic() ]; | |
| 146 | + } | |
| 147 | + // ***** <<< End | |
| 148 | + // ***** Fill SitemapDynamic models from Sitemap component with existing models | |
| 149 | + /** | |
| 150 | + * @var SitemapDynamic[] $old_entity_models | |
| 151 | + */ | |
| 152 | + $old_entity_models = SitemapDynamic::find() | |
| 153 | + ->indexBy('entity') | |
| 154 | + ->all(); | |
| 155 | + foreach ($entity_models as $index => $entity_model) { | |
| 156 | + if (isset( $old_entity_models[ $entity_model->entity ] )) { | |
| 157 | + $entity_model->status = $old_entity_models[ $entity_model->entity ]->status; | |
| 158 | + $entity_model->priority = $old_entity_models[ $entity_model->entity ]->priority; | |
| 159 | + } | |
| 160 | + } | |
| 161 | + // ***** <<< End | |
| 162 | + } | |
| 163 | + return $this->render( | |
| 164 | + 'update', | |
| 165 | + [ | |
| 166 | + 'models' => $models, | |
| 167 | + 'entity_models' => $entity_models, | |
| 168 | + ] | |
| 169 | + ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * Create activeField for static sitemap | |
| 174 | + * | |
| 175 | + * @return string | |
| 176 | + */ | |
| 177 | + public function actionCreateStatic() | |
| 178 | + { | |
| 179 | + $content = ''; | |
| 180 | + $request = \Yii::$app->request; | |
| 181 | + $formId = $request->get('formId'); | |
| 182 | + $count = $request->get('count'); | |
| 183 | + if (empty( $formId ) || empty( $count )) { | |
| 184 | + return $this->renderContent($content); | |
| 185 | + } | |
| 186 | + $model = new SitemapStatic(); | |
| 187 | + $form = new ActiveForm(); | |
| 188 | + $content .= $form->field( | |
| 189 | + $model, | |
| 190 | + "[$count]url", | |
| 191 | + [ | |
| 192 | + 'options' => [ | |
| 193 | + 'class' => 'form-group col-xs-8 col-sm-9', | |
| 194 | + ], | |
| 195 | + ] | |
| 196 | + ) | |
| 197 | + ->textInput() | |
| 198 | + ->render(); | |
| 199 | + $content .= $form->field( | |
| 200 | + $model, | |
| 201 | + "[$count]priority", | |
| 202 | + [ | |
| 203 | + 'options' => [ | |
| 204 | + 'class' => 'form-group col-xs-3 col-sm-2', | |
| 205 | + ], | |
| 206 | + ] | |
| 207 | + ) | |
| 208 | + ->textInput() | |
| 209 | + ->render(); | |
| 210 | + $content .= Html::icon( | |
| 211 | + 'minus', | |
| 212 | + [ | |
| 213 | + 'class' => 'col-xs-1 field-group-remove', | |
| 214 | + 'onclick' => 'sitemap_remove(this)', | |
| 215 | + ] | |
| 216 | + ); | |
| 217 | + foreach ($form->attributes as $index => $attribute) { | |
| 218 | + $content .= Html::script("$('#w0').yiiActiveForm('add', " . Json::htmlEncode($attribute) . ");"); | |
| 219 | + } | |
| 220 | + $content = Html::tag( | |
| 221 | + 'div', | |
| 222 | + $content, | |
| 223 | + [ | |
| 224 | + 'class' => 'row field-group', | |
| 225 | + ] | |
| 226 | + ); | |
| 227 | + $this->layout = false; | |
| 228 | + return $this->renderContent($content); | |
| 229 | + } | |
| 230 | + | |
| 231 | + /** | |
| 232 | + * Generate sitemap XML to Sitemap::$path | |
| 233 | + * | |
| 234 | + * @return bool | |
| 235 | + */ | |
| 236 | + public function actionGenerate() | |
| 237 | + { | |
| 238 | + $response = \Yii::$app->response; | |
| 239 | + $response->format = $response::FORMAT_JSON; | |
| 240 | + /** | |
| 241 | + * @var Sitemap $sitemap | |
| 242 | + */ | |
| 243 | + $sitemap = \Yii::$app->get('sitemap'); | |
| 244 | + return $sitemap->generateXML(); | |
| 245 | + } | |
| 246 | + } | |
| 247 | + | |
| 0 | 248 | \ No newline at end of file | ... | ... |
backend/views/layouts/main.php
| 1 | +<?php | |
| 2 | + use artbox\gentelella\widgets\XPanel; | |
| 3 | + use yii\bootstrap\Html; | |
| 4 | + use yii\web\View; | |
| 5 | + | |
| 6 | + /** | |
| 7 | + * @var View $this | |
| 8 | + */ | |
| 9 | + $this->title = \Yii::t('core', 'Sitemap'); | |
| 10 | + $this->params[ 'breadcrumbs' ][] = $this->title; | |
| 11 | +?> | |
| 12 | +<div class="sitemap-main"> | |
| 13 | + <?php | |
| 14 | + $xPanel = XPanel::begin( | |
| 15 | + [ | |
| 16 | + 'toolbar' => false, | |
| 17 | + 'title' => $this->title, | |
| 18 | + 'options' => [ | |
| 19 | + 'class' => 'sitemap-buttons', | |
| 20 | + ], | |
| 21 | + ] | |
| 22 | + ); | |
| 23 | + ?> | |
| 24 | + <div class="row"> | |
| 25 | + <div class="col-xs-12 col-sm-6 text-center"> | |
| 26 | + <?php | |
| 27 | + echo Html::a( | |
| 28 | + Html::icon( | |
| 29 | + 'edit', | |
| 30 | + [ | |
| 31 | + 'prefix' => 'fa fa-', | |
| 32 | + ] | |
| 33 | + ) . \Yii::t('core', 'Edit'), | |
| 34 | + [ 'update' ], | |
| 35 | + [ | |
| 36 | + 'class' => 'btn btn-app', | |
| 37 | + ] | |
| 38 | + ); | |
| 39 | + ?> | |
| 40 | + </div> | |
| 41 | + <div class="col-xs-12 col-sm-6 text-center"> | |
| 42 | + <?php | |
| 43 | + echo Html::a( | |
| 44 | + Html::icon( | |
| 45 | + 'cogs', | |
| 46 | + [ | |
| 47 | + 'prefix' => 'fa fa-', | |
| 48 | + 'class' => 'indexed', | |
| 49 | + ] | |
| 50 | + ) . Html::tag( | |
| 51 | + 'span', | |
| 52 | + \Yii::t('core', 'Generate'), | |
| 53 | + [ | |
| 54 | + 'class' => 'indexed', | |
| 55 | + ] | |
| 56 | + ) . Html::tag( | |
| 57 | + 'div', | |
| 58 | + ' ', | |
| 59 | + [ | |
| 60 | + 'class' => 'spinner', | |
| 61 | + ] | |
| 62 | + ), | |
| 63 | + [ 'generate' ], | |
| 64 | + [ | |
| 65 | + 'class' => 'btn btn-app ajax', | |
| 66 | + ] | |
| 67 | + ); | |
| 68 | + ?> | |
| 69 | + </div> | |
| 70 | + </div> | |
| 71 | + <?php | |
| 72 | + $xPanel::end(); | |
| 73 | + ?> | |
| 74 | +</div> | ... | ... |
| 1 | +<?php | |
| 2 | + use common\models\SitemapDynamic; | |
| 3 | + use common\models\SitemapStatic; | |
| 4 | + use artbox\gentelella\widgets\XPanel; | |
| 5 | + use yii\bootstrap\ActiveForm; | |
| 6 | + use yii\bootstrap\Html; | |
| 7 | + use yii\helpers\Url; | |
| 8 | + use yii\web\View; | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * @var View $this | |
| 12 | + * @var SitemapStatic[] $models | |
| 13 | + * @var SitemapDynamic[] $entity_models | |
| 14 | + */ | |
| 15 | + $this->params[ 'breadcrumbs' ][] = [ | |
| 16 | + 'label' => \Yii::t('core', 'Sitemap'), | |
| 17 | + 'url' => [ 'index' ], | |
| 18 | + ]; | |
| 19 | + $this->title = \Yii::t('core', 'Update sitemap'); | |
| 20 | + $this->params[ 'breadcrumbs' ][] = $this->title; | |
| 21 | + $form = ActiveForm::begin(); | |
| 22 | + $xPanel = XPanel::begin( | |
| 23 | + [ | |
| 24 | + 'title' => \Yii::t('core', 'Static pages'), | |
| 25 | + 'toolbarLayout' => '{collapse}', | |
| 26 | + 'options' => [ | |
| 27 | + 'class' => 'dynamic_fields', | |
| 28 | + ], | |
| 29 | + ] | |
| 30 | + ); | |
| 31 | + foreach ($models as $index => $model) { | |
| 32 | + echo Html::tag( | |
| 33 | + 'div', | |
| 34 | + $form->field( | |
| 35 | + $model, | |
| 36 | + "[$index]url", | |
| 37 | + [ | |
| 38 | + 'options' => [ | |
| 39 | + 'class' => 'form-group col-xs-8 col-sm-9', | |
| 40 | + ], | |
| 41 | + ] | |
| 42 | + ) | |
| 43 | + ->textInput() . $form->field( | |
| 44 | + $model, | |
| 45 | + "[$index]priority", | |
| 46 | + [ | |
| 47 | + 'options' => [ | |
| 48 | + 'class' => 'form-group col-xs-3 col-sm-2', | |
| 49 | + ], | |
| 50 | + ] | |
| 51 | + ) | |
| 52 | + ->textInput() . Html::icon( | |
| 53 | + 'minus', | |
| 54 | + [ | |
| 55 | + 'class' => 'col-xs-1 field-group-remove', | |
| 56 | + 'onclick' => 'sitemap_remove(this)', | |
| 57 | + ] | |
| 58 | + ), | |
| 59 | + [ | |
| 60 | + 'class' => 'row field-group', | |
| 61 | + ] | |
| 62 | + ); | |
| 63 | + } | |
| 64 | + echo Html::button( | |
| 65 | + \Yii::t('core', 'Add field'), | |
| 66 | + [ | |
| 67 | + 'class' => 'btn btn-default', | |
| 68 | + 'onclick' => 'sitemap_add(this)', | |
| 69 | + 'data' => [ | |
| 70 | + 'url' => Url::to([ 'create-static' ]), | |
| 71 | + ], | |
| 72 | + ] | |
| 73 | + ); | |
| 74 | + $xPanel::end(); | |
| 75 | + $xPanel2 = XPanel::begin( | |
| 76 | + [ | |
| 77 | + 'title' => \Yii::t('core', 'Dynamic pages'), | |
| 78 | + 'toolbarLayout' => '{collapse}', | |
| 79 | + ] | |
| 80 | + ); | |
| 81 | +?> | |
| 82 | + <div class="col-xs-9 col-sm-10"></div> | |
| 83 | + <div class="col-xs-3 col-sm-2 text-center"><?php echo $entity_models[ 0 ]->getAttributeLabel('priority'); ?></div> | |
| 84 | + <div class="clearfix"></div> | |
| 85 | +<?php | |
| 86 | + foreach ($entity_models as $index => $entity_model) { | |
| 87 | + ?> | |
| 88 | + <div class="form-group col-xs-9 col-sm-10"> | |
| 89 | + <?php | |
| 90 | + echo Html::tag( | |
| 91 | + 'div', | |
| 92 | + Html::activeInput( | |
| 93 | + 'text', | |
| 94 | + $entity_model, | |
| 95 | + "[$index]entity", | |
| 96 | + [ | |
| 97 | + 'class' => 'form-control', | |
| 98 | + 'readonly' => 1, | |
| 99 | + ] | |
| 100 | + ) . Html::tag( | |
| 101 | + 'span', | |
| 102 | + Html::activeCheckbox( | |
| 103 | + $entity_model, | |
| 104 | + "[$index]status", | |
| 105 | + [ | |
| 106 | + 'label' => false, | |
| 107 | + ] | |
| 108 | + ), | |
| 109 | + [ | |
| 110 | + 'class' => 'input-group-addon', | |
| 111 | + ] | |
| 112 | + ), | |
| 113 | + [ | |
| 114 | + 'class' => 'input-group', | |
| 115 | + ] | |
| 116 | + ); | |
| 117 | + ?> | |
| 118 | + </div> | |
| 119 | + <div class="form-group col-xs-3 col-sm-2"> | |
| 120 | + <?php | |
| 121 | + echo $form->field($entity_model, "[$index]priority") | |
| 122 | + ->label(false) | |
| 123 | + ->input( | |
| 124 | + 'number', | |
| 125 | + [ | |
| 126 | + 'step' => 0.1, | |
| 127 | + 'min' => 0, | |
| 128 | + 'max' => 1, | |
| 129 | + ] | |
| 130 | + ); | |
| 131 | + ?> | |
| 132 | + </div> | |
| 133 | + <?php | |
| 134 | + } | |
| 135 | + $xPanel2::end(); | |
| 136 | + echo Html::submitButton( | |
| 137 | + \Yii::t('core', 'Save'), | |
| 138 | + [ | |
| 139 | + 'class' => 'btn btn-success', | |
| 140 | + ] | |
| 141 | + ); | |
| 142 | + echo Html::submitButton( | |
| 143 | + \Yii::t('core', 'Save and generate'), | |
| 144 | + [ | |
| 145 | + 'class' => 'btn btn-primary', | |
| 146 | + 'name' => 'action', | |
| 147 | + 'value' => 'generate', | |
| 148 | + ] | |
| 149 | + ); | |
| 150 | + $form::end(); | |
| 151 | + $js = <<<JS | |
| 152 | + function sitemap_add(e) { | |
| 153 | + var url = $(e) | |
| 154 | + .data('url'); | |
| 155 | + var form_id = $(e) | |
| 156 | + .parents('form') | |
| 157 | + .attr('id'); | |
| 158 | + var form = $('#' + form_id); | |
| 159 | + var count = $('.field-group').length; | |
| 160 | + $.get( | |
| 161 | + url, { | |
| 162 | + 'formId': form_id, | |
| 163 | + 'count': count | |
| 164 | + }, function(data) { | |
| 165 | + form.find('.field-group') | |
| 166 | + .last() | |
| 167 | + .after(data); | |
| 168 | + } | |
| 169 | + ); | |
| 170 | + } | |
| 171 | + function sitemap_remove(e) { | |
| 172 | + if($('.field-group').length > 1) { | |
| 173 | + $(e) | |
| 174 | + .parents('.field-group') | |
| 175 | + .remove(); | |
| 176 | + } | |
| 177 | + } | |
| 178 | +JS; | |
| 179 | + $this->registerJs($js, $this::POS_END); | |
| 0 | 180 | \ No newline at end of file | ... | ... |
| 1 | +<?php | |
| 2 | + namespace common\components; | |
| 3 | + | |
| 4 | + use common\models\SitemapDynamic; | |
| 5 | + use common\models\SitemapStatic; | |
| 6 | + use yii\base\Object; | |
| 7 | + use yii\db\ActiveRecord; | |
| 8 | + use yii\helpers\Html; | |
| 9 | + use yii\helpers\Url; | |
| 10 | + | |
| 11 | + class Sitemap extends Object | |
| 12 | + { | |
| 13 | + public $entities = []; | |
| 14 | + public $path = '@frontend/web/sitemap.xml'; | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * Generate sitemap XML in $path | |
| 18 | + * | |
| 19 | + * @return bool | |
| 20 | + */ | |
| 21 | + public function generateXML(): bool | |
| 22 | + { | |
| 23 | + return $this->saveXML($this->generateOneShot()); | |
| 24 | + } | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Save generated xml to $path file | |
| 28 | + * | |
| 29 | + * @param string $xml | |
| 30 | + * | |
| 31 | + * @return bool | |
| 32 | + */ | |
| 33 | + protected function saveXML(string $xml): bool | |
| 34 | + { | |
| 35 | + $realpath = \Yii::getAlias($this->path); | |
| 36 | + if (file_put_contents($realpath, $xml)) { | |
| 37 | + return true; | |
| 38 | + } else { | |
| 39 | + return false; | |
| 40 | + } | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Generate xml from configs | |
| 45 | + * | |
| 46 | + * @return string | |
| 47 | + */ | |
| 48 | + public function generateOneShot(): string | |
| 49 | + { | |
| 50 | + $content = '<?xml version="1.0" encoding="UTF-8"?>'; | |
| 51 | + $content .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; | |
| 52 | + /** | |
| 53 | + * @var SitemapStatic[] $static | |
| 54 | + */ | |
| 55 | + // ***** Begin generating static pages | |
| 56 | + $static = SitemapStatic::find() | |
| 57 | + ->all(); | |
| 58 | + foreach ($static as $item) { | |
| 59 | + $content .= Html::tag( | |
| 60 | + 'url', | |
| 61 | + Html::tag('loc', $item->url) . Html::tag('lastmod', date('Y-m-d')) . Html::tag( | |
| 62 | + 'changefreq', | |
| 63 | + 'monthly' | |
| 64 | + ) . Html::tag('priority', $item->priority) | |
| 65 | + ); | |
| 66 | + } | |
| 67 | + // ***** <<< End | |
| 68 | + /** | |
| 69 | + * @var SitemapDynamic $dynamic | |
| 70 | + */ | |
| 71 | + $dynamic = SitemapDynamic::find() | |
| 72 | + ->indexBy('entity') | |
| 73 | + ->where([ 'status' => 1 ]) | |
| 74 | + ->all(); | |
| 75 | + $entities = $this->entities; | |
| 76 | + foreach ($entities as $entity) { | |
| 77 | + /** | |
| 78 | + * @var string $class | |
| 79 | + */ | |
| 80 | + $class = $entity[ 'class' ]; | |
| 81 | + /** | |
| 82 | + * @var ActiveRecord $classInstance | |
| 83 | + */ | |
| 84 | + $classInstance = new $class(); | |
| 85 | + if (is_subclass_of($classInstance, ActiveRecord::className())) { | |
| 86 | + if (!empty( $dynamic[ $class ] )) { | |
| 87 | + /** | |
| 88 | + * @var SitemapDynamic $model | |
| 89 | + */ | |
| 90 | + $model = $dynamic[ $class ]; | |
| 91 | + $query = $classInstance::find(); | |
| 92 | + if (isset( $entity[ 'conditions' ] )) { | |
| 93 | + foreach ($entity[ 'conditions' ] as $condition) { | |
| 94 | + $query->where($condition); | |
| 95 | + } | |
| 96 | + } | |
| 97 | + $result = $query->all(); | |
| 98 | + foreach ($result as $record) { | |
| 99 | + $content .= Html::tag( | |
| 100 | + 'url', | |
| 101 | + Html::tag( | |
| 102 | + 'loc', | |
| 103 | + Url::to( | |
| 104 | + [ | |
| 105 | + $entity[ 'url' ], | |
| 106 | + 'id' => $record->getAttribute('id'), | |
| 107 | + ], | |
| 108 | + true | |
| 109 | + ) | |
| 110 | + ) . Html::tag('lastmod', date('Y-m-d')) . Html::tag( | |
| 111 | + 'changefreq', | |
| 112 | + 'monthly' | |
| 113 | + ) . Html::tag('priority', $model->priority) | |
| 114 | + ); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + } | |
| 118 | + } | |
| 119 | + $content .= '</urlset>'; | |
| 120 | + return $content; | |
| 121 | + } | |
| 122 | + } | |
| 0 | 123 | \ No newline at end of file | ... | ... |
common/config/main.php
| ... | ... | @@ -4,10 +4,10 @@ |
| 4 | 4 | return [ |
| 5 | 5 | 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', |
| 6 | 6 | 'components' => [ |
| 7 | - 'cache' => [ | |
| 7 | + 'cache' => [ | |
| 8 | 8 | 'class' => 'yii\caching\FileCache', |
| 9 | 9 | ], |
| 10 | - 'i18n' => [ | |
| 10 | + 'i18n' => [ | |
| 11 | 11 | 'translations' => [ |
| 12 | 12 | 'core' => [ |
| 13 | 13 | 'class' => 'yii\i18n\PhpMessageSource', |
| ... | ... | @@ -15,11 +15,16 @@ |
| 15 | 15 | ], |
| 16 | 16 | ], |
| 17 | 17 | ], |
| 18 | - 'filedb' => [ | |
| 18 | + 'filedb' => [ | |
| 19 | 19 | 'class' => 'yii2tech\filedb\Connection', |
| 20 | 20 | 'path' => '@common/config', |
| 21 | 21 | ], |
| 22 | - 'seo' => [ | |
| 22 | + 'sitemapdb' => [ | |
| 23 | + 'class' => 'yii2tech\filedb\Connection', | |
| 24 | + 'path' => '@common/config', | |
| 25 | + 'primaryKeyName' => 'id', | |
| 26 | + ], | |
| 27 | + 'seo' => [ | |
| 23 | 28 | 'class' => SeoComponent::className(), |
| 24 | 29 | ], |
| 25 | 30 | ], | ... | ... |
| 1 | +<?php | |
| 2 | + namespace common\models; | |
| 3 | + | |
| 4 | + use Yii; | |
| 5 | + use yii\helpers\ArrayHelper; | |
| 6 | + use yii2tech\filedb\ActiveRecord; | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * Class SitemapDynamic | |
| 10 | + * | |
| 11 | + * @property int $id | |
| 12 | + * @property string $entity | |
| 13 | + * @property bool $status | |
| 14 | + * @property float $priority | |
| 15 | + */ | |
| 16 | + class SitemapDynamic extends ActiveRecord | |
| 17 | + { | |
| 18 | + | |
| 19 | + const STATUS_ENABLED = 1; | |
| 20 | + const STATUS_DISABLED = 0; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * @inheritdoc | |
| 24 | + */ | |
| 25 | + public static function getDb() | |
| 26 | + { | |
| 27 | + return \Yii::$app->get('sitemapdb'); | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @inheritdoc | |
| 32 | + */ | |
| 33 | + public function attributes() | |
| 34 | + { | |
| 35 | + return [ | |
| 36 | + 'id', | |
| 37 | + 'entity', | |
| 38 | + 'status', | |
| 39 | + 'priority', | |
| 40 | + ]; | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @inheritdoc | |
| 45 | + */ | |
| 46 | + public static function primaryKey() | |
| 47 | + { | |
| 48 | + return [ 'id' ]; | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * @inheritdoc | |
| 53 | + */ | |
| 54 | + public function rules() | |
| 55 | + { | |
| 56 | + return [ | |
| 57 | + [ | |
| 58 | + [ | |
| 59 | + 'entity', | |
| 60 | + 'status', | |
| 61 | + 'priority', | |
| 62 | + ], | |
| 63 | + 'required', | |
| 64 | + ], | |
| 65 | + [ | |
| 66 | + [ | |
| 67 | + 'status', | |
| 68 | + ], | |
| 69 | + 'boolean', | |
| 70 | + ], | |
| 71 | + [ | |
| 72 | + [ | |
| 73 | + 'entity', | |
| 74 | + ], | |
| 75 | + 'string', | |
| 76 | + ], | |
| 77 | + [ | |
| 78 | + [ | |
| 79 | + 'priority', | |
| 80 | + ], | |
| 81 | + 'double', | |
| 82 | + 'min' => 0, | |
| 83 | + 'max' => 1, | |
| 84 | + ], | |
| 85 | + ]; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * @inheritdoc | |
| 90 | + */ | |
| 91 | + public function attributeLabels() | |
| 92 | + { | |
| 93 | + return [ | |
| 94 | + 'id' => Yii::t('core', 'ID'), | |
| 95 | + 'entity' => Yii::t('core', 'Model'), | |
| 96 | + 'status' => Yii::t('core', 'Status'), | |
| 97 | + 'priority' => Yii::t('core', 'Priority'), | |
| 98 | + ]; | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Find maximum ID value from SitemapStatic models | |
| 103 | + */ | |
| 104 | + public static function max(): int | |
| 105 | + { | |
| 106 | + $models = self::find() | |
| 107 | + ->all(); | |
| 108 | + $array = ArrayHelper::getColumn($models, self::primaryKey()[ 0 ], false); | |
| 109 | + if (empty( $array )) { | |
| 110 | + return 0; | |
| 111 | + } else { | |
| 112 | + return max($array); | |
| 113 | + } | |
| 114 | + } | |
| 115 | + } | |
| 0 | 116 | \ No newline at end of file | ... | ... |
| 1 | +<?php | |
| 2 | + namespace common\models; | |
| 3 | + | |
| 4 | + use Yii; | |
| 5 | + use yii\helpers\ArrayHelper; | |
| 6 | + use yii2tech\filedb\ActiveRecord; | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * Class SitemapStatic | |
| 10 | + * | |
| 11 | + * @property int $id | |
| 12 | + * @property string $url | |
| 13 | + * @property float $priority | |
| 14 | + */ | |
| 15 | + class SitemapStatic extends ActiveRecord | |
| 16 | + { | |
| 17 | + /** | |
| 18 | + * @inheritdoc | |
| 19 | + */ | |
| 20 | + public static function getDb() | |
| 21 | + { | |
| 22 | + return \Yii::$app->get('sitemapdb'); | |
| 23 | + } | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * @inheritdoc | |
| 27 | + */ | |
| 28 | + public function attributes() | |
| 29 | + { | |
| 30 | + return [ | |
| 31 | + 'id', | |
| 32 | + 'url', | |
| 33 | + 'priority', | |
| 34 | + ]; | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @inheritdoc | |
| 39 | + */ | |
| 40 | + public static function primaryKey() | |
| 41 | + { | |
| 42 | + return [ 'id' ]; | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * @inheritdoc | |
| 47 | + */ | |
| 48 | + public function rules() | |
| 49 | + { | |
| 50 | + return [ | |
| 51 | + [ | |
| 52 | + [ | |
| 53 | + 'url', | |
| 54 | + 'priority', | |
| 55 | + ], | |
| 56 | + 'required', | |
| 57 | + ], | |
| 58 | + [ | |
| 59 | + [ | |
| 60 | + 'priority', | |
| 61 | + ], | |
| 62 | + 'double', | |
| 63 | + 'min' => 0, | |
| 64 | + 'max' => 1, | |
| 65 | + ], | |
| 66 | + [ | |
| 67 | + [ | |
| 68 | + 'url', | |
| 69 | + ], | |
| 70 | + 'string', | |
| 71 | + ], | |
| 72 | + ]; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * @inheritdoc | |
| 77 | + */ | |
| 78 | + public function attributeLabels() | |
| 79 | + { | |
| 80 | + return [ | |
| 81 | + 'id' => Yii::t('core', 'ID'), | |
| 82 | + 'url' => Yii::t('core', 'Url'), | |
| 83 | + 'priority' => Yii::t('core', 'Priority'), | |
| 84 | + ]; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Find maximum ID value from SitemapStatic models | |
| 89 | + */ | |
| 90 | + public static function max(): int | |
| 91 | + { | |
| 92 | + $models = self::find() | |
| 93 | + ->all(); | |
| 94 | + $array = ArrayHelper::getColumn($models, self::primaryKey()[ 0 ], false); | |
| 95 | + if (empty( $array )) { | |
| 96 | + return 0; | |
| 97 | + } else { | |
| 98 | + return max($array); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + } | |
| 0 | 102 | \ No newline at end of file | ... | ... |