• Document Manager Snippet#

  • Pleth Reply #1, 3 years, 2 months ago

    Reply
    Just wanted to share a snippet I have been using to return unordered lists based on documents in a given directory. With a couple of modifications it has been coming in handy for several instances. I had actually used a variation of this previously to populate an unordered list styled for a photogallery.
    Anyway, the advantage has been that is simplifies updating of lists (meeting minutes, newsletters, etc..) for the end user. They just need to upload a new document into the appropriate directory. Hope it is useful for someone.
    <?php
    /* -------------------------------------------------------------
    :: Snippet: Returns Directory Contents (Document Manager)
    ----------------------------------------------------------------
        Short Description: 
           Returns Directory Contents (Documents)
    
        Date:
            11/18/2008
    
    ----------------------------------------------------------------
    :: Example Call             
    ----------------------------------------------------------------
    [!documentMan? &Location=`FileName` &docSort=`sort`!]
    
    - A call that describes the directory inside of assets/files/ that you want called in.
    - docSort, use 'sort' for alphanumeric sorting  or 'rsort' for reverse alphanumeric sorting.
    
    ------------------------------------------------------------- */
    
    // Shows all files in the directory (assumes you are in assets/files/),
    
    $newline = '';
    
    $dir = $modx->config['(site_url)'].'assets/files/'.$Location.'/'; // set path to files
    
    $dir_array = array(); // main array - contains all file names in directory
    
    
    // open directory and parse file list 
    
    if (is_dir($dir)) { 
    
       if ($dh = opendir($dir)) { 
    
          // iterate over file list to create full directory array
    
          while (($filename = readdir($dh)) !== false) { 
    
              if (($filename != ".") && ($filename != "..") && ($filename !="WS_FTP.LOG") && (!preg_match('/^.thumb_/', $filename))) { // skip self, parent, and ftp log and thumb prefix if returning images
    		  
    	       $dir_array[] = $filename; // add the filename to the array
              } 
          } 
          closedir($dh);  // close directory 
       }
    
       $docSort($dir_array,SORT_STRING); // sorting.
    
       $n = count($dir_array); // total number of files -- might want this for something
    
        $output = '';
    	
    	$output .= '<h3>'.$Location.','.$n.' Documents</h3>';
    
        $output .= '<ul>';
    
        foreach ($dir_array as $value) {  // iterate through array of filenames.
    
             $output .= '<li><a href="'.$dir.''.$value.'">'.$value.'</a></li>';
    
        } 
    
        $output .='</ul>';
        
    }
    
    return $output;
    ?>
    



  • Everett Reply #2, 3 years, 1 month ago

    Reply
    Thanks for sharing!


  • cbaone Reply #3, 3 years, 1 month ago

    Reply
    If I understand correctly this could be modified to create a list of documents for download too, right? I'm currently integrating FileDownloadPE to allow users access to a document download page where there will be several PDFs available for download. Could a modified version of this be used to list the documents in an unordered list?


  • Pleth Reply #4, 3 years, 1 month ago

    Reply
    I believe the answer is yes. I use it to return the contents of directories for list of PDFs or lists of images. It just takes the weight off of having to create the code by hand. As far as I can tell it can return the contents of any directory.
    The code above creates an unordered list, but I have also modified it to populate lists of images in javascript photogalleries like jCarousel. That way the end user (or developer) only has to upload the new images into a directory and the rest is taken care of by the snippet.
    I created a post here about it if it helps: http://www.gregorysmart.com/2008/11/18/documentman-modx-document-management-snippet/


  • cbaone Reply #5, 3 years, 1 month ago

    Reply
    Great, thanks! I'll take a look at the link.


  • Darkelve Reply #6, 2 years, 11 months ago

    Reply
    What if I want to 'nest'... I want each new subfolder to be a new nested list, with the name of the subfolder as the 'heading'.





  • Pleth Reply #7, 2 years, 11 months ago

    Reply
    I am using this in several places to call in multiple directories. Now, I am not a PHP programmer and I know some of this could be improved by a good PHP person with a more elegant approach, but so far this has worked great for me. The code below is an example I use in combination with a javascript photo gallery. Each snippet call builds a list of documents (images) that are then associated with a link in the unordered list. By only having the snippet build the lists I feel I have retained a lot of flexibility. Each of the &Location calls is just a directory in assets/images/. I don't have to code my lists by hand anymore, just add the new items to one of the directories. I am not sure if this helps with the "nested" portion of your question, but I hope it helps.

    <ul>
    [!documentMan? &Location=`gallery-022709` &gNumb=`4` &docSort=`sort`!]
    <li><a href="javascript:void(viewer4.show(0))">February 21, 2009 ADDY Awards</a></li>
    <!-- divider -->
    [!documentMan? &Location=`gallery-010809` &gNumb=`3` &docSort=`sort`!]
    <li><a href="javascript:void(viewer3.show(0))">January 1, 2009 Speaker Luncheon</a></li>
    <!-- divider -->
    [!documentMan? &Location=`gallery-110608` &gNumb=`2` &docSort=`sort`!]
    <li><a href="javascript:void(viewer2.show(0))">November 6, 2008 Speaker Luncheon</a></li>
    <!-- divider -->
    [!documentMan? &Location=`gallery-090408` &gNumb=`1` &docSort=`sort`!]
    <li><a href="javascript:void(viewer1.show(0))">September 4, 2008 Speaker Luncheon</a></li>
    <!-- divider -->
    
    </ul>
    

    Here are a couple of posts that might help too.
    http://www.gregorysmart.com/2009/02/27/modx-documentman-and-jquery-portfolio/
    http://www.gregorysmart.com/2008/11/18/documentman-modx-document-management-snippet/


  • Darkelve Reply #8, 2 years, 11 months ago

    Reply
    Pleth, your suggestion is very useful, but what I want is to somehow 'recursively' list all documents... in a way that I can have a 'main' folder that has documents in it -or is empty- and subfolders that have documetns in them -or are empty. Then it would show the files if there are any, and any files in the subfolders as nested lists. Also it would be nice if one could specify the maximum 'depth' the list should be.

    I'd try it myself, but my knowledge of PHP is at 'newbie' level... x)

    So in the following example, with the folder structure in the quote and a DocMan/snippet call, it would output the code for the list below.

    Main folder: Publications
    Subfolders: Publications->Newspapers; Publications->Magazines
    Subfolder lvl.2: Publications->Magazines->SpecialEditions

    <h2>Publications</h2>
    <ul>
    <li>Newspapers
       <ul>
       <li><a href="gg-1.pdf">The grand gazette Issue 1</a></li>
       <li><a href="gg-2.pdf">The grand gazette Issue 2</a></li>
       <li><a href="gg-3.pdf">The grand gazette Issue 3</a></li>
       </ul>
    </li>
    
    <li>Magazines
       <ul>
       <li><a href="bm-1.pdf">Best Mag In The World Issue 1</a></li>
       <li><a href="bm-2.pdf">Best Mag In The World Issue 2</a></li>
       
       <li>Special Editions
        <ul><li><a href="bm-spsp.pdf">Best Mag In The World Spring Special</li></ul>
        </li>
       </ul>
    
    
    </li>
    
    </ul>
    




  • pepebe Reply #9, 2 years, 3 months ago

    Reply
    I have found this topic on friday and during the weekend I have started to create a similar snippet offering additional features.

    http://modxcms.com/forums/index.php/topic,40853.msg246401.html#msg246401

    It's "a snippet recursively listing all included directories and files within a specific directory."

    Features:
    It will create a complete directory tree using a nested list.
    You can control which file extensions are shown or not and also filter files containing a specific string.

    The snippet is still work in progress, but the basic functionality is already there.

    Regards,

    pepebe