Jul 04, 2009, 04:53 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Search via SMF or Google: modx forums all of modxcms.com web
  MODxCMS.com   Forums   Help Login Register  
News:Donate to MODx: Donations
Pages: [1]   Go Down
  Print  
Author Topic: Document Manager Snippet  (Read 1322 times)
0 Members and 1 Guest are viewing this topic.
pleth
Member
**
Posts: 86



WWW
« on: Nov 18, 2008, 10:20 AM »

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.
Code:
<?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;
?>

Logged

Greg
Everett
Committed to MODx
*****
Posts: 566



WWW
« Reply #1 on: Jan 01, 2009, 11:53 PM »

Thanks for sharing!
Logged
cbaone
Committed to MODx
*****
Posts: 530


Utah Web Design


WWW
« Reply #2 on: Jan 08, 2009, 02:26 PM »

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?
Logged

The ModX has you...
Utah Web Design
pleth
Member
**
Posts: 86



WWW
« Reply #3 on: Jan 08, 2009, 04:26 PM »

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/
Logged

Greg
cbaone
Committed to MODx
*****
Posts: 530


Utah Web Design


WWW
« Reply #4 on: Jan 09, 2009, 04:08 AM »

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

The ModX has you...
Utah Web Design
Darkelve
Full Member
***
Posts: 119


« Reply #5 on: Mar 06, 2009, 09:29 AM »

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'.



Logged
pleth
Member
**
Posts: 86



WWW
« Reply #6 on: Mar 06, 2009, 09:44 AM »

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.

Code:
<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/
Logged

Greg
Darkelve
Full Member
***
Posts: 119


« Reply #7 on: Mar 09, 2009, 02:22 AM »

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.

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

Code:
<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>

« Last Edit: Mar 09, 2009, 02:53 AM by Darkelve » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP

Copyright © 2005-2008 MODxCMS, All rights reserved. Contact Us
Styles by ziworks.com

Powered by SMF | SMF © 2006-2008, Simple Machines LLC

Valid XHTML 1.0! Valid CSS!