Dec 04, 2008, 01:16 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  
Pages: [1]   Go Down
  Print  
Author Topic: Snippet return for directory contents  (Read 2279 times)
0 Members and 1 Guest are viewing this topic.
pleth
Jr. Member
*
Posts: 38



WWW
« on: May 14, 2008, 08:51 PM »

Lately I have used jCarousel and Modx for a site with a photo gallery and it works quite well. Since it works with a simple unordered list on the gallery page I got to thinking that it would be nice to have a snippet that could return the contents of an assets directory in that list item format.

It seems this would create a gallery within Modx that the CMS user could manage by simply uploading to/deleting from a specified directory. Has anyone done anything like this?
Logged

Greg
BobRay
Coding Team
*
Posts: 1,810



WWW
« Reply #1 on: May 14, 2008, 10:47 PM »

I've done something similar (if I'm understanding you).  It's a fairly simple process.

I've adapted some code I had laying around. This is very quick and dirty code and untested, but should get you started.

Code:
<?php

// Shows all files in the directory,

$newline = "<br/>";

$dir = $modx->config['base_path'].'assets/yourfiles'; // 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")) { // skip self, parent, and ftp log

   $dir_array[] = $filename; // add the filename to the array
          
}
      }
      
closedir($dh);  // close directory
   
}

   
natsort($dir_array); // sort in ascending order -- delete if you don't need them sorted.
   // $dir_array = array_reverse($dir_array, false); // reverse array (descending) if needed.

   
$n = count($dir_array); // total number of files -- might want this for something

    
$output = "";

    
$output .= "<ul>"

    
foreach ($dir_array as $value) {  // iterate through array of filenames.

         
$output .= '<li>'.$value.'</li>';

    }

    
$output .='</ul>';
    
}

return
$output;

?>

BTW, you might look at the MaxiGallery snippet if you haven't already. It does all this for you and makes for much easier and more full-featured gallery management by the users (e.g. picture order, uploading and deleting, titles, descriptions, dropshadows, masks, slideshow, etc).

Hope this helps,

Bob

Logged

MODx info for newbies: http://bobsguides.com/MODx.html
powersitedesign
Jr. Member
*
Posts: 1


« Reply #2 on: May 15, 2008, 11:10 AM »

Hey BobRay -

I am working w/ Greg on this and we think we almost have it.  The only thing we want to do now is to exclude the .thumb_ file prefix so the thumbnails don't show, do you have any suggestions for that?

We tried this,

if (($filename != ".") && ($filename != "..") && ($filename !=
"WS_FTP.LOG") && (!preg_match('/^\.*thumb_$/', $filename ) ) { // skip
self, parent, and ftp log and thumb prefix

and it didn't seem to exclude them,
thanks for your help in advance.

Cotton Rohrscheib, Partner
Pleth Networks, LLC
http://www.pleth.com
Logged
ganeshXL
Testers
*
Posts: 1,549



WWW
« Reply #3 on: May 15, 2008, 12:49 PM »

using strstr or stristr would be easier... http://ch2.php.net/stristr
Logged

smashingred
Marketing & Design Team
*
Posts: 1,124


HTML, CSS, Marketing, Design, and more...


WWW
« Reply #4 on: May 15, 2008, 02:16 PM »

I think that @pleth has it working now. He found me on twitter and I gave him the correct pattern to add to BobRay's post.

I changed the following:
Code:
if (($filename != ".") && ($filename != "..") && ($filename != "WS_FTP.LOG")) { // skip self, parent, and ftp log
to
Code:
if (($filename != ".") && ($filename != "..") && ($filename != "WS_FTP.LOG") && (!preg_match('/^.thumb_/', $filename))) { // skip self, parent, and ftp log
And it works.

The pattern just checks to see if the filename starts with .thumb_ and if it does skip it.
Logged

Jay Gilmore
SmashingRed Web & Marketing
Follow me on Twitter
Configs
Local: MAMP 1.7.2 Mac OS 10.5, Apache 2.0.59, PHP 5.2.6, MySQL 5.0.41
Remote: Linux, Apache 1.3.41, PHP 5.2.5, MySQL 5.0.51a-community
MODx For Newbies
About MODx (read For Optimal Results)
Installation and Configuration
Beginners Guide to MODx
About MODx and More
What's Coming Next
Just because it's possible to build a dropdown menu doesn't mean you should.
pleth
Jr. Member
*
Posts: 38



WWW
« Reply #5 on: May 15, 2008, 02:36 PM »

Thanks guys, we appreciate the help.
Logged

Greg
pleth
Jr. Member
*
Posts: 38



WWW
« Reply #6 on: May 16, 2008, 11:07 AM »

Just thought I would post this to let you know where we ended up on this. I realize there are other solutions available and am planning on diving into MaxiGallery next but this solution proved sufficient as well as a good exercise. Maybe this will be useful to someone as I think it could be applied to other types of documents. Once again, thanks.

Code:
<?php
/* -------------------------------------------------------------
:: Snippet: Returns Directory Contents
----------------------------------------------------------------
    Short Description:
       Returns Directory Contents, excludes self, parent, ftp log and thumbnail with prefix (.thumb_)

    Date:
        05/16/2008

----------------------------------------------------------------
:: Example Call             
----------------------------------------------------------------
[!directory? &Location=`yourdirectory`!]
    A call that describes the directory inside of assets/images/ that you want called in.

------------------------------------------------------------- */

// Shows all files in the directory (assumes you are in assets/images/),

$newline = '';

$dir = $modx->config['(site_url)'].'assets/images/'.$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
  
       $dir_array[] = $filename; // add the filename to the array
          
}
      }
      
closedir($dh);  // close directory
   
}

   
natsort($dir_array); // sort in ascending order -- delete if you don't need them sorted.
   // $dir_array = array_reverse($dir_array, false); // reverse array (descending) if needed.

   
$n = count($dir_array); // total number of files -- might want this for something

    
$output = '';

    
$output .= '<ul id="mycarousel" class="jcarousel-skin-tango">';

    foreach (
$dir_array as $value) {  // iterate through array of filenames.

         
$output .= '<li><img src="'.$dir.''.$value.'" /></li>';

    }

    
$output .='</ul>';
    
}

return
$output;
?>

Logged

Greg
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 1.1.4 | SMF © 2005, Simple Machines LLC

Valid XHTML 1.0! Valid CSS!