User Resource Report II

Use a snippet property to customize the user resource report


In the previous article, we produced a list of resources created by the current user. In this one, we'll send a property in the snippet tag to tell the snippet which of the user-related fields we want to use for our search.


The Snippet Tag

With the added property, our snippet tag will look like this::

[[UserResourceReport? &searchField=`createdby`]]

The Tpl Chunk

The Tpl chunk (ResourceReportTpl) will take this form:

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

If you want include TVs in the output, you could modify the code to get them, but doing so is fairly complex, so it's best to just use getResources (or better yet, pdoResources), which will set them for you.


The Code

Here's the code of our snippet, modified to use the &searchField property. We'll set createdby as the default in case the user forgets to include the &searchField property.

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

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

return $output;

As you can see, The addition of the new property doesn't change our snippet very much.


A Complete Report

Now that our snippet will get resources based on any field, we can produce a complete report for the user by doing something like this:

<h2>Resources You've Created</h2>
[[!UserResourceReport? &searchField=`createdby`]]

<h2>Resources You've Published</h2>
[[!UserResourceReport? &searchField=`publishedby`]]

<h2>Resources You've Edited</h2>
[[!UserResourceReport? &searchField=`publishedby`]]

Coming Up

Our list of resources will generally be in the order that the resources were created. You might well want to sort them (usually by pagetitle). We'll see how to do that in the next article.



Comments (0)


Please login to comment.

  (Login)