pepebe
7/3/2013 - 11:56 PM

Dynamically set current user's redactor.image_upload_path Setting based on the alias of the currently edited resource's parent

Dynamically set current user's redactor.image_upload_path Setting based on the alias of the currently edited resource's parent

<?php
switch ($modx->event->name) {
    case 'OnDocFormPrerender':
    if(!$resource) return;
    
    $key = 'redactor.image_upload_path';
    $userId = $modx->user->get('id');
    $usersetting = $modx->getObject('modUserSetting', array('key' => $key, 'user' => $userId));
    if(!$usersetting) return;
    
    $modx->log(modX::LOG_LEVEL_INFO,"RedactorSettingsManager read: $key, $userId, {$usersetting->get('value')}");
    
    $parentID = $page_id = $resource->get('parent');
    if(!$parentID) return;
    
    $parent = $modx->getObject('modResource',$parentID);
    $alias = $parent->get('alias');
    
    $usersetting->set('value',"uploads/images/$alias/");
    $usersetting->save();

    $modx->log(modX::LOG_LEVEL_INFO,"RedactorSettingsManager update: $key, $userId, {$usersetting->get('value')}");
    
    $contextKey = $modx->context->get('key');
    
    unset($_SESSION["modx.$contextKey.user.config"]);
    $modx->setOption('redactor.image_upload_path',"uploads/images/$alias/");
    
    $modx->log(modX::LOG_LEVEL_INFO,"RedactorSettingsManager read system: {$modx->getOption('redactor.image_upload_path')}");
    break;

}