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

EditProfileForm::saveProfile()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 25
rs 9.3222
c 0
b 0
f 0
cc 5
nc 9
nop 0
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 $send_email;
15
  public $partner_email1;
16
  public $partner_email2;
17
  public $partner_email3;
18
19
  private $user;
20
21
  /**
22
   * Creates a form model
23
   *
24
   * @param  object                          $user
25
   * @param  array                           $config name-value pairs that will be used to initialize the object properties
26
   * @throws \yii\base\InvalidParamException if token is empty or not valid
27
   */
28
  public function __construct(\common\models\User $user, $config = []) {
29
    $this->user = $user;
30
    parent::__construct($config);
31
  }
32
33
  /**
34
   * @inheritdoc
35
   */
36
  public function rules()
37
  {
38
    return [
39
      ['timezone', 'string', 'min' => 2, 'max' => 255],
40
      ['timezone', 'in', 'range'=>DateTimeZone::listIdentifiers()],
41
42
      ['send_email', 'boolean'],
43
      [['partner_email1', 'partner_email2', 'partner_email3'], 'email'],
44
      [['partner_email1'], 'required',
45
        'when' => function($model) { return $model->send_email; },
46
        'message' => "If you've elected to send email reports, at least one partner email must be set.",
47
        "whenClient" => "function(attribute, value) {
48
          return $('#editprofileform-send_email').is(':checked');
49
        }"]
50
    ];
51
  }
52
53
  /**
54
     * @codeCoverageIgnore
55
     */
56
  public function attributeLabels() {
57
    return [
58
      'partner_email1' => "Partner Email #1",
59
      'partner_email2' => "Partner Email #2",
60
      'partner_email3' => "Partner Email #3",
61
      'send_email'     => 'Send an email when I complete a check-in',
62
    ];
63
  }
64
65
  /**
66
   * saves user's profile info.
67
   *
68
   * @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...
69
   */
70
  public function saveProfile() {
71
    if ($this->validate()) {
72
      $user  = $this->user;
73
74
      if($this->timezone) {
75
        $user->timezone = $this->timezone;
76
      }
77
      if($this->send_email) {
78
        $user->send_email = true;
79
      } else {
80
        $user->send_email = false;
81
      }
82
      if($this->send_email) {
83
        $user->partner_email1  = $this->partner_email1;
84
        $user->partner_email2  = $this->partner_email2;
85
        $user->partner_email3  = $this->partner_email3;
86
      } else {
87
        $user->partner_email1  = null;
88
        $user->partner_email2  = null;
89
        $user->partner_email3  = null;
90
      }
91
      $user->save();
92
      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...
93
    }
94
    return null;
95
  }
96
97
  public function loadUser() {
98
    $user                  = $this->user;
99
    $this->timezone        = $user->timezone;
100
    $this->partner_email1  = $user->partner_email1;
101
    $this->partner_email2  = $user->partner_email2;
102
    $this->partner_email3  = $user->partner_email3;
103
    $this->send_email      = $user->send_email;
104
  }
105
}
106