Forums / Developer / Getting selected Enum elements in php.

Getting selected Enum elements in php.

Author Message

Dave Dykshoorn

Thursday 21 August 2003 2:07:03 pm

There has to be documentation on this somewhere but I can't find any so here it goes.

I have modified the User class and added an enum attribute to it.
(Multiple choice, option style, 2 elements with values)

Now my question is how can I retrieve the selected elements from the enum for the current user?

I've gotten as far as this:

$user = &eZUser::currentUser();
$userObject = &$user->attribute( "contentobject" );
$datamap = &$userObject->attribute ( "data_map" );

foreach ($datamap as $key => $value)
{
if ($key == 'my_enum')
{
$contentClassAttribute = $value->attribute ( 'contentclass_attribute' );
$contentClassAttributeID = $contentClassAttribute->attribute('id' );
$contentClassAttributeVersion = $contentClassAttribute->attribute('version' );
$array_enumValue =
eZEnumValue::fetchAllElements( $contentClassAttributeID, $contentClassAttributeVersion );
}
}

(adapted from developer/ez_publish_3/documentation/development/importing_attribute_data)
I Know that this doesn't work because fetchAllElements fetches all elements (amazing). Is there a function that fetches selected elements from a Enum? Or am I approaching this all wrong?

I'm stumped, and I'm sure there's an easy way to do this, if there is anyone that could direct me to the appropriate documentation or give me a little guidance I would be most appreciative.

Thanks in advance!

Dave

Dave Dykshoorn

Thursday 21 August 2003 3:01:56 pm

I figured it out.

$user = &eZUser::currentUser();
$userObject = &$user->attribute( "contentobject" );
$datamap = &$userObject->attribute ( "data_map" );

foreach ($datamap as $key => $value)
{
if ($key == 'my_enum')
{
$enumObjectContent = $value->attribute('content');
$SelectedEnums = $enumObjectContent->ObjectEnumerations;
}
}

$SelectedEnums contains all of the enums that were selected.

print_r is a personal friend of mine. :)

Jerry Jalava

Thursday 21 August 2003 5:44:27 pm

Hi Dave,

Thanks for sharing this important piece of information. :)
I would just like to ask how do you use the $SelectedEnums variable?
Do you somehow parse the EnumValue to a variable?
How could this be done?

Thanks,
Jerry

Jerry Jalava

Thursday 21 August 2003 5:54:49 pm

Figured it out...

I used this kind of code:

$Max = count( $SelectedEnums );
$Max = $Max-1;
foreach ($SelectedEnums as $Ekey => $Evalue)
{
if ($Ekey <= $Max)
{
$EnumArray = get_object_vars( $Evalue );
$EnumValue[$Ekey] = $EnumArray["EnumValue"];
}
}

Just if someone needs same kind of implementation...

Regards,
Jerry

Dave Dykshoorn

Friday 22 August 2003 10:35:20 am

I'm glad it was useful. This is how I used $selectedEnums

I needed to figure out the values that were selected (EnumValue) and then put them into a comma seperated list. Here's how I did it, it's pretty basic.

$myList = "";
foreach( $SelectedEnums as $SelectedEnum )
{
if ($myList == "" )
$myList = $SelectedEnum->EnumValue;
else
$myList= $myList . ',' . $SelectedEnum->EnumValue;
}