We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28788
    • 79 Posts
    Hi guys,

    I seem to remember a snippet or plugin or something that created and bound a new directory to each new resource so that uploaded images or files were only relevant to that resource. I’ve search everywhere and I can’t seem to find it, sound familiar to anyone?

    Cheers
      • 4310
      • 2,310 Posts
      This plugin creates a folder named with the id of the document in the files folder, then creates a folder named images inside.
      In this example it will only happen when the template has an id of 42 and the documents parent has an id of 29 and either folder doesn’t already exist, it is triggered when you save a document.
      The "OnDocFormSave" System Event should be checked.
      Adjust folders, folder paths, template & parent as needed.
      <?php // remove this line, it's just for syntax highlighting
      $e = &$modx->Event;
      $docid = $e->params['id'];
      $dir = $modx->config['rb_base_dir']."files/".$docid;
      $images_dir = $modx->config['rb_base_dir']."files/".$docid."/images";
      $folder_permissions = octdec($modx->config['new_folder_permissions']);
      
      function mkdirs($strPath, $mode){ // recursive mkdir function
              if (is_dir($strPath)) return true;
              $pStrPath = dirname($strPath);
              if (!mkdirs($pStrPath, $mode)) return false;
              return @mkdir($strPath);
      }
      
      switch ($e->name) {
      case "OnDocFormSave":
      if($_REQUEST['template'] == 42 && $_REQUEST['parent'] == 29)
      {
      if(!file_exists( $dir )){
        $old_umask = umask(0);
              mkdirs($dir,$folder_permissions);
        umask($old_umask);
          $sub_umask = umask(0);
              mkdirs($images_dir,$folder_permissions);
        umask($sub_umask);
        }
        }
        break;
        
        default: "";
      
        return;
      }
        • 28788
        • 79 Posts
        Wow that’s brilliant thanks so much, giving the folder the the ID of the document is a brilliant idea.

        So what I want to do is run your plugin (perhaps on OnDocFormRender instead so I can upload to it?) to create the folder, then set the equivalent of @DIRECTORY assets/files/[+id+]/ to bind that folder to my document.

        I guess it’s a case of using @EVAL instead of @DIRECTORY and returning the file path with the document id?

        Many thanks bunk58 I’m off to give that a go.
          • 28788
          • 79 Posts
          And here we go:

          <?php // Remove
          
          @EVAL $listDir = array(); $docid = $_GET['id']; if ($handle = opendir('../assets/files/'. $docid)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != ".DS_Store") {  $listDir[] = $file; } }  closedir($handle); } return $listDir;
          
          ?>
          


          This is the input option values for a multi select listbox (or drop down list etc).

          Thanks again.
            • 4310
            • 2,310 Posts
            smiley
              • 1289
              • 6 Posts
              Hi guys,

              i am working for a test project.
              Here is my setup

              Depatment (Context) ---> user1 --->filemanager_path (assets/templates/department1/upload), filemanager_url (assets/templates/department1/upload)

              Depatment2 (Context) ---> user2 --->filemanager_path (assets/templates/department2/upload), filemanager_url (assets/templates/department2/upload)

              Depatment3 (Context) ---> user1 --->filemanager_path (assets/templates/department1/upload), filemanager_url (assets/templates/department3/upload)

              I have created 3 Users Group for each accounts.

              Now my problem is while tried to login each account, it does not display any files in the folder’s tab.

              All i want is to see the upload folder file for each users and hide other context that are not belongs to user.

              Your help will be highly appreciated.


              Thanks & god bless you all..