|
11 | 11 |
|
12 | 12 | use hrzg\filemanager\assets\AfmAsset;
|
13 | 13 | use yii\base\Widget;
|
| 14 | +use yii\helpers\ArrayHelper; |
| 15 | +use yii\helpers\Json; |
14 | 16 | use yii\helpers\Url;
|
15 | 17 | use yii\web\View;
|
16 | 18 |
|
@@ -40,24 +42,22 @@ class FileManagerWidget extends Widget
|
40 | 42 | public $template = '<div data-ng-app="FileManagerApp"><div class="ng-cloak"><angular-filemanager></angular-filemanager></div></div>';
|
41 | 43 |
|
42 | 44 | /**
|
43 |
| - * @var string |
| 45 | + * @var boolean|null |
44 | 46 | */
|
45 |
| - public $thumbnailUrlPrefix; |
| 47 | + public $enableThumbnails; |
46 | 48 |
|
47 | 49 | /**
|
48 |
| - * @var string |
| 50 | + * @var boolean|null |
49 | 51 | */
|
50 |
| - public $thumbnailUrlSuffix; |
| 52 | + public $enableIconPreviewView; |
51 | 53 |
|
52 | 54 | /**
|
53 |
| - * @var boolean |
| 55 | + * array of angular fileManagerApp options |
| 56 | + * |
| 57 | + * @var null|array |
54 | 58 | */
|
55 |
| - public $enableThumbnails; |
| 59 | + public $options; |
56 | 60 |
|
57 |
| - /** |
58 |
| - * @var boolean |
59 |
| - */ |
60 |
| - public $enableIconPreviewView; |
61 | 61 | /**
|
62 | 62 | * @inheritdoc
|
63 | 63 | */
|
@@ -97,74 +97,103 @@ protected function setFileManagerConfig()
|
97 | 97 | $title = empty($this->title) ? getenv('AFM_TITLE') : $this->title;
|
98 | 98 | $lang = \Yii::$app->language;
|
99 | 99 |
|
100 |
| - // resolve options for JavaScript |
101 |
| - $enableThumbnails = $this->enableThumbnails ? 'true' : 'false'; |
102 |
| - $enableIconPreviewView = $this->enableIconPreviewView ? 'true' : 'false'; |
| 100 | + $this->options = is_array($this->options) ? $this->options : []; |
| 101 | + |
| 102 | + $defaults = [ |
| 103 | + 'appName' => $title, |
| 104 | + 'defaultLang' => $lang, |
| 105 | + // which functions should be activated in fileManagerApp? |
| 106 | + 'searchForm' => true, |
| 107 | + 'sidebar' => true, |
| 108 | + 'breadcrumb' => true, |
| 109 | + 'hidePermissions' => true, |
| 110 | + |
| 111 | + // Handler Urls |
| 112 | + 'listUrl' => $this->handlerUrl, |
| 113 | + 'uploadUrl' => $this->handlerUrl, |
| 114 | + 'renameUrl' => $this->handlerUrl, |
| 115 | + 'copyUrl' => $this->handlerUrl, |
| 116 | + 'moveUrl' => $this->handlerUrl, |
| 117 | + 'removeUrl' => $this->handlerUrl, |
| 118 | + 'getContentUrl' => $this->handlerUrl, |
| 119 | + 'createFolderUrl' => $this->handlerUrl, |
| 120 | + 'downloadFileUrl' => $this->handlerUrl, |
| 121 | + 'downloadMultipleUrl' => $this->handlerUrl, |
| 122 | + 'compressUrl' => $this->handlerUrl, |
| 123 | + 'extractUrl' => $this->handlerUrl, |
| 124 | + 'permissionsUrl' => $this->handlerUrl, |
| 125 | + |
| 126 | + // Additional settings |
| 127 | + 'multipleDownloadFileName' => 'filemanager.zip', |
| 128 | + 'showSizeForDirectories' => false, |
| 129 | + 'useBinarySizePrefixes' => false, |
| 130 | + 'downloadFilesByAjax' => true, |
| 131 | + 'previewImagesInModal' => true, |
| 132 | + 'enablePermissionsRecursive' => false, |
| 133 | + 'enableThumbnails' => false, |
| 134 | + 'enableIconPreviewView' => false, |
| 135 | + |
| 136 | + // File patterns |
| 137 | + 'isEditableFilePattern' => '/\.(!)/i', |
| 138 | + 'isImageFilePattern' => '/\.(jpe?g|gif|bmp|png|svg|tiff?)$/i', |
| 139 | + 'isExtractableFilePattern' => '/\.(gz|tar|rar|g?zip)$/i', |
| 140 | + // define allowed actions for filemanager |
| 141 | + 'allowedActions' => [ |
| 142 | + 'upload' => true, |
| 143 | + 'rename' => false, |
| 144 | + 'move' => true, |
| 145 | + 'copy' => true, |
| 146 | + 'edit' => true, |
| 147 | + 'compress' => true, |
| 148 | + 'compressChooseName' => true, |
| 149 | + 'extract' => true, |
| 150 | + 'download' => true, |
| 151 | + 'downloadMultiple' => true, |
| 152 | + 'downloadLink' => true, |
| 153 | + 'preview' => true, |
| 154 | + 'remove' => true, |
| 155 | + 'createFolder' => true, |
| 156 | + 'pickFiles' => false, |
| 157 | + 'pickFolders' => false, |
| 158 | + 'changePermissions' => $allowPermissions, |
| 159 | + ] |
| 160 | + ]; |
| 161 | + |
| 162 | + // these options are regEx and therefor can not be stored as strings in json |
| 163 | + // if preset, they will be handled later, see search/replace |
| 164 | + $specialOpts = [ |
| 165 | + 'isEditableFilePattern', |
| 166 | + 'isImageFilePattern', |
| 167 | + 'isExtractableFilePattern' |
| 168 | + ]; |
| 169 | + // store specialOpts for replacement |
| 170 | + $specialValues = []; |
| 171 | + |
| 172 | + $config = ArrayHelper::merge($defaults, $this->options); |
| 173 | + |
| 174 | + // now insert placeholders for $specialValues that can not be stored as strings, e.g. regexP |
| 175 | + foreach ($specialOpts as $opt) { |
| 176 | + if (!empty($config[$opt])) { |
| 177 | + $specialValues[$opt] = [ |
| 178 | + 'value' => $config[$opt], |
| 179 | + 'placeholder' => '###' . strtoupper($opt) . '###', |
| 180 | + ]; |
| 181 | + $config[$opt] = $specialValues[$opt]['placeholder']; |
| 182 | + } |
| 183 | + } |
| 184 | + // generate config json |
| 185 | + $configJson = json_encode($config); |
| 186 | + // then replace placeholder strings in json config that can/should not be strings... |
| 187 | + foreach ($specialValues as $special) { |
| 188 | + $configJson = str_replace('"' . $special['placeholder']. '"', $special['value'], $configJson); |
| 189 | + } |
103 | 190 |
|
| 191 | + // finally create JS to init fileManagerApp |
104 | 192 | $initFilemanagerJs = <<<JS
|
105 | 193 | angular.module('FileManagerApp').config(['fileManagerConfigProvider', function (config) {
|
106 | 194 | var defaults = config.\$get();
|
107 |
| - var handler = '$this->handlerUrl'; |
108 |
| - config.set({ |
109 |
| -
|
110 |
| - // Application |
111 |
| - appName: '$title', |
112 |
| - defaultLang: '$lang', |
113 |
| - searchForm: true, |
114 |
| - sidebar: true, |
115 |
| - breadcrumb: true, |
116 |
| - hidePermissions: true, |
117 |
| -
|
118 |
| - // Allowed actions |
119 |
| - allowedActions: angular.extend(defaults.allowedActions, { |
120 |
| - remove: true, |
121 |
| - list: true, |
122 |
| - move: true, |
123 |
| - rename: true, |
124 |
| - copy: true, |
125 |
| - download: true, |
126 |
| - downloadMultiple: false, |
127 |
| - downloadLink: true, |
128 |
| - changePermissions: '$allowPermissions', |
129 |
| - compress: false, |
130 |
| - compressChooseName: false, |
131 |
| - extract: false, |
132 |
| - upload: true |
133 |
| - }), |
134 |
| -
|
135 |
| - // Handler |
136 |
| - listUrl: handler, |
137 |
| - uploadUrl: handler, |
138 |
| - renameUrl: handler, |
139 |
| - copyUrl: handler, |
140 |
| - moveUrl: handler, |
141 |
| - removeUrl: handler, |
142 |
| - getContentUrl: handler, |
143 |
| - createFolderUrl: handler, |
144 |
| - downloadFileUrl: handler, |
145 |
| - downloadMultipleUrl: handler, |
146 |
| - compressUrl: handler, |
147 |
| - extractUrl: handler, |
148 |
| - permissionsUrl: handler, |
149 |
| -
|
150 |
| - // Additional settings |
151 |
| - multipleDownloadFileName: 'filemanager.zip', |
152 |
| - showSizeForDirectories: false, |
153 |
| - useBinarySizePrefixes: false, |
154 |
| - downloadFilesByAjax: true, |
155 |
| - previewImagesInModal: true, |
156 |
| - enablePermissionsRecursive: false, |
157 |
| - thumbnailUrlPrefix: '{$this->thumbnailUrlPrefix}', |
158 |
| - thumbnailUrlSuffix: '{$this->thumbnailUrlSuffix}', |
159 |
| - enableThumbnails: {$enableThumbnails}, |
160 |
| - enableIconPreviewView: {$enableIconPreviewView}, |
161 |
| -
|
162 |
| - // File patterns |
163 |
| - isEditableFilePattern: /\.(!)/i, |
164 |
| - isImageFilePattern: /\.(jpe?g|gif|bmp|png|svg|tiff?)$/i, |
165 |
| - isExtractableFilePattern: /\.(gz|tar|rar|g?zip)$/i |
166 |
| - //tplPath: 'src/templates' |
167 |
| - }); |
| 195 | + // we use merge here to get deep.copy, see: https://docs.angularjs.org/api/ng/function/angular.merge |
| 196 | + config.set(angular.merge(defaults, $configJson)); |
168 | 197 | }]);
|
169 | 198 | JS;
|
170 | 199 |
|
|
0 commit comments