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\Type\Str; |
9
|
|
|
|
10
|
|
|
class FormFeedbackAdd extends Model |
11
|
|
|
{ |
12
|
|
|
public $name; |
13
|
|
|
public $email; |
14
|
|
|
public $captcha; |
15
|
|
|
public $message; |
16
|
|
|
|
17
|
|
|
private $_hash; |
18
|
|
|
|
19
|
|
|
private $_useCaptcha = true; |
20
|
|
|
|
21
|
|
|
public function __construct($captcha = true) |
22
|
|
|
{ |
23
|
|
|
$this->_useCaptcha = (bool)$captcha; |
24
|
|
|
parent::__construct(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set user data if he is logged in |
30
|
|
|
*/ |
31
|
|
|
public function before() |
32
|
|
|
{ |
33
|
|
|
if (App::$User->isAuth()) { |
34
|
|
|
$data = App::$User->identity(); |
35
|
|
|
$this->name = $data->getProfile()->nick; |
36
|
|
|
$this->email = $data->email; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Labels to display form |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function labels() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
'name' => __('Name'), |
47
|
|
|
'email' => __('Email'), |
48
|
|
|
'message' => __('Message'), |
49
|
|
|
'captcha' => __('Captcha') |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Rules to validate send form |
55
|
|
|
*/ |
56
|
|
|
public function rules() |
57
|
|
|
{ |
58
|
|
|
$rules = [ |
59
|
|
|
[['name', 'email', 'message'], 'required'], |
60
|
|
|
['name', 'length_min', '2'], |
61
|
|
|
['message', 'length_min', 10], |
62
|
|
|
['email', 'email'], |
63
|
|
|
['captcha', 'used'] |
64
|
|
|
]; |
65
|
|
|
if (true === $this->_useCaptcha) { |
66
|
|
|
$rules[] = ['captcha', 'App::$Captcha::validate']; |
67
|
|
|
} |
68
|
|
|
return $rules; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function make() |
72
|
|
|
{ |
73
|
|
|
// calculate security hash to direct-on access |
74
|
|
|
$hash = Str::randomLatinNumeric(mt_rand(16, 64)); |
75
|
|
|
|
76
|
|
|
// init new row and set row data |
77
|
|
|
$record = new FeedbackPost(); |
78
|
|
|
$record->name = App::$Security->strip_tags($this->name); |
79
|
|
|
$record->email = App::$Security->strip_tags($this->email); |
80
|
|
|
$record->message = App::$Security->strip_tags($this->message); |
81
|
|
|
$record->hash = $hash; |
82
|
|
|
if (App::$User->isAuth()) { |
83
|
|
|
$record->user_id = App::$User->identity()->getId(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$record->ip = App::$Request->getClientIp(); |
87
|
|
|
// save row to db |
88
|
|
|
$record->save(); |
89
|
|
|
|
90
|
|
|
// send notification to email |
91
|
|
|
$this->sendEmail($record); |
92
|
|
|
|
93
|
|
|
return $record; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Send email notification after feedback request creation |
98
|
|
|
* @param $record |
99
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
100
|
|
|
*/ |
101
|
|
|
private function sendEmail($record) |
102
|
|
|
{ |
103
|
|
|
// prepare email template |
104
|
|
|
$template = App::$View->render('feedback/mail/created', [ |
105
|
|
|
'record' => $record |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
// get website default email |
109
|
|
|
$sender = App::$Properties->get('adminEmail'); |
110
|
|
|
|
111
|
|
|
// build swift mailer handler |
112
|
|
|
$mailMessage = \Swift_Message::newInstance(App::$Translate->get('Feedback', 'Request #%id% is created', ['id' => $record->id])) |
113
|
|
|
->setFrom([$sender]) |
114
|
|
|
->setTo([$record->email]) |
115
|
|
|
->setBody($template, 'text/html'); |
116
|
|
|
// send message over swift instance |
117
|
|
|
App::$Mailer->send($mailMessage); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string|null |
122
|
|
|
*/ |
123
|
|
|
public function getHash() |
124
|
|
|
{ |
125
|
|
|
return $this->_hash; |
126
|
|
|
} |
127
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: