Template Report with Pagetitles

Add pagetitles to the template report created in the previous Blog article.


In the previous article, we looked at a snippet that shows the number of published and unpublished resources connected to each template. In this one, we'll expand that to show each template with a list of the pagetitles of its attached resources. We'll call this one TemplateReportPlus.


The Code

Put this tag on a page where you'd like to see the report:

[[!TemplateReportPlus]]

Paste this code into a snippet called TemplateReportPlus:

/* TemplateReportPlus snippet */
$templates = $modx->getCollection('modTemplate');

$output = '<h2>Template Report</h2>';
$output .= "\n<pre>";

foreach ($templates as $template) {
    $id = $template->get('id');
    $name = $template->get('templatename');

    $output .= "\n\n" . $name ;

    /* Published resources */
    $c1 = $modx->newQuery('modResource');
    $c1->where(array(
        'published' => '1',
        'template'  => $id,
    ));
    $output .= "\n    Published";
    $docs = $modx->getCollection('modResource', $c1);

    if (empty($docs)) {
        $output .= "\n        (none)";
    } else {
        foreach ($docs as $doc) {
            $output .= "\n        " . $doc->get('pagetitle');
        }
    }

    /* Unpublished resources */
    $c2 = $modx->newQuery('modResource');
    $c2->where(array(
        'published' => '0',
        'template'  => $id,
    ));
    $output .= "\n    Unpublished";

    $docs = $modx->getCollection('modResource', $c2);

    if (empty($docs)) {
        $output .= "\n        (none)";
    } else {
        foreach ($docs as $doc) {
            $output .= "\n        " . $doc->get('pagetitle');
        }
    }
}
return  $output . '</pre>';

How it Works

The code gets all the site's templates with getCollection() in line 2, then loops through them. For each template, first, we get the published resources (looping through them to add each pagetitle to the output). Then, we get that template's unpublished resources and do the same with them. Finally, we return the output. We've wrapped the output in pre tags to preserve the indentation.

This snippet could be made significantly faster, but it probably isn't going to run very often, so it's not worth the trouble to optimize it.



Comments (1)

  1. Susan OttwellJan 19, 2015 at 03:00 PM

    Actually this sounds like it might be a useful utility to add to a custom Dashboard, like Shawn Wilkerson's Site Summary widget. http://www.shawnwilkerson.com/modx-revolution/2012/11/26/site-summary-widget-/-snippet/


Please login to comment.

  (Login)