Created
December 18, 2018 08:30
-
-
Save mnoskov/e44d53bcfa7797285ef7d1431445587c to your computer and use it in GitHub Desktop.
Separate upload folders for each user group in Evolution CMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<?php | |
/** | |
* UploadSeparator | |
* | |
* Separate upload folders for each user group | |
* | |
* @category plugin | |
* @version 0.1.0 | |
* @author kassio | |
* @internal @properties &folders=Groups to folders relation;text;1==44w9mFQp||2==jG7h8-Mv &common_folder=Name of the common folder;text;common | |
* @internal @events OnFileBrowserInit | |
* @internal @modx_category Manager and Admin | |
* @internal @installset sample | |
*/ | |
if ($modx->Event->name == 'OnFileBrowserInit') { | |
$groups = $modx->getUserDocGroups(); | |
$group = array_shift($groups); | |
$folders = []; | |
foreach (explode('||', $params['folders']) as $raw) { | |
list($key, $value) = explode('==', $raw); | |
$folders[$key] = $value; | |
} | |
if ($group && isset($folders[$group])) { | |
// creating directories | |
$parts = ['uploads', $folders[$group]]; | |
$path = $params['config']['uploadDir']; | |
$dir = ''; | |
do { | |
$dir .= '/' . array_shift($parts); | |
if (!file_exists($path . $dir)) { | |
mkdir($path . $dir); | |
} | |
} while (!empty($parts)); | |
// creating link to common folder | |
if (!empty($_GET['type']) && is_scalar($_GET['type']) && !file_exists($path . $dir . '/' . $_GET['type'])) { | |
mkdir($path . $dir . '/' . $_GET['type']); | |
} | |
if (!empty($_GET['type']) && !empty($params['common_folder'])) { | |
$folder = $_GET['type'] . '/' . $params['common_folder']; | |
if (file_exists($params['config']['uploadDir'] . '/' . $folder) && !file_exists($params['config']['uploadDir'] . $dir . '/' . $folder)) { | |
symlink('../../../' . $folder, $params['config']['uploadDir'] . $dir . '/' . $folder); | |
} | |
} | |
// setting new path values | |
foreach (['assetsURL', 'uploadURL', 'uploadDir'] as $key) { | |
$params['config'][$key] .= $dir; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment