User Resource Report I

Producing a report showing resources created by the current user

In the last few articles, we saw how to display user information for the user-related fields of a single resource. In this series of articles, we'll go in a slightly different direction and look at how to display information about a collection of resources acted on by the current user. As we did in the previous article, we'll use a Tpl chunk to format the output.

Getting the Resources

This couldn't be much simpler. We can get the current user's ID with this code:

$userId = $modx->user->get('id');

As we saw in the previous articles, the user-related fields of a resource (createdby, editedby, publishedby) hold the ID of the user who performed each action. It's a short step from there to getting a collection of the user's resources. For example, if we want all the resources created by the user, we can do this:

$userId = $modx->user->get('id');
$docs = $modx->getCollection('modResource', array('createdby' => $userId));

We'll need a Tpl chunk to display the output and some more code to process each resource, but first, let's look at an easy way to create the associative array we need for the placeholders in the Tpl chunk.


Every MODX object is descended from the basic xPDO object. That object has a method called toArray(), which means that every MODX object (including resources, users, chunks, snippets, plugins, templates, template variables, and lots more) inherits that method. Once you have the object, you can call its toArray() method.

The toArray() method creates exactly the kind of associative array that $modx->getChunk() is expecting. (If you need to review how $modx->getChunk() works, take a look at the previous article.) Basically, toArray() returns an array where the keys are the fields of the object and the values are the contents of those fields. Here's an abbreviated version of the array toArray() returns for a resource:

   'pagetitle' => 'MyPage',
   'longtitle' => 'This is My Page',
   'description' => 'This is my first page',
   'alias' => 'mypage',
   'createdby' => 22,
   'content' => 'This is the content of my first page',

toArray() doesn't always return *all* the fields of the object (though it usually does). For the user object, for example, the toArray() method is overridden to remove the password and hash values of the user, so those are not returned.

The Snippet Tag

We'll call our snippet UserResourceReport, so the tag will look like this:


The Tpl Chunk

The Tpl chunk will contain placeholder tags for the resource fields we want to show. The snippet will actually set placeholders for all the fields returned by toArray(), so you can add or remove fields to show just by editing the Tpl chunk. All the resource fields will be in the array, but if you don't set a placeholder for a given field, MODX will ignore it. We'll call our Tpl chunk ResourceReportTpl:

<p class="page_field">Description: [[+description]]</p>
<p class="page_field">Summary: [[+summary]]</p>

You can have as many resource fields as you like and format them however you want See this page for a full list of the resource fields.

The Code

Here's the code to show the list of resources created by the current user:

/* UserResourceReport snippet */
$userId = $modx->user->get('id');
$docs = $modx->getCollection('modResource', array('createdby' => $userId));
$output = '';

foreach($docs as $doc) {
   $fields = $doc->toArray();
   $output .= $modx->getChunk('ResourceReportTpl', $fields);

return $output;

As you can see, there's not much to the snippet. It really demonstrates the power of xPDO and MODX. It makes a single query to the database and will quickly produce a display of information about all the resources the user has created on the site. You may want to exclude deleted and unpublished resources. That's easily done by changing the criteria used by getCollection():

$c = array(
    'createdby' => $userId,
    'deleted' => false,
    'published' => true,

$modx->getCollection('modResource', $c);

Coming Up

If we want to change from resources created by the user, to resources published or edited by the user we need to change the code of our snippet. In the next article, we'll send the field we want to use as an argument to the snippet.

Comments (0)

Please login to comment.