Completed
Push — master ( 12fb0c...f8a228 )
by Klochok
04:03
created

SettingsController::setGlobalOrientation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 15
cp 0
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 20
1
<?php
2
/**
3
 * Theme Manager for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-thememanager
6
 * @package   yii2-thememanager
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\thememanager\controllers;
12
13
use hipanel\models\IndexPageUiOptions;
14
use hiqdev\thememanager\models\Settings;
15
use hiqdev\thememanager\Module;
16
use hiqdev\thememanager\storage\SettingsStorageInterface;
17
use Yii;
18
use yii\web\Response;
19
20
class SettingsController extends \yii\web\Controller
21
{
22
    /**
23
     * @var Module
24
     */
25
    public $module;
26
27
    /**
28
     * @return Settings
29
     */
30
    public function getModel()
31
    {
32
        return $this->module->getManager()->getSettings();
33
    }
34
35
    /**
36
     * @return SettingsStorageInterface
37
     */
38
    public function getThemeSettingsStorage()
39
    {
40
        return Yii::$app->get('themeSettingsStorage');
41
    }
42
43
    public function getUiOptionsStorage()
44
    {
45
        return Yii::$app->get('uiOptionsStorage');
46
    }
47
48
    /**
49
     * Settings form.
50
     *
51
     * @return string|Response
52
     */
53
    public function actionIndex()
54
    {
55
        $model = $this->getModel();
56
57
        $data = Yii::$app->request->post($model->formName());
58
        if (empty($data)) {
59
            $data = $this->getThemeSettingsStorage()->get();
60
        }
61
62
        if (Yii::$app->request->getIsPost() && $model->load($data) && $model->validate()) {
63
            $this->getThemeSettingsStorage()->set($model);
64
            $this->setGlobalOrientation($model->filterOrientation);
0 ignored issues
show
Documentation introduced by
The property filterOrientation does not exist on object<hiqdev\thememanager\models\Settings>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
            Yii::$app->session->setFlash('success', Yii::t('hiqdev:thememanager', 'Layout settings saved.'));
66
        }
67
68
        if (Yii::$app->request->isAjax) {
69
            return $this->renderAjax('//settings/_form', compact('model'));
70
        }
71
72
        return $this->render('index', compact('model'));
73
    }
74
75
    /**
76
     * @param null $orientation
77
     */
78
    public function setGlobalOrientation($orientation = null)
79
    {
80
        if ($orientation !== null) {
81
            $settings = $this->getUiOptionsStorage()->getAllUiOptions();
82
            foreach ($settings as $route => $data) {
83
                $model = new IndexPageUiOptions($data);
84
                $model->orientation = $orientation;
85
                if ($model->validate()) {
86
                    $this->getUiOptionsStorage()->set($route, $model->toArray());
87
                } else {
88
                    Yii::warning('SettingsController - IndexPageUiModel validation errors: ' . json_encode($model->getErrors()));
89
                }
90
            }
91
        }
92
    }
93
94
    /**
95
     * @param $theme
96
     * @return Response
97
     */
98
    public function actionChangeTheme($theme)
99
    {
100
        if ($this->module->getManager()->has($theme)) {
101
            $model = $this->getModel();
102
            $model->theme = $theme;
103
            $this->getThemeSettingsStorage()->set($model);
104
            Yii::$app->session->setFlash('success', Yii::t('hiqdev:thememanager', 'Theme changed'));
105
        }
106
107
        return $this->redirect(['index']);
108
    }
109
110
    public function actionCollapsedSidebar()
111
    {
112
        if (Yii::$app->request->isAjax) {
113
            $model = $this->getModel();
114
            $model->collapsed_sidebar = Yii::$app->request->post('collapsed_sidebar');
0 ignored issues
show
Documentation introduced by
The property collapsed_sidebar does not exist on object<hiqdev\thememanager\models\Settings>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
            $this->getThemeSettingsStorage()->set($model);
116
        }
117
    }
118
}
119