Dynamic button based on directory content

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
newagekat
Forum Members
Forum Members
Posts: 161
Joined: Mon Feb 06, 2006 6:06 pm

Dynamic button based on directory content

Post by newagekat »

Hi:

I'm not sure if this is the right place to post this question, but it seems a little advanced for the General Discussion.

I'm using CMS1.4.1 on linux server running PHP 5 as CGI.  I'm also using the friendly URL option.

Scenario:
This website showcases lyrics.  Some of the lyrics have audio. The audio files will reside in uploads/audio/ directory. I don’t have the client’s audio yet, so I created a test page called heatwave http://obadiahlyrics.com/lyrics/heatwave.php and uploaded Heatwave.mp3 to uploads/audio.   The image link does not show up.  I know the file can play cause it shows up on my playlist here http://obadiahlyrics.com/audio.php

What I wanted the code snippet to do is show an audio image that links to the mp3 if the mp3 file exists or show nothing if it does not.  The .mp3 needs to replace the image when the image is clicked and play automatically.

I created a user defined tag and placed the following code in it:

Code: Select all

	
	/* 
		gets the full path to the file and strips out everything but the filename 
		In this case has to be .php files
		@return - returns just the filename without the extension.
	*/
	
	
	function getMyFileName() {
		$path = $_SERVER['PHP_SELF'];
		$bname = basename($path,'.php');
		return $bname;
	}
	
	
	
	function formatForFindFile($fn) {
		$xp = explode("-",$fn); // turn php filename into an array of words  eg: dot-ball => array('dot','ball');
		
		foreach($xp as $k => $v) {
			$v2 = strtolower($v); // make sure everything is lowercase first
			$xp[$k] = ucfirst($v2); // uppercase the first letter for each word and assign it back into the array
		}
		
		$newFN = implode(" ",$xp); // turn array back into string with spaces: eg: "Dot Ball"
		
		return $newFN;
	}


	/*
		gets the base filename of this file and appends .mp3 to it and returns to the caller
	*/
	function getMp3FileLink() {
		$fileName = getMyFileName();
		$fileName = formatForFindFile($fileName);
		$fileName .= ".mp3";
		
		$serverPathToFile = "../uploads/audio";
		$browserPathToFile = "/uploads/audio"; 
		
		if(is_file($serverPathToFile."/".$fileName)) {
			$output = '<a href="'.$browserPathToFile.'/'.$fileName.'" title="Play this audio"><img src="/uploads/images/playAudio.gif"></a>';
			return $output;
		} else {
			return "Hello";
		}
	}
	
$syn = getMp3FileLink();

		
	/* smarty stuff */
	$smarty->assign('mp3Link', $syn); 

In my template, I have included the following:

Code: Select all

<div class="audiobtn">{$mp3Link}</div>
Currently, the Hello message comes up, but the audio does not, even though I have it uploaded. 

I know this is probably a file path issue, but I can't seem to figure it out.  I'm hoping a CMS php guru can make a detailed suggestion?  I'm not a coder, i got the script for a colleague, but he has no experience with CMSMS.

Much appreciate any efforts.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Dynamic button based on directory content

Post by Nullig »

Have you tried variations on this:

$serverPathToFile = "../uploads/audio";

Like putting the full path or getting rid of the "..".

Nullig
newagekat
Forum Members
Forum Members
Posts: 161
Joined: Mon Feb 06, 2006 6:06 pm

Re: Dynamic button based on directory content

Post by newagekat »

Nullig wrote: Have you tried variations on this:

$serverPathToFile = "../uploads/audio";

Like putting the full path or getting rid of the "..".

Nullig
Thanks for responding.  Yes.  I've tried these combos:
$serverPathToFile = "/uploads/audio";
$serverPathToFile = "uploads/audio";
$serverPathToFile = "http://obadiahlyrics.com/uploads/audio";
$serverPathToFile = "http://www.obadiahlyrics.com/uploads/audio";

still the same result.

I know that cmsms converts a parent into a directory when no directory actually exists.  could this be the reason?  or maybe an .htacess issue?
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Dynamic button based on directory content

Post by Nullig »

I'm talking about the FULL path, the same as the $config['root_path'] in your config.php.

Nullig
newagekat
Forum Members
Forum Members
Posts: 161
Joined: Mon Feb 06, 2006 6:06 pm

Re: Dynamic button based on directory content

Post by newagekat »

Nullig wrote: I'm talking about the FULL path, the same as the $config['root_path'] in your config.php.

Nullig
Yes, I just did  and still the same result. 
the code for this is in a user defined tag called {definemp3} and I placed that tag in the tempate above the doctype.  I'm calling the variable using {$mp3Link} variable created in the code.  Maybe the smarty part of the code needs to be removed?

Alternatively, could this be a permission issue?  I went through hell with the dynamic playlist only to find out is was a permission issue.

Really appreciate your assistance.
Last edited by newagekat on Thu Dec 11, 2008 7:51 pm, edited 1 time in total.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Dynamic button based on directory content

Post by Nullig »

This isn't really a CMSMS issue - it's a php coding one.

It seems to me that you're making it overly complicated. You just want to see if there's a file there, create a link if there is and display an icon for the link.

How are you planning to enter the list of lyrics available?

Nullig
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Dynamic button based on directory content

Post by Nullig »

Here is a UDT that I use to read a directory and list the files with links. I called it fileDoc.

Code: Select all

$directory=$config[root_path] . 'uploads/doc/';
$handler = opendir($directory);

$stringToWrite='<p>';

while ($file = readdir($handler)) {
   if ($file != '.' && $file != '..') {
      $extension = substr($file, -3, 3);
      $newfile = str_replace("_", " ", $file);
      $stringToWrite.='<a class="'. $extension . '" href="' . $directory . $file .'">' .  $newfile . '</a><br />';
   }
}

closedir($handler);

$stringToWrite.='</p>';

echo $stringToWrite;

Nullig
newagekat
Forum Members
Forum Members
Posts: 161
Joined: Mon Feb 06, 2006 6:06 pm

Re: Dynamic button based on directory content

Post by newagekat »

Nullig wrote: Here is a UDT that I use to read a directory and list the files with links. I called it fileDoc.

Code: Select all

$directory=$config[root_path] . 'uploads/doc/';
$handler = opendir($directory);

$stringToWrite='<p>';

while ($file = readdir($handler)) {
   if ($file != '.' && $file != '..') {
      $extension = substr($file, -3, 3);
      $newfile = str_replace("_", " ", $file);
      $stringToWrite.='<a class="'. $extension . '" href="' . $directory . $file .'">' .  $newfile . '</a><br />';
   }
}

closedir($handler);

$stringToWrite.='</p>';

echo $stringToWrite;

Nullig
Hi:

I just added your script and modified it in as much as I know how to this:

Code: Select all

$directory=$config[root_path] . 'uploads/audio/';
$handler = opendir($directory);

$stringToWrite='<a href="';

while ($file = readdir($handler)) {
   if ($file != '.' && $file != '..') {
      $extension = substr($file, -3, 3);
      $newfile = str_replace("_", " ", $file);
      $stringToWrite.='<a class="'. $extension . '" href="' . $directory . $file .'">' .  $newfile . '</a><br />';
   }
}

closedir($handler);

$stringToWrite.='" target="_self"><img src="uploads/images/playAudio.gif" alt="Play Audio"></a>';

echo $stringToWrite;

I'm calling it like this

Code: Select all

<div class="audiobtn">{listmp3}</div>
I do have a simpler script which I placed in a new user defined tag:

Code: Select all

// get the directory path
$dir = dirname(__FILE__) . '/';
// get the PHP filename
$mp3 = basename(__FILE__);

// strip the .php extension off the filename, add .mp3
$mp3 = substr($mp3, 0, strrpos($mp3, '.')) . '.mp3';
// check to see if the mp3 file exists
if (file_exists($dir . $mp3)) {
echo $mp3;
} else {
echo 'audio.php';
}
I call it in the template like this:

Code: Select all

<div class="audiobtn"><a href="{mp3link}"><img src="uploads/images/playAudio.gif" alt="Play Audio"></a>
</div>
the problems with this code is that the image shows up even if the audio is not available and then redirects to the audio page.

btw, I just realized you're in White Rock.  I'm in North Van.  Would you object to my calling you?
newagekat
Forum Members
Forum Members
Posts: 161
Joined: Mon Feb 06, 2006 6:06 pm

Re: Dynamic button based on directory content

Post by newagekat »

I think I have an idea, but I may need the assistant of a brainiac.

if I get the client to add a tag whenever audio exists for the page, that would solve the dynamic issues. the tag would look something like this:

Code: Select all

<div class="audiobtn"><a href="upoads/audio/{title}.mp3" target="_blank"><img src="uploads/images/playAudio.gif" alt="Play Audio"></a>
</div>
unfortunately, the target needs to be the image it is replacing or a small popup.  Does anyone have any ideas?  i feel like i'm going round and round and now event the basics are a stretch.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Dynamic button based on directory content

Post by Nullig »

If you're comfortable playing around in code, there is a small module I made called "LinkMgr", which you could probably hack to make an easier input for your client to use for this.

Nullig
newagekat
Forum Members
Forum Members
Posts: 161
Joined: Mon Feb 06, 2006 6:06 pm

Re: Dynamic button based on directory content

Post by newagekat »

Nullig wrote: If you're comfortable playing around in code, there is a small module I made called "LinkMgr", which you could probably hack to make an easier input for your client to use for this.

Nullig
Thank you, but as you've noticed, I'm not that great at code.  Everything else, sure, but code is like looking at an alien language.  I can barely grasp English, most day.

If you or anyone out there can spare the time, I would really appreciate making this as simple for the client as possible. 

Nullig would you be averse to my emailing, calling, or otherwise contacting you by IM?
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Dynamic button based on directory content

Post by Nullig »

Sent you a PM.
Post Reply

Return to “Developers Discussion”