/
SimpleComment.php
191 lines (160 loc) · 6.27 KB
/
SimpleComment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace itsurka\simpleComment;
use app\models\CommentAttachment;
use itsurka\simpleComment\models\Comment;
use itsurka\simpleComment\models\CommentForm;
use yii\base\Exception;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
\Yii::setAlias('@simplecomment', __DIR__);
require_once(\Yii::getAlias('@simplecomment') . '/models/Comment.php');
require_once(\Yii::getAlias('@simplecomment') . '/models/CommentForm.php');
require_once(\Yii::getAlias('@simplecomment') . '/models/CommentAttachment.php');
class SimpleComment extends \yii\base\Widget
{
/**
* AR model for which will be displayed the comments widget.
*
* @var ActiveRecord
*/
public $model;
/**
* AR model - comments author.
*
* @var ActiveRecord
*/
public $author;
/**
* AR models name attribute.
*
* @var string
*/
public $authorNameAttribute;
/**
* Avatar URL. Use this or $authorAvatarFunction param.
*
* @var string
*/
public $authorAvatarAttribute;
/**
* To get avatar URL. Use this or $authorAvatarAttribute param.
*
* @var string
*/
public $authorAvatarFunction;
/**
* Avatar pixels width.
*
* @var int
*/
public $authorAvatarWidth = 10;
/**
* Avatar pixels height.
*
* @var int
*/
public $authorAvatarHeight;
/**
* todo
*
* @var string
*/
public $template = "{form}\n{comments}";
public function init()
{
$this->validateInputParams();
}
public function run()
{
$commentForm = new CommentForm();
$commentForm = $this->handlePostComment($commentForm);
echo $this->_renderForm($commentForm);
echo $this->_renderComments();
}
private function _renderForm($commentForm)
{
return $this->renderFile(\Yii::getAlias('@simplecomment') . '/views/form.php', [
'model' => $this->model,
'commentForm' => $commentForm,
]);
}
private function _renderComments()
{
return $this->renderFile(\Yii::getAlias('@simplecomment') . '/views/comments.php', [
'query' => Comment::find()->where('relModelClass = :relModelClass AND relModelId = :relModelId', [
'relModelClass' => get_class($this->model),
'relModelId' => $this->model->id,
]),
'viewParams' => [
'author' => $this->author,
'authorNameAttribute' => $this->authorNameAttribute,
'authorAvatarAttribute' => $this->authorAvatarAttribute,
'authorAvatarFunction' => $this->authorAvatarFunction,
'authorAvatarWidth' => $this->authorAvatarWidth !== null ? $this->authorAvatarWidth : null,
'authorAvatarHeight' => $this->authorAvatarHeight !== null ? $this->authorAvatarHeight : null,
]
]);
}
/**
* Проверка входящих параметров
*
* @throws \yii\base\Exception
*/
private function validateInputParams()
{
if (is_null($this->model)) {
throw new Exception('Параметр model не задан');
}
if (!$this->model instanceof ActiveRecord) {
throw new Exception('Параметр model должен быть объектом класса ActiveRecord');
}
if (!is_null($this->author)) {
if (!$this->author instanceof ActiveRecord) {
throw new Exception('Параметр author должен быть объектом класса ActiveRecord');
}
if (!is_null($this->authorNameAttribute) && !$this->author->hasAttribute($this->authorNameAttribute)) {
throw new Exception('Параметр author не содержит свойство ' . $this->authorNameAttribute);
}
if (!is_null($this->authorAvatarAttribute) && !$this->author->hasAttribute($this->authorAvatarAttribute)) {
throw new Exception('Параметр author не содержит свойство ' . $this->authorAvatarAttribute);
}
}
}
private function handlePostComment(CommentForm $commentForm)
{
$request = \Yii::$app->request;
if ($request->getIsPost() && $request->post('cs-comment-form-submit') !== null) {
$commentForm->attributes = $request->post('CommentForm');
if ($commentForm->validate()) {
$comment = new Comment();
$comment->relModelClass = get_class($this->model);
$comment->relModelId = $this->model->id;
$comment->text = $commentForm->text;
$comment->insert();
$comment->refresh();
// обработка файла
if ($uploadedFile = UploadedFile::getInstance($commentForm, 'attachment')) {
$fileName = md5(uniqid() . $comment->id . rand(1, 1000)) . '.' . $uploadedFile->extension;
$folder = '/uploads/sc/' . date('Y') . '/' . date('m') . '/' . date('d');
if (!is_dir(\Yii::getAlias('@webroot') . $folder)) {
if (!@mkdir(\Yii::getAlias('@webroot') . $folder, 0777, true)) {
throw new Exception('Не удалось создать директорию');
}
}
if (!$uploadedFile->saveAs(\Yii::getAlias('@webroot') . $folder . '/' . $fileName)) {
throw new Exception('Не удалось скопировать файл');
}
$commentAttachment = new CommentAttachment();
$commentAttachment->commentId = $comment->id;
$commentAttachment->fileName = $fileName;
$commentAttachment->filePath = \Yii::getAlias('@webroot') . $folder . '/' . $fileName;
$commentAttachment->fileUrl = $folder . '/' . $fileName;
$commentAttachment->fileExtension = $uploadedFile->extension;
$commentAttachment->insert();
}
\Yii::$app->controller->refresh();
}
}
return $commentForm;
}
}