|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* HiPanel core package |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://hipanel.com/ |
|
7
|
|
|
* @package hipanel-core |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Created by PhpStorm. |
|
14
|
|
|
* User: tofid |
|
15
|
|
|
* Date: 11.02.15 |
|
16
|
|
|
* Time: 17:59. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace hipanel\behaviors; |
|
20
|
|
|
|
|
21
|
|
|
use hipanel\models\File as FileModel; |
|
22
|
|
|
use yii\base\Behavior; |
|
23
|
|
|
use yii\db\BaseActiveRecord; |
|
24
|
|
|
use yii\helpers\FileHelper; |
|
25
|
|
|
use yii\web\UploadedFile; |
|
26
|
|
|
|
|
27
|
|
|
class File extends Behavior |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string the attribute which holds the attachment. |
|
31
|
|
|
*/ |
|
32
|
|
|
public $attribute = 'file'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array the scenarios in which the behavior will be triggered |
|
36
|
|
|
*/ |
|
37
|
|
|
public $scenarios = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string the attribute that will receive the file id |
|
41
|
|
|
*/ |
|
42
|
|
|
public $savedAttribute; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var UploadedFile the uploaded file instance. |
|
46
|
|
|
*/ |
|
47
|
|
|
private $_file; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function events() |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
BaseActiveRecord::EVENT_BEFORE_INSERT => 'saveUploadedFile', |
|
56
|
|
|
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'saveUploadedFile', |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Event handler for beforeSave. |
|
62
|
|
|
* @param \yii\base\ModelEven|boolean $event |
|
63
|
|
|
*/ |
|
64
|
|
|
public function saveUploadedFile($event) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$model = $this->owner; |
|
67
|
|
|
$arr_ids = []; |
|
|
|
|
|
|
68
|
|
|
if (in_array($model->scenario, $this->scenarios, true)) { |
|
69
|
|
|
$this->_file = UploadedFile::getInstances($model, $this->attribute); |
|
|
|
|
|
|
70
|
|
|
if (is_array($this->_file) && !empty($this->_file)) { |
|
71
|
|
|
$this->owner->{$this->savedAttribute} = implode(',', FileModel::fileSave($this->_file)); |
|
72
|
|
|
// foreach ($this->_file as $file) { |
|
|
|
|
|
|
73
|
|
|
// if ($file instanceof UploadedFile) { |
|
74
|
|
|
// // Move to temporary destination |
|
75
|
|
|
// $tempDestination = FileModel::getTempFolder() . DIRECTORY_SEPARATOR . uniqid() . '.' . $file->extension; |
|
76
|
|
|
// FileHelper::createDirectory(dirname($tempDestination)); |
|
77
|
|
|
// $file->saveAs($tempDestination); |
|
78
|
|
|
// // Prepare to final destination |
|
79
|
|
|
// $url = FileModel::getTmpUrl(basename($tempDestination)); |
|
80
|
|
|
// $response = FileModel::perform('Put', [ |
|
81
|
|
|
// 'url' => $url, |
|
82
|
|
|
// 'filename' => basename($tempDestination) |
|
83
|
|
|
// ]); |
|
84
|
|
|
// |
|
85
|
|
|
// $file_id = $arr_ids[] = $response['id']; |
|
86
|
|
|
// $finalDestination = $this->getPath($file_id) . DIRECTORY_SEPARATOR . $file_id; |
|
87
|
|
|
// FileHelper::createDirectory(dirname($finalDestination)); |
|
88
|
|
|
// if (!rename($tempDestination, $finalDestination)) |
|
89
|
|
|
// throw new \LogicException('rename function is not work'); |
|
90
|
|
|
// if (is_file($tempDestination)) |
|
91
|
|
|
// unlink($tempDestination); |
|
92
|
|
|
// } |
|
93
|
|
|
// } |
|
94
|
|
|
// $this->owner->{$this->savedAttribute} = implode(',', $arr_ids); |
|
95
|
|
|
} else { |
|
96
|
|
|
// Protect attribute |
|
97
|
|
|
unset($model->{$this->attribute}); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.