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
|
|
|
const DEFAULT_CATEGORY_THRESHOLD = 4; |
14
|
|
|
|
15
|
|
|
public $timezone; |
16
|
|
|
public $expose_graph; |
17
|
|
|
public $send_email; |
18
|
|
|
public $email_category; |
19
|
|
|
public $partner_email1; |
20
|
|
|
public $partner_email2; |
21
|
|
|
public $partner_email3; |
22
|
|
|
|
23
|
|
|
private $user; |
24
|
|
|
private $categories; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates a form model |
28
|
|
|
* |
29
|
|
|
* @param object $user |
30
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
31
|
|
|
* @throws \yii\base\InvalidParamException if token is empty or not valid |
32
|
|
|
*/ |
33
|
|
|
public function __construct(\common\models\User $user, $config = []) { |
34
|
|
|
$this->user = $user; |
35
|
|
|
$this->categories = \common\models\Category::getCategories(); |
36
|
|
|
parent::__construct($config); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function rules() { |
43
|
|
|
return [ |
44
|
|
|
['email_category', 'filter', 'filter' => 'intval', 'skipOnEmpty' => true], |
45
|
|
|
[['timezone', 'expose_graph', 'send_email'], 'required'], |
46
|
|
|
['timezone', 'string', 'min' => 2, 'max' => 255], |
47
|
|
|
['timezone', 'in', 'range'=>DateTimeZone::listIdentifiers()], |
48
|
|
|
['expose_graph', 'boolean'], |
49
|
|
|
['send_email', 'boolean'], |
50
|
|
|
['email_category', 'integer'], |
51
|
|
|
['email_category', 'in', 'range'=>array_keys($this->categories)], |
52
|
|
|
[['partner_email1', 'partner_email2', 'partner_email3'], 'email'], |
53
|
|
|
[['partner_email1', 'email_category'], 'required', |
54
|
|
|
'when' => function($model) { return $model->send_email; }, |
55
|
|
|
'message' => "If you've elected to send email reports, at least one partner email must be set.", |
56
|
|
|
"whenClient" => "function(attribute, value) { |
57
|
|
|
return $('#editprofileform-send_email').is(':checked'); |
58
|
|
|
}"] |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @codeCoverageIgnore |
64
|
|
|
*/ |
65
|
|
|
public function attributeLabels() { |
66
|
|
|
return [ |
67
|
|
|
'partner_email1' => "Partner Email #1", |
68
|
|
|
'partner_email2' => "Partner Email #2", |
69
|
|
|
'partner_email3' => "Partner Email #3", |
70
|
|
|
'send_email' => 'Send an email when I select behaviors at or above a specific FASTER category', |
71
|
|
|
'email_category' => 'FASTER Category', |
72
|
|
|
'expose_graph' => 'Share my behaviors graph via a link' |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* saves user's profile info. |
78
|
|
|
* |
79
|
|
|
* @return User|null the saved model or null if saving fails |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function saveProfile() { |
82
|
|
|
if ($this->validate()) { |
83
|
|
|
$user = $this->user; |
84
|
|
|
|
85
|
|
|
$graph = Yii::$container |
86
|
|
|
->get(\common\components\Graph::class, [$this->user]); |
87
|
|
|
|
88
|
|
|
if($this->timezone) { |
89
|
|
|
$user->timezone = $this->timezone; |
90
|
|
|
} |
91
|
|
|
if($this->expose_graph) { |
92
|
|
|
$user->expose_graph = true; |
93
|
|
|
|
94
|
|
|
// generate behaviors graph image |
95
|
|
|
$checkins_last_month = (Yii::$container->get(\common\interfaces\UserBehaviorInterface::class)) |
96
|
|
|
->getCheckInBreakdown(); |
97
|
|
|
|
98
|
|
|
// if they haven't done a check-in in the last month this will explode |
99
|
|
|
// because $checkins_last_month is an empty array |
100
|
|
|
if($checkins_last_month) { |
101
|
|
|
$graph->create($checkins_last_month, true); |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
$user->expose_graph = false; |
105
|
|
|
// remove behaviors graph image |
106
|
|
|
$graph->destroy(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if($this->send_email) { |
110
|
|
|
$user->send_email = true; |
111
|
|
|
$user->email_category = $this->email_category; |
112
|
|
|
$user->partner_email1 = $this->partner_email1; |
113
|
|
|
$user->partner_email2 = $this->partner_email2; |
114
|
|
|
$user->partner_email3 = $this->partner_email3; |
115
|
|
|
} else { |
116
|
|
|
$user->send_email = false; |
117
|
|
|
$user->email_category = 4; // default to "Speeding Up" |
118
|
|
|
$user->partner_email1 = null; |
119
|
|
|
$user->partner_email2 = null; |
120
|
|
|
$user->partner_email3 = null; |
121
|
|
|
} |
122
|
|
|
$user->save(); |
123
|
|
|
return $user; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function loadUser() { |
130
|
|
|
$user = $this->user; |
131
|
|
|
$this->timezone = $user->timezone; |
132
|
|
|
$this->partner_email1 = $user->partner_email1; |
133
|
|
|
$this->partner_email2 = $user->partner_email2; |
134
|
|
|
$this->partner_email3 = $user->partner_email3; |
135
|
|
|
$this->expose_graph = $user->expose_graph; |
136
|
|
|
$this->send_email = $user->send_email; |
137
|
|
|
$this->email_category = $user->email_category; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|