Share » Learn » eZ Publish » Fetching User Objects with PHP - part 1

Fetching User Objects with PHP - part 1

Friday 24 September 2010 7:57:12 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Step 3: Extracting user information

Now that we have a few user objects to play with, let's look at how we can use pull out the user information from them.

Information fetched from the eZUser object

You will be able to see from the script output above we can pull out the login name, content object ID and email directly from the eZ User object. Instead of doing this, we will pull them out using the attribute() method. We are doing this because it follows the way objects are accessed within template files and there is less chance of this functionality changing in future releases. It is therefore good practice to do it in this way. We can find out which attributes can be accessed by using the definition() function of a class. For eZUser, we get the following attributes (please note these two functions are available for all eZContentObjects you may want to export):

  • contentobject_id
  • login
  • email
  • password_hash
  • password_hash_type

And here’s the code we can use to extract the information. Please note that since we are currently only pulling out the same user multiple times we will just use one of the array elements for the next code examples. Later we will add code to extract information from multiple users.

$user = $users['name']; //let’s just use one of the user’s we have extracted
// print_r( $user->definition() ); //getting the definition of what attributes we can extract. Uncomment to see for the definition for yourself
// printing out the attributes we can get directly from the eZUser object:
$cli->output( 'Username: ' . $user->attribute( 'login' ) );
$cli->output( 'Email: ' . $user->attribute( 'email' ) );
$cli->output( 'Content Object ID: ' . $user->attribute( 'contentobject_id' ) );
 

Fetching other user fields

Pulling out the other user fields is trickier and is hard to do with the eZUser class. Luckily, the eZContentObject class allows you to pull out the DataMap of an object in an associative array. We can convert an eZUser class into an eZContentObject class really easily using the contentObject() function of the eZUser class. Once we have done this we can then extract the data map:

// printing out other user fields:
$contentObject = $user->attribute( 'contentobject' ); //converting it to a eZContentObject using the attribute method

// extracting the dataMap:
$dataMap = $contentObject->dataMap();

foreach( $dataMap as $key => $value ) //let's start off simply and just print off each value:
{
    $cli->output( "$key: $value->attribute( 'data_text' )" );
}

The script should produce the following output:

displaying user attributes

 

As you can probably see from the example, even when you run the script with the default User class, there are a couple of things to look out for :

 

Correctly displaying images and numbers

Although the majority of the user details are displayed properly, some are not. Before we start let’s have a quick recap of how template logic can be different according to different types of field:

Name: {$node.data_map.name.data_text}
Price: {$node.data_map.price.data_float}
In Stock: {$node.data_map.no_in_stock.data_int}

This is also the case when you extract the information in PHP. The example code we have been using is simplified in that we have assumed every item can be returned as a string (or DataText as it is known in eZ.) Depending on the type of your variable you will want to display, you need to display it in the same way you would as if you were in the template.

For basic types (for instance strings and numbers, we can make use of the toString() method of each attribute (each item we look at is an eZContentObjectAttribute which provides this automatically). For other fields though, we may want to carry out more fine tuning. In our user example, there are two such fields. We will cover the user_account field shortly but in the meantime let’s take a look at how we can display our image in a helpful format.

For the image, as with most complex types, we have more than one possible value we can display. In this case possible values we may want to show are an image path, the image itself or the alternate text. Since we’re working through the command line we can’t show the image itself so let’s show the other two instead. Although simple information can often be extracted straight from the eZContentObjectAttribute, in cases where it is not you can use the content() method which will return the attribute as it sits natively in eZ Publish. In the case of the image, the content will return an ezimage. Looking at the eZImage class reference, these are quite simple to pull out. Let’s take a look at an extending our example from above but displaying content based on the content type. By doing this we can then easily extend when we need to extract additional field types (as we will need to shortly) :

// printing out other user fields:
$contentObject = $user->attribute( 'contentobject' );
$dataMap = $contentObject->attribute( 'data_map' );

foreach( $dataMap as $key => $value ) //looping through each field
{
    $type = $value->dataType(); //looking at what type the current field is
    switch( $type->DataTypeString ) //base the switch on the type name
    {
        case 'ezimage':
            $content = $value->attribute( 'content' ); 
            $displayText = $content->displayText();
            $imageAlias = $content->imageAlias('original');
            $imagePath = $imageAlias['url'];
            $cli->output( "$key: $displayText ($imagePath)" );
            break;
        case 'ezstring': //for basic text & ints
        case 'eztext':
        case 'ezint':
        case 'ezfloat':
            $cli->output( "$key: ".$value->toString() );
            break;
        default: //by default let's show what the type is (along with the toString representation):
            $cli->output( $key.' ' . $type->DataTypeString . ' - ' . $value->toString() );
        break;
    }
}
 

Displaying user_account

The other field of note is called user_account. In the last example if you run the code it will tell you this is an eZUser object. In the cases we have been looking at so far we have always started with a eZUser object and this merely returns the same object we started with. That being the case, why is this useful? Well mostly because you won't always be dealing with eZUser objects in the first place. The second part of this tutorial will look at fetches using a method in eZContentObjectTreeNode. This method will return eZContentObjectTreeNodes, which means we can not access the eZUser attribute method directly to give us the user's email address and username. In this situation we can instead pull this information from the user account field (if you are working with eZContentObjects you will need to do something similar).

The following code adds to the above to ensure user accounts are dealt with :

// printing out other user fields:
$contentObject = $user->attribute( 'contentobject' );
$dataMap = $contentObject->attribute( 'data_map' );

foreach( $dataMap as $key => $value ) //looping through each field
{
    $type = $value->dataType(); //looking at what type the current field is
    
    switch( $type->DataTypeString ) //base the switch on the type name
    {
        case 'ezuser':
            $user_account = $value->attribute( 'content' );
            $cli->output( 'Username: ' . $user_account->attribute('login') );
            $cli->output( 'Email: ' . $user_account->attribute('email') );
        break;
        case 'ezimage':
            $content = $value->attribute( 'content' ); 
            $displayText = $content->displayText();
            $imageAlias = $content->imageAlias('original');
            $imagePath = $imageAlias['url'];
            $cli->output("$key: $displayText ($imagePath)");
            break;
        case 'ezstring': //for basic text & ints
        case 'eztext':
        case 'ezint':
        case 'ezfloat':
            $cli->output( "$key: " . $value->toString() );
            break;
        default: //by default let's show what the type is (along with the toString representation):
            $cli->output( $key . ' ' . $type->DataTypeString . ' - ' . $value->toString() );
        break;
    }
}
 

An aside: Extracting user information from nodes and content objects

We just looked at extracting user account information from the data map rather than from the eZUser object directly. Let’s now take a quick look at a standalone example of dealing with extracting this information if we only have a node ID to illustrate this technique :

$userNode = eZContentObjectTreeNode::fetch( 15 ); //simple fetch by node id
$userObj = $userNode->attribute( 'object' );
$dataMap = $userObj->attribute( 'data_map' );

$user_account = $dataMap['user_account']->attribute( 'content' );
$cli->output( 'Username: ' . $user_account->attribute( 'login' ) );
$cli->output( 'Email: ' . $user_account->attribute( 'email' ) );

To handle this we convert the eZContentObjectNode into an eZContentObject and we can then pull out the content from the datamap as we have just done. I would recommend using the switch statement in the preceding example to pull this information out.

If you are dealing with a eZContentObject, you simply need to use the code from the 3rd line, where the datamap is extracted (using your own content object).

 

Fetching other user information

Fetching user roles

Fetching user roles is straightforward when we have a user node or if we can get hold of the content object id. The following examples should give you what you need to pull out the roles.(adjust this using the previous code examples if you want to extract from an email or a eZContentObject instead).

// by username (replace the next line with code from step 2 if you want to fetch by email address):
$user = eZUser::fetchByName( 'admin' );
$roles = $user->attribute( 'roles' ); //extract the role IDs
// fetch each one in turn and print:
print_r( $roles );

// if we have an eZContentObject ID (we can use this code if we have a eZContentObject or an eZContentObjectTreeNode): 
$roles = eZRole::fetchByUser( array( 14 ), true );
print_r( $roles );

Fetching user generated content

Since we have the ID for the user, we can also pull out what content they have created. The eZContentObjectVersion class has the method we need, all we need to do is tell it what status the content should have. In this example we are using just published content but your other options are also given below.

$userContent = eZContentObjectVersion::fetchForUser( $users['name']->attribute( "contentobject_id" ), eZContentObjectVersion::STATUS_PUBLISHED );
print_r( $userContent );

Here are a list of all possible eZContentObjectVersions:

  • eZContentObjectVersion::STATUS_DRAFT
  • eZContentObjectVersion::STATUS_PENDING
  • eZContentObjectVersion::STATUS_PUBLISHED
  • eZContentObjectVersion::>STATUS_REJECTED
 
36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)