<?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();


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

$cli->output( "********Fetching all Site Members********" );
$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 );
$all_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params, $parent_node_id );

$cli->output( 'Count: ' . $all_user_count );

$cli->output( "********Fetching all Site Members created in the 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
// (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 ) ) );

$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 );
$cli->output( 'Count: ' . $monthly_new_user_count );

$cli->output( "********Fetching all Site Members modified in the last month (bar new users)********" );
$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->attribute( 'node_id' );

$modified_users = eZContentObjectTreeNode::subTreeByNodeID( $params, $parent_node_id );
$monthly_modified_user_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params, $parent_node_id );
$cli->output( 'Count: ' . $monthly_modified_user_count );
$cli->output( "********all users who wish to be contacted by email********" );
$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->attribute( 'node_id' );

$users_to_email = eZContentObjectTreeNode::subTreeByNodeID( $params, $parent_node_id );
$users_to_email_count = eZContentObjectTreeNode::subTreeCountByNodeID( $params, $parent_node_id );
$cli->output( 'Count: ' . $users_to_email_count );
$script->shutdown(); //stop the script
?>
