|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Profile; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\ProfileField; |
|
6
|
|
|
use Apps\ActiveRecord\User; |
|
7
|
|
|
use Ffcms\Core\Arch\Model; |
|
8
|
|
|
use Ffcms\Core\Helper\Date; |
|
9
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
10
|
|
|
use Ffcms\Core\Interfaces\iUser; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class FormSettings. Business logic of user personal settings form |
|
14
|
|
|
* @package Apps\Model\Front\Profile |
|
15
|
|
|
*/ |
|
16
|
|
|
class FormSettings extends Model |
|
17
|
|
|
{ |
|
18
|
|
|
public $nick; |
|
19
|
|
|
public $sex; |
|
20
|
|
|
public $birthday; |
|
21
|
|
|
public $city; |
|
22
|
|
|
public $hobby; |
|
23
|
|
|
public $phone; |
|
24
|
|
|
public $url; |
|
25
|
|
|
|
|
26
|
|
|
public $custom_data = []; |
|
27
|
|
|
|
|
28
|
|
|
private $_user; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* FormSettings constructor. Pass user object inside |
|
32
|
|
|
* @param iUser|User $user |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(iUser $user) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->_user = $user; |
|
37
|
|
|
parent::__construct(true); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set default data |
|
42
|
|
|
*/ |
|
43
|
|
|
public function before() |
|
44
|
|
|
{ |
|
45
|
|
|
$profile = $this->_user->profile->toArray(); // object to array (property's is protected of access) |
|
|
|
|
|
|
46
|
|
|
foreach ($profile as $property => $value) { |
|
47
|
|
|
// if property exist - lets pass data to model |
|
48
|
|
|
if (property_exists($this, $property)) { |
|
49
|
|
|
if ($property === 'birthday') { |
|
50
|
|
|
if (Str::likeEmpty($value)) { |
|
51
|
|
|
$this->birthday = null; |
|
52
|
|
|
} else { |
|
53
|
|
|
$this->birthday = Date::convertToDatetime($value, Date::FORMAT_TO_DAY); |
|
54
|
|
|
} |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
$this->{$property} = $value; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Form display labels |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public function labels(): array |
|
67
|
|
|
{ |
|
68
|
|
|
$labels = [ |
|
69
|
|
|
'nick' => __('Nickname'), |
|
70
|
|
|
'sex' => __('Sex'), |
|
71
|
|
|
'birthday' => __('Birthday'), |
|
72
|
|
|
'city' => __('City'), |
|
73
|
|
|
'hobby' => __('Hobby'), |
|
74
|
|
|
'phone' => __('Phone'), |
|
75
|
|
|
'url' => __('Website') |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
// labels for custom fields |
|
79
|
|
|
foreach (ProfileField::all() as $custom) { |
|
80
|
|
|
$labels['custom_data.' . $custom->id] = $custom->getLocaled('name'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $labels; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Rules for validation |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function rules(): array |
|
91
|
|
|
{ |
|
92
|
|
|
$rules = [ |
|
93
|
|
|
['sex', 'required'], |
|
94
|
|
|
[['city', 'hobby', 'phone', 'url', 'nick', 'birthday'], 'used'], |
|
95
|
|
|
['nick', 'length_max', '50'], |
|
96
|
|
|
['city', 'length_max', '50'], |
|
97
|
|
|
['sex', 'in', [0, 1, 2]], |
|
98
|
|
|
['hobby', 'length_max', '50'], |
|
99
|
|
|
['phone', 'phone'], |
|
100
|
|
|
['url', 'url'] |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
|
|
// custom profile fields |
|
104
|
|
|
foreach (ProfileField::all() as $custom) { |
|
105
|
|
|
$rules[] = [ |
|
106
|
|
|
'custom_data.' . $custom->id, |
|
107
|
|
|
'used' |
|
108
|
|
|
]; |
|
109
|
|
|
$rules[] = [ |
|
110
|
|
|
'custom_data.' . $custom->id, |
|
111
|
|
|
(int)$custom->reg_cond === 1 ? 'direct_match' : 'reverse_match', |
|
112
|
|
|
$custom->reg_exp |
|
113
|
|
|
]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $rules; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Save data after validation |
|
121
|
|
|
*/ |
|
122
|
|
|
public function save() |
|
123
|
|
|
{ |
|
124
|
|
|
$profile = $this->_user->profile; |
|
|
|
|
|
|
125
|
|
|
$profile->nick = $this->nick; |
|
126
|
|
|
$profile->sex = $this->sex; |
|
127
|
|
|
$profile->birthday = (Str::likeEmpty($this->birthday) ? null : Date::convertToDatetime($this->birthday, Date::FORMAT_SQL_DATE)); |
|
128
|
|
|
$profile->city = $this->city; |
|
129
|
|
|
$profile->hobby = $this->hobby; |
|
130
|
|
|
$profile->phone = $this->phone; |
|
131
|
|
|
$profile->url = $this->url; |
|
132
|
|
|
$profile->custom_data = $this->custom_data; |
|
133
|
|
|
|
|
134
|
|
|
$profile->save(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|