|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of yii2-imperavi-widget. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @see https://github.com/vova07/yii2-imperavi-widget |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace vova07\imperavi\actions; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\base\Action; |
|
15
|
|
|
use yii\base\InvalidConfigException; |
|
16
|
|
|
use yii\helpers\FileHelper; |
|
17
|
|
|
use yii\web\Response; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* `GetImagesAction` returns a `JSON` array of the images found under the specified directory and subdirectories. |
|
21
|
|
|
* This array can be used in Imperavi Redactor to insert images that have been already uploaded. |
|
22
|
|
|
* |
|
23
|
|
|
* Usage: |
|
24
|
|
|
* |
|
25
|
|
|
* ```php |
|
26
|
|
|
* public function actions() |
|
27
|
|
|
* { |
|
28
|
|
|
* return [ |
|
29
|
|
|
* 'get-image' => [ |
|
30
|
|
|
* 'class' => 'vova07\imperavi\actions\GetImagesAction', |
|
31
|
|
|
* 'url' => 'http://my-site.com/statics/', |
|
32
|
|
|
* 'path' => '/var/www/my-site.com/web/statics', |
|
33
|
|
|
* 'options' => ['only' => ['*.jpg', '*.jpeg', '*.png', '*.gif', '*.ico']], |
|
34
|
|
|
* ] |
|
35
|
|
|
* ]; |
|
36
|
|
|
* } |
|
37
|
|
|
* ``` |
|
38
|
|
|
* |
|
39
|
|
|
* @author Vasile Crudu <[email protected]> |
|
40
|
|
|
* |
|
41
|
|
|
* @link https://github.com/vova07 |
|
42
|
|
|
*/ |
|
43
|
|
|
class GetImagesAction extends Action |
|
44
|
|
|
{ |
|
45
|
|
|
/** |
|
46
|
|
|
* @var string Files directory path. |
|
47
|
|
|
*/ |
|
48
|
|
|
public $path; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string Files http URL. |
|
52
|
|
|
*/ |
|
53
|
|
|
public $url; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array FileHelper options. |
|
57
|
|
|
* |
|
58
|
|
|
* @see FileHelper::findFiles() |
|
59
|
|
|
*/ |
|
60
|
|
|
public $options = ['only' => ['*.jpg', '*.jpeg', '*.png', '*.gif', '*.ico']]; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
12 |
|
public function init() |
|
66
|
|
|
{ |
|
67
|
12 |
|
if ($this->url === null) { |
|
68
|
3 |
|
throw new InvalidConfigException('The "url" attribute must be set.'); |
|
69
|
|
|
} else { |
|
70
|
9 |
|
$this->url = rtrim($this->url, '/') . '/'; |
|
71
|
|
|
} |
|
72
|
9 |
|
if ($this->path === null) { |
|
73
|
3 |
|
throw new InvalidConfigException('The "path" attribute must be set.'); |
|
74
|
|
|
} else { |
|
75
|
6 |
|
$this->path = Yii::getAlias($this->path); |
|
76
|
|
|
} |
|
77
|
3 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function run() |
|
83
|
|
|
{ |
|
84
|
3 |
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
85
|
|
|
|
|
86
|
3 |
|
$files = []; |
|
87
|
|
|
|
|
88
|
3 |
|
foreach (FileHelper::findFiles($this->path, $this->options) as $path) { |
|
89
|
3 |
|
$url = $this->url . str_replace($this->path, '', $path) |
|
90
|
3 |
|
|
|
91
|
|
|
$files[] = [ |
|
|
|
|
|
|
92
|
3 |
|
'id' => $url, |
|
93
|
3 |
|
'title' => $url, |
|
94
|
3 |
|
'thumb' => $url, |
|
95
|
3 |
|
'image' => $url, |
|
96
|
3 |
|
]; |
|
97
|
|
|
} |
|
98
|
1 |
|
|
|
99
|
|
|
return $files; |
|
100
|
3 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|