|
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']; |
|
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
|
|
|
//一時パス用の設定 |
|
26
|
|
|
if ($title === null) { |
|
27
|
|
|
$title = $fileInfo['file_name']; |
|
28
|
|
|
} |
|
29
|
|
View Code Duplication |
if (isset($options['resize'])) { |
|
|
|
|
|
|
30
|
|
|
$fileInfo['resize'] = $options['resize']; |
|
31
|
|
|
unset($options['resize']); |
|
32
|
|
|
} |
|
33
|
|
|
$options = array_merge( |
|
34
|
|
|
$this->defaultOption, |
|
35
|
|
|
$options |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
return $this->Html->link( |
|
|
|
|
|
|
39
|
|
|
$title, |
|
40
|
|
|
$this->urlArray($fileInfo, $options), |
|
41
|
|
|
$options |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* image |
|
47
|
|
|
* @author hagiwara |
|
48
|
|
|
* @param array $fileInfo |
|
49
|
|
|
* @param array $options |
|
50
|
|
|
*/ |
|
51
|
|
|
public function image($fileInfo, $options = []) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!empty($fileInfo)) { |
|
54
|
|
View Code Duplication |
if (isset($options['resize'])) { |
|
|
|
|
|
|
55
|
|
|
$fileInfo['resize'] = $options['resize']; |
|
56
|
|
|
unset($options['resize']); |
|
57
|
|
|
} |
|
58
|
|
|
return $this->Html->image($this->urlArray($fileInfo, $options), $options); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
return ''; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* url |
|
65
|
|
|
* @author hagiwara |
|
66
|
|
|
* @param array $fileInfo |
|
67
|
|
|
* @param boolean $full |
|
68
|
|
|
* @param array $options |
|
69
|
|
|
*/ |
|
70
|
|
|
public function url($fileInfo, $full = false, $options = []) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!isset($fileInfo['resize'])) { |
|
73
|
|
|
$fileInfo['resize'] = false; |
|
74
|
|
|
} |
|
75
|
|
|
return $this->Url->build($this->urlArray($fileInfo, $options), $full); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* urlArray |
|
80
|
|
|
* @author hagiwara |
|
81
|
|
|
* @param array $fileInfo |
|
82
|
|
|
*/ |
|
83
|
|
|
private function urlArray($fileInfo, $options) |
|
84
|
|
|
{ |
|
85
|
|
|
if (!empty($fileInfo['tmp_file_name'])) { |
|
86
|
|
|
return [ |
|
87
|
|
|
'controller' => 'contents_file', |
|
88
|
|
|
'action' => 'loader', |
|
89
|
|
|
'plugin' => 'ContentsFile', |
|
90
|
|
|
'model' => $fileInfo['model'], |
|
91
|
|
|
'field_name' => $fileInfo['field_name'], |
|
92
|
|
|
'tmp_file_name' => $fileInfo['tmp_file_name'], |
|
93
|
|
|
]; |
|
94
|
|
|
} else { |
|
95
|
|
|
if (!isset($fileInfo['resize'])) { |
|
96
|
|
|
$fileInfo['resize'] = false; |
|
97
|
|
|
} |
|
98
|
|
|
// S3のホスティングの場合 |
|
99
|
|
|
if ( |
|
100
|
|
|
array_key_exists('static_s3', $options) && |
|
101
|
|
|
$options['static_s3'] == true && |
|
102
|
|
|
Configure::read('ContentsFile.Setting.type') == 's3' && |
|
103
|
|
|
!is_null(Configure::read('ContentsFile.Setting.S3.static_domain')) |
|
104
|
|
|
) { |
|
105
|
|
|
return $this->makeStaticS3Url($fileInfo); |
|
106
|
|
|
} else { |
|
107
|
|
|
// loaderを通す場合 |
|
108
|
|
|
return [ |
|
109
|
|
|
'controller' => 'contents_file', |
|
110
|
|
|
'action' => 'loader', |
|
111
|
|
|
'plugin' => 'ContentsFile', |
|
112
|
|
|
'model' => $fileInfo['model'], |
|
113
|
|
|
'field_name' => $fileInfo['field_name'], |
|
114
|
|
|
'model_id' => $fileInfo['model_id'], |
|
115
|
|
|
'resize' => $fileInfo['resize'], |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* makeStaticS3Url |
|
123
|
|
|
* 静的ホスティング用のURL作成 |
|
124
|
|
|
* @author hagiwara |
|
125
|
|
|
* @param array $fileInfo |
|
126
|
|
|
*/ |
|
127
|
|
|
private function makeStaticS3Url($fileInfo) |
|
128
|
|
|
{ |
|
129
|
|
|
$staticS3Url = Configure::read('ContentsFile.Setting.S3.static_domain') . '/' . Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $fileInfo['model'] . '/' . $fileInfo['model_id'] . '/'; |
|
130
|
|
|
if ($fileInfo['resize'] == false) { |
|
131
|
|
|
$staticS3Url .= $fileInfo['field_name']; |
|
132
|
|
|
} else { |
|
133
|
|
|
$resizeText = ''; |
|
134
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
135
|
|
|
empty($fileInfo['resize']['width']) |
|
136
|
|
|
) { |
|
137
|
|
|
$resizeText .= '0'; |
|
138
|
|
|
} else { |
|
139
|
|
|
$resizeText .= $fileInfo['resize']['width']; |
|
140
|
|
|
} |
|
141
|
|
|
$resizeText .= '_'; |
|
142
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
143
|
|
|
empty($fileInfo['resize']['height']) |
|
144
|
|
|
) { |
|
145
|
|
|
$resizeText .= '0'; |
|
146
|
|
|
} else { |
|
147
|
|
|
$resizeText .= $fileInfo['resize']['height']; |
|
148
|
|
|
} |
|
149
|
|
|
$staticS3Url .= 'contents_file_resize_' . $fileInfo['field_name'] . '/' . $resizeText; |
|
150
|
|
|
} |
|
151
|
|
|
return $staticS3Url; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
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.