Issues (1256)

controllers/admin/SitemapController.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace app\controllers\admin;
4
5
use Yii;
6
use Itstructure\Sitemap\SitemapDataHandler;
7
use Itstructure\AdminModule\controllers\AdminController;
8
use app\traits\AdminBeforeActionTrait;
9
10
/**
11
 * Class SitemapController
12
 *
13
 * @package app\controllers\admin
14
 */
15
class SitemapController extends AdminController
16
{
17
    use AdminBeforeActionTrait;
0 ignored issues
show
The trait app\traits\AdminBeforeActionTrait requires the property $controller which is not provided by app\controllers\admin\SitemapController.
Loading history...
18
19
    /** @var string Alias to directory contains sitemap-models */
20
    public $modelsPath = '@app/models/sitemap';
21
22
    /** @var string Namespace of sitemap-models files */
23
    public $modelsNamespace = 'app\models\sitemap';
24
25
    /** @var string Path to saving sitemap-files. As webroot: "http://example.com" */
26
    public $savePathAlias = '@app/web';
27
28
    /** @var string Name of sitemap-file, saved to webroot: "http://example.com/sitemap.xml" */
29
    public $sitemapFileName = 'sitemap.xml';
30
31
    /** @var array Default config for sitemap builder */
32
    public $builderConfig = [
33
        'urlsPerFile' => 100,
34
    ];
35
36
    /** @var string Base Url for sitemap links. As webroot: "http://example.com" */
37
    public $baseUrl;
38
39
    /**
40
     * Render sitemap.
41
     *
42
     * @return string|\yii\web\Response
43
     */
44
    public function actionIndex()
45
    {
46
        if (Yii::$app->request->isPost) {
47
48
            $this->export();
49
50
            return $this->redirect([
51
                '/admin/sitemap'
52
            ]);
53
        }
54
55
        return $this->render('index', [
56
            'sitemapContent' => $this->readSitemapFile()
57
        ]);
58
    }
59
60
    /**
61
     * Export sitemap in to "savePathAlias" path.
62
     *
63
     * @return void
64
     */
65
    private function export()
66
    {
67
        $dataHandler = new SitemapDataHandler($this->savePathAlias, $this->sitemapFileName, $this->baseUrl, [
68
            'builderConfig' => $this->builderConfig,
69
        ]);
70
71
        $models = $dataHandler->getModelsClasses($this->modelsPath, $this->modelsNamespace);
72
        $dataHandler->handleModels($models);
73
    }
74
75
    /**
76
     * Read sitemap file.
77
     *
78
     * @return null|string
79
     */
80
    private function readSitemapFile()
81
    {
82
        $filePath = Yii::getAlias($this->savePathAlias) . '/' . $this->sitemapFileName;
83
84
        if (!file_exists($filePath)) {
85
            return null;
86
        }
87
88
        $fileContent = file_get_contents($filePath);
89
90
        return htmlspecialchars($fileContent, ENT_QUOTES);
91
    }
92
}
93