TranslatemanagerController.php 4.05 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
3
4
5
6
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
7
8
9
10
 * @see https://www.open2.0.regione.lombardia.it Developers'community
 * @license GPLv3
 * @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
 *
waltermazza's avatar
waltermazza committed
11
 * @package    arter
Marcello Pivanti's avatar
Marcello Pivanti committed
12
13
14
15
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

waltermazza's avatar
waltermazza committed
16
namespace arter\amos\translation\controllers;
Marcello Pivanti's avatar
Marcello Pivanti committed
17

waltermazza's avatar
waltermazza committed
18
use arter\amos\core\helpers\BreadcrumbHelper;
Marcello Pivanti's avatar
Marcello Pivanti committed
19
20
21
22
23
use Yii;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\helpers\FileHelper;
use yii\helpers\Url;
waltermazza's avatar
waltermazza committed
24
use arter\amos\dashboard\controllers\base\DashboardController;
Marcello Pivanti's avatar
Marcello Pivanti committed
25
use yii\web\Controller;
waltermazza's avatar
waltermazza committed
26
use arter\amos\translation\AmosTranslation;
Marcello Pivanti's avatar
Marcello Pivanti committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

/**
 * Translation controller
 */
class TranslatemanagerController extends Controller {

    public $defaultAction = 'index';
    
    /**
     * @var string $layout Layout for the dashboard
     */
    public $layout = 'main';
    public $dirs = [];

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['clean-cache'],
                        'allow' => true,
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function init() {

        parent::init();
        $this->setAssetDirs();
        // custom initialization code goes here
    }

   public function actionCleanCache(){
        $this->cleanCacheDirs();
        return $this->redirect(BreadcrumbHelper::lastCrumbUrl());
   }

    /**
     * set asset dirs
     */
    public function setAssetDirs()
    {
        array_push($this->dirs, Yii::getAlias('@vendor/../frontend/runtime/cache'));
        array_push($this->dirs, Yii::getAlias('@vendor/../backend/runtime/cache'));
        array_push($this->dirs, Yii::getAlias('@vendor/../console/runtime/cache'));

        array_push($this->dirs, Yii::getAlias('@vendor/../frontend/runtime/translateCache'));
        array_push($this->dirs, Yii::getAlias('@vendor/../backend/runtime/translateCache'));
        array_push($this->dirs, Yii::getAlias('@vendor/../console/runtime/translateCache'));
    }

    /**
     * clean all asset dirs
     */
    public function cleanCacheDirs()
    {
        $nbr_cleaned = 0;

        foreach ($this->dirs as $asset_dir) {
            if (!is_dir($asset_dir)) {
                //echo 'Did not find ' . $asset_dir . '/ .. skipping';
                continue;
            }
            //echo '<p>Checking ' . $asset_dir . '/ to remove old caches .. </p>';

            $nbr_cleaned += self::cleanCacheDir($asset_dir);
        }
        \Yii::$app->getSession()->addFlash('success',
                    AmosTranslation::t('amostranslation', '#cache_deleted'));
        //echo '<p>Finished</p>';

        return $nbr_cleaned;
    }



    /**
     * clean asset dir
     * may remove subdirs in asset dir
     *
     * @param string $asset_dir
     * @return int
     */
    public function cleanCacheDir($asset_dir)
    {

        $now = time();
        $asset_temp_dirs = glob($asset_dir . '/*', GLOB_ONLYDIR);

        // check if less than want to keep
        if (!count($asset_temp_dirs)) {
            return 0;
        }

        // get all dirs and sort by modified
        $modified = [];
        foreach ($asset_temp_dirs as $asset_temp_dir) {
            $modified[$asset_temp_dir] = filemtime($asset_temp_dir);
        }

        asort($modified);
        $nbr_dirs = count($modified);

        // keep last dirs
        for ($i = min($nbr_dirs, 0); $i > 0; $i--) {
            array_pop($modified);
        }

        // remove dirs
        foreach ($modified as $dir => $mod) {
            //echo '<p>' . 'removed ' . $dir . '</p>';
            FileHelper::removeDirectory($dir);
        }

        return $nbr_dirs;
    }

}