|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ContentsFile\View\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Core\Configure; |
|
6
|
|
|
use Cake\View\Helper; |
|
7
|
|
|
|
|
8
|
|
|
class ContentsFileHelper extends Helper { |
|
9
|
|
|
|
|
10
|
|
|
public $helpers = ['Html', 'Url', 'Form']; |
|
11
|
|
|
private $defaultOption = [ |
|
12
|
|
|
'target' => '_blank', |
|
13
|
|
|
'escape' => false, |
|
14
|
|
|
'download' => false |
|
15
|
|
|
]; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* link |
|
19
|
|
|
* @author hagiwara |
|
20
|
|
|
* @param array $fileInfo |
|
21
|
|
|
* @param array $options |
|
22
|
|
|
* @param string $title |
|
23
|
|
|
*/ |
|
24
|
|
|
public function link($fileInfo, $options = [], $title = null) |
|
25
|
|
|
{ |
|
26
|
|
|
if (empty($fileInfo)) { |
|
27
|
|
|
return ''; |
|
28
|
|
|
} |
|
29
|
|
|
//一時パス用の設定 |
|
30
|
|
|
if ($title === null) { |
|
31
|
|
|
$title = $fileInfo['file_name']; |
|
32
|
|
|
} |
|
33
|
|
View Code Duplication |
if (isset($options['resize'])) { |
|
|
|
|
|
|
34
|
|
|
$fileInfo['resize'] = $options['resize']; |
|
35
|
|
|
unset($options['resize']); |
|
36
|
|
|
} |
|
37
|
|
|
$options = array_merge( |
|
38
|
|
|
$this->defaultOption, |
|
39
|
|
|
$options |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
return $this->Html->link( |
|
|
|
|
|
|
43
|
|
|
$title, |
|
44
|
|
|
$this->urlArray($fileInfo, $options), |
|
45
|
|
|
$options |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* image |
|
51
|
|
|
* @author hagiwara |
|
52
|
|
|
* @param array $fileInfo |
|
53
|
|
|
* @param array $options |
|
54
|
|
|
*/ |
|
55
|
|
|
public function image($fileInfo, $options = []) |
|
56
|
|
|
{ |
|
57
|
|
|
if (empty($fileInfo)) { |
|
58
|
|
|
return ''; |
|
59
|
|
|
} |
|
60
|
|
|
$options = array_merge( |
|
61
|
|
|
$this->defaultOption, |
|
62
|
|
|
$options |
|
63
|
|
|
); |
|
64
|
|
|
if (!empty($fileInfo)) { |
|
65
|
|
View Code Duplication |
if (isset($options['resize'])) { |
|
|
|
|
|
|
66
|
|
|
$fileInfo['resize'] = $options['resize']; |
|
67
|
|
|
unset($options['resize']); |
|
68
|
|
|
} |
|
69
|
|
|
return $this->Html->image($this->urlArray($fileInfo, $options), $options); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
return ''; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* url |
|
76
|
|
|
* @author hagiwara |
|
77
|
|
|
* @param array $fileInfo |
|
78
|
|
|
* @param boolean $full |
|
79
|
|
|
* @param array $options |
|
80
|
|
|
*/ |
|
81
|
|
|
public function url($fileInfo, $full = false, $options = []) |
|
82
|
|
|
{ |
|
83
|
|
|
if (empty($fileInfo)) { |
|
84
|
|
|
return []; |
|
85
|
|
|
} |
|
86
|
|
|
if (!isset($fileInfo['resize'])) { |
|
87
|
|
|
$fileInfo['resize'] = false; |
|
88
|
|
|
} |
|
89
|
|
|
$options = array_merge( |
|
90
|
|
|
$this->defaultOption, |
|
91
|
|
|
$options |
|
92
|
|
|
); |
|
93
|
|
|
return $this->Url->build($this->urlArray($fileInfo, $options), $full); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* contentsFileHidden |
|
98
|
|
|
* |
|
99
|
|
|
* バリデーションに引っかかった際にファイルをそのまま送る用 |
|
100
|
|
|
* |
|
101
|
|
|
* @author hagiwara |
|
102
|
|
|
* @param array|null $contentFileData |
|
103
|
|
|
* @param text $field |
|
104
|
|
|
*/ |
|
105
|
|
|
public function contentsFileHidden($contentFileData, $field) |
|
106
|
|
|
{ |
|
107
|
|
|
$hiddenInput = ''; |
|
108
|
|
|
if (!empty($contentFileData)) { |
|
109
|
|
|
foreach ($contentFileData as $fieldParts => $v) { |
|
110
|
|
|
$hiddenInput .= $this->Form->input($field . '.' . $fieldParts, ['type' => 'hidden']); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
return $hiddenInput; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* urlArray |
|
118
|
|
|
* @author hagiwara |
|
119
|
|
|
* @param array $fileInfo |
|
120
|
|
|
*/ |
|
121
|
|
|
private function urlArray($fileInfo, $options) |
|
122
|
|
|
{ |
|
123
|
|
|
if (!empty($fileInfo['tmp_file_name'])) { |
|
124
|
|
|
return [ |
|
125
|
|
|
'controller' => 'contents_file', |
|
126
|
|
|
'action' => 'loader', |
|
127
|
|
|
'plugin' => 'ContentsFile', |
|
128
|
|
|
'model' => $fileInfo['model'], |
|
129
|
|
|
'field_name' => $fileInfo['field_name'], |
|
130
|
|
|
'tmp_file_name' => $fileInfo['tmp_file_name'], |
|
131
|
|
|
// prefixは無視する |
|
132
|
|
|
'prefix' => false, |
|
133
|
|
|
'download' => $options['download'], |
|
134
|
|
|
]; |
|
135
|
|
|
} else { |
|
136
|
|
|
if (!isset($fileInfo['resize'])) { |
|
137
|
|
|
$fileInfo['resize'] = false; |
|
138
|
|
|
} |
|
139
|
|
|
// S3のホスティングの場合 |
|
140
|
|
|
// downloadとかはつけられないので無視 |
|
141
|
|
|
if ( |
|
142
|
|
|
array_key_exists('static_s3', $options) && |
|
143
|
|
|
$options['static_s3'] == true && |
|
144
|
|
|
Configure::read('ContentsFile.Setting.type') == 's3' && |
|
145
|
|
|
!is_null(Configure::read('ContentsFile.Setting.S3.static_domain')) |
|
146
|
|
|
) { |
|
147
|
|
|
return $this->makeStaticS3Url($fileInfo); |
|
148
|
|
|
} else { |
|
149
|
|
|
// loaderを通す場合 |
|
150
|
|
|
return [ |
|
151
|
|
|
'controller' => 'contents_file', |
|
152
|
|
|
'action' => 'loader', |
|
153
|
|
|
'plugin' => 'ContentsFile', |
|
154
|
|
|
'model' => $fileInfo['model'], |
|
155
|
|
|
'field_name' => $fileInfo['field_name'], |
|
156
|
|
|
'model_id' => $fileInfo['model_id'], |
|
157
|
|
|
'resize' => $fileInfo['resize'], |
|
158
|
|
|
// prefixは無視する |
|
159
|
|
|
'prefix' => false, |
|
160
|
|
|
'download' => $options['download'], |
|
161
|
|
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* makeStaticS3Url |
|
168
|
|
|
* 静的ホスティング用のURL作成 |
|
169
|
|
|
* @author hagiwara |
|
170
|
|
|
* @param array $fileInfo |
|
171
|
|
|
*/ |
|
172
|
|
|
private function makeStaticS3Url($fileInfo) |
|
173
|
|
|
{ |
|
174
|
|
|
$staticS3Url = Configure::read('ContentsFile.Setting.S3.static_domain') . '/' . Configure::read('ContentsFile.Setting.S3.fileDir') . $fileInfo['model'] . '/' . $fileInfo['model_id'] . '/'; |
|
175
|
|
|
if ($fileInfo['resize'] == false) { |
|
176
|
|
|
if (Configure::read('ContentsFile.Setting.randomFile') === true && $fileInfo['file_random_path'] != '') { |
|
177
|
|
|
$staticS3Url .= $fileInfo['file_random_path']; |
|
178
|
|
|
} else { |
|
179
|
|
|
$staticS3Url .= $fileInfo['field_name']; |
|
180
|
|
|
} |
|
181
|
|
|
} else { |
|
182
|
|
|
$resizeText = ''; |
|
183
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
184
|
|
|
empty($fileInfo['resize']['width']) |
|
185
|
|
|
) { |
|
186
|
|
|
$resizeText .= '0'; |
|
187
|
|
|
} else { |
|
188
|
|
|
$resizeText .= $fileInfo['resize']['width']; |
|
189
|
|
|
} |
|
190
|
|
|
$resizeText .= '_'; |
|
191
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
192
|
|
|
empty($fileInfo['resize']['height']) |
|
193
|
|
|
) { |
|
194
|
|
|
$resizeText .= '0'; |
|
195
|
|
|
} else { |
|
196
|
|
|
$resizeText .= $fileInfo['resize']['height']; |
|
197
|
|
|
} |
|
198
|
|
|
if (Configure::read('ContentsFile.Setting.randomFile') === true && $fileInfo['file_random_path'] != '') { |
|
199
|
|
|
$staticS3Url .= 'contents_file_resize_' . $fileInfo['file_random_path'] . '/' . $resizeText; |
|
200
|
|
|
} else { |
|
201
|
|
|
$staticS3Url .= 'contents_file_resize_' . $fileInfo['field_name'] . '/' . $resizeText; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
return $staticS3Url; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.