Plugin Idea: Default Field Values Per User

By Perchpole - September 20, 2011

Hello, All...

Whilst working on a community project I've run into an issue that's given me an idea for a useful plugin. I'm not entirely sure if it's possible but here's the scenario...

Under normal circumstances, when user Joe Bloggs adds a record to the system he is able to assign it to a category by means of a drop-down list. Instead of allowing him to choose, however, I want the plugin to assign a value automatically based on his user number.

For example, whenever Joe adds a new record, the category selector would have a pre-defined value of "Book Club" - which Joe cannot change.

Alternatively, instead of assigning a default value, the plugin could simply limit the available categories in the drop-down list.

So, if Joe is a member of the Book Club and the Wine Appreciation Society, only these two options are available to him in the drop-down category selector list.

I think this would be very useful.

Is it possible?

:o)

Perchpole

Re: [Perchpole] Plugin Idea: Default Field Values Per User

By Jason - September 20, 2011

Hi,

Your second option, limiting the values in the drop down, can be done without a plugin by setting your own mysql select statement for the drop down. You would have access to the $CURRENT_USER array, so you'd be able to limit options based on values set in their user accounts record.

The first options, giving a default value behind the scenes, would require a plugin. One option would be to hide the drop down by either making it admin only, or using the show/hide fields pluging (http://www.interactivetools.com/add-ons/detail.php?Show-Hide-Fields-For-Users-1036) then you can have a custom plugin that, when a new record is saved, assigns updates the record with a category value somehow based on the current user.

If you'd like us to create that plugin for you, 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: [Jason] Plugin Idea: Default Field Values Per User

By Perchpole - September 28, 2011

Hi, Jason -

I've followed you advice and put together some code. I feel I'm close to achieving my goal but it isn't working as expected.

I created a new multi-select list in the cms_accounts editor (called "categoryAccess"). This echoes the available $categories. I can then assign the categories on a per user basis.

Next, in the cms_records editor I have set up the $category selector with the following MySQL code:

<?php
$categoryAccess = join( ",", explode( "\t", trim( $CURRENT_USER['categoryAccess'], "\t" ) ) );
?>
SELECT cms_category.num, cms_category.name
FROM cms_category
WHERE cms_category.num
IN ('$categoryAccess')
GROUP BY cms_category.num


In theory only those categories which match the current user's categoryAccess numbers should be returned.

It should work - but it doesn't! For some reason I'm not aware of the $categoryAccess variable is not being passed to the query.

Can you give me some advice, please!

:0)

Perch

Re: [Perchpole] Plugin Idea: Default Field Values Per User

By Jason - September 28, 2011

Hi Perch,

You definitely have the right idea. What you need to do is use <?php ?> tags to echo out your variables into the SQL string.

Try this:

<?php
$categoryAccess = join( ",", explode( "\t", trim( $CURRENT_USER['categoryAccess'], "\t" ) ) );
?>
SELECT num, name
FROM `<?php echo $TABLE_PREFIX;?>category`
WHERE num
IN (<?php echo $categoryAccess;?>)
GROUP BY num


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: [Jason] Plugin Idea: Default Field Values Per User

By Perchpole - September 28, 2011

Way-hey!

Thanks, Jason.

:0)

Perch