This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Learn » eZ Publish » Fetching User Objects with PHP - part 2

Fetching User Objects with PHP - part 2

Monday 18 October 2010 10:38:28 am

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

Step 2 : Practical Examples

Let's look at some more practical uses for our fetches. Let's assume we are working for a company who's marketing department are very keen on analysing and utilising our site's user data. They've made some requests about data they would like from the system:

  1. A complete list of site users and the total number of users
  2. The total number of new users last month and who those users were
  3. The total number of users who modified their account last month (apart from the new users)
  4. The number of users who wish to be contacted by email and who these users are

First of all, that is a lot of requests! Luckily though, the code required is very similar. We are going to look at these in turn before creating a cronjob which will pull together a lot of what we have looked at so far.

The key assumption in the following code examples is that our month runs from the 1st to the end of the month. We are also assuming that the new month will have started when the code is run (even if it only started a couple of minutes ago).

 

A complete list of site users and the total number of users

It would be easy to do this using the eZRole:: fetchUserByRole() function to extract users by role however there can potentially be a couple of issues with doing this:

  • If you have a very large number of users and you are loading content into a template file your page could timeout
  • If you need to use more than one role you need to carry out multiple queries

Due to these limitations, and also to look at how we would do this using eZContentObjectTreeNode::subTreeByNodeID, we will use this instead.

The code for this is actually pretty similar to our basic example. Since we know only site users and not admin users will be included within this folder, we can remove the limit we imposed and return all of the:

$includeClasses = array( 'user' ); //please note this refers to content classes created in the CMS, rather than PHP files
 
$params = array( 'ClassFilterType' => 'include',
              'ClassFilterArray' => $includeClasses);
 
// getting the ID of where the users sit in the cms (limiting the area eZ has to search for the objects):
$parent_node = eZContentObjectTreeNode::fetchByURLPath( 'users/members' );
$parent_node_id = $parent_node->attribute( 'node_id' );
$all_users = eZContentObjectTreeNode::subTreeByNodeID( $params, $parent_node_id );
 

Getting the count is straightforward. There is a comparable function to subTreeByNodeID which exports the count rather than the data itself. It can be used in exactly the same way and so we can add the following line to the bottom of our previous code to get the total number:

...
$all_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );
 

The total number of new users last month and who those users were

We now need to extend the previous example by ensuring only people from the last month are included. We are going to work out the start and end of the month and ensure that we only extract users created within these two dates. We can do this by making use of an attribute_filter, just as we would do in a Fetch statement within our templates .

Our attribute filter is as follows. Some straightforward PHP is used to work out the start and end dates. If you wanted to extract information from a different timespan then you could easily modify this code. For example, you may want to export the information on a weekly basis.

//working out the month start and end dates for the previous month (assuming the month runs from the 1st to the end of the month), code compatible with all versions of PHP 5:
$first_of_month = strtotime( date( "Y-m-1" ) ); //this is our endtime, midnight on the first of the current month
$first_of_last_month = strtotime( '-1 month' , $first_of_month ); //our start time is one month before then.
 
$attributeFilter  = array( array( 'published', 'between', array( $first_of_last_month, $first_of_month) ) ); //our attribute filter is ready to add to our parameters
 

All we need to do now is add the attribute filter we have created to our previous example which should give us the following code:

$includeClasses = array( 'user' );
$sortBy = array( "name", true ); //let's sort by name
 
//working out the month start and end dates for the previous month, compatible with all versions of PHP 5:
$first_of_month = strtotime( date( "Y-m-1" ) ); //this is our endtime, midnight on the first of the current month
$first_of_last_month = strtotime( '-1 month' , $first_of_month ); //our start time is one month before then.
 
$attributeFilter  = array( array( 'published', 'between', array( $first_of_last_month,$first_of_month ) ) );
 
$params = array( 'SortBy' => $sortBy,
                'ClassFilterType' => 'include',
                'ClassFilterArray' => $includeClasses,
                'AttributeFilter' => $attributeFilter);
 
//getting the ID of where the users sit in the cms (limiting the area eZ has to search for the objects):
$parent_node = eZContentObjectTreeNode::fetchByURLPath( 'users/members' ); //note the lowercase. Use underscores rather than hyphens if spaces are included in the path
$parent_node_id = $parent_node->attribute( 'node_id' );
 
$new_users = eZContentObjectTreeNode::subTreeByNodeID( $params, $parent_node_id );
$monthly_new_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );
 
36 542 Users on board!

Tutorial menu

Printable

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

Author(s)

Proudly Developed with from