-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModule.php
111 lines (98 loc) · 2.94 KB
/
Module.php
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
<?php
namespace robote13\catalog;
use Yii;
use yii\base\Event;
use yii\helpers\ArrayHelper;
use vova07\fileapi\FileAPI;
/**
* shop-catalog module definition class
*/
class Module extends \yii\base\Module
{
/**
* @inheritdoc
*/
public $defaultRoute = 'product';
/**
* @inheritdoc
*/
public $controllerNamespace = 'robote13\catalog\frontend\controllers';
/**
* @var string
*/
public $fileapiComponent = 'fileapi';
/**
* @var boolean
*/
public $enableBadge = true;
/**
* @var array default FileAPI component settings
* @see FileAPI
*/
private $previewUploaderOptions = [
'tempPath' => '@app/runtime/catalog-previews',
'url' => '@web/catalog-previews',
'imageTransforms'=>[
'cropped'=>[
'maxWidth'=>150,
'maxHeight'=>150,
'preview'=> true
]
],
'filesystem'=>'catalogPreviews'
];
/**
* @inheritdoc
*/
public function init()
{
Yii::$container->set('sidanval\tabular\TabularForm', components\TabularForm::className());
parent::init();
$this->initFileAPI();
$this->setDefaultViewPath();
}
/**
* Preview uploader setter.
* @see FileAPI
* @param array $options
*/
public function setPreviewUploaderOptions($options)
{
$this->previewUploaderOptions = ArrayHelper::merge($this->previewUploaderOptions, $options);
}
/**
*
* @param string $dimension 'width' or 'height'
* @return integer size in pixels, 0 if transformation variant is not found
*/
public function getCropDimension($dimension,$transformVariant = 'cropped')
{
$dimension = ucfirst($dimension);
return ArrayHelper::getValue($this->previewUploaderOptions, "imageTransforms.{$transformVariant}.max{$dimension}",0);
}
private function setDefaultViewPath()
{
if(!is_dir($this->viewPath))
{
$pos = strrpos($this->controllerNamespace,'\\');
$this->viewPath = str_replace('\\', '/', ltrim('@'.substr($this->controllerNamespace,0,$pos).'/views','\\'));
}
}
private function initFileAPI(){
if($this->enableBadge)
{
Yii::$container->setSingleton($this->fileapiComponent, array_merge($this->previewUploaderOptions,['class' => FileAPI::className()]));
Event::on(models\ProductBase::className(), models\ProductBase::EVENT_INIT,function($event){
$event->sender->attachBehavior('uploadBehavior',[
'class' => 'vova07\fileapi\behaviors\UploadBehavior',
'fileapi' => $this->fileapiComponent,
'attributes' => [
'badge' => [
'url' => Yii::getAlias('@web/previews/')
]
]
]);
});
}
}
}