Question about Filename field translation

8 posts by 3 authors in: Forums > CMS Builder
Last Post: September 14, 2012   (RSS)

By Shore - September 12, 2012

Hello,

When CMSB transforms the Filename field to the URL slug, does it get saved to the database somewhere so that I could query for the string?

If not, is there a way to build/decrypt the Filename-translation so that I could lookup a record based off the "Title" field for example?

In addition to the ability to lookup by the 'num' at the end of the URL, I'd like to be able to lookup records based on the URL value that CMSB generates from the Filename field.

Thanks!

Re: [cmscott] Question about Filename field translation

By Jason - September 13, 2012

Hi,

The value of the "filename fields" field isn't stored in the database but in the schema file of that given section. There really isn't an easy way of doing this, as a section could have multiple values in filename fields and there isn't a way of knowing which one was used unless you're looking at the values of an individual record.

That being said, if you wanted to base links off of a title instead of a num, you could build up your links manually.

for example:

<a href = "articleDetail.php?title=<?php echo urlencode($record['title']);?>"> <?php echo $record['title'];?> </a>

Then in your detail page, your where clause could be based off of the value of $_REQUEST['title']

If you require this type of searching while still using the value of the _link field, we can certainly look at creating some custom code for you to do that. If you're interested in this, please email consulting@interactivetools.com and we can go over some options.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [cmscott] Question about Filename field translation

By Dave - September 13, 2012

Hi cmscott,

There's a function in /lib/viewer_functions.php called getFilenameFieldValue() that generates the 'slug', but the problem without using the record number is there is nothing to prevent duplicates, so if you have two records:

Hello-World-1
Hello-World-2

Based on the slug without a record number you couldn't get to the original record. Will that be a problem?

Or to approach it another way, if you can let me know what you're trying to accomplish I might be able to think of some other suggestions.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Question about Filename field translation

By Shore - September 13, 2012

Hi Dave,

I'm looking up the record with both the num and title (trying to at least) fields. So there can still be duplicate titles in the table. I should have mentioned that.

Looking up both fields prevents duplicate pages, otherwise you can write anything between the ? and the num and the page resolves to the same content based on the num. I'm also enforcing lowercase. Throwing a 404 error page if the record isn't found.

Hello-World-1 (200)
Goodbye-World-1 (200)
heLlo-wOrlD-1 (200)

Should be:

Hello-World-1 (200)
Goodbye-World-1 (404)
heLlo-wOrlD-1 (404)

IDEALLY, I'd like to add another field to the table that gets auto-populated with the slug - where in my case the slug is the Filename field + num. I'm not too concerned with being able to edit the slug, although that would be nice - kind of like what Wordpress does with their titles.

That way I can easily lookup just the 'slug' field instead of both the 'title' and 'num' fields with no need to worry about de-normalizing the 'title' field for special characters.

Let's say 'title' is my Filename field...

num: 100
title: Get 20% Off Product ABC
slug: get-20-off-product-abc-100

Request comes in, query the db using the slug and return the content or throw a 404 if record isn't found.

Basically, instead of a _link field, I want a _slug field as I described.

I'm also using mod_rewrite to rewrite the viewer urls, but that doesn't impact anything here. Also note I already made slight adjustments to the _link in viewer_functions.php such as strtolower().

/products/product.php?the-slug gets rewritten to /products/the-slug

OTHERWISE, I need a way to de-normalize the 'title' field based on the Filename field.

Hope that makes sense, took a while to write it.

Thanks!

Re: [cmscott] Question about Filename field translation

By Dave - September 14, 2012

Hi cmscott,

Ok, I think I got it.

Just a heads up that we are working on a non-free "Permalinks" plugin that we'll be releasing in the next few weeks that does a lot of this. But pending the actual release date, price, feature list of that, etc I'd carry on with what you are doing.

I've attached a sample plugin I quickly wrote which checks "on save" in the CMS if a section has a field called "slug" and if it does it updates it with that value. If you want to give that a try just upload it to /plugins/, activate it, add a field called "slug" and save a record. (Note make sure you're running 2.17).

The code uses our internal utility functions but should be pretty readable so you can use it for another purpose if needed. It is as follows:

<?php


addAction('record_postsave', 'autoFillSlug', null, 4);

// after a record is saved, run this code
function autoFillSlug($tableName, $isNewRecord, $oldRecord, $recordNum) {
if (!array_key_exists('slug', $_REQUEST)) { return; }

// get section schema and record
$schema = loadSchema($tableName);
$record = mysql_get($tableName, $recordNum);

// generate the slug
require_once SCRIPT_DIR . "/lib/viewer_functions.php";
$slug = getFilenameFieldValue($record, @$schema['_filenameFields']);
$slug .= $recordNum;

// update the record
mysql_update($tableName, $recordNum, null, array('slug' => $slug));
}

?>


Obviously, I can't help too much with custom code, but I hope this points you in the right direction. Let me know if that works for you and if you need anything else. Cheers!
Dave Edis - Senior Developer
interactivetools.com
Attachments:

autofillslug.php 1K

Re: [Dave] Question about Filename field translation

By Shore - September 14, 2012

Hi Dave,

Looks like that could work, I'll test it out later today and let you know how I make out.

Very helpful :)

Thanks!

Re: [cmscott] Question about Filename field translation

By Shore - September 14, 2012

Hi Dave,

Works like a charm! Thank you sir.

Cheers
[:)]