Completed
Pull Request — master (#163)
by Corey
02:42
created

EditProfileForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace site\models;
3
4
use yii\base\Model;
5
use Yii;
6
use \DateTimeZone;
7
8
/**
9
 * edit profile form
10
 */
11
class EditProfileForm extends Model
12
{
13
  public $timezone;
14
  public $expose_graph;
15
  public $send_email;
16
  public $partner_email1;
17
  public $partner_email2;
18
  public $partner_email3;
19
20
  private $user;
21
22
  /**
23
   * Creates a form model
24
   *
25
   * @param  object                          $user
26
   * @param  array                           $config name-value pairs that will be used to initialize the object properties
27
   * @throws \yii\base\InvalidParamException if token is empty or not valid
28
   */
29
  public function __construct(\common\models\User $user, $config = []) {
30
    $this->user = $user;
31
    parent::__construct($config);
32
  }
33
34
  /**
35
   * @inheritdoc
36
   */
37
  public function rules()
38
  {
39
    return [
40
      ['timezone', 'string', 'min' => 2, 'max' => 255],
41
      ['timezone', 'in', 'range'=>DateTimeZone::listIdentifiers()],
42
43
      ['send_email', 'boolean'],
44
      [['partner_email1', 'partner_email2', 'partner_email3'], 'email'],
45
      [['partner_email1'], 'required',
46
        'when' => function($model) { return $model->send_email; },
47
        'message' => "If you've elected to send email reports, at least one partner email must be set.",
48
        "whenClient" => "function(attribute, value) {
49
          return $('#editprofileform-send_email').is(':checked');
50
        }"]
51
    ];
52
  }
53
54
  public function attributeLabels() {
55
    return [
56
      'partner_email1' => "Partner Email #1",
57
      'partner_email2' => "Partner Email #2",
58
      'partner_email3' => "Partner Email #3",
59
      'send_email'     => 'Send an email when I complete a check-in',
60
      'expose_graph'   => 'Share my scores graph via a link'
61
    ];
62
  }
63
64
  /**
65
   * saves user's profile info.
66
   *
67
   * @return User|null the saved model or null if saving fails
0 ignored issues
show
Bug introduced by
The type site\models\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
68
   */
69
  public function saveProfile()
70
  {
71
    if ($this->validate()) {
72
      $user  = $this->user;
73
74
      if($this->timezone)
75
        $user->timezone = $this->timezone;
76
      if($this->expose_graph) {
77
        $user->expose_graph = true;
78
      } else {
79
        $user->expose_graph = false;
80
      }
81
      if($this->send_email) {
82
        $user->partner_email1  = $this->partner_email1;
83
        $user->partner_email2  = $this->partner_email2;
84
        $user->partner_email3  = $this->partner_email3;
85
      } else {
86
        $user->partner_email1  = null;
87
        $user->partner_email2  = null;
88
        $user->partner_email3  = null;
89
      }
90
      $user->save();
91
      return $user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type common\models\User which is incompatible with the documented return type null|site\models\User.
Loading history...
92
    }
93
    return null;
94
  }
95
96
  public function loadUser() {
97
    $user                  = $this->user;
98
    $this->timezone        = $user->timezone;
99
    $this->partner_email1  = $user->partner_email1;
100
    $this->partner_email2  = $user->partner_email2;
101
    $this->partner_email3  = $user->partner_email3;
102
    $this->expose_graph    = $user->expose_graph;
103
    $this->send_email      = $user->send_email;
104
  }
105
}
106