-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAttachmentBehavior.php
More file actions
382 lines (348 loc) · 13.2 KB
/
AttachmentBehavior.php
File metadata and controls
382 lines (348 loc) · 13.2 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* AttachmentBehavior
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
/**
* Class AttachmentBehavior
*
*/
class AttachmentBehavior extends ModelBehavior {
/**
* @var array モデル毎のAttachmentビヘイビア設定
*/
protected $_settings = array();
/**
* @var UploadFile $UploadFile UploadFile
*/
public $UploadFile = null;
/**
* @var UploadFilesContent 関連テーブルのモデル
*/
public $UploadFilesContent = null;
/**
* @var array アップロードされたファイル情報
*/
protected $_uploadedFiles = array();
/**
* SetUp Attachment behavior
*
* @param Model $model instance of model
* @param array $config array of configuration settings.
* @throws CakeException 先にOriginalKeyが登録されてないと例外
* @return void
*/
public function setup(Model $model, $config = array()) {
//ビヘイビアの優先順位
$this->settings['priority'] = 8;
// 先にOriginalKeyをロードしてもらう
if (! $model->Behaviors->loaded('NetCommons.OriginalKey')) {
$error = '"NetCommons.OriginalKeyBehavior" not loaded in ' . $model->alias . '. ';
$error .= 'Load "NetCommons.OriginalKeyBehavior" before loading "AttachmentBehavior"';
throw new CakeException($error);
}
$this->UploadFile = ClassRegistry::init('Files.UploadFile');
foreach ($config as $filed => $options) {
$this->uploadSettings($model, $filed, $options);
}
$this->UploadFilesContent = ClassRegistry::init('Files.UploadFilesContent');
$model->Behaviors->load('Files.UploadFileValidate');
$model->Behaviors->load('Files.UploadValidatorWrap');
}
/**
* After find callback. Can be used to modify any results returned by find.
*
* @param Model $model Model using this behavior
* @param mixed $results The results of the find operation
* @param bool $primary Whether this model is being queried directly (vs. being queried as an association)
* @return mixed An array value will replace the value of $results - any other value will be ignored.
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function afterFind(Model $model, $results, $primary = false) {
// Recursiveと連動 @see https://github.com/NetCommons3/NetCommons3/issues/68
if ($model->recursive < 0) {
return $results;
}
foreach ($results as $key => $content) {
if (isset($content[$model->alias]['id'])) {
$contentId = $content[$model->alias]['id'];
$conditions = [
'UploadFilesContent.plugin_key' => Inflector::underscore($model->plugin),
'UploadFilesContent.content_id' => $contentId,
];
$uploadFiles = $this->UploadFilesContent->find('all', ['conditions' => $conditions]);
foreach ($uploadFiles as $uploadFile) {
$fieldName = $uploadFile['UploadFile']['field_name'];
$results[$key]['UploadFile'][$fieldName] = $uploadFile['UploadFile'];
}
}
}
return $results;
}
/**
* before validate PHPのアップロードエラーがあったらvalidationErrorをセットする
*
* @param Model $model モデル
* @param array $options オプション
* @return void
*/
public function beforeValidate(Model $model, $options = array()) {
if (isset($this->_settings[$model->alias]['fileFields'])) {
foreach (array_keys($this->_settings[$model->alias]['fileFields']) as $fieldName) {
if (isset($model->data[$model->alias][$fieldName])) {
$fileData = $model->data[$model->alias][$fieldName];
// php upload errorだったらvalidationerrorにする
if ($fileData['error'] !== UPLOAD_ERR_OK &&
$fileData['error'] !== UPLOAD_ERR_NO_FILE) {
$model->validationErrors[$fieldName][] =
__d('files', 'Failed uploading file.');
}
}
}
}
}
/**
* beforeSave
* 元モデルのデータに返す値をセットする
*
* @param Model $model Model
* @param array $options Options
* @return mixed
*/
public function beforeSave(Model $model, $options = array()) {
foreach ($this->_settings[$model->alias]['fileFields'] as $fieldName => $filedOptions) {
if (isset($model->data[$model->alias][$fieldName])) {
$fileData = $model->data[$model->alias][$fieldName];
// $fileData['error'] があったら処理中止。バリデーションエラーにする。
if ($fileData['name']) {
// 元データにファイル名フィールドが定義されてたら埋める
$fileNameFieldName = Hash::get($filedOptions, 'fileNameFieldName');
if ($fileNameFieldName) {
$model->data[$model->alias][$fileNameFieldName] =
$fileData['name'];
}
// サイズフィールドをうめる
$sizeFieldName = Hash::get($filedOptions, 'sizeFieldName');
if ($sizeFieldName) {
$model->data[$model->alias][$sizeFieldName] =
$fileData['size'];
}
}
}
}
return parent::beforeSave($model);
}
/**
* afterSave
*
* @param Model $model モデル
* @param bool $created 新規作成
* @param array $options オプション
* @throws Exception
* @return void
*/
public function afterSave(Model $model, $created, $options = array()) {
foreach ($this->_settings[$model->alias]['fileFields'] as $fieldName => $filedOptions) {
if (isset($model->data[$model->alias][$fieldName])) {
$fileData = $model->data[$model->alias][$fieldName];
if ($fileData['name']) {
$uploadFile = $this->UploadFile->create();
$pathInfo = pathinfo($fileData['name']);
$uploadFile['UploadFile']['plugin_key'] = Inflector::underscore($model->plugin);
$keyField = Hash::get($filedOptions, 'contentKeyFieldName', 'key');
$uploadFile['UploadFile']['content_key'] = $model->data[$model->alias][$keyField];
$uploadFile['UploadFile']['field_name'] = $fieldName;
$uploadFile['UploadFile']['original_name'] = $fileData['name'];
$uploadFile['UploadFile']['extension'] = $pathInfo['extension'];
$uploadFile['UploadFile']['real_file_name'] = $fileData;
//MIMEタイプがimageではじまってなかったらサムネイルはつくらない
$filedOptions['thumbnails'] = $this->_isImageFile($fileData['tmp_name']);
// フィールド毎にオプションを設定しなおしてsave実行
$this->UploadFile->setOptions($filedOptions);
// ε( v ゚ω゚) < 例外処理
$this->_uploadedFiles[$fieldName] = $this->UploadFile->save($uploadFile);
}
}
}
// アップロードがなかったら以前のデータを挿入する
// formからhiddenで UploadFile.field_name.id 形式でデータが渡ってくる
// $data['UploadFile']にはモデルデータ編集時に添付されてるファイルについてのデータが入っている
$uploadFiles = Hash::get($model->data, 'UploadFile', array());
foreach ($uploadFiles as $uploadFile) {
// 同じfield_nameでアップロードされてるなら以前のファイルへの関連レコードを新規に追加する必要は無い(過去の関連レコードはそのまま)
if (isset($this->_uploadedFiles[$uploadFile['field_name']])) {
// 新たにアップロードされてる
// 履歴のないモデル(is_latest, is_activeカラムがない)だったら、以前のファイルを削除する
// 履歴のないモデルか?
if (!$model->hasField('is_latest')) {
// 履歴をもたないモデルなら以前のファイルを削除
$this->UploadFile->removeFile($model->id, $uploadFile['id']);
}
} else {
// 同じfield_nameでアップロードされてなければ以前のファイルへの関連レコードを入れる
$removePath = $model->alias . '.' . $uploadFile['field_name'] . '.remove';
if (Hash::get($model->data, $removePath, false)) {
// ファイル削除にチェックが入ってるのでリンクしない
// 今のコンテンツIDで関連テーブルのレコードがあったら、ユーザモデルのように履歴のないモデルなのでそのときは関連テーブルを消す必要があるのでremoveFileは呼んでおく。
$this->UploadFile->removeFile($model->id, $uploadFile['id']);
} else {
$uploadFileId = $uploadFile['id'];
$this->_saveUploadFilesContent($model, $uploadFileId);
}
}
}
// 関連テーブルの挿入
foreach ($this->_uploadedFiles as $uploadedFile) {
$uploadFileId = $uploadedFile['UploadFile']['id'];
$this->_saveUploadFilesContent($model, $uploadFileId);
}
$this->_uploadedFiles = array();
}
/**
* After delete
*
* @param Model $model 元モデル
* @return void
*/
public function afterDelete(Model $model) {
// afterDeleteだと$model->idで消したデータのIDがとれるだけ
$contentId = $model->id;
$this->UploadFile->deleteLink($model->plugin, $contentId);
}
/**
* アップロードフィールドの設定
*
* @param Model $model モデル
* @param string $field フィールド名
* @param array $options オプション
* @return void
*/
public function uploadSettings(Model $model, $field, $options = array()) {
if (is_int($field)) {
$field = $options;
$options = array();
}
$this->_settings[$model->alias]['fileFields'][$field] = $options;
//
$model->validate[$field]['size'] =
[
'rule' => ['validateRoomFileSizeLimit']
];
// 元モデルに拡張子バリデータをセットする
$uploadAllowExtension = $this->UploadFile->getAllowExtension();
$model->validate[$field]['extension'] = [
// システム設定の値をとってくる。trimすること
'rule' => ['isValidExtension', $uploadAllowExtension, false],
'message' => __d('files', 'It is upload disabled file format')
];
$model->validate[$field]['size'] = [
'rule' => 'validateRoomFileSizeLimit',
];
}
/**
* ファイルサイズバリデート ルームの合計ファイルサイズ制限に収まってるかチェックする。
*
* @param Model $model 元モデル
* @param array $check 検査対象
* @return bool|string OK true, エラー時はメッセージを返す
* @see UploadFile::validateSize()
*/
//public function validateUploadFileSize(Model $model, $check) {
// $result = $this->UploadFile->validateSize($check);
// return $result;
//}
//
//public function isValidRoomFileSizeLimit(Model $model, $size) {
// $check = [
// 'size' => $size
// ];
// $result = $this->UploadFile->validateSize($check);
// return $result;
//}
/**
* コンテンツに、物理ファイルを添付する処理
*
* @param Model $model 元モデル
* @param array $data コンテンツデータ
* @param string $fieldName 添付するフィールド名
* @param File|string $file 添付するファイルのFileインスタンスかファイルパス
* @param string $keyFieldName コンテンツキーのフィールド名 省略可能 デフォルト key
* @return void
*/
public function attachFile(Model $model, $data, $fieldName, $file, $keyFieldName = 'key') {
if (!is_a($file, 'File')) {
// $fileがpathのとき
$file = new File($file);
}
$pluginKey = Inflector::underscore($model->plugin);
$contentKey = $data[$model->alias][$keyFieldName];
$contentId = $data[$model->alias]['id'];
$this->UploadFile->attach($pluginKey, $contentKey, $contentId, $fieldName, $file);
}
/**
* ダウンロードカウントアップ
*
* @param Model $model 元モデル
* @param array $data UploadFile Model Data
* @param string $fieldName アップロードファイルフィールド名
* @return void
*/
public function downloadCountUp(Model $model, $data, $fieldName) {
$uploadFile = [
'UploadFile' => $data['UploadFile'][$fieldName]
];
$this->UploadFile->countUp($uploadFile);
}
/**
* Attachmentビヘイビアで添付されたファイルのパスを返す
*
* @param Model $model Model
* @param array $uploadFileData UploadFile Data Attachmentビヘイビアで取得される形式
* @param string $fieldName フィールド名
* @return string ファイルパス
*/
public function getRealFilePath(Model $model, $uploadFileData, $fieldName) {
$data = array();
$data['UploadFile'] = $uploadFileData['UploadFile'][$fieldName];
return $this->UploadFile->getRealFilePath($data);
}
/**
* コンテンツとアップロードファイルの関連テーブルを保存
*
* @param Model $model モデル
* @param int $uploadFileId アップロードファイルID
* @return array
*/
protected function _saveUploadFilesContent(Model $model, $uploadFileId) {
$contentId = $model->data[$model->alias]['id'];
$contentIsActive = Hash::get($model->data[$model->alias], 'is_active', null);
$contentIsLatest = Hash::get($model->data[$model->alias], 'is_latest', null);
$data = [
'content_id' => $contentId,
'content_is_active' => $contentIsActive,
'content_is_latest' => $contentIsLatest,
'upload_file_id' => $uploadFileId,
'plugin_key' => Inflector::underscore($model->plugin),
];
$data = $this->UploadFilesContent->create($data);
// ε( v ゚ω゚) < 例外処理
$this->UploadFilesContent->save($data);
return array($contentId, $data);
}
/**
* MimeTypeをみてimageか判定する
*
* @param string $file ファイルへのパス
* @return bool
*/
protected function _isImageFile($file) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $file);
finfo_close($finfo);
$result = (substr($mimeType, 0, 5) === 'image');
return $result;
}
}