|
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
|
|
|
]; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* link |
|
18
|
|
|
* @author hagiwara |
|
19
|
|
|
* @param array $fileInfo |
|
20
|
|
|
* @param array $options |
|
21
|
|
|
* @param string $title |
|
22
|
|
|
*/ |
|
23
|
|
|
public function link($fileInfo, $options = [], $title = null) |
|
24
|
|
|
{ |
|
25
|
|
|
if (empty($fileInfo)) { |
|
26
|
|
|
return ''; |
|
27
|
|
|
} |
|
28
|
|
|
//一時パス用の設定 |
|
29
|
|
|
if ($title === null) { |
|
30
|
|
|
$title = $fileInfo['file_name']; |
|
31
|
|
|
} |
|
32
|
|
|
if (isset($options['resize'])) { |
|
33
|
|
|
$fileInfo['resize'] = $options['resize']; |
|
34
|
|
|
unset($options['resize']); |
|
35
|
|
|
} |
|
36
|
|
|
$options = array_merge( |
|
37
|
|
|
$this->defaultOption, |
|
38
|
|
|
$options |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
return $this->Html->link( |
|
|
|
|
|
|
42
|
|
|
$title, |
|
43
|
|
|
$this->urlArray($fileInfo, $options), |
|
44
|
|
|
$options |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* image |
|
50
|
|
|
* @author hagiwara |
|
51
|
|
|
* @param array $fileInfo |
|
52
|
|
|
* @param array $options |
|
53
|
|
|
*/ |
|
54
|
|
|
public function image($fileInfo, $options = []) |
|
55
|
|
|
{ |
|
56
|
|
|
if (empty($fileInfo)) { |
|
57
|
|
|
return ''; |
|
58
|
|
|
} |
|
59
|
|
|
if (!empty($fileInfo)) { |
|
60
|
|
|
if (isset($options['resize'])) { |
|
61
|
|
|
$fileInfo['resize'] = $options['resize']; |
|
62
|
|
|
unset($options['resize']); |
|
63
|
|
|
} |
|
64
|
|
|
return $this->Html->image($this->urlArray($fileInfo, $options), $options); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
return ''; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* url |
|
71
|
|
|
* @author hagiwara |
|
72
|
|
|
* @param array $fileInfo |
|
73
|
|
|
* @param boolean $full |
|
74
|
|
|
* @param array $options |
|
75
|
|
|
*/ |
|
76
|
|
|
public function url($fileInfo, $full = false, $options = []) |
|
77
|
|
|
{ |
|
78
|
|
|
if (empty($fileInfo)) { |
|
79
|
|
|
return []; |
|
80
|
|
|
} |
|
81
|
|
|
if (!isset($fileInfo['resize'])) { |
|
82
|
|
|
$fileInfo['resize'] = false; |
|
83
|
|
|
} |
|
84
|
|
|
return $this->Url->build($this->urlArray($fileInfo, $options), $full); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* contentsFileHidden |
|
89
|
|
|
* |
|
90
|
|
|
* バリデーションに引っかかった際にファイルをそのまま送る用 |
|
91
|
|
|
* |
|
92
|
|
|
* @author hagiwara |
|
93
|
|
|
* @param entity $entity |
|
94
|
|
|
* @param text $field |
|
95
|
|
|
*/ |
|
96
|
|
|
public function contentsFileHidden($entity, $field) |
|
97
|
|
|
{ |
|
98
|
|
|
$contentFileField = 'contents_file_' . $field; |
|
99
|
|
|
$hiddenInput = ''; |
|
100
|
|
|
if (!empty($entity->{$contentFileField})) { |
|
101
|
|
|
foreach ($entity->{$contentFileField} as $fieldParts => $v) { |
|
102
|
|
|
$hiddenInput .= $this->Form->input($contentFileField . '.' . $fieldParts, ['type' => 'hidden']); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
return $hiddenInput; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* urlArray |
|
110
|
|
|
* @author hagiwara |
|
111
|
|
|
* @param array $fileInfo |
|
112
|
|
|
*/ |
|
113
|
|
|
private function urlArray($fileInfo, $options) |
|
114
|
|
|
{ |
|
115
|
|
|
if (!empty($fileInfo['tmp_file_name'])) { |
|
116
|
|
|
return [ |
|
117
|
|
|
'controller' => 'contents_file', |
|
118
|
|
|
'action' => 'loader', |
|
119
|
|
|
'plugin' => 'ContentsFile', |
|
120
|
|
|
'model' => $fileInfo['model'], |
|
121
|
|
|
'field_name' => $fileInfo['field_name'], |
|
122
|
|
|
'tmp_file_name' => $fileInfo['tmp_file_name'], |
|
123
|
|
|
// prefixは無視する |
|
124
|
|
|
'prefix' => false, |
|
125
|
|
|
]; |
|
126
|
|
|
} else { |
|
127
|
|
|
if (!isset($fileInfo['resize'])) { |
|
128
|
|
|
$fileInfo['resize'] = false; |
|
129
|
|
|
} |
|
130
|
|
|
// S3のホスティングの場合 |
|
131
|
|
|
if ( |
|
132
|
|
|
array_key_exists('static_s3', $options) && |
|
133
|
|
|
$options['static_s3'] == true && |
|
134
|
|
|
Configure::read('ContentsFile.Setting.type') == 's3' && |
|
135
|
|
|
!is_null(Configure::read('ContentsFile.Setting.S3.static_domain')) |
|
136
|
|
|
) { |
|
137
|
|
|
return $this->makeStaticS3Url($fileInfo); |
|
138
|
|
|
} else { |
|
139
|
|
|
// loaderを通す場合 |
|
140
|
|
|
return [ |
|
141
|
|
|
'controller' => 'contents_file', |
|
142
|
|
|
'action' => 'loader', |
|
143
|
|
|
'plugin' => 'ContentsFile', |
|
144
|
|
|
'model' => $fileInfo['model'], |
|
145
|
|
|
'field_name' => $fileInfo['field_name'], |
|
146
|
|
|
'model_id' => $fileInfo['model_id'], |
|
147
|
|
|
'resize' => $fileInfo['resize'], |
|
148
|
|
|
// prefixは無視する |
|
149
|
|
|
'prefix' => false, |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* makeStaticS3Url |
|
157
|
|
|
* 静的ホスティング用のURL作成 |
|
158
|
|
|
* @author hagiwara |
|
159
|
|
|
* @param array $fileInfo |
|
160
|
|
|
*/ |
|
161
|
|
|
private function makeStaticS3Url($fileInfo) |
|
162
|
|
|
{ |
|
163
|
|
|
$staticS3Url = Configure::read('ContentsFile.Setting.S3.static_domain') . '/' . Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $fileInfo['model'] . '/' . $fileInfo['model_id'] . '/'; |
|
164
|
|
|
if ($fileInfo['resize'] == false) { |
|
165
|
|
|
$staticS3Url .= $fileInfo['field_name']; |
|
166
|
|
|
} else { |
|
167
|
|
|
$resizeText = ''; |
|
168
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
169
|
|
|
empty($fileInfo['resize']['width']) |
|
170
|
|
|
) { |
|
171
|
|
|
$resizeText .= '0'; |
|
172
|
|
|
} else { |
|
173
|
|
|
$resizeText .= $fileInfo['resize']['width']; |
|
174
|
|
|
} |
|
175
|
|
|
$resizeText .= '_'; |
|
176
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
177
|
|
|
empty($fileInfo['resize']['height']) |
|
178
|
|
|
) { |
|
179
|
|
|
$resizeText .= '0'; |
|
180
|
|
|
} else { |
|
181
|
|
|
$resizeText .= $fileInfo['resize']['height']; |
|
182
|
|
|
} |
|
183
|
|
|
$staticS3Url .= 'contents_file_resize_' . $fileInfo['field_name'] . '/' . $resizeText; |
|
184
|
|
|
} |
|
185
|
|
|
return $staticS3Url; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.