Module.php
7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace artbox\core\components\imagemanager;
use Yii;
use yii\base\UnknownClassException;
use yii\base\InvalidConfigException;
use yii\web\AssetManager;
use artbox\core\components\imagemanager\assets\ImageManagerModuleAsset;
/**
* imagemanager module definition class
*/
class Module extends \yii\base\Module
{
public $defaultRoute = 'manager';
//stylesheet for modal iframe
public $cssFiles = [];
//allowed Extensions for upload
public $allowedFileExtensions = [
'jpg',
'jpeg',
'gif',
'png',
];
//set assetPublishedUrl
public $assetPublishedUrl;
/**
* @var bool|callable Variable that defines if the upload action will be available
* This variable defaults to true, to enable uploading by default
* It is also possible to give a callable function, in which case the function will be executed
*/
public $canUploadImage = true;
/**
* @var bool|callable Variable that defines if the delete action will be available
* This variable default to true, to enable removal if image
* It is also possible to give a callable function, in which case the function will be executed
*/
public $canRemoveImage = true;
/**
* @var bool|callable Variable that defines if blameable behavior is used.
* This can be a boolean, or a callable function that returns a boolean
*/
public $setBlameableBehavior = false;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
//set language
if (!isset(Yii::$app->i18n->translations[ 'imagemanager' ])) {
Yii::$app->i18n->translations[ 'imagemanager' ] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@artbox/core/components/imagemanager/messages',
];
}
//check extensions
$this->_checkExtensionsExists();
//check mediaPath isset
if (Yii::$app->imagemanager->mediaPath === null) {
throw new InvalidConfigException("Component param 'mediaPath' need to be set to a location");
}
//set asset path
$this->assetPublishedUrl = ( new AssetManager )->getPublishedUrl(
"@artbox/core/components/imagemanager/assets/source"
);
// Check if the canRemoveImage variable is callable
if (is_callable($this->canRemoveImage)) {
$this->canRemoveImage = call_user_func($this->canRemoveImage);
}
// Check if the canUploadImage variable is callable
if (is_callable($this->canUploadImage)) {
$this->canUploadImage = call_user_func($this->canUploadImage);
}
// Check if blameable behavior is callable
if (is_callable($this->setBlameableBehavior)) {
$this->setBlameableBehavior = call_user_func($this->setBlameableBehavior);
}
// Check if the variable configuration is correct in order for the module to function
$this->_checkVariableConfiguration();
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
//set view
$view = $action->controller->getView();
//set asset
ImageManagerModuleAsset::register($view);
//get parameters
$viewMode = Yii::$app->request->get("view-mode", "page");
/* @var $action \yii\base\Action */
if ($viewMode == "iframe") {
//set stylesheet for modal
if (is_array($this->cssFiles) && count($this->cssFiles) > 0) {
//if exists loop through files and add them to iframe mode
foreach ($this->cssFiles AS $cssFile) {
//registrate file
$view->registerCssFile($cssFile, [ 'depends' => 'yii\bootstrap\BootstrapAsset' ]);
}
}
}
return true;
}
return false;
}
/**
* Check if extensions exists
*
* @throws UnknownClassException Throw error if extension is not found
*/
private function _checkExtensionsExists()
{
//kartik file uploaded is installed
if (!class_exists('kartik\file\FileInput')) {
throw new UnknownClassException(
"Can't find: kartik\\file\FileInput. Install \"kartik-v/yii2-widget-fileinput\": \"@dev\""
);
}
//check Yii imagine is installed
if (!class_exists('yii\imagine\Image')) {
throw new UnknownClassException(
"Can't find: yii\imagine\Image. Install \"yiisoft/yii2-imagine\": \"~2.0.0\""
);
}
}
/**
* Check if the module variables have the content that is expected
*
* @throws InvalidConfigException
*/
private function _checkVariableConfiguration()
{
// Check if the canUploadImage is boolean
if (!is_bool($this->canUploadImage)) {
throw new InvalidConfigException(
'$canUploadImage variable only supports a boolean value, if you have a custom function you must return a boolean'
);
}
// Check if the canRemoveImage is boolean
if (!is_bool($this->canRemoveImage)) {
throw new InvalidConfigException(
'$removeImageAllowed variable only supports a boolean value, if you have a custom function you must return a boolean'
);
}
// Check if the setBlamableBehavior is boolean
if (!is_bool($this->setBlameableBehavior)) {
throw new InvalidConfigException(
'$setBlameableBehavior only supports a boolean value, if you have a customer function make sure that you return a boolean'
);
}
// Check if the blameable behavior is set to true
if ($this->setBlameableBehavior) {
// Get the migration record
$mRecordMigrationRun = Yii::$app->db->createCommand(
'SELECT * FROM `migration` WHERE `version` = \'m170223_113221_addBlameableBehavior\''
)
->queryOne();
if ($mRecordMigrationRun === false) {
throw new InvalidConfigException(
'Image Manager: You have not run the latest migration, see the documentation how to do this.'
);
}
}
}
}