<?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 Individual Users********" );
 
$users['name'] = eZUser::fetchByName( 'admin' );
$users['email'] = eZUser::fetchByEmail( 'admin@admin.com' );
$users['object_id'] = eZUser::fetch(14);

$users['current'] = eZUser::currentuser();

// print_r($users);


// $cli->output( "********Printing a user's details********" );
$user = $users['name'];
// print_r( $user->definition() ); //getting the definition of what attributes we can extract directly from the object.

// 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' ) );

// printing out the other user fields, these are accessible via the data map:
$contentObject = $user->attribute( 'contentobject' ); //converting the eZUser object to a eZContentObject to allow us to get the datamap
$dataMap = $contentObject->attribute( 'data_map' ); //extracting the dataMap:


/*
// $cli->output( "********Simple Variable Output********" );
foreach( $dataMap as $key => $value ) //let's start off simply and just print off each value:
{
	$cli->output( "$key: $value->attribute( 'data_text' )" );
}
*/
// $cli->output( "********Type Dependent Variable Output********" );
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 of the attribute):
			$cli->output( $key . ' ' . $type->DataTypeString . ' - ' . $value->toString() );
		break;
	}
}



// $cli->output( "********Fetching Roles********" );
// 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( $users['name']->attribute("contentobject_id") ), true );
// print_r($roles);

// $cli->output( "********Fetching User Created Content********" );
$userContent = eZContentObjectVersion::fetchForUser( $users['name']->attribute( "contentobject_id" ), eZContentObjectVersion::STATUS_PUBLISHED );
// print_r($userContent);


// $cli->output( "********Fetching All Users********" );
$allUsers = eZUser::fetchContentList();
// print_r($allUsers[0]);
/*
foreach( $allUsers as $key => $user )
{
	$userObj = eZUser::fetch( $user['id'] );
	$cli->output( 'Element: ' . $key );
	$cli->output( 'Username: ' . $userObj->attribute( 'login' ) );
	$cli->output( 'Email: ' . $userObj->attribute( 'email' ) );
	$cli->output( 'Content Object ID: ' . $userObj->attribute( 'contentobject_id' ) );
	$cli->output( '**********' );
}
*/
$cli->output( "********Fetching Users By Role********" );
$adminRole = eZRole::fetchByName( 'Administrator' );
$roleUsers = $adminRole->fetchUserByRole(); //this will return eZContentObjects within separate arrays

foreach( $roleUsers as $key => $userHolder )
{
	$object_type = $userHolder['user_object']->attribute( 'class_name' );


	if ( $object_type == 'user_group' ) //we will more than likely be dealing with a user group so we need to pull out the users from this
	{
		$user_group = $userHolder['user_object']->attribute( 'main_node' ); //convert so we can access the children of the node
		
		foreach( $user_group->attribute( 'children' ) as $group_user ) //user_group will contain  eZContentObjectTreeNodes, let's just output the name (details above on accessing other fields).
		{
			$cli->output( 'name: ' . $group_user->attribute( 'name' ) );
		}
	}
	else //if we have a user we are looking at a eZContentObject
	{
		$cli->output( 'name: ' . $userHolder['user_object']->attribute( 'name' ) );
	}
}

$cli->output( "********Fetching Logged in Users********" );
$loggedInCount = eZUser::fetchLoggedInCount();
$loggedIn = eZUser::fetchLoggedInList( true );

$cli->output( 'Logged in Users: '.$loggedInCount );


foreach( $loggedIn as $user )
{
	$cli->output( 'name: ' . $user->attribute( 'login' ) );
}


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