EditForm::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace humhub\modules\privacybox\models\forms;
4
5
use Yii;
6
7
class EditForm extends \yii\base\Model
8
{
9
10
    public $active;
11
    public $title;
12
    public $content;
13
    public $reset;
14
    public $statement;
15
16
    /**
17
     * @inheritdocs
18
     */
19
    public function rules()
20
    {
21
        return array(
22
            array(['title', 'content', 'statement'], 'required'),
23
            array(['reset', 'active'], 'safe')
24
        );
25
    }
26
27
    /**
28
     * @inheritdocs
29
     */
30
    public function init()
31
    {
32
        $settings = Yii::$app->getModule('privacybox')->settings;
33
        $this->title = $settings->get('title', Yii::t('PrivacyboxModule.models_forms_EditForm', 'Privacy Policy'));
34
        $this->statement = $settings->get('statement', Yii::t('PrivacyboxModule.models_forms_EditForm', 'Please Read and Agree to our Privacy Policy'));
35
        $this->content = $settings->get('content');
36
        $this->active = $settings->get('active', false);
37
    }
38
39
    /**
40
     * Declares customized attribute labels.
41
     * If not declared here, an attribute would have a label that is
42
     * the same as its name with the first letter in upper case.
43
     */
44
    public function attributeLabels()
45
    {
46
        return array(
47
            'active' => Yii::t('PrivacyboxModule.models_forms_EditForm', 'Active'),
48
            'title' => Yii::t('PrivacyboxModule.models_forms_EditForm', 'Title'),
49
            'statement' => Yii::t('PrivacyboxModule.models_forms_EditForm', 'Statement'),
50
            'content' => Yii::t('PrivacyboxModule.models_forms_EditForm', 'Content'),
51
            'reset' => Yii::t('PrivacyboxModule.models_forms_EditForm', 'Mark as unseen for all users'),
52
        );
53
    }
54
55
    /**
56
     * Saves the given form settings.
57
     */
58
    public function save()
59
    {
60
        $settings = Yii::$app->getModule('privacybox')->settings;
61
        $settings->set('title', $this->title);
62
        $settings->set('statement', $this->statement);
63
        $settings->set('content', $this->content);
64
65
        if ($this->active) {
66
            $settings->set('active', true);
67
        } else {
68
            $settings->set('active', false);
69
        }
70
71
        // Note the reset flag only affects the timestamp if the conditionas are active
72
        $lastTimeStamp = $settings->get('timestamp');
73
        if ($this->active && ($this->reset || $lastTimeStamp == null)) {
74
            $settings->set('timestamp', time());
75
        }
76
77
        return true;
78
    }
79
80
}
81