I have a request if someone could help out.
I would like to have a dirify plug-in / add-on - none is available and the normal suggestion is to build one yourself. I had a look at the Plug-in build documentation and it is way beyond my comprehension/skill levels - I can do design and html but I am as useful as cheese is to house building when it comes to real programming.
Thing is I do actually have the code needed - this is a conversion of the MT dirify function and Adam Kalsey produced a php version way back in 2004 - I thought that it woudl be a good starting point.
I had a look at how some other plug-ins are structured and tried to build a dirify plug in the same way but when I upload the file into EE the Plug in manager page either does not recognise it or worse fails to load the plug-in management page.
This is the code I have so far:
<?php
/*
=====================================================
Bundled together as a plug-in By Simon Cox for his own use but the code is from Adam Kalsey - http://kalsey.com/2004/07/dirify_in_php/ and the copyright remains with him not me.
=====================================================
File: pi.dirify.php
-----------------------------------------------------
Purpose: to strip output of spaces and change upper case charcters to lowercase to enable data to be used as a url.
e.g. Custom Field: SRN has data ABC 01 001.
Dirify changes that into abc01001 so that it can be used in a text field and also in a url: src="/images/{SRN}.jpg" giving an out put of src="/images/abc01001.jpg" - useful if you have a large cataloge of products and images to go with them as the SRN number can be entered and the appropriate image is shown.
=====================================================
*/
$plugin_info = array(
'pi_name' => 'Dirify',
'pi_version' => '1.0',
'pi_author' => 'Simon Cox',
'pi_author_url' => 'http://www.simoncox',
'pi_description' => 'Dirification',
'pi_usage' => Dirify::usage()
);
class Dirify {
var $return_data;
/** ----------------------------------------
/** Dirify
/** ----------------------------------------*/
function dirify($s) {
$s = convert_high_ascii($s); ## convert high-ASCII chars to 7bit.
$s = strtolower($s); ## lower-case.
$s = strip_tags($s); ## remove HTML tags.
$s = preg_replace('!&[^;\s]+;!','',$s); ## remove HTML entities.
$s = preg_replace('![^\w\s]!','',$s); ## remove non-word/space chars.
$s = preg_replace('!\s+!','_',$s); ## change space chars to underscores.
return $s;
}
/* END */
// ----------------------------------------
// Plugin Usage
// ----------------------------------------
function convert_high_ascii($s) {
$HighASCII = array(
"!\xc0!" => 'A', # A`
"!\xe0!" => 'a', # a`
"!\xc1!" => 'A', # A'
"!\xe1!" => 'a', # a'
"!\xc2!" => 'A', # A^
"!\xe2!" => 'a', # a^
"!\xc4!" => 'Ae', # A:
"!\xe4!" => 'ae', # a:
"!\xc3!" => 'A', # A~
"!\xe3!" => 'a', # a~
"!\xc8!" => 'E', # E`
"!\xe8!" => 'e', # e`
"!\xc9!" => 'E', # E'
"!\xe9!" => 'e', # e'
"!\xca!" => 'E', # E^
"!\xea!" => 'e', # e^
"!\xcb!" => 'Ee', # E:
"!\xeb!" => 'ee', # e:
"!\xcc!" => 'I', # I`
"!\xec!" => 'i', # i`
"!\xcd!" => 'I', # I'
"!\xed!" => 'i', # i'
"!\xce!" => 'I', # I^
"!\xee!" => 'i', # i^
"!\xcf!" => 'Ie', # I:
"!\xef!" => 'ie', # i:
"!\xd2!" => 'O', # O`
"!\xf2!" => 'o', # o`
"!\xd3!" => 'O', # O'
"!\xf3!" => 'o', # o'
"!\xd4!" => 'O', # O^
"!\xf4!" => 'o', # o^
"!\xd6!" => 'Oe', # O:
"!\xf6!" => 'oe', # o:
"!\xd5!" => 'O', # O~
"!\xf5!" => 'o', # o~
"!\xd8!" => 'Oe', # O/
"!\xf8!" => 'oe', # o/
"!\xd9!" => 'U', # U`
"!\xf9!" => 'u', # u`
"!\xda!" => 'U', # U'
"!\xfa!" => 'u', # u'
"!\xdb!" => 'U', # U^
"!\xfb!" => 'u', # u^
"!\xdc!" => 'Ue', # U:
"!\xfc!" => 'ue', # u:
"!\xc7!" => 'C', # ,C
"!\xe7!" => 'c', # ,c
"!\xd1!" => 'N', # N~
"!\xf1!" => 'n', # n~
"!\xdf!" => 'ss'
);
$find = array_keys($HighASCII);
$replace = array_values($HighASCII);
$s = preg_replace($find,$replace,$s);
return $s;
}
?>
I am sure it probably transgresses some of the EE code standards that I read but to be honest I don’t understand them. Help would be appreciated as I am sure I would not be the only one who would like this.
TIA Simon
Thanks Lisa - this helps a lot. However I use an Apache server and the names of all the images are in lower case and the SRN product numbers are in upper case. I could go through and rename them with the appropriate upper case characters but I shudder at the thought of the RSI…
Ingmar’s right that ExpressionEngine’s url title creation method can handle this for you, with a bit more finesse than the PHP above. The only thing you need to add for your specific usage is a strtolower(). Follow the Plugin API instructions for creating your plugin file, but for the function use:
function Plugin_name($str = '')
{
global $REGX;
$this->return_data = strtolower($REGX->create_url_title($str));
}
Thanks Ingmar and yes that’s exactly what I would like to do but the url_title conversion takes the current URL title and converts that - I would like to do the same thing but to a custom field data instead - I cannot see how I could use the url_title to do this.
I did not necessarily mean directly, but it should be possible to reuse EE’s code for that purpose.
EDIT: I see Derek has shown you a very elegant solution along these lines.
I have followed the Plug-In building instructions - and have built the file. It now shows up the the control panel so that s good progress. On the template though I am using the plug-in like this:
/images/{exp:dirify}{srn}{/exp:dirify}_215.jpg</a>
but the source code gives me this back:
/images/_215.jpg</a>
As you can see there is no srn number being returned.
I think I have implemented Derek’s code in the plug-in correctly:
class Dirify {
var $return_data;
function Dirify($str = '')
{
global $REGX;
$this->return_data = strtolower($REGX->create_url_title($str));
}
// ----------------------------------------
// Plugin Usage
// ----------------------------------------
// This function describes how the plugin is used.
// Make sure and use output buffering
function usage()
{
ob_start();
?>
Dirify allows you to change the characters of a custom field and use them in a url - as url_title does. This will allow one custom field to be used both for an SRN and the file name for a products image.
{exp:dirify} {your_custom_field} {/exp:dirify}
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
// END
}
Just below this in the template I am running the same anchor code, but without the dirify, as a check - thats OK - its picking up the correct image values to display the image.
Sorry, Simon, The code I used was just to demonstrate the use of the create_url_title() function, not as complete code. You will still need to use the $TMPL->tagdata to use in a template, otherwise the plugin will only function as a field formatting plugin. The complete function would be:
function Dirify($str = '')
{
global $REGX, $TMPL;
if ($str == '')
{
$str = $TMPL->tagdata;
}
$this->return_data = strtolower($REGX->create_url_title($str));
}
Well, the whole plugin really can consist of three or four lines or so. Let’s see:
<?php $plugin_info = array ('pi_name' => 'Dirify', 'pi_version' => '1.0');
class Dirify { function Dirify() { global $REGX, $TMPL;
$this->return_data = strtolower($REGX->create_url_title($TMPL->fetch_param("input"))); }} ?>
Makes sense? Flesh out as desired. Needs a parameter named “input”, use like so:
{exp:weblog:entries}
{exp:dirify input="{title}"}
{/exp:weblog:entries}
or any other way to provide “input”.
EDIT: If you use Dereks approach, there’s no need for a paramter, but you need to open and close your plugin. Your choice.
Brilliant! Thanks eversomuch guys. I have it working and I opted for Ingmar’s input variation in the end. It is substituting spaces for underscores (as expected because that’s exactly what url_title does) but I am quite happy about that as I have a rename.exe utility somewhere that will allow me to rename the hundreds of images on the site I am rebuilding. This plug-in will make it much easier for the business users to input hundreds of products to the site - one less field to input into.
If anyone else needs this I will fill the forms and send it in but I am not going to claim any sort of authorship on it!
Again many thanks.
I had a couple of minutes spare today and installed the Replace plug-in and then wrapped it around the dirify call giving me this:
img src="/images/{exp:replace find="_"}{exp:dirify input="{product_number}"}{/exp:replace}_215.jpg" alt="{title}" width="215" height="215" >
And now I don’t even have to change any of the image names!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.