We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33968
    • 863 Posts
    Before 2.2 I used the modx-combo-browser to allow users to upload images into their own folder, located in assets/uploads/6/, where '6' was the currently logged in user id. My code was roughly like this, meaning the browser would open in their own folder and not allow navigation outside of that.
    {
         xtype: 'modx-combo-browser'
        ,name: 'image'
        ,value: config.record.image
        ,basePath: 'assets/uploads/' + CMP.user.id + '/'
        ,baseUrl: 'assets/uploads/' + CMP.user.id + '/'
    }
    

    However, basePath doesn't appear to work in 2.2 and seems to have been replaced with the source option instead.

    I created a media source for assets/uploads/ and used the openTo option to open the browser in the user's subfolder, however the user can also see and navigate to any other user folder too. I can't allow that.
    {
         xtype: 'modx-combo-browser'
        ,name: 'image'
        ,value: config.record.image
        ,source: 3   // where source 3 = 'assets/uploads/'
        ,openTo: CMP.user.id + '/'
    }
    

    It's not practical to create a new Media Source for every user (over 50 of them). How can I configure my file browser to append a relative path to the media source base? [ed. note: okyanet last edited this post 14 years, 7 months ago.]
      • 33968
      • 863 Posts
      Well... I've temporarily got around this by modifying modx.combo.js and modx.view.js to include the rootId parameter used by 'modx-tree-directory'.

      Then configuring the browser like so:
      {
           xtype: 'modx-combo-browser'
          ,name: 'image'
          ,value: config.record.image
          ,source: 3   // where source 3 = 'assets/uploads/'
          ,openTo: CMP.user.id + '/'
          ,rootId: CMP.user.id + '/'
      }
      

      That has the result I want, opening the browser in a specific folder relative to the media source and setting that as the root so the user can't navigate elsewhere.

      I hate modding the core though. I considered extending MODx.combo.browser but it looks like a lot of code to duplicate just to get one extra parameter in there. Is there a better way to do this?

      EDIT: added a refactor request - http://tracker.modx.com/issues/7010 [ed. note: okyanet last edited this post 14 years, 7 months ago.]
        • 4172
        • 5,888 Posts
        you can use a plugin for the systemEvent OnMediaSourceGetProperties

        the plugin 'processMediaPath':


        <?php
        
        if (!function_exists('processMediaPathes')) {
        function processMediaPathes($path)
        {
        global $modx;
        $chunk = $modx->newObject('modChunk');
        $chunk->setContent($path);
        $chunk->setCacheable(false);
        return $chunk->process();
        }
        }
        
        
        switch ($modx->event->name) {
        case 'OnMediaSourceGetProperties':
        
        $sourceUpdateActionID = 0;
        if ($object = $modx->getObject('modAction', array('namespace' => 'core', 'controller' => 'source/update', ))) {
        $sourceUpdateActionID = $object->get('id');
        }
        
        $params = $modx->fromJson($scriptProperties['properties']);
        // don't manpulate-path if updating mediasource
        if (empty($params['processPath']) || $_REQUEST['a'] == $sourceUpdateActionID) {
        $modx->event->output($scriptProperties['properties']);
        return;
        }
        
        
        
        //$path = $params['basePath']['value'];
        //$modx->parser->processElementTags('',$path,true,true);
        $params['basePath']['value'] = processMediaPathes($params['basePath']['value']);
        $params['baseUrl']['value'] = processMediaPathes($params['baseUrl']['value']);
        
        $modx->event->output($modx->toJson($params));
        /*
        $debug['http_referer'] = $_SERVER['HTTP_REFERER'];
        $debug['basePath'] = $params['basePath']['value'];
        
        $chunk = $modx->getObject('modChunk', array('name' => 'debug'));
        $chunk->setContent($chunk->get('snippet') . print_r($debug, 1));
        $chunk->save();
        */
        break;
        }
        return; 


        pathes in your mediasource:

        assets/images/[[getUserID]]/


        then create a new property in your mediasource:

        name: processPath
        value: 1


        the snippet getUserID:

        if (is_object($modx->resource)){
        $createdby = $modx->resource->get('createdby');
        //at frontend or if created resource is active, return createdby
        if (!empty($createdby)){
        return $createdby;
        }
        }
        // else (backend) return userid
        return $modx->user->get('id'); 


        something like that should do it, modify it to your needs in your CMP, this is for resources and uses the createdby-field. [ed. note: Bruno17 last edited this post 14 years, 7 months ago.]
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 33968
          • 863 Posts
          Thanks Bruno - I hadn't thought of using a plugin and wasn't aware of the new OnMediaSource events.

          I realise Media Sources are still in their infancy now however, which is why I wanted to draw attention to this use case so that the additional config options might be incorporated into upcoming releases. I use this technique in nearly all my CMPs to control exactly where people are able to upload files, and to prevent them straying into other users' folders. It would certainly allow for a lot more flexibility without having to modify the core.
            • 33968
            • 863 Posts
            Update - Shaun has kindly sorted this out and made the 'rootId' option available for the 2.2.1 release.
              • 37377
              • 64 Posts
              Is it possible to upload to the sub directory as well?
              I have my combo-browser configured as follows and it opens to the right dir but when I upload files they end up in the parent dir.


              {
                          xtype: 'modx-combo-browser'
                          ,fieldLabel: _('printing.afbeelding')
                          ,name: 'afbeelding'
                          ,anchor: '100%'
              			,source: 2
              			,openTo:config.record.product_id+'/'
              			,rootId:config.record.product_id+'/'
                      }


              Any ideas?
                • 20728
                • 53 Posts
                Steffan, did you ever solve the upload issue? We've just hit that problem too.
                  Enovate Design | Web Design Agency Essex
                  • 20728
                  • 53 Posts
                  Ah, there's a solution to the upload issue here:

                  http://tracker.modx.com/issues/9229
                    Enovate Design | Web Design Agency Essex
                    • 33968
                    • 863 Posts
                    Unbelievable... as the fix required a core mod, every time I upgrade this bug re-surfaces. I forget that I fixed it, so I google around and end up back here again tongue

                    I've transferred that issue over to GitHub now:
                    https://github.com/modxcms/revolution/issues/790
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      You might find this of use - you can use something similar for user ID instead of resource ID, I would presume. http://rtfm.modx.com/extras/revo/migx/migx.tutorials/migx.use-resource-specific-media-source-and-multifile-uploader
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org