One quarter English
One quarter English

Fetching eZ Publish User Objects with PHP: Part Two

June 8th, 2010

This is the second part of this tutorial to demonstrate how you can access and extract user information from your eZ Publish database using PHP.

Part One of the tutorial provided ways to extract individual users and demonstrated how to display all of the user information. It also demonstrated some of the built in ways you can extract multiple users.

This part of the tutorial concentrates on the eZContentTreeNode::subTreeByNodeID() method. We will look at its basic use and some practical examples before creating a cronjob to automate the process.

Please see Part One for a basic script you can use to hold the code you create in this part.

Filtering Users using subTreeByNodeID

We’ve looked at a few ways of pulling out multiple users but by far the most useful is achieved by using Fetch statements. We’re going to be using the eZContentObjectTreeNode function subTreeByNodeID as this is extremely flexible and can be adapted to suit whatever content classes you need to export or access in your PHP.

The basic syntax of the function call is as follows. This example we will pull out the first 5 users found on the site within Users/Members:

$offset = 0;//Offset defaults to zero if omitted
$limit=5;//lets limit the fetch to 5 to prevent too many results from being returned
$depth = 1;//Depth defaults to one if it is excluded
$includeClasses = array(‘user’);//please note this refers to content classes created in the CMS, rather than PHP files

$params = array(‘Depth’=>$depth,
                                ‘Offset’=>$offset,
                                ‘Limit’=>$limit,
                                ‘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->NodeID;
$results = eZContentObjectTreeNode::subTreeByNodeID( $params,$parent_node_id );

There are a few areas to pay attention to in this example so we’ll break it down and look at each area. We will start at the end since this is the function call itself. We will then look at what we are passing in to the function:

$results = eZContentObjectTreeNode::subTreeByNodeID( $params, $parent_node_id );

The code is equivalent to the following fetch statement:

{def $fetch_nodes=fetch( ‘content’, ‘list’, hash( ‘parent_node_id’, 2, ‘offset’, 0, ‘limit’, 10) )}

Unlike the fetch statement where a single associative array contains both the parent node ID and the other possible parameters, the PHP version separates the two. Our first parameter can contain filters, offsets and limits while our second parameter is solely the parent node ID.

The Parameters Array
$offset = 0;//Offset defaults to zero if omitted
$limit=5;//lets limit the fetch to 5 to prevent too many results from being returned
$depth = 1;//Depth defaults to one if it is excluded
$includeClasses = array(‘user’);//please note this refers to content classes created in the CMS, rather than PHP files

$params = array(‘Depth’=>$depth,
                                ‘Offset’=>$offset,
                                ‘Limit’=>$limit,
                                ‘ClassFilterType’=>‘include’,
                                ‘ClassFilterArray’=>$includeClasses);

Our code is split into two parts. The first part is used to set the variables and the second just stores these in an array. Below is a complete list of the parameters you can pass into the system. They work in the same way as the parameters do in the fetch statement. Our following examples will include the other parameters so you should have a fully working example of what you need to export.

Parameter About Example
Depth By default set to 1, this value tells eZ how many levels it should search down. array( ‘Depth’ => 3 )
Offset By default set to 0, this value is usually used in conjunction with Limit to control the offset of the results. array( ‘Offset’ => 20 )
Limit By default set to 0 which pulls out all of the results, this can be set to make your queries more resource friendly. array( ‘Limit’ => 20 )
SortBy This can be used to sort in order of name, published date or any custom values. You must use true/false to specify ASC/DESC. array( ‘Limit’ => array(“name”,true) )
AttributeFilter This is the key parameter which we will look into in more detail. For user export you may use this in a number of ways, whether to check users wish to be contacted or users for within a specified timespan. Our example here is simple. We will look at more complex examples but I would also recommend looking at the content list documentation on the eZ.no site for more examples. array( ‘AttributeFilter’ => array( array( ‘name’, ‘like’, ‘David*’ ) ))
ExtendedAttributeFilter This is outside the scope of this tutorial, check out the examples in the documentation if you need to use this with your custom queries.
ClassFilterType / ClassFilterArray These are used in tandem so we will show their use together. ClassFilterType can be set to include or exclude. The classes filtered relate to the content types created in the CMS (such as User and Article) and do not relate to physical PHP file types (like eZUser). Please note that you need to use the class identifier rather than the class name. array(‘ClassFilterType’=>’include’,'ClassFilterArray’=>array(‘user’))
GroupBy Deprecated and not really that useful. Check out the codebase of eZContentObjectTreeNode if you want to use this.
The Parent Node ID
$parent_node = eZContentObjectTreeNode::fetchByURLPath(‘users/members’);//note the lowercase. URLs use underscores rather than hyphens
$parent_node_id = $parent_node->NodeID;

There are a couple of points to mention here. Although we could easily hard code a value for the parent node id in our code I prefer not too for code readability and because there is less chance of making a mistake. Note the use of lowercase and also be wary that you change any hyphens to underscores (for example My-Content would become my_content).

Finally, be aware that a lot of our examples in part one of this tutorial have used the Content Object ID whereas here we need to use the Node ID.

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:

First of all, that is a lot of requests but luckily 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.

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

Although it would be possible to use the eZUser function to extract all users this would also give us the admin users of the site and also the anonymous site user. Due to this we will instead use the function eZContentObjectTreeNode::subTreeByNodeID. We have already written the code we need to do this. When we looked at this function we used the path “Users/Members”. Since we know only users will be included within this folder, we can remove the limit we imposed and return all of the users:

$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->NodeID;
$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 user data. 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 );

Users created in the past calendar month

We now need to extend the previous example by ensuring only people from the last month are included. We are going to pull out both 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.

We are going to include the number of users in this case in the same example:

$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->NodeID;

$new_users = eZContentObjectTreeNode::subTreeByNodeID( $params,$parent_node_id );
$monthly_new_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );

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, 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

Users modified in the past calendar month

This example is slightly more complicated. We want to extract users who were modified in the last calendar month but this can not include users who were created. Recall that fetch statements can have multiple attribute filters. To carry out our query we need to ensure two things:

  • The user was not created between the first and last dates of last month
  • The user was modified between the first and last dates of last month

In our last example we used “between” to extract the users who were published within the previous month so to exclude those users we can modify the code to be “not_between”. We can then use “between” on the modified date to ensure we pick up the modified users. The following code should do the trick:

//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( ‘modified’, ‘between’, array($first_of_last_month,$first_of_month) ),
                                                   array( ‘published’, ‘not_between’, array($first_of_last_month,$first_of_month) ) );

As you can see the only addition to the attribute_filter is an additional array element. For a fully functional example, we can modify our previous code as follows:

$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( ‘modified’, ‘between’, array($first_of_last_month,$first_of_month) ),
                                                   array( ‘published’, ‘not_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->NodeID;

$modified_users = eZContentObjectTreeNode::subTreeByNodeID( $params,$parent_node_id );
$monthly_modified_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );

Users who have logged in over the past month

Although achievable, this code is quite complex. To work out the number of users who have logged in over the past month the best approach would be to create an extended attribute filter. I will demonstrate how to create a suitable filter in a future post.

Users who wish to be contacted by email

Many commercial sites need content such as this and you may well require multiple fields (e.g. contact by third parties, contact by phone, contact by post etc). When you start adding custom fields to the user class I would suggest creating a new content class as the content will not be required for some user types (admin users for instance). In this case though for simplicity we will add a new field to the standard user type.

Go to the Setup in the CMS and then click on the “Classes” link. Within the Class Groups you should see “Users” listed so click on this. If we were creating a new user type here we can choose to copy the existing user group and add new fields to the new class. Since we are not just click on the pencil symbol to edit the class. Once you do so go to the bottom of the class definition and you should be given an option to add an attribute (below is what you see in eZ Publish 4.3 where it is located in the bottom right):

Create Attribute

Create a field similar to the one in the screenshot. The details for it are as follows:

Field Type: checkbox
Field Name: I wish to be contacted by email
Identifier: contact_by_email
Checkboxes: All Unchecked

Now create a user and make sure your new checkbox is checked, otherwise we will have no results.

We now need to write an attribute filter that checks for users who wish to be contacted. This will work in the same way asit does in template files so we can use the following:

$attributeFilter  = array( array( ‘user/contact_by_email’, ‘=’, 1 ) );

We don’t need to check any dates in this case so our final code is as follows:

$includeClasses = array(‘user’);
$sortBy = array("name",true);//let’s sort by name

$attributeFilter  = array( array( ‘user/contact_by_email’, ‘=’, 1 ) );

$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->NodeID;

$users_to_email = eZContentObjectTreeNode::subTreeByNodeID( $params,$parent_node_id );
$users_to_email_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );

Putting it all together

Let’s put all of what we have covered together and create a full cronjob script for exporting user information that can be run on a monthly basis. We will export all new users created by the system each month and store it in a tab delimited text file (we will not use a CSV as there is a good chance that if you are storing user address fields that they will contain commas). We will also send an email confirmation when the file has been created which will include stats on the number of new site users, the total number of site users and the number of users who want to be emailed.

Before we begin we need to make sure a directory exists to store our exports. Create a directory in your var folder called “user_exports” (also make sure eZ Publish can write to the directory).

To make our code more legible we will move the code we created in the previous part of this tutorial to display user details into a separate function. We will also modify the information so it is shown in a format suitable for a tab delimited file. We will be using the eZContentObjectTreeNode::subTreeByNodeID() function to extract users. Due to this we will be dealing with eZContentObjectTreeNodes and so the code in the function will reflect this:

function show_user($userNode)
{
        //first echo the id for reference:

        $return_string = $userNode->NodeID;
       
        //now extract the fields:
       
        $userObj  =$userNode->object();//we will handle pulling the information out of these later.
        $dataMap = $userObj->dataMap();
       
        //print each in turn:
        foreach($dataMap as $key=>$value)
        {
                $type = $value->dataType();
                switch($type->DataTypeString)
                {
                        case ‘ezuser’:
                                $user_account = $dataMap[‘user_account’]->content();
                                $return_string.="\t$user_account->Login\t$user_account->Email";
                                break;
                        case ‘ezstring’:
                                $return_string.="\t$value->DataText";
                                break;
                        case ‘ezint’:
                                $return_string.="\t$value->DataInt";
                                break;
                        case ‘ezfloat’:
                                $return_string.="\t$value->DataFloat";
                                break;
                        case ‘ezimage’:
                                $content = $value->content();
                                $displayText = $content->displayText();
                                $imageAlias = $content->imageAlias(‘original’);
                                $imagePath = $imageAlias[‘url’];
                                $return_string.="$displayText ($imagePath)";
                               
                                //print_r($value->content());
                                break;
                }
        }
        $return_string.="\n";
        return $return_string;
}

We also need to create a header row for our data export. To make things easy for us, let’s modify the code we have just created and display the key for each field instead of the field value itself:

function show_header($userNode)
{
        $return_string = "Node ID";
       
        $userObj  =$userNode->object();//we will handle pulling the information out of these later.
        $dataMap = $userObj->dataMap();
       
        foreach(array_keys($dataMap) as $key)
        {
                $return_string.="\t$key";
        }
        $return_string.="\n";
        return $return_string;

}

Now that we have the functions created to show a header and to show a user, let’s put them into use and store details of all new users into a variable:

$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->NodeID;

$new_users = eZContentObjectTreeNode::subTreeByNodeID( $params,$parent_node_id );
$monthly_new_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );

/*print header info:*/

$file_content = show_header($new_users[0]);

foreach($new_users as $user)
{
  $file_content.= show_user($user);
}

We can pull out the total number of users who have modified their details and the number of users who wish to be emailed using the code we have already looked at. I will omit repeating the code here so we can move on to the other functionality required which is storing the data in a file and emailing the stats to the site admin (the full source code is available at the end of the tutorial).

First, let’s create and store the new user content in a file:

//creating the file (filename based on start and end dates of the data):
$file_name = date(‘d-m-Y’,$first_of_last_month).‘_’.date(‘d-m-Y’, strtotime( ‘-1 day’ , $first_of_month )).".txt";
$var_path = $_SERVER[‘PWD’].‘/var/user_export/’;
$file_path = $var_path.$file_name;

//creating the file and storing our content:
$file_pointer = fopen($file_path,‘x’);
fwrite($file_pointer, $file_content);

For the email, let’s pull the site admin’s email from the site.ini file:

//pulling out the site admin’s email address for sending the email:
$site_ini = eZIni::instance( "site.ini");
$admin_email =$site_ini->BlockValues[‘MailSettings’][‘AdminEmail’];

Now we have the admin’s email, let’s create our email message and sent it to them:

//sending the email containing user stats:
$email_subject = "Users report for".date(‘d-m-Y’,$first_of_last_month).‘ to ‘.date(‘d-m-Y’, strtotime( ‘-1 day’ , $first_of_month ));
$email_content = ‘The New Users Report from the start of  ’.date(‘d-m-Y’,$first_of_last_month).‘ to the end of ‘.date(‘d-m-Y’, strtotime( ‘-1 day’ , $first_of_month )).‘ has been created at: ‘.$file_path;
$email_content .= "\n\nHere is a summary of the user stats:\n\n";
$email_content .= ‘New Users: ‘.$monthly_new_user_count."\n";
$email_content .= ‘Modified Users: ‘.$monthly_modified_user_count."\n";
$email_content .= ‘Users who wish to be emailed: ‘.$users_to_email_count."\n";

//sending our email with a link to our file to the site admin:
mail($admin_email,$email_subject,$email_content);//we don’t need any real styling so just use the standard php email function

Conclusions

And that is it! If you open up the file in a spreadsheet application you should be able to ensure each tab is shown in a separate column. The script we have created is flexible and you could pull out any kind of content based on the principles we have looked at over these tutorials, just by modifying the Fetch statement we are using. I’ve included the full cronjob script we have created in the final section of this tutorial below but please get in touch if you want a hard copy.

The full code

The full code can be found below. If you want a hard copy, contact me on my contact page and I’ll send you one.

<?php
set_time_limit ( 0 ); //ensure the script does not time out
       
require ‘autoload.php’;//make sure relevant eZ Classes can be loaded

$cli = eZCLI::instance();//provides interface with CLI

//Setting up the script object itself:
$script = eZScript::instance( array( ‘description’ =>  "eZ Publish user export.\n\n" .
                                                                "Methods of exporting user information from eZ Publish \n" .
                                                                "\n",
                                             ‘use-session’ => false,
                                             ‘use-modules’ => true,
                                             ‘use-extensions’ => true,
                                                                                ‘debug-output’ => true,
                                                                                ‘debug-message’ =>true
                                             
                                             ) );
       
$script->startup();
$script->initialize();

//let’s pull out the user fields from a user object, since we will have a lot of them anyway:
function show_header($userNode)
{
        $return_string = "Node ID";
       
        $userObj  =$userNode->object();//we will handle pulling the information out of these later.
        $dataMap = $userObj->dataMap();
       
        foreach(array_keys($dataMap) as $key)
        {
                $return_string.="\t$key";
        }
        $return_string.="\n";
        return $return_string;

}

function show_user($userNode)
{
        //first echo the id for reference:

        $return_string = $userNode->NodeID;
       
        //now extract the fields:
       
        $userObj  =$userNode->object();//we will handle pulling the information out of these later.
        $dataMap = $userObj->dataMap();
       
        //print each in turn:
        foreach($dataMap as $key=>$value)
        {
                $type = $value->dataType();
                switch($type->DataTypeString)
                {
                        case ‘ezuser’:
                                $user_account = $dataMap[‘user_account’]->content();
                                $return_string.="\t$user_account->Login\t$user_account->Email";
                                break;
                        case ‘ezstring’:
                                $return_string.="\t$value->DataText";
                                break;
                        case ‘ezint’:
                                $return_string.="\t$value->DataInt";
                                break;
                        case ‘ezfloat’:
                                $return_string.="\t$value->DataFloat";
                                break;
                        case ‘ezimage’:
                                $content = $value->content();
                                $displayText = $content->displayText();
                                $imageAlias = $content->imageAlias(‘original’);
                                $imagePath = $imageAlias[‘url’];
                                $return_string.="$displayText ($imagePath)";
                               
                                //print_r($value->content());
                                break;
                }
        }
        $return_string.="\n";
        return $return_string;

}

/*ensuring the current user has the rights to the user information:*/
$user = eZUser::fetchByName( ‘admin’ );
eZUser::setCurrentlyLoggedInUser( $user, $user->attribute(‘contentobject_id’) );

/*Users created last month*/
$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->NodeID;

$new_users = eZContentObjectTreeNode::subTreeByNodeID( $params,$parent_node_id );
$monthly_new_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );

/*print header info:*/
$file_content = show_header($new_users[0]);

foreach($new_users as $user)//add each user to our variable:
{
  $file_content.= show_user($user);
}

/*Extracting other required stats:*/

/*firstly, modified users*/
$attributeFilter  = array( array( ‘modified’, ‘between’, array($first_of_last_month,$first_of_month) ),
                                                   array( ‘published’, ‘not_between’, array($first_of_last_month,$first_of_month) ) );
$params = array(‘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->NodeID;

$monthly_modified_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );

/*Now our contactable users*/
$attributeFilter  = array( array( ‘user/contact_by_email’, ‘=’, 1 ) );
$params = array(‘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->NodeID;
$users_to_email_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params,$parent_node_id );

/*Writing stats to file and emailing to the site admin:*/
//creating the file (filename based on start and end dates of the data):
$file_name = date(‘d-m-Y’,$first_of_last_month).‘_’.date(‘d-m-Y’, strtotime( ‘-1 day’ , $first_of_month )).".txt";
$var_path = $_SERVER[‘PWD’].‘/var/user_export/’;
$file_path = $var_path.$file_name;

//creating the file and storing our content:
$file_pointer = fopen($file_path,‘x’);
fwrite($file_pointer, $file_content);

//sending the email containing user stats:

//pulling out the site admin’s email address for sending the email:
$site_ini = eZIni::instance( "site.ini");
$admin_email =$site_ini->BlockValues[‘MailSettings’][‘AdminEmail’];

$email_subject = "Users report for".date(‘d-m-Y’,$first_of_last_month).‘ to ‘.date(‘d-m-Y’, strtotime( ‘-1 day’ , $first_of_month ));
$email_content = ‘The New Users Report from the start of  ’.date(‘d-m-Y’,$first_of_last_month).‘ to the end of ‘.date(‘d-m-Y’, strtotime( ‘-1 day’ , $first_of_month )).‘ has been created at: ‘.$file_path;
$email_content .= "\n\nHere is a summary of the user stats:\n\n";
$email_content .= ‘New Users: ‘.$monthly_new_user_count."\n";
$email_content .= ‘Modified Users: ‘.$monthly_modified_user_count."\n";
$email_content .= ‘Users who wish to be emailed: ‘.$users_to_email_count."\n";

//sending our email with a link to our file to the site admin:
mail($admin_email,$email_subject,$email_content);//we don’t need any real styling so just use the standard php email function

$script->shutdown();//stop the script
?>

Tags: ,

Leave a Reply

(will not be published)