1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Feedback; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\FeedbackPost; |
6
|
|
|
use Ffcms\Core\App; |
7
|
|
|
use Ffcms\Core\Arch\Model; |
8
|
|
|
use Ffcms\Core\Helper\Crypt; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class FormFeedbackAdd. Add new feedback request business logic model |
12
|
|
|
* @package Apps\Model\Front\Feedback |
13
|
|
|
*/ |
14
|
|
|
class FormFeedbackAdd extends Model |
15
|
|
|
{ |
16
|
|
|
public $name; |
17
|
|
|
public $email; |
18
|
|
|
public $captcha; |
19
|
|
|
public $message; |
20
|
|
|
|
21
|
|
|
private $_hash; |
22
|
|
|
|
23
|
|
|
private $_useCaptcha = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* FormFeedbackAdd constructor. Pass captcha marker enabled inside |
27
|
|
|
* @param bool $captcha |
28
|
|
|
*/ |
29
|
|
|
public function __construct($captcha = true) |
30
|
|
|
{ |
31
|
|
|
$this->_useCaptcha = (bool)$captcha; |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set user data if he is logged in |
38
|
|
|
*/ |
39
|
|
|
public function before() |
40
|
|
|
{ |
41
|
|
|
if (App::$User->isAuth()) { |
42
|
|
|
$data = App::$User->identity(); |
43
|
|
|
$this->name = $data->profile->nick; |
44
|
|
|
$this->email = $data->email; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Labels to display form |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function labels(): array |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
'name' => __('Name'), |
56
|
|
|
'email' => __('Email'), |
57
|
|
|
'message' => __('Message'), |
58
|
|
|
'captcha' => __('Captcha') |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Rules to validate send form |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function rules(): array |
67
|
|
|
{ |
68
|
|
|
$rules = [ |
69
|
|
|
[['name', 'email', 'message'], 'required'], |
70
|
|
|
['name', 'length_min', '2'], |
71
|
|
|
['message', 'length_min', 10], |
72
|
|
|
['email', 'email'], |
73
|
|
|
['captcha', 'used'] |
74
|
|
|
]; |
75
|
|
|
if (true === $this->_useCaptcha) { |
76
|
|
|
$rules[] = ['captcha', 'App::$Captcha::validate']; |
77
|
|
|
} |
78
|
|
|
return $rules; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Process submit new request |
83
|
|
|
* @return FeedbackPost |
84
|
|
|
*/ |
85
|
|
|
public function make() |
86
|
|
|
{ |
87
|
|
|
// calculate security hash to direct-on access |
88
|
|
|
$hash = Crypt::randomString(mt_rand(16, 64)); |
89
|
|
|
|
90
|
|
|
// init new row and set row data |
91
|
|
|
$record = new FeedbackPost(); |
92
|
|
|
$record->name = $this->name; |
93
|
|
|
$record->email = $this->email; |
94
|
|
|
$record->message = $this->message; |
95
|
|
|
$record->hash = $hash; |
96
|
|
|
if (App::$User->isAuth()) { |
97
|
|
|
$record->user_id = App::$User->identity()->getId(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$record->ip = App::$Request->getClientIp(); |
101
|
|
|
// save row to db |
102
|
|
|
$record->save(); |
103
|
|
|
|
104
|
|
|
if (App::$Mailer) { |
105
|
|
|
// send notification to email |
106
|
|
|
App::$Mailer->tpl('feedback/_mail/created', [ |
107
|
|
|
'record' => $record |
108
|
|
|
])->send($record->email, App::$Translate->get('Feedback', 'Request #%id% is created', ['id' => $record->id])); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $record; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string|null |
116
|
|
|
*/ |
117
|
|
|
public function getHash() |
118
|
|
|
{ |
119
|
|
|
return $this->_hash; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|